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
29 * $FreeBSD: src/lib/libstand/zalloc_malloc.c,v 1.5 1999/08/28 00:05:35 peter Exp $
30 * $DragonFly: src/lib/libstand/zalloc_malloc.c,v 1.3 2005/03/13 15:10:03 swildner Exp $
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);
59 bytes += MALLOCALIGN + 1;
64 while ((res = znalloc(&MallocPool, bytes)) == NULL) {
65 int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
68 if ((base = sbrk(incr)) == (char *)-1)
70 zextendPool(&MallocPool, base, incr);
71 zfree(&MallocPool, base, incr);
74 if (++MallocCount > MallocMax)
75 MallocMax = MallocCount;
78 res->ga_Magic = GAMAGIC;
80 res->ga_Bytes = bytes;
82 *((char *)res + bytes - 1) = -2;
84 return((char *)res + MALLOCALIGN);
93 Guard *res = (void *)((char *)ptr - MALLOCALIGN);
96 if (res->ga_Magic != GAMAGIC)
97 panic("free: guard1 fail @ %p", ptr);
101 if (*((char *)res + res->ga_Bytes - 1) != -2)
102 panic("free: guard2 fail @ %p + %d", ptr, res->ga_Bytes - MALLOCALIGN);
103 *((char *)res + res->ga_Bytes - 1) = -1;
106 bytes = res->ga_Bytes;
107 zfree(&MallocPool, res, bytes);
116 calloc(size_t n1, size_t n2)
118 iaddr_t bytes = (iaddr_t)n1 * (iaddr_t)n2;
121 if ((res = malloc(bytes)) != NULL) {
124 if (++MallocCount > MallocMax)
125 MallocMax = MallocCount;
132 * realloc() - I could be fancier here and free the old buffer before
133 * allocating the new one (saving potential fragmentation
134 * and potential buffer copies). But I don't bother.
138 realloc(void *ptr, size_t size)
143 if ((res = malloc(size)) != NULL) {
145 old = *(size_t *)((char *)ptr - MALLOCALIGN) - MALLOCALIGN;
147 bcopy(ptr, res, old);
149 bcopy(ptr, res, size);
153 if (++MallocCount > MallocMax)
154 MallocMax = MallocCount;
156 if (DidAtExit == 0) {
168 reallocf(void *ptr, size_t size)
172 if ((res = realloc(ptr, size)) == NULL)
182 printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
184 zallocstats(&MallocPool);