summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/malloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/malloc.c')
-rw-r--r--src/lib/libc/stdlib/malloc.c67
1 files changed, 20 insertions, 47 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c
index 603cc55f18..0af2e2fdea 100644
--- a/src/lib/libc/stdlib/malloc.c
+++ b/src/lib/libc/stdlib/malloc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: malloc.c,v 1.104 2008/10/29 14:05:15 otto Exp $ */ 1/* $OpenBSD: malloc.c,v 1.105 2008/11/02 08:50:41 otto Exp $ */
2/* 2/*
3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> 3 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net>
4 * 4 *
@@ -149,7 +149,6 @@ static int malloc_hint; /* call madvice on free pages? */
149static int malloc_junk; /* junk fill? */ 149static int malloc_junk; /* junk fill? */
150static int malloc_move; /* move allocations to end of page? */ 150static int malloc_move; /* move allocations to end of page? */
151static int malloc_realloc; /* always realloc? */ 151static int malloc_realloc; /* always realloc? */
152static int malloc_silent; /* avoid outputting warnings? */
153static int malloc_xmalloc; /* xmalloc behaviour? */ 152static int malloc_xmalloc; /* xmalloc behaviour? */
154static int malloc_zero; /* zero fill? */ 153static int malloc_zero; /* zero fill? */
155static size_t malloc_guard; /* use guard pages after allocations? */ 154static size_t malloc_guard; /* use guard pages after allocations? */
@@ -355,31 +354,6 @@ wrterror(char *p)
355 abort(); 354 abort();
356} 355}
357 356
358static void
359wrtwarning(char *p)
360{
361 char *q = " warning: ";
362 struct iovec iov[5];
363
364 if (malloc_abort)
365 wrterror(p);
366 else if (malloc_silent)
367 return;
368
369 iov[0].iov_base = __progname;
370 iov[0].iov_len = strlen(__progname);
371 iov[1].iov_base = malloc_func;
372 iov[1].iov_len = strlen(malloc_func);
373 iov[2].iov_base = q;
374 iov[2].iov_len = strlen(q);
375 iov[3].iov_base = p;
376 iov[3].iov_len = strlen(p);
377 iov[4].iov_base = "\n";
378 iov[4].iov_len = 1;
379
380 writev(STDERR_FILENO, iov, 5);
381}
382
383/* 357/*
384 * Cache maintenance. We keep at most malloc_cache pages cached. 358 * Cache maintenance. We keep at most malloc_cache pages cached.
385 * If the cache is becoming full, unmap pages in the cache for real, 359 * If the cache is becoming full, unmap pages in the cache for real,
@@ -428,7 +402,7 @@ unmap(struct dir_info *d, void *p, size_t sz)
428 } 402 }
429 } 403 }
430 if (tounmap > 0) 404 if (tounmap > 0)
431 wrtwarning("malloc cache underflow"); 405 wrterror("malloc cache underflow");
432 for (i = 0; i < malloc_cache; i++) { 406 for (i = 0; i < malloc_cache; i++) {
433 r = &d->free_regions[i]; 407 r = &d->free_regions[i];
434 if (r->p == NULL) { 408 if (r->p == NULL) {
@@ -443,9 +417,9 @@ unmap(struct dir_info *d, void *p, size_t sz)
443 } 417 }
444 } 418 }
445 if (i == malloc_cache) 419 if (i == malloc_cache)
446 wrtwarning("malloc free slot lost"); 420 wrterror("malloc free slot lost");
447 if (d->free_regions_size > malloc_cache) 421 if (d->free_regions_size > malloc_cache)
448 wrtwarning("malloc cache overflow"); 422 wrterror("malloc cache overflow");
449} 423}
450 424
451static void 425static void
@@ -525,7 +499,7 @@ map(struct dir_info *d, size_t sz, int zero_fill)
525 if (p != MAP_FAILED) 499 if (p != MAP_FAILED)
526 malloc_used += sz; 500 malloc_used += sz;
527 if (d->free_regions_size > malloc_cache) 501 if (d->free_regions_size > malloc_cache)
528 wrtwarning("malloc cache"); 502 wrterror("malloc cache");
529 /* zero fill not needed */ 503 /* zero fill not needed */
530 return p; 504 return p;
531} 505}
@@ -628,10 +602,7 @@ omalloc_init(struct dir_info *d)
628 malloc_junk = 1; 602 malloc_junk = 1;
629 break; 603 break;
630 case 'n': 604 case 'n':
631 malloc_silent = 0;
632 break;
633 case 'N': 605 case 'N':
634 malloc_silent = 1;
635 break; 606 break;
636 case 'p': 607 case 'p':
637 malloc_move = 0; 608 malloc_move = 0;
@@ -660,7 +631,7 @@ omalloc_init(struct dir_info *d)
660 default: 631 default:
661 j = malloc_abort; 632 j = malloc_abort;
662 malloc_abort = 0; 633 malloc_abort = 0;
663 wrtwarning("unknown char in MALLOC_OPTIONS"); 634 wrterror("unknown char in MALLOC_OPTIONS");
664 malloc_abort = j; 635 malloc_abort = j;
665 break; 636 break;
666 } 637 }
@@ -675,9 +646,11 @@ omalloc_init(struct dir_info *d)
675 malloc_junk = 1; 646 malloc_junk = 1;
676 647
677#ifdef MALLOC_STATS 648#ifdef MALLOC_STATS
678 if (malloc_stats && (atexit(malloc_exit) == -1)) 649 if (malloc_stats && (atexit(malloc_exit) == -1)) {
679 wrtwarning("atexit(2) failed." 650 char *q = "malloc() warning: atexit(2) failed."
680 " Will not be able to dump malloc stats on exit"); 651 " Will not be able to dump stats on exit\n";
652 write(STDERR_FILENO, q, strlen(q));
653 }
681#endif /* MALLOC_STATS */ 654#endif /* MALLOC_STATS */
682 655
683 d->regions_bits = 9; 656 d->regions_bits = 9;
@@ -1024,11 +997,11 @@ free_bytes(struct dir_info *d, struct region_info *r, void *ptr)
1024 i = ((uintptr_t)ptr & MALLOC_PAGEMASK) >> info->shift; 997 i = ((uintptr_t)ptr & MALLOC_PAGEMASK) >> info->shift;
1025 998
1026 if ((uintptr_t)ptr & ((1UL << (info->shift)) - 1)) { 999 if ((uintptr_t)ptr & ((1UL << (info->shift)) - 1)) {
1027 wrtwarning("modified chunk-pointer"); 1000 wrterror("modified chunk-pointer");
1028 return; 1001 return;
1029 } 1002 }
1030 if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) { 1003 if (info->bits[i / MALLOC_BITS] & (1UL << (i % MALLOC_BITS))) {
1031 wrtwarning("chunk is already free"); 1004 wrterror("chunk is already free");
1032 return; 1005 return;
1033 } 1006 }
1034 1007
@@ -1149,7 +1122,7 @@ malloc_recurse(void)
1149 1122
1150 if (noprint == 0) { 1123 if (noprint == 0) {
1151 noprint = 1; 1124 noprint = 1;
1152 wrtwarning("recursive call"); 1125 wrterror("recursive call");
1153 } 1126 }
1154 malloc_active--; 1127 malloc_active--;
1155 _MALLOC_UNLOCK(); 1128 _MALLOC_UNLOCK();
@@ -1197,14 +1170,14 @@ ofree(void *p)
1197 1170
1198 r = find(&g_pool, p); 1171 r = find(&g_pool, p);
1199 if (r == NULL) { 1172 if (r == NULL) {
1200 wrtwarning("bogus pointer (double free?)"); 1173 wrterror("bogus pointer (double free?)");
1201 return; 1174 return;
1202 } 1175 }
1203 REALSIZE(sz, r); 1176 REALSIZE(sz, r);
1204 if (sz > MALLOC_MAXCHUNK) { 1177 if (sz > MALLOC_MAXCHUNK) {
1205 if (sz - malloc_guard >= MALLOC_PAGESIZE - MALLOC_MINSIZE) { 1178 if (sz - malloc_guard >= MALLOC_PAGESIZE - MALLOC_MINSIZE) {
1206 if (r->p != p) 1179 if (r->p != p)
1207 wrtwarning("bogus pointer"); 1180 wrterror("bogus pointer");
1208 } else { 1181 } else {
1209#if notyetbecause_of_realloc 1182#if notyetbecause_of_realloc
1210 /* shifted towards the end */ 1183 /* shifted towards the end */
@@ -1217,7 +1190,7 @@ ofree(void *p)
1217 } 1190 }
1218 if (malloc_guard) { 1191 if (malloc_guard) {
1219 if (sz < malloc_guard) 1192 if (sz < malloc_guard)
1220 wrtwarning("guard size"); 1193 wrterror("guard size");
1221 if (!malloc_freeprot) { 1194 if (!malloc_freeprot) {
1222 if (mprotect((char *)p + PAGEROUND(sz) - 1195 if (mprotect((char *)p + PAGEROUND(sz) -
1223 malloc_guard, malloc_guard, 1196 malloc_guard, malloc_guard,
@@ -1243,7 +1216,7 @@ ofree(void *p)
1243 if (p != NULL) { 1216 if (p != NULL) {
1244 r = find(&g_pool, p); 1217 r = find(&g_pool, p);
1245 if (r == NULL) { 1218 if (r == NULL) {
1246 wrtwarning("bogus pointer (double free?)"); 1219 wrterror("bogus pointer (double free?)");
1247 return; 1220 return;
1248 } 1221 }
1249 free_bytes(&g_pool, r, p); 1222 free_bytes(&g_pool, r, p);
@@ -1285,7 +1258,7 @@ orealloc(void *p, size_t newsz)
1285 1258
1286 r = find(&g_pool, p); 1259 r = find(&g_pool, p);
1287 if (r == NULL) { 1260 if (r == NULL) {
1288 wrtwarning("bogus pointer (double free?)"); 1261 wrterror("bogus pointer (double free?)");
1289 return NULL; 1262 return NULL;
1290 } 1263 }
1291 if (newsz >= SIZE_MAX - malloc_guard - MALLOC_PAGESIZE) { 1264 if (newsz >= SIZE_MAX - malloc_guard - MALLOC_PAGESIZE) {
@@ -1297,7 +1270,7 @@ orealloc(void *p, size_t newsz)
1297 goldsz = oldsz; 1270 goldsz = oldsz;
1298 if (oldsz > MALLOC_MAXCHUNK) { 1271 if (oldsz > MALLOC_MAXCHUNK) {
1299 if (oldsz < malloc_guard) 1272 if (oldsz < malloc_guard)
1300 wrtwarning("guard size"); 1273 wrterror("guard size");
1301 oldsz -= malloc_guard; 1274 oldsz -= malloc_guard;
1302 } 1275 }
1303 1276