xref: /freebsd/stand/libsa/zalloc_malloc.c (revision 96190b4fef3b4a0cc3ca0606b0c4e3e69a5e6717)
1 /*
2  * This module derived from code donated to the FreeBSD Project by
3  * Matthew Dillon <dillon@backplane.com>
4  *
5  * Copyright (c) 1998 The FreeBSD Project
6  * All rights reserved.
7  *
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.
16  *
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.
28  */
29 
30 /*
31  * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk
32  */
33 
34 #include "zalloc_defs.h"
35 
36 static MemPool	MallocPool;
37 
38 #ifdef DMALLOCDEBUG
39 static int MallocMax;
40 static int MallocCount;
41 
42 void mallocstats(void);
43 #endif
44 
45 #ifdef malloc
46 #undef malloc
47 #undef free
48 #endif
49 
50 static void *Malloc_align(size_t, size_t);
51 
52 #ifndef MIN
53 # define MIN(a,b) ((a) <= (b)) ? (a) : (b)
54 #endif
55 
56 void *
57 Malloc(size_t bytes, const char *file __unused, int line __unused)
58 {
59 	return (Malloc_align(bytes, 1));
60 }
61 
62 void *
63 Memalign(size_t alignment, size_t bytes, const char *file __unused,
64     int line __unused)
65 {
66 	if (alignment == 0)
67 		alignment = 1;
68 
69 	return (Malloc_align(bytes, alignment));
70 }
71 
72 static void *
73 Malloc_align(size_t bytes, size_t alignment)
74 {
75 	Guard *res;
76 
77 #ifdef USEENDGUARD
78 	bytes += MALLOCALIGN + 1;
79 #else
80 	bytes += MALLOCALIGN;
81 #endif
82 
83 	while ((res = znalloc(&MallocPool, bytes, alignment)) == NULL) {
84 		int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
85 		char *base;
86 
87 		if ((base = sbrk(incr)) == (char *)-1)
88 			return (NULL);
89 		zextendPool(&MallocPool, base, incr);
90 		zfree(&MallocPool, base, incr);
91 	}
92 #ifdef DMALLOCDEBUG
93 	if (++MallocCount > MallocMax)
94 		MallocMax = MallocCount;
95 #endif
96 #ifdef USEGUARD
97 	res->ga_Magic = GAMAGIC;
98 #endif
99 	res->ga_Bytes = bytes;
100 #ifdef USEENDGUARD
101 	*((signed char *)res + bytes - 1) = -2;
102 #endif
103 
104 	return ((char *)res + MALLOCALIGN);
105 }
106 
107 void
108 Free(void *ptr, const char *file, int line)
109 {
110 	size_t bytes;
111 
112 	if (ptr != NULL) {
113 		Guard *res = (void *)((char *)ptr - MALLOCALIGN);
114 
115 		if (file == NULL)
116 			file = "unknown";
117 #ifdef USEGUARD
118 		if (res->ga_Magic == GAFREE) {
119 			printf("free: duplicate free @ %p from %s:%d\n",
120 			    ptr, file, line);
121 			return;
122 		}
123 		if (res->ga_Magic != GAMAGIC) {
124 			size_t dump_bytes;
125 
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",
129 			    ptr, file, line);
130 		}
131 		res->ga_Magic = GAFREE;
132 #endif
133 #ifdef USEENDGUARD
134 		if (*((signed char *)res + res->ga_Bytes - 1) == -1) {
135 			printf("free: duplicate2 free @ %p from %s:%d\n",
136 			    ptr, file, line);
137 			return;
138 		}
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;
143 #endif
144 
145 		bytes = res->ga_Bytes;
146 		zfree(&MallocPool, res, bytes);
147 #ifdef DMALLOCDEBUG
148 		--MallocCount;
149 #endif
150 	}
151 }
152 
153 
154 void *
155 Calloc(size_t n1, size_t n2, const char *file, int line)
156 {
157 	uintptr_t bytes = (uintptr_t)n1 * (uintptr_t)n2;
158 	void *res;
159 
160 	if ((res = Malloc(bytes, file, line)) != NULL) {
161 		bzero(res, bytes);
162 #ifdef DMALLOCDEBUG
163 		if (++MallocCount > MallocMax)
164 			MallocMax = MallocCount;
165 #endif
166 	}
167 	return (res);
168 }
169 
170 /*
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.
174  */
175 
176 void *
177 Realloc(void *ptr, size_t size, const char *file, int line)
178 {
179 	void *res;
180 	size_t old;
181 
182 	if ((res = Malloc(size, file, line)) != NULL) {
183 		if (ptr != NULL) {
184 			Guard *g = (Guard *)((char *)ptr - MALLOCALIGN);
185 
186 			old = g->ga_Bytes - MALLOCALIGN;
187 			if (old < size)
188 				bcopy(ptr, res, old);
189 			else
190 				bcopy(ptr, res, size);
191 			Free(ptr, file, line);
192 		} else {
193 #ifdef DMALLOCDEBUG
194 			if (++MallocCount > MallocMax)
195 				MallocMax = MallocCount;
196 #ifdef EXITSTATS
197 			if (DidAtExit == 0) {
198 				DidAtExit = 1;
199 				atexit(mallocstats);
200 			}
201 #endif
202 #endif
203 		}
204 	}
205 	return (res);
206 }
207 
208 void *
209 Reallocf(void *ptr, size_t size, const char *file, int line)
210 {
211 	void *res;
212 
213 	if ((res = Realloc(ptr, size, file, line)) == NULL)
214 		Free(ptr, file, line);
215 	return (res);
216 }
217 
218 #ifdef DMALLOCDEBUG
219 
220 void
221 mallocstats(void)
222 {
223 	printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
224 #ifdef ZALLOCDEBUG
225 	zallocstats(&MallocPool);
226 #endif
227 }
228 
229 #endif
230