priv: Use PRIV_NETINET_RAW
[dragonfly.git] / lib / libstand / zalloc_malloc.c
blobb3f1ac9ec60ff207482a13c1745eb93db0742b0f
1 /*
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
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
27 * SUCH DAMAGE.
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;
41 #ifdef DMALLOCDEBUG
42 static int MallocMax;
43 static int MallocCount;
45 void mallocstats(void);
46 #endif
48 #ifdef malloc
49 #undef malloc
50 #undef free
51 #endif
53 void *
54 malloc(size_t bytes)
56 Guard *res;
58 #ifdef USEENDGUARD
59 bytes += MALLOCALIGN + 1;
60 #else
61 bytes += MALLOCALIGN;
62 #endif
64 while ((res = znalloc(&MallocPool, bytes)) == NULL) {
65 int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
66 char *base;
68 if ((base = sbrk(incr)) == (char *)-1)
69 return(NULL);
70 zextendPool(&MallocPool, base, incr);
71 zfree(&MallocPool, base, incr);
73 #ifdef DMALLOCDEBUG
74 if (++MallocCount > MallocMax)
75 MallocMax = MallocCount;
76 #endif
77 #ifdef USEGUARD
78 res->ga_Magic = GAMAGIC;
79 #endif
80 res->ga_Bytes = bytes;
81 #ifdef USEENDGUARD
82 *((char *)res + bytes - 1) = -2;
83 #endif
84 return((char *)res + MALLOCALIGN);
87 void
88 free(void *ptr)
90 size_t bytes;
92 if (ptr != NULL) {
93 Guard *res = (void *)((char *)ptr - MALLOCALIGN);
95 #ifdef USEGUARD
96 if (res->ga_Magic != GAMAGIC)
97 panic("free: guard1x fail @ %p", ptr);
98 res->ga_Magic = -1;
99 #endif
100 #ifdef USEENDGUARD
101 if (*((char *)res + res->ga_Bytes - 1) != -2) {
102 panic("free: guard2 fail @ %p + %d %d/-2",
103 ptr,
104 res->ga_Bytes - MALLOCALIGN - 1,
105 *((char *)res + res->ga_Bytes - 1));
107 *((char *)res + res->ga_Bytes - 1) = -1;
108 #endif
110 bytes = res->ga_Bytes;
111 zfree(&MallocPool, res, bytes);
112 #ifdef DMALLOCDEBUG
113 --MallocCount;
114 #endif
119 void *
120 calloc(size_t n1, size_t n2)
122 uintptr_t bytes = (uintptr_t)n1 * (uintptr_t)n2;
123 void *res;
125 if ((res = malloc(bytes)) != NULL) {
126 bzero(res, bytes);
127 #ifdef DMALLOCDEBUG
128 if (++MallocCount > MallocMax)
129 MallocMax = MallocCount;
130 #endif
132 return(res);
136 * realloc() - I could be fancier here and free the old buffer before
137 * allocating the new one (saving potential fragmentation
138 * and potential buffer copies). But I don't bother.
141 void *
142 realloc(void *ptr, size_t size)
144 void *res;
145 size_t old;
147 if ((res = malloc(size)) != NULL) {
148 if (ptr) {
149 old = *(size_t *)((char *)ptr - MALLOCALIGN) - MALLOCALIGN;
150 #ifdef USEENDGUARD
151 --old;
152 #endif
153 if (old < size)
154 bcopy(ptr, res, old);
155 else
156 bcopy(ptr, res, size);
157 free(ptr);
158 } else {
159 #ifdef DMALLOCDEBUG
160 if (++MallocCount > MallocMax)
161 MallocMax = MallocCount;
162 #ifdef EXITSTATS
163 if (DidAtExit == 0) {
164 DidAtExit = 1;
165 atexit(mallocstats);
167 #endif
168 #endif
171 return(res);
174 void *
175 reallocf(void *ptr, size_t size)
177 void *res;
179 if ((res = realloc(ptr, size)) == NULL)
180 free(ptr);
181 return(res);
184 #ifdef DMALLOCDEBUG
186 void
187 mallocstats(void)
189 printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
190 #ifdef ZALLOCDEBUG
191 zallocstats(&MallocPool);
192 #endif
195 #endif