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
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
34 * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk
37 #include "zalloc_defs.h"
39 static MemPool MallocPool;
43 static int MallocCount;
45 void mallocstats(void);
55 free_region(void *start, void *end)
57 zextendPool(&MallocPool, start, (caddr_t)end - (caddr_t)start);
58 zfree(&MallocPool, start, (caddr_t)end - (caddr_t)start);
63 Malloc(size_t bytes, const char *file, int line)
68 bytes += MALLOCALIGN + 1;
73 while ((res = znalloc(&MallocPool, bytes)) == NULL) {
74 int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
77 if ((base = sbrk(incr)) == (char *)-1)
79 zextendPool(&MallocPool, base, incr);
80 zfree(&MallocPool, base, incr);
83 if (++MallocCount > MallocMax)
84 MallocMax = MallocCount;
87 res->ga_Magic = GAMAGIC;
89 res->ga_Bytes = bytes;
91 *((char *)res + bytes - 1) = -2;
93 return((char *)res + MALLOCALIGN);
97 Free(void *ptr, const char *file, int line)
102 Guard *res = (void *)((char *)ptr - MALLOCALIGN);
107 if (res->ga_Magic == GAFREE) {
108 printf("free: duplicate free @ %p from %s:%d\n", ptr, file, line);
111 if (res->ga_Magic != GAMAGIC)
112 panic("free: guard1 fail @ %p from %s:%p", ptr, file, line);
113 res->ga_Magic = GAFREE;
116 if (*((char *)res + res->ga_Bytes - 1) == -1) {
117 printf("free: duplicate2 free @ %p from %s:%d\n", ptr, file, line);
120 if (*((char *)res + res->ga_Bytes - 1) != -2)
121 panic("free: guard2 fail @ %p + %d from %s:%d", ptr, res->ga_Bytes - MALLOCALIGN, file, line);
122 *((char *)res + res->ga_Bytes - 1) = -1;
125 bytes = res->ga_Bytes;
126 zfree(&MallocPool, res, bytes);
135 Calloc(size_t n1, size_t n2, const char *file, int line)
137 iaddr_t bytes = (iaddr_t)n1 * (iaddr_t)n2;
140 if ((res = Malloc(bytes, file, line)) != NULL) {
143 if (++MallocCount > MallocMax)
144 MallocMax = MallocCount;
151 * realloc() - I could be fancier here and free the old buffer before
152 * allocating the new one (saving potential fragmentation
153 * and potential buffer copies). But I don't bother.
157 Realloc(void *ptr, size_t size, const char *file, int line)
162 if ((res = Malloc(size, file, line)) != NULL) {
164 old = *(size_t *)((char *)ptr - MALLOCALIGN) - MALLOCALIGN;
166 bcopy(ptr, res, old);
168 bcopy(ptr, res, size);
169 Free(ptr, file, line);
172 if (++MallocCount > MallocMax)
173 MallocMax = MallocCount;
175 if (DidAtExit == 0) {
187 Reallocf(void *ptr, size_t size, const char *file, int line)
191 if ((res = Realloc(ptr, size, file, line)) == NULL)
192 Free(ptr, file, line);
201 printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
203 zallocstats(&MallocPool);