2 * This module derived from code donated to the FreeBSD Project by
3 * Matthew Dillon <dillon@backplane.com>
5 * Copyright (c) 1998 The FreeBSD Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk
34 #include "zalloc_defs.h"
36 static MemPool MallocPool;
40 static int MallocCount;
42 void mallocstats(void);
50 static void *Malloc_align(size_t, size_t);
53 # define MIN(a,b) ((a) <= (b)) ? (a) : (b)
57 Malloc(size_t bytes, const char *file __unused, int line __unused)
59 return (Malloc_align(bytes, 1));
63 Memalign(size_t alignment, size_t bytes, const char *file __unused,
69 return (Malloc_align(bytes, alignment));
73 Malloc_align(size_t bytes, size_t alignment)
78 bytes += MALLOCALIGN + 1;
83 while ((res = znalloc(&MallocPool, bytes, alignment)) == NULL) {
84 int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
87 if ((base = sbrk(incr)) == (char *)-1)
89 zextendPool(&MallocPool, base, incr);
90 zfree(&MallocPool, base, incr);
93 if (++MallocCount > MallocMax)
94 MallocMax = MallocCount;
97 res->ga_Magic = GAMAGIC;
99 res->ga_Bytes = bytes;
101 *((signed char *)res + bytes - 1) = -2;
104 return ((char *)res + MALLOCALIGN);
108 Free(void *ptr, const char *file, int line)
113 Guard *res = (void *)((char *)ptr - MALLOCALIGN);
118 if (res->ga_Magic == GAFREE) {
119 printf("free: duplicate free @ %p from %s:%d\n",
123 if (res->ga_Magic != GAMAGIC) {
126 dump_bytes = MIN((ptr - MallocPool.mp_Base), 512);
127 hexdump(ptr - dump_bytes, dump_bytes);
128 panic("free: guard1 fail @ %p from %s:%d",
131 res->ga_Magic = GAFREE;
134 if (*((signed char *)res + res->ga_Bytes - 1) == -1) {
135 printf("free: duplicate2 free @ %p from %s:%d\n",
139 if (*((signed char *)res + res->ga_Bytes - 1) != -2)
140 panic("free: guard2 fail @ %p + %zu from %s:%d",
141 ptr, res->ga_Bytes - MALLOCALIGN, file, line);
142 *((signed char *)res + res->ga_Bytes - 1) = -1;
145 bytes = res->ga_Bytes;
146 zfree(&MallocPool, res, bytes);
155 Calloc(size_t n1, size_t n2, const char *file, int line)
157 uintptr_t bytes = (uintptr_t)n1 * (uintptr_t)n2;
160 if ((res = Malloc(bytes, file, line)) != NULL) {
163 if (++MallocCount > MallocMax)
164 MallocMax = MallocCount;
171 * realloc() - I could be fancier here and free the old buffer before
172 * allocating the new one (saving potential fragmentation
173 * and potential buffer copies). But I don't bother.
177 Realloc(void *ptr, size_t size, const char *file, int line)
182 if ((res = Malloc(size, file, line)) != NULL) {
184 Guard *g = (Guard *)((char *)ptr - MALLOCALIGN);
186 old = g->ga_Bytes - MALLOCALIGN;
188 bcopy(ptr, res, old);
190 bcopy(ptr, res, size);
191 Free(ptr, file, line);
194 if (++MallocCount > MallocMax)
195 MallocMax = MallocCount;
197 if (DidAtExit == 0) {
209 Reallocf(void *ptr, size_t size, const char *file, int line)
213 if ((res = Realloc(ptr, size, file, line)) == NULL)
214 Free(ptr, file, line);
223 printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
225 zallocstats(&MallocPool);