tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / kern / kern_malloc.c
blob4043900d446dd243c91367fc8b6cc723c81f50d9
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 - 2007 The AROS Dev Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
23 #include <conf.h>
25 #include <sys/param.h>
26 #include <sys/malloc.h>
28 #include <kern/amiga_includes.h>
30 struct SignalSemaphore malloc_semaphore;
31 static BOOL initialized = FALSE;
32 static APTR mem_pool = NULL;
34 BOOL
35 malloc_init(void)
37 #if defined(__AROS__)
38 D(bug("[AROSTCP](kern_malloc.c) malloc_init()\n"));
39 #endif
40 if (!initialized) {
41 mem_pool = CreatePool(MEMF_PUBLIC, __MALLOC_POOLSIZE, __MALLOC_POOLSIZE_THRESHOLD);
42 if(mem_pool == NULL)
44 #if defined(__AROS__)
45 D(bug("[AROSTCP](kern_malloc.c) malloc_init: Failed to Allocate Pool\n"));
46 #endif
47 return FALSE;
49 #if defined(__AROS__)
50 D(bug("[AROSTCP](kern_malloc.c) malloc_init: Pool Allocated (%d bytes) @ 0x%p\n", __MALLOC_POOLSIZE, mem_pool));
51 #endif
53 * Initialize malloc_semaphore for use.
54 * Do not call bsd_malloc() or bsd_free() before this!
56 InitSemaphore(&malloc_semaphore);
57 initialized = TRUE;
59 return TRUE;
62 VOID
63 malloc_deinit(void)
65 #if defined(__AROS__)
66 D(bug("[AROSTCP](kern_malloc.c) malloc_deinit()\n"));
67 #endif
68 DeletePool(mem_pool);
69 #if defined(__AROS__)
70 D(bug("[AROSTCP](kern_malloc.c) malloc_deinit: Pool deleted\n"));
71 #endif
72 initialized = FALSE;
73 return;
76 void *
77 bsd_malloc(unsigned long size, int type, int flags)
79 register void *mem;
80 #if defined(__AROS__)
81 D(bug("[AROSTCP](kern_malloc.c) bsd_malloc()\n"));
82 #endif
84 #ifdef DEBUG_MEM
85 size += 4;
86 #endif
87 ObtainSemaphore(&malloc_semaphore);
88 mem = AllocVecPooled(mem_pool, size);
89 #ifdef DEBUG_MEM
90 *((ULONG *)mem)++=0xbaadab00;
91 #endif
92 #if defined(__AROS__)
93 D(bug("[AROSTCP](kern_malloc.c) bsd_malloc: Allocated %d bytes @ 0x%p, from pool @ 0x%p\n", size, mem, mem_pool));
94 #endif
95 ReleaseSemaphore(&malloc_semaphore);
97 return mem;
100 void
101 bsd_free(void *addr, int type)
103 #if defined(__AROS__)
104 D(bug("[AROSTCP](kern_malloc.c) bsd_free(0x%p, pool @ 0x%p)\n", addr, mem_pool));
105 #endif
107 ObtainSemaphore(&malloc_semaphore);
108 #ifdef DEBUG_MEM
109 if (*--((ULONG *)addr) == 0xbaadab00)
111 *((ULONG *)addr) = 0xabadcafe;
112 #endif
113 FreeVecPooled(mem_pool, addr);
114 #ifdef DEBUG_MEM
116 else
118 #if defined(__AROS__)
119 D(bug("[AROSTCP](kern_malloc.c) bsd_free: Attempt to free non-allocated memory at 0x%p!!!\n", addr));
120 #endif
121 log(LOG_CRIT,"Attempt to free non-allocated memory at 0x%p!!!", addr);
123 #endif
124 ReleaseSemaphore(&malloc_semaphore);
128 * bsd_realloc() cannot be used for reallocating
129 * last freed block for obvious reasons
131 * This function is only called from one place, and there
132 * it is used to shorten the allocated block. Only this particular
133 * behaviour is implemented
136 void *
137 bsd_realloc(void * mem, unsigned long size, int type, int flags)
139 void *new_mem = NULL;
140 #if defined(__AROS__)
141 D(bug("[AROSTCP](kern_malloc.c) bsd_realloc(0x%p : %d bytes, pool @ 0x%p)\n", mem, size, mem_pool));
142 #endif
144 ObtainSemaphore(&malloc_semaphore);
145 #ifdef DEBUG_MEM
147 ULONG *realmem = mem-4;
148 unsigned long realsize = size+4;
149 if (*realmem == 0xbaadab00)
151 #else
152 #define realmem mem
153 #define realsize size
154 #endif
155 new_mem = AllocVecPooled(mem_pool, realsize);
156 D(bug("[AROSTCP](kern_malloc.c) bsd_realloc: Allocated %d bytes @ 0x%p\n", realsize, new_mem));
157 if(new_mem != NULL) {
158 #ifdef DEBUG_MEM
159 *((ULONG *)new_mem)++ = 0xbaadab00;
160 #endif
161 CopyMem(mem, new_mem, size);
162 FreeVecPooled(mem_pool, realmem);
164 #ifdef DEBUG_MEM
166 else
168 #if defined(__AROS__)
169 D(bug("[AROSTCP](kern_malloc.c) bsd_free: Attempt to reallocate non-allocated memory at 0x%p!!!", mem));
170 #endif
171 log(LOG_CRIT,"Attempt to reallocate non-allocated memory at 0x%p!!!", mem);
174 #endif
175 ReleaseSemaphore(&malloc_semaphore);
177 return new_mem;