2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
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,
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
;
38 D(bug("[AROSTCP](kern_malloc.c) malloc_init()\n"));
41 mem_pool
= CreatePool(MEMF_PUBLIC
, __MALLOC_POOLSIZE
, __MALLOC_POOLSIZE_THRESHOLD
);
45 D(bug("[AROSTCP](kern_malloc.c) malloc_init: Failed to Allocate Pool\n"));
50 D(bug("[AROSTCP](kern_malloc.c) malloc_init: Pool Allocated (%d bytes) @ 0x%p\n", __MALLOC_POOLSIZE
, mem_pool
));
53 * Initialize malloc_semaphore for use.
54 * Do not call bsd_malloc() or bsd_free() before this!
56 InitSemaphore(&malloc_semaphore
);
66 D(bug("[AROSTCP](kern_malloc.c) malloc_deinit()\n"));
70 D(bug("[AROSTCP](kern_malloc.c) malloc_deinit: Pool deleted\n"));
77 bsd_malloc(unsigned long size
, int type
, int flags
)
81 D(bug("[AROSTCP](kern_malloc.c) bsd_malloc()\n"));
87 ObtainSemaphore(&malloc_semaphore
);
88 mem
= AllocVecPooled(mem_pool
, size
);
90 *((ULONG
*)mem
)++=0xbaadab00;
93 D(bug("[AROSTCP](kern_malloc.c) bsd_malloc: Allocated %d bytes @ 0x%p, from pool @ 0x%p\n", size
, mem
, mem_pool
));
95 ReleaseSemaphore(&malloc_semaphore
);
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
));
107 ObtainSemaphore(&malloc_semaphore
);
109 if (*--((ULONG
*)addr
) == 0xbaadab00)
111 *((ULONG
*)addr
) = 0xabadcafe;
113 FreeVecPooled(mem_pool
, addr
);
118 #if defined(__AROS__)
119 D(bug("[AROSTCP](kern_malloc.c) bsd_free: Attempt to free non-allocated memory at 0x%p!!!\n", addr
));
121 log(LOG_CRIT
,"Attempt to free non-allocated memory at 0x%p!!!", addr
);
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
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
));
144 ObtainSemaphore(&malloc_semaphore
);
147 ULONG
*realmem
= mem
-4;
148 unsigned long realsize
= size
+4;
149 if (*realmem
== 0xbaadab00)
153 #define realsize size
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
) {
159 *((ULONG
*)new_mem
)++ = 0xbaadab00;
161 CopyMem(mem
, new_mem
, size
);
162 FreeVecPooled(mem_pool
, realmem
);
168 #if defined(__AROS__)
169 D(bug("[AROSTCP](kern_malloc.c) bsd_free: Attempt to reallocate non-allocated memory at 0x%p!!!", mem
));
171 log(LOG_CRIT
,"Attempt to reallocate non-allocated memory at 0x%p!!!", mem
);
175 ReleaseSemaphore(&malloc_semaphore
);