xref: /illumos-gate/usr/src/boot/libsa/zalloc_malloc.c (revision 22028508fd28d36ff74dc02c5774a8ba1f0db045)
1*22028508SToomas Soome /*
2*22028508SToomas Soome  * This module derived from code donated to the FreeBSD Project by
3*22028508SToomas Soome  * Matthew Dillon <dillon@backplane.com>
4*22028508SToomas Soome  *
5*22028508SToomas Soome  * Copyright (c) 1998 The FreeBSD Project
6*22028508SToomas Soome  * All rights reserved.
7*22028508SToomas Soome  *
8*22028508SToomas Soome  * Redistribution and use in source and binary forms, with or without
9*22028508SToomas Soome  * modification, are permitted provided that the following conditions
10*22028508SToomas Soome  * are met:
11*22028508SToomas Soome  * 1. Redistributions of source code must retain the above copyright
12*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer.
13*22028508SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
14*22028508SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
15*22028508SToomas Soome  *    documentation and/or other materials provided with the distribution.
16*22028508SToomas Soome  *
17*22028508SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18*22028508SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19*22028508SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20*22028508SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21*22028508SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22*22028508SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23*22028508SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24*22028508SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25*22028508SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26*22028508SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27*22028508SToomas Soome  * SUCH DAMAGE.
28*22028508SToomas Soome  */
29*22028508SToomas Soome 
30*22028508SToomas Soome #include <sys/cdefs.h>
31*22028508SToomas Soome 
32*22028508SToomas Soome /*
33*22028508SToomas Soome  * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk
34*22028508SToomas Soome  */
35*22028508SToomas Soome 
36*22028508SToomas Soome #include "zalloc_defs.h"
37*22028508SToomas Soome 
38*22028508SToomas Soome static MemPool	MallocPool;
39*22028508SToomas Soome 
40*22028508SToomas Soome #ifdef DMALLOCDEBUG
41*22028508SToomas Soome static int MallocMax;
42*22028508SToomas Soome static int MallocCount;
43*22028508SToomas Soome 
44*22028508SToomas Soome void mallocstats(void);
45*22028508SToomas Soome #endif
46*22028508SToomas Soome 
47*22028508SToomas Soome #ifdef malloc
48*22028508SToomas Soome #undef malloc
49*22028508SToomas Soome #undef free
50*22028508SToomas Soome #endif
51*22028508SToomas Soome 
52*22028508SToomas Soome static void *Malloc_align(size_t, size_t);
53*22028508SToomas Soome 
54*22028508SToomas Soome void *
Malloc(size_t bytes,const char * file __unused,int line __unused)55*22028508SToomas Soome Malloc(size_t bytes, const char *file __unused, int line __unused)
56*22028508SToomas Soome {
57*22028508SToomas Soome 	return (Malloc_align(bytes, 1));
58*22028508SToomas Soome }
59*22028508SToomas Soome 
60*22028508SToomas Soome void *
Memalign(size_t alignment,size_t bytes,const char * file __unused,int line __unused)61*22028508SToomas Soome Memalign(size_t alignment, size_t bytes, const char *file __unused,
62*22028508SToomas Soome     int line __unused)
63*22028508SToomas Soome {
64*22028508SToomas Soome 	if (alignment == 0)
65*22028508SToomas Soome 		alignment = 1;
66*22028508SToomas Soome 
67*22028508SToomas Soome 	return (Malloc_align(bytes, alignment));
68*22028508SToomas Soome }
69*22028508SToomas Soome 
70*22028508SToomas Soome static void *
Malloc_align(size_t bytes,size_t alignment)71*22028508SToomas Soome Malloc_align(size_t bytes, size_t alignment)
72*22028508SToomas Soome {
73*22028508SToomas Soome 	Guard *res;
74*22028508SToomas Soome 
75*22028508SToomas Soome #ifdef USEENDGUARD
76*22028508SToomas Soome 	bytes += MALLOCALIGN + 1;
77*22028508SToomas Soome #else
78*22028508SToomas Soome 	bytes += MALLOCALIGN;
79*22028508SToomas Soome #endif
80*22028508SToomas Soome 
81*22028508SToomas Soome 	while ((res = znalloc(&MallocPool, bytes, alignment)) == NULL) {
82*22028508SToomas Soome 		int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
83*22028508SToomas Soome 		char *base;
84*22028508SToomas Soome 
85*22028508SToomas Soome 		if ((base = sbrk(incr)) == (char *)-1)
86*22028508SToomas Soome 			return (NULL);
87*22028508SToomas Soome 		zextendPool(&MallocPool, base, incr);
88*22028508SToomas Soome 		zfree(&MallocPool, base, incr);
89*22028508SToomas Soome 	}
90*22028508SToomas Soome #ifdef DMALLOCDEBUG
91*22028508SToomas Soome 	if (++MallocCount > MallocMax)
92*22028508SToomas Soome 		MallocMax = MallocCount;
93*22028508SToomas Soome #endif
94*22028508SToomas Soome #ifdef USEGUARD
95*22028508SToomas Soome 	res->ga_Magic = GAMAGIC;
96*22028508SToomas Soome #endif
97*22028508SToomas Soome 	res->ga_Bytes = bytes;
98*22028508SToomas Soome #ifdef USEENDGUARD
99*22028508SToomas Soome 	*((signed char *)res + bytes - 1) = -2;
100*22028508SToomas Soome #endif
101*22028508SToomas Soome 
102*22028508SToomas Soome 	return ((char *)res + MALLOCALIGN);
103*22028508SToomas Soome }
104*22028508SToomas Soome 
105*22028508SToomas Soome void
Free(void * ptr,const char * file,int line)106*22028508SToomas Soome Free(void *ptr, const char *file, int line)
107*22028508SToomas Soome {
108*22028508SToomas Soome 	size_t bytes;
109*22028508SToomas Soome 
110*22028508SToomas Soome 	if (ptr != NULL) {
111*22028508SToomas Soome 		Guard *res = (void *)((char *)ptr - MALLOCALIGN);
112*22028508SToomas Soome 
113*22028508SToomas Soome 		if (file == NULL)
114*22028508SToomas Soome 			file = "unknown";
115*22028508SToomas Soome #ifdef USEGUARD
116*22028508SToomas Soome 		if (res->ga_Magic == GAFREE) {
117*22028508SToomas Soome 			printf("free: duplicate free @ %p from %s:%d\n",
118*22028508SToomas Soome 			    ptr, file, line);
119*22028508SToomas Soome 			return;
120*22028508SToomas Soome 		}
121*22028508SToomas Soome 		if (res->ga_Magic != GAMAGIC)
122*22028508SToomas Soome 			panic("free: guard1 fail @ %p from %s:%d",
123*22028508SToomas Soome 			    ptr, file, line);
124*22028508SToomas Soome 		res->ga_Magic = GAFREE;
125*22028508SToomas Soome #endif
126*22028508SToomas Soome #ifdef USEENDGUARD
127*22028508SToomas Soome 		if (*((signed char *)res + res->ga_Bytes - 1) == -1) {
128*22028508SToomas Soome 			printf("free: duplicate2 free @ %p from %s:%d\n",
129*22028508SToomas Soome 			    ptr, file, line);
130*22028508SToomas Soome 			return;
131*22028508SToomas Soome 		}
132*22028508SToomas Soome 		if (*((signed char *)res + res->ga_Bytes - 1) != -2)
133*22028508SToomas Soome 			panic("free: guard2 fail @ %p + %zu from %s:%d",
134*22028508SToomas Soome 			    ptr, res->ga_Bytes - MALLOCALIGN, file, line);
135*22028508SToomas Soome 		*((signed char *)res + res->ga_Bytes - 1) = -1;
136*22028508SToomas Soome #endif
137*22028508SToomas Soome 
138*22028508SToomas Soome 		bytes = res->ga_Bytes;
139*22028508SToomas Soome 		zfree(&MallocPool, res, bytes);
140*22028508SToomas Soome #ifdef DMALLOCDEBUG
141*22028508SToomas Soome 		--MallocCount;
142*22028508SToomas Soome #endif
143*22028508SToomas Soome 	}
144*22028508SToomas Soome }
145*22028508SToomas Soome 
146*22028508SToomas Soome void *
Calloc(size_t n1,size_t n2,const char * file,int line)147*22028508SToomas Soome Calloc(size_t n1, size_t n2, const char *file, int line)
148*22028508SToomas Soome {
149*22028508SToomas Soome 	uintptr_t bytes = (uintptr_t)n1 * (uintptr_t)n2;
150*22028508SToomas Soome 	void *res;
151*22028508SToomas Soome 
152*22028508SToomas Soome 	if ((res = Malloc(bytes, file, line)) != NULL) {
153*22028508SToomas Soome 		bzero(res, bytes);
154*22028508SToomas Soome #ifdef DMALLOCDEBUG
155*22028508SToomas Soome 		if (++MallocCount > MallocMax)
156*22028508SToomas Soome 			MallocMax = MallocCount;
157*22028508SToomas Soome #endif
158*22028508SToomas Soome 	}
159*22028508SToomas Soome 	return (res);
160*22028508SToomas Soome }
161*22028508SToomas Soome 
162*22028508SToomas Soome /*
163*22028508SToomas Soome  * realloc() - I could be fancier here and free the old buffer before
164*22028508SToomas Soome  *	       allocating the new one (saving potential fragmentation
165*22028508SToomas Soome  *	       and potential buffer copies).  But I don't bother.
166*22028508SToomas Soome  */
167*22028508SToomas Soome 
168*22028508SToomas Soome void *
Realloc(void * ptr,size_t size,const char * file,int line)169*22028508SToomas Soome Realloc(void *ptr, size_t size, const char *file, int line)
170*22028508SToomas Soome {
171*22028508SToomas Soome 	void *res;
172*22028508SToomas Soome 	size_t old;
173*22028508SToomas Soome 
174*22028508SToomas Soome 	if ((res = Malloc(size, file, line)) != NULL) {
175*22028508SToomas Soome 		if (ptr != NULL) {
176*22028508SToomas Soome 			Guard *g = (Guard *)((char *)ptr - MALLOCALIGN);
177*22028508SToomas Soome 
178*22028508SToomas Soome 			old = g->ga_Bytes - MALLOCALIGN;
179*22028508SToomas Soome 			if (old < size)
180*22028508SToomas Soome 				bcopy(ptr, res, old);
181*22028508SToomas Soome 			else
182*22028508SToomas Soome 				bcopy(ptr, res, size);
183*22028508SToomas Soome 			Free(ptr, file, line);
184*22028508SToomas Soome 		} else {
185*22028508SToomas Soome #ifdef DMALLOCDEBUG
186*22028508SToomas Soome 			if (++MallocCount > MallocMax)
187*22028508SToomas Soome 				MallocMax = MallocCount;
188*22028508SToomas Soome #ifdef EXITSTATS
189*22028508SToomas Soome 			if (DidAtExit == 0) {
190*22028508SToomas Soome 				DidAtExit = 1;
191*22028508SToomas Soome 				atexit(mallocstats);
192*22028508SToomas Soome 			}
193*22028508SToomas Soome #endif
194*22028508SToomas Soome #endif
195*22028508SToomas Soome 		}
196*22028508SToomas Soome 	}
197*22028508SToomas Soome 	return (res);
198*22028508SToomas Soome }
199*22028508SToomas Soome 
200*22028508SToomas Soome void *
Reallocf(void * ptr,size_t size,const char * file,int line)201*22028508SToomas Soome Reallocf(void *ptr, size_t size, const char *file, int line)
202*22028508SToomas Soome {
203*22028508SToomas Soome 	void *res;
204*22028508SToomas Soome 
205*22028508SToomas Soome 	if ((res = Realloc(ptr, size, file, line)) == NULL)
206*22028508SToomas Soome 		Free(ptr, file, line);
207*22028508SToomas Soome 	return (res);
208*22028508SToomas Soome }
209*22028508SToomas Soome 
210*22028508SToomas Soome #ifdef DMALLOCDEBUG
211*22028508SToomas Soome 
212*22028508SToomas Soome void
mallocstats(void)213*22028508SToomas Soome mallocstats(void)
214*22028508SToomas Soome {
215*22028508SToomas Soome 	printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
216*22028508SToomas Soome #ifdef ZALLOCDEBUG
217*22028508SToomas Soome 	zallocstats(&MallocPool);
218*22028508SToomas Soome #endif
219*22028508SToomas Soome }
220*22028508SToomas Soome 
221*22028508SToomas Soome #endif
222