Initial import from FreeBSD RELENG_4:
[dragonfly.git] / lib / libstand / zalloc_malloc.c
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  * $FreeBSD: src/lib/libstand/zalloc_malloc.c,v 1.5 1999/08/28 00:05:35 peter Exp $
30  */
31
32 /*
33  * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk
34  */
35
36 #include "zalloc_defs.h"
37
38 static MemPool  MallocPool;
39
40 #ifdef DMALLOCDEBUG
41 static int MallocMax;
42 static int MallocCount;
43
44 void mallocstats(void);
45 #endif
46
47 #ifdef malloc
48 #undef malloc
49 #undef free
50 #endif
51
52 #ifdef __alpha__
53 void
54 free_region(void *start, void *end)
55 {
56     zextendPool(&MallocPool, start, (caddr_t)end - (caddr_t)start);
57     zfree(&MallocPool, start, (caddr_t)end - (caddr_t)start);
58 }
59 #endif
60
61 void *
62 malloc(size_t bytes)
63 {
64     Guard *res;
65
66 #ifdef USEENDGUARD
67     bytes += MALLOCALIGN + 1;
68 #else
69     bytes += MALLOCALIGN;
70 #endif
71
72     while ((res = znalloc(&MallocPool, bytes)) == NULL) {
73         int incr = (bytes + BLKEXTENDMASK) & ~BLKEXTENDMASK;
74         char *base;
75
76         if ((base = sbrk(incr)) == (char *)-1)
77             return(NULL);
78         zextendPool(&MallocPool, base, incr);
79         zfree(&MallocPool, base, incr);
80     }
81 #ifdef DMALLOCDEBUG
82     if (++MallocCount > MallocMax)
83         MallocMax = MallocCount;
84 #endif
85 #ifdef USEGUARD
86     res->ga_Magic = GAMAGIC;
87 #endif
88     res->ga_Bytes = bytes;
89 #ifdef USEENDGUARD
90     *((char *)res + bytes - 1) = -2;
91 #endif
92     return((char *)res + MALLOCALIGN);
93 }
94
95 void
96 free(void *ptr)
97 {
98     size_t bytes;
99
100     if (ptr != NULL) {
101         Guard *res = (void *)((char *)ptr - MALLOCALIGN);
102
103 #ifdef USEGUARD
104         if (res->ga_Magic != GAMAGIC)
105             panic("free: guard1 fail @ %p", ptr);
106         res->ga_Magic = -1;
107 #endif
108 #ifdef USEENDGUARD
109         if (*((char *)res + res->ga_Bytes - 1) != -2)
110             panic("free: guard2 fail @ %p + %d", ptr, res->ga_Bytes - MALLOCALIGN);
111         *((char *)res + res->ga_Bytes - 1) = -1;
112 #endif
113
114         bytes = res->ga_Bytes;
115         zfree(&MallocPool, res, bytes);
116 #ifdef DMALLOCDEBUG
117         --MallocCount;
118 #endif
119     }
120 }
121
122
123 void *
124 calloc(size_t n1, size_t n2)
125 {
126     iaddr_t bytes = (iaddr_t)n1 * (iaddr_t)n2;
127     void *res;
128
129     if ((res = malloc(bytes)) != NULL) {
130         bzero(res, bytes);
131 #ifdef DMALLOCDEBUG
132         if (++MallocCount > MallocMax)
133             MallocMax = MallocCount;
134 #endif
135     }
136     return(res);
137 }
138
139 /*
140  * realloc() - I could be fancier here and free the old buffer before
141  *             allocating the new one (saving potential fragmentation
142  *             and potential buffer copies).  But I don't bother.
143  */
144
145 void *
146 realloc(void *ptr, size_t size)
147 {
148     void *res;
149     size_t old;
150
151     if ((res = malloc(size)) != NULL) {
152         if (ptr) {
153             old = *(size_t *)((char *)ptr - MALLOCALIGN) - MALLOCALIGN;
154             if (old < size)
155                 bcopy(ptr, res, old);
156             else
157                 bcopy(ptr, res, size);
158             free(ptr);
159         } else {
160 #ifdef DMALLOCDEBUG
161             if (++MallocCount > MallocMax)
162                 MallocMax = MallocCount;
163 #ifdef EXITSTATS
164             if (DidAtExit == 0) {
165                 DidAtExit = 1;
166                 atexit(mallocstats);
167             }
168 #endif
169 #endif
170         }
171     }
172     return(res);
173 }
174
175 void *
176 reallocf(void *ptr, size_t size)
177 {
178     void *res;
179
180     if ((res = realloc(ptr, size)) == NULL)
181         free(ptr);
182     return(res);
183 }
184
185 #ifdef DMALLOCDEBUG
186
187 void
188 mallocstats(void)
189 {
190     printf("Active Allocations: %d/%d\n", MallocCount, MallocMax);
191 #ifdef ZALLOCDEBUG
192     zallocstats(&MallocPool);
193 #endif
194 }
195
196 #endif
197