zalloc_malloc.c (84221) | zalloc_malloc.c (100394) |
---|---|
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 --- 14 unchanged lines hidden (view full) --- 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#include <sys/cdefs.h> | 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 --- 14 unchanged lines hidden (view full) --- 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#include <sys/cdefs.h> |
31__FBSDID("$FreeBSD: head/lib/libstand/zalloc_malloc.c 84221 2001-09-30 22:28:01Z dillon $"); | 31__FBSDID("$FreeBSD: head/lib/libstand/zalloc_malloc.c 100394 2002-07-20 04:18:20Z peter $"); |
32 33/* 34 * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk 35 */ 36 37#include "zalloc_defs.h" 38 39static MemPool MallocPool; --- 15 unchanged lines hidden (view full) --- 55free_region(void *start, void *end) 56{ 57 zextendPool(&MallocPool, start, (caddr_t)end - (caddr_t)start); 58 zfree(&MallocPool, start, (caddr_t)end - (caddr_t)start); 59} 60#endif 61 62void * | 32 33/* 34 * MALLOC.C - malloc equivalent, runs on top of zalloc and uses sbrk 35 */ 36 37#include "zalloc_defs.h" 38 39static MemPool MallocPool; --- 15 unchanged lines hidden (view full) --- 55free_region(void *start, void *end) 56{ 57 zextendPool(&MallocPool, start, (caddr_t)end - (caddr_t)start); 58 zfree(&MallocPool, start, (caddr_t)end - (caddr_t)start); 59} 60#endif 61 62void * |
63malloc(size_t bytes) | 63Malloc(size_t bytes, const char *file, int line) |
64{ 65 Guard *res; 66 67#ifdef USEENDGUARD 68 bytes += MALLOCALIGN + 1; 69#else 70 bytes += MALLOCALIGN; 71#endif --- 17 unchanged lines hidden (view full) --- 89 res->ga_Bytes = bytes; 90#ifdef USEENDGUARD 91 *((char *)res + bytes - 1) = -2; 92#endif 93 return((char *)res + MALLOCALIGN); 94} 95 96void | 64{ 65 Guard *res; 66 67#ifdef USEENDGUARD 68 bytes += MALLOCALIGN + 1; 69#else 70 bytes += MALLOCALIGN; 71#endif --- 17 unchanged lines hidden (view full) --- 89 res->ga_Bytes = bytes; 90#ifdef USEENDGUARD 91 *((char *)res + bytes - 1) = -2; 92#endif 93 return((char *)res + MALLOCALIGN); 94} 95 96void |
97free(void *ptr) | 97Free(void *ptr, const char *file, int line) |
98{ 99 size_t bytes; 100 101 if (ptr != NULL) { 102 Guard *res = (void *)((char *)ptr - MALLOCALIGN); 103 | 98{ 99 size_t bytes; 100 101 if (ptr != NULL) { 102 Guard *res = (void *)((char *)ptr - MALLOCALIGN); 103 |
104 if (file == NULL) 105 file = "unknown"; |
|
104#ifdef USEGUARD | 106#ifdef USEGUARD |
107 if (res->ga_Magic == GAFREE) { 108 printf("free: duplicate free @ %p from %s:%d\n", ptr, file, line); 109 return; 110 } |
|
105 if (res->ga_Magic != GAMAGIC) | 111 if (res->ga_Magic != GAMAGIC) |
106 panic("free: guard1 fail @ %p", ptr); 107 res->ga_Magic = -1; | 112 panic("free: guard1 fail @ %p from %s:%p", ptr, file, line); 113 res->ga_Magic = GAFREE; |
108#endif 109#ifdef USEENDGUARD | 114#endif 115#ifdef USEENDGUARD |
116 if (*((char *)res + res->ga_Bytes - 1) == -1) { 117 printf("free: duplicate2 free @ %p from %s:%d\n", ptr, file, line); 118 return; 119 } |
|
110 if (*((char *)res + res->ga_Bytes - 1) != -2) | 120 if (*((char *)res + res->ga_Bytes - 1) != -2) |
111 panic("free: guard2 fail @ %p + %d", ptr, res->ga_Bytes - MALLOCALIGN); | 121 panic("free: guard2 fail @ %p + %d from %s:%d", ptr, res->ga_Bytes - MALLOCALIGN, file, line); |
112 *((char *)res + res->ga_Bytes - 1) = -1; 113#endif 114 115 bytes = res->ga_Bytes; 116 zfree(&MallocPool, res, bytes); 117#ifdef DMALLOCDEBUG 118 --MallocCount; 119#endif 120 } 121} 122 123 124void * | 122 *((char *)res + res->ga_Bytes - 1) = -1; 123#endif 124 125 bytes = res->ga_Bytes; 126 zfree(&MallocPool, res, bytes); 127#ifdef DMALLOCDEBUG 128 --MallocCount; 129#endif 130 } 131} 132 133 134void * |
125calloc(size_t n1, size_t n2) | 135Calloc(size_t n1, size_t n2, const char *file, int line) |
126{ 127 iaddr_t bytes = (iaddr_t)n1 * (iaddr_t)n2; 128 void *res; 129 | 136{ 137 iaddr_t bytes = (iaddr_t)n1 * (iaddr_t)n2; 138 void *res; 139 |
130 if ((res = malloc(bytes)) != NULL) { | 140 if ((res = Malloc(bytes, file, line)) != NULL) { |
131 bzero(res, bytes); 132#ifdef DMALLOCDEBUG 133 if (++MallocCount > MallocMax) 134 MallocMax = MallocCount; 135#endif 136 } 137 return(res); 138} 139 140/* 141 * realloc() - I could be fancier here and free the old buffer before 142 * allocating the new one (saving potential fragmentation 143 * and potential buffer copies). But I don't bother. 144 */ 145 146void * | 141 bzero(res, bytes); 142#ifdef DMALLOCDEBUG 143 if (++MallocCount > MallocMax) 144 MallocMax = MallocCount; 145#endif 146 } 147 return(res); 148} 149 150/* 151 * realloc() - I could be fancier here and free the old buffer before 152 * allocating the new one (saving potential fragmentation 153 * and potential buffer copies). But I don't bother. 154 */ 155 156void * |
147realloc(void *ptr, size_t size) | 157Realloc(void *ptr, size_t size, const char *file, int line) |
148{ 149 void *res; 150 size_t old; 151 | 158{ 159 void *res; 160 size_t old; 161 |
152 if ((res = malloc(size)) != NULL) { | 162 if ((res = Malloc(size, file, line)) != NULL) { |
153 if (ptr) { 154 old = *(size_t *)((char *)ptr - MALLOCALIGN) - MALLOCALIGN; 155 if (old < size) 156 bcopy(ptr, res, old); 157 else 158 bcopy(ptr, res, size); | 163 if (ptr) { 164 old = *(size_t *)((char *)ptr - MALLOCALIGN) - MALLOCALIGN; 165 if (old < size) 166 bcopy(ptr, res, old); 167 else 168 bcopy(ptr, res, size); |
159 free(ptr); | 169 Free(ptr, file, line); |
160 } else { 161#ifdef DMALLOCDEBUG 162 if (++MallocCount > MallocMax) 163 MallocMax = MallocCount; 164#ifdef EXITSTATS 165 if (DidAtExit == 0) { 166 DidAtExit = 1; 167 atexit(mallocstats); 168 } 169#endif 170#endif 171 } 172 } 173 return(res); 174} 175 176void * | 170 } else { 171#ifdef DMALLOCDEBUG 172 if (++MallocCount > MallocMax) 173 MallocMax = MallocCount; 174#ifdef EXITSTATS 175 if (DidAtExit == 0) { 176 DidAtExit = 1; 177 atexit(mallocstats); 178 } 179#endif 180#endif 181 } 182 } 183 return(res); 184} 185 186void * |
177reallocf(void *ptr, size_t size) | 187Reallocf(void *ptr, size_t size, const char *file, int line) |
178{ 179 void *res; 180 | 188{ 189 void *res; 190 |
181 if ((res = realloc(ptr, size)) == NULL) 182 free(ptr); | 191 if ((res = Realloc(ptr, size, file, line)) == NULL) 192 Free(ptr, file, line); |
183 return(res); 184} 185 186#ifdef DMALLOCDEBUG 187 188void 189mallocstats(void) 190{ 191 printf("Active Allocations: %d/%d\n", MallocCount, MallocMax); 192#ifdef ZALLOCDEBUG 193 zallocstats(&MallocPool); 194#endif 195} 196 197#endif 198 | 193 return(res); 194} 195 196#ifdef DMALLOCDEBUG 197 198void 199mallocstats(void) 200{ 201 printf("Active Allocations: %d/%d\n", MallocCount, MallocMax); 202#ifdef ZALLOCDEBUG 203 zallocstats(&MallocPool); 204#endif 205} 206 207#endif 208 |