1199767f8SToomas Soome /*
2f5700834SToomas Soome  * This module derived from code donated to the FreeBSD Project by
3199767f8SToomas Soome  * Matthew Dillon <dillon@backplane.com>
4199767f8SToomas Soome  *
5199767f8SToomas Soome  * Copyright (c) 1998 The FreeBSD Project
6199767f8SToomas Soome  * All rights reserved.
7199767f8SToomas Soome  *
8199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
9199767f8SToomas Soome  * modification, are permitted provided that the following conditions
10199767f8SToomas Soome  * are met:
11199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
12199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
13199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
14199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
15199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
16199767f8SToomas Soome  *
17199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27199767f8SToomas Soome  * SUCH DAMAGE.
28199767f8SToomas Soome  */
29199767f8SToomas Soome 
30199767f8SToomas Soome #include <sys/cdefs.h>
31199767f8SToomas Soome 
32199767f8SToomas Soome /*
33199767f8SToomas Soome  * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk
34199767f8SToomas Soome  */
35199767f8SToomas Soome 
36199767f8SToomas Soome #include "zalloc_defs.h"
37199767f8SToomas Soome 
38199767f8SToomas Soome static MemPool	MallocPool;
39199767f8SToomas Soome 
40199767f8SToomas Soome #ifdef DMALLOCDEBUG
41199767f8SToomas Soome static int MallocMax;
42199767f8SToomas Soome static int MallocCount;
43199767f8SToomas Soome 
44199767f8SToomas Soome void mallocstats(void);
45199767f8SToomas Soome #endif
46199767f8SToomas Soome 
47199767f8SToomas Soome #ifdef malloc
48199767f8SToomas Soome #undef malloc
49199767f8SToomas Soome #undef free
50199767f8SToomas Soome #endif
51199767f8SToomas Soome 
52081aa5f6SToomas Soome static void *Malloc_align(size_t, size_t);
53081aa5f6SToomas Soome 
54199767f8SToomas Soome void *
Malloc(size_t bytes,const char * file __unused,int line __unused)558eef2ab6SToomas Soome Malloc(size_t bytes, const char *file __unused, int line __unused)
56081aa5f6SToomas Soome {
57081aa5f6SToomas Soome 	return (Malloc_align(bytes, 1));
58081aa5f6SToomas Soome }
59081aa5f6SToomas Soome 
60081aa5f6SToomas Soome void *
Memalign(size_t alignment,size_t bytes,const char * file __unused,int line __unused)61081aa5f6SToomas Soome Memalign(size_t alignment, size_t bytes, const char *file __unused,
62081aa5f6SToomas Soome     int line __unused)
63081aa5f6SToomas Soome {
64081aa5f6SToomas Soome 	if (alignment == 0)
65081aa5f6SToomas Soome 		alignment = 1;
66081aa5f6SToomas Soome 
67081aa5f6SToomas Soome 	return (Malloc_align(bytes, alignment));
68081aa5f6SToomas Soome }
69081aa5f6SToomas Soome 
70081aa5f6SToomas Soome static void *
Malloc_align(size_t bytes,size_t alignment)71081aa5f6SToomas Soome Malloc_align(size_t bytes, size_t alignment)
72199767f8SToomas Soome {
73f5700834SToomas Soome 	Guard *res;
74199767f8SToomas Soome 
75199767f8SToomas Soome #ifdef USEENDGUARD
76f5700834SToomas Soome 	bytes += MALLOCALIGN + 1;
77199767f8SToomas Soome #else
78f5700834SToomas Soome 	bytes += MALLOCALIGN;
79199767f8SToomas Soome #endif
80199767f8SToomas Soome 
81081aa5f6SToomas Soome 	while ((res = znalloc(&MallocPool, bytes, alignment)) == NULL) {
82f5700834SToomas Soome 		int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
83f5700834SToomas Soome 		char *base;
84199767f8SToomas Soome 
85f5700834SToomas Soome 		if ((base = sbrk(incr)) == (char *)-1)
86f5700834SToomas Soome 			return (NULL);
87f5700834SToomas Soome 		zextendPool(&MallocPool, base, incr);
88f5700834SToomas Soome 		zfree(&MallocPool, base, incr);
89f5700834SToomas Soome 	}
90199767f8SToomas Soome #ifdef DMALLOCDEBUG
91f5700834SToomas Soome 	if (++MallocCount > MallocMax)
92f5700834SToomas Soome 		MallocMax = MallocCount;
93199767f8SToomas Soome #endif
94199767f8SToomas Soome #ifdef USEGUARD
95f5700834SToomas Soome 	res->ga_Magic = GAMAGIC;
96199767f8SToomas Soome #endif
97f5700834SToomas Soome 	res->ga_Bytes = bytes;
98199767f8SToomas Soome #ifdef USEENDGUARD
99f5700834SToomas Soome 	*((signed char *)res + bytes - 1) = -2;
100199767f8SToomas Soome #endif
101199767f8SToomas Soome 
102f5700834SToomas Soome 	return ((char *)res + MALLOCALIGN);
103199767f8SToomas Soome }
104199767f8SToomas Soome 
105199767f8SToomas Soome void
Free(void * ptr,const char * file,int line)106199767f8SToomas Soome Free(void *ptr, const char *file, int line)
107199767f8SToomas Soome {
108f5700834SToomas Soome 	size_t bytes;
109199767f8SToomas Soome 
110f5700834SToomas Soome 	if (ptr != NULL) {
111f5700834SToomas Soome 		Guard *res = (void *)((char *)ptr - MALLOCALIGN);
112199767f8SToomas Soome 
113f5700834SToomas Soome 		if (file == NULL)
114f5700834SToomas Soome 			file = "unknown";
115199767f8SToomas Soome #ifdef USEGUARD
116f5700834SToomas Soome 		if (res->ga_Magic == GAFREE) {
117f5700834SToomas Soome 			printf("free: duplicate free @ %p from %s:%d\n",
118f5700834SToomas Soome 			    ptr, file, line);
119f5700834SToomas Soome 			return;
120f5700834SToomas Soome 		}
121f5700834SToomas Soome 		if (res->ga_Magic != GAMAGIC)
122f5700834SToomas Soome 			panic("free: guard1 fail @ %p from %s:%d",
123f5700834SToomas Soome 			    ptr, file, line);
124f5700834SToomas Soome 		res->ga_Magic = GAFREE;
125199767f8SToomas Soome #endif
126199767f8SToomas Soome #ifdef USEENDGUARD
127f5700834SToomas Soome 		if (*((signed char *)res + res->ga_Bytes - 1) == -1) {
128f5700834SToomas Soome 			printf("free: duplicate2 free @ %p from %s:%d\n",
129f5700834SToomas Soome 			    ptr, file, line);
130f5700834SToomas Soome 			return;
131f5700834SToomas Soome 		}
132f5700834SToomas Soome 		if (*((signed char *)res + res->ga_Bytes - 1) != -2)
133f5700834SToomas Soome 			panic("free: guard2 fail @ %p + %zu from %s:%d",
134f5700834SToomas Soome 			    ptr, res->ga_Bytes - MALLOCALIGN, file, line);
135f5700834SToomas Soome 		*((signed char *)res + res->ga_Bytes - 1) = -1;
136199767f8SToomas Soome #endif
137199767f8SToomas Soome 
138f5700834SToomas Soome 		bytes = res->ga_Bytes;
139f5700834SToomas Soome 		zfree(&MallocPool, res, bytes);
140199767f8SToomas Soome #ifdef DMALLOCDEBUG
141f5700834SToomas Soome 		--MallocCount;
142199767f8SToomas Soome #endif
143f5700834SToomas Soome 	}
144199767f8SToomas Soome }
145199767f8SToomas Soome 
146199767f8SToomas Soome void *
Calloc(size_t n1,size_t n2,const char * file,int line)147199767f8SToomas Soome Calloc(size_t n1, size_t n2, const char *file, int line)
148199767f8SToomas Soome {
149f5700834SToomas Soome 	uintptr_t bytes = (uintptr_t)n1 * (uintptr_t)n2;
150f5700834SToomas Soome 	void *res;
151199767f8SToomas Soome 
152f5700834SToomas Soome 	if ((res = Malloc(bytes, file, line)) != NULL) {
153f5700834SToomas Soome 		bzero(res, bytes);
154199767f8SToomas Soome #ifdef DMALLOCDEBUG
155f5700834SToomas Soome 		if (++MallocCount > MallocMax)
156f5700834SToomas Soome 			MallocMax = MallocCount;
157199767f8SToomas Soome #endif
158f5700834SToomas Soome 	}
159f5700834SToomas Soome 	return (res);
160199767f8SToomas Soome }
161199767f8SToomas Soome 
162199767f8SToomas Soome /*
163199767f8SToomas Soome  * realloc() - I could be fancier here and free the old buffer before
164f5700834SToomas Soome  *	       allocating the new one (saving potential fragmentation
165199767f8SToomas Soome  *	       and potential buffer copies).  But I don't bother.
166199767f8SToomas Soome  */
167199767f8SToomas Soome 
168199767f8SToomas Soome void *
Realloc(void * ptr,size_t size,const char * file,int line)169199767f8SToomas Soome Realloc(void *ptr, size_t size, const char *file, int line)
170199767f8SToomas Soome {
171f5700834SToomas Soome 	void *res;
172f5700834SToomas Soome 	size_t old;
173f5700834SToomas Soome 
174f5700834SToomas Soome 	if ((res = Malloc(size, file, line)) != NULL) {
175f5700834SToomas Soome 		if (ptr != NULL) {
176f5700834SToomas Soome 			Guard *g = (Guard *)((char *)ptr - MALLOCALIGN);
177f5700834SToomas Soome 
178f5700834SToomas Soome 			old = g->ga_Bytes - MALLOCALIGN;
179f5700834SToomas Soome 			if (old < size)
180f5700834SToomas Soome 				bcopy(ptr, res, old);
181f5700834SToomas Soome 			else
182f5700834SToomas Soome 				bcopy(ptr, res, size);
183f5700834SToomas Soome 			Free(ptr, file, line);
184f5700834SToomas Soome 		} else {
185199767f8SToomas Soome #ifdef DMALLOCDEBUG
186f5700834SToomas Soome 			if (++MallocCount > MallocMax)
187f5700834SToomas Soome 				MallocMax = MallocCount;
188199767f8SToomas Soome #ifdef EXITSTATS
189f5700834SToomas Soome 			if (DidAtExit == 0) {
190f5700834SToomas Soome 				DidAtExit = 1;
191f5700834SToomas Soome 				atexit(mallocstats);
192f5700834SToomas Soome 			}
193199767f8SToomas Soome #endif
194199767f8SToomas Soome #endif
195f5700834SToomas Soome 		}
196199767f8SToomas Soome 	}
197f5700834SToomas Soome 	return (res);
198199767f8SToomas Soome }
199199767f8SToomas Soome 
200199767f8SToomas Soome void *
Reallocf(void * ptr,size_t size,const char * file,int line)201199767f8SToomas Soome Reallocf(void *ptr, size_t size, const char *file, int line)
202199767f8SToomas Soome {
203f5700834SToomas Soome 	void *res;
204199767f8SToomas Soome 
205f5700834SToomas Soome 	if ((res = Realloc(ptr, size, file, line)) == NULL)
206f5700834SToomas Soome 		Free(ptr, file, line);
207f5700834SToomas Soome 	return (res);
208199767f8SToomas Soome }
209199767f8SToomas Soome 
210199767f8SToomas Soome #ifdef DMALLOCDEBUG
211199767f8SToomas Soome 
212199767f8SToomas Soome void
mallocstats(void)213199767f8SToomas Soome mallocstats(void)
214199767f8SToomas Soome {
215f5700834SToomas Soome 	printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
216199767f8SToomas Soome #ifdef ZALLOCDEBUG
217f5700834SToomas Soome 	zallocstats(&MallocPool);
218199767f8SToomas Soome #endif
219199767f8SToomas Soome }
220199767f8SToomas Soome 
221199767f8SToomas Soome #endif
222