diff options
author | tedu <> | 2014-04-23 15:07:27 +0000 |
---|---|---|
committer | tedu <> | 2014-04-23 15:07:27 +0000 |
commit | 909fa81274d3ab37a13fd6a376b38c3652c1ec4f (patch) | |
tree | 394712d053d9922ec73212d1facca1e1c5867b1e /src | |
parent | bef35d38b7cc486e4c4a429c36326b26d4fbf2aa (diff) | |
download | openbsd-909fa81274d3ab37a13fd6a376b38c3652c1ec4f.tar.gz openbsd-909fa81274d3ab37a13fd6a376b38c3652c1ec4f.tar.bz2 openbsd-909fa81274d3ab37a13fd6a376b38c3652c1ec4f.zip |
remove Z option and default to something halfway to J.
we always junk small chunks now, and the first part of pages,
but only after free. J still does the old thing. j disables everything.
Consider experimental as we evaluate performance in the real world.
ok otto
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 54 |
1 files changed, 22 insertions, 32 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 6c38d6ad87..e7a113c8cd 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.157 2014/04/23 10:47:15 espie Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.158 2014/04/23 15:07:27 tedu Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011 Otto Moerbeek <otto@drijf.net> |
4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
@@ -167,7 +167,6 @@ struct malloc_readonly { | |||
167 | int malloc_move; /* move allocations to end of page? */ | 167 | int malloc_move; /* move allocations to end of page? */ |
168 | int malloc_realloc; /* always realloc? */ | 168 | int malloc_realloc; /* always realloc? */ |
169 | int malloc_xmalloc; /* xmalloc behaviour? */ | 169 | int malloc_xmalloc; /* xmalloc behaviour? */ |
170 | int malloc_zero; /* zero fill? */ | ||
171 | size_t malloc_guard; /* use guard pages after allocations? */ | 170 | size_t malloc_guard; /* use guard pages after allocations? */ |
172 | u_int malloc_cache; /* free pages we cache */ | 171 | u_int malloc_cache; /* free pages we cache */ |
173 | #ifdef MALLOC_STATS | 172 | #ifdef MALLOC_STATS |
@@ -414,7 +413,7 @@ map(struct dir_info *d, size_t sz, int zero_fill) | |||
414 | d->free_regions_size -= psz; | 413 | d->free_regions_size -= psz; |
415 | if (zero_fill) | 414 | if (zero_fill) |
416 | memset(p, 0, sz); | 415 | memset(p, 0, sz); |
417 | else if (mopts.malloc_junk && | 416 | else if (mopts.malloc_junk == 2 && |
418 | mopts.malloc_freeunmap) | 417 | mopts.malloc_freeunmap) |
419 | memset(p, SOME_FREEJUNK, sz); | 418 | memset(p, SOME_FREEJUNK, sz); |
420 | return p; | 419 | return p; |
@@ -433,7 +432,7 @@ map(struct dir_info *d, size_t sz, int zero_fill) | |||
433 | d->free_regions_size -= psz; | 432 | d->free_regions_size -= psz; |
434 | if (zero_fill) | 433 | if (zero_fill) |
435 | memset(p, 0, sz); | 434 | memset(p, 0, sz); |
436 | else if (mopts.malloc_junk && mopts.malloc_freeunmap) | 435 | else if (mopts.malloc_junk == 2 && mopts.malloc_freeunmap) |
437 | memset(p, SOME_FREEJUNK, sz); | 436 | memset(p, SOME_FREEJUNK, sz); |
438 | return p; | 437 | return p; |
439 | } | 438 | } |
@@ -463,6 +462,7 @@ omalloc_init(struct dir_info **dp) | |||
463 | * Default options | 462 | * Default options |
464 | */ | 463 | */ |
465 | mopts.malloc_abort = 1; | 464 | mopts.malloc_abort = 1; |
465 | mopts.malloc_junk = 1; | ||
466 | mopts.malloc_move = 1; | 466 | mopts.malloc_move = 1; |
467 | mopts.malloc_cache = MALLOC_DEFAULT_CACHE; | 467 | mopts.malloc_cache = MALLOC_DEFAULT_CACHE; |
468 | 468 | ||
@@ -536,7 +536,7 @@ omalloc_init(struct dir_info **dp) | |||
536 | mopts.malloc_junk = 0; | 536 | mopts.malloc_junk = 0; |
537 | break; | 537 | break; |
538 | case 'J': | 538 | case 'J': |
539 | mopts.malloc_junk = 1; | 539 | mopts.malloc_junk = 2; |
540 | break; | 540 | break; |
541 | case 'n': | 541 | case 'n': |
542 | case 'N': | 542 | case 'N': |
@@ -559,7 +559,8 @@ omalloc_init(struct dir_info **dp) | |||
559 | mopts.malloc_cache = MALLOC_DEFAULT_CACHE; | 559 | mopts.malloc_cache = MALLOC_DEFAULT_CACHE; |
560 | break; | 560 | break; |
561 | case 'S': | 561 | case 'S': |
562 | mopts.malloc_freeunmap = mopts.malloc_junk = 1; | 562 | mopts.malloc_freeunmap = 1; |
563 | mopts.malloc_junk = 2; | ||
563 | mopts.malloc_guard = MALLOC_PAGESIZE; | 564 | mopts.malloc_guard = MALLOC_PAGESIZE; |
564 | mopts.malloc_cache = 0; | 565 | mopts.malloc_cache = 0; |
565 | break; | 566 | break; |
@@ -575,12 +576,6 @@ omalloc_init(struct dir_info **dp) | |||
575 | case 'X': | 576 | case 'X': |
576 | mopts.malloc_xmalloc = 1; | 577 | mopts.malloc_xmalloc = 1; |
577 | break; | 578 | break; |
578 | case 'z': | ||
579 | mopts.malloc_zero = 0; | ||
580 | break; | ||
581 | case 'Z': | ||
582 | mopts.malloc_zero = 1; | ||
583 | break; | ||
584 | default: { | 579 | default: { |
585 | static const char q[] = "malloc() warning: " | 580 | static const char q[] = "malloc() warning: " |
586 | "unknown char in MALLOC_OPTIONS\n"; | 581 | "unknown char in MALLOC_OPTIONS\n"; |
@@ -591,13 +586,6 @@ omalloc_init(struct dir_info **dp) | |||
591 | } | 586 | } |
592 | } | 587 | } |
593 | 588 | ||
594 | /* | ||
595 | * We want junk in the entire allocation, and zero only in the part | ||
596 | * the user asked for. | ||
597 | */ | ||
598 | if (mopts.malloc_zero) | ||
599 | mopts.malloc_junk = 1; | ||
600 | |||
601 | #ifdef MALLOC_STATS | 589 | #ifdef MALLOC_STATS |
602 | if (mopts.malloc_stats && (atexit(malloc_exit) == -1)) { | 590 | if (mopts.malloc_stats && (atexit(malloc_exit) == -1)) { |
603 | static const char q[] = "malloc() warning: atexit(2) failed." | 591 | static const char q[] = "malloc() warning: atexit(2) failed." |
@@ -971,7 +959,7 @@ malloc_bytes(struct dir_info *d, size_t size, void *f) | |||
971 | k += (lp - bp->bits) * MALLOC_BITS; | 959 | k += (lp - bp->bits) * MALLOC_BITS; |
972 | k <<= bp->shift; | 960 | k <<= bp->shift; |
973 | 961 | ||
974 | if (mopts.malloc_junk && bp->size > 0) | 962 | if (mopts.malloc_junk == 2 && bp->size > 0) |
975 | memset((char *)bp->page + k, SOME_JUNK, bp->size); | 963 | memset((char *)bp->page + k, SOME_JUNK, bp->size); |
976 | return ((char *)bp->page + k); | 964 | return ((char *)bp->page + k); |
977 | } | 965 | } |
@@ -1069,16 +1057,16 @@ omalloc(size_t sz, int zero_fill, void *f) | |||
1069 | sz - mopts.malloc_guard < MALLOC_PAGESIZE - | 1057 | sz - mopts.malloc_guard < MALLOC_PAGESIZE - |
1070 | MALLOC_LEEWAY) { | 1058 | MALLOC_LEEWAY) { |
1071 | /* fill whole allocation */ | 1059 | /* fill whole allocation */ |
1072 | if (mopts.malloc_junk) | 1060 | if (mopts.malloc_junk == 2) |
1073 | memset(p, SOME_JUNK, psz - mopts.malloc_guard); | 1061 | memset(p, SOME_JUNK, psz - mopts.malloc_guard); |
1074 | /* shift towards the end */ | 1062 | /* shift towards the end */ |
1075 | p = ((char *)p) + ((MALLOC_PAGESIZE - MALLOC_LEEWAY - | 1063 | p = ((char *)p) + ((MALLOC_PAGESIZE - MALLOC_LEEWAY - |
1076 | (sz - mopts.malloc_guard)) & ~(MALLOC_MINSIZE-1)); | 1064 | (sz - mopts.malloc_guard)) & ~(MALLOC_MINSIZE-1)); |
1077 | /* fill zeros if needed and overwritten above */ | 1065 | /* fill zeros if needed and overwritten above */ |
1078 | if (zero_fill && mopts.malloc_junk) | 1066 | if (zero_fill && mopts.malloc_junk == 2) |
1079 | memset(p, 0, sz - mopts.malloc_guard); | 1067 | memset(p, 0, sz - mopts.malloc_guard); |
1080 | } else { | 1068 | } else { |
1081 | if (mopts.malloc_junk) { | 1069 | if (mopts.malloc_junk == 2) { |
1082 | if (zero_fill) | 1070 | if (zero_fill) |
1083 | memset((char *)p + sz - mopts.malloc_guard, | 1071 | memset((char *)p + sz - mopts.malloc_guard, |
1084 | SOME_JUNK, psz - sz); | 1072 | SOME_JUNK, psz - sz); |
@@ -1146,7 +1134,7 @@ malloc(size_t size) | |||
1146 | malloc_recurse(); | 1134 | malloc_recurse(); |
1147 | return NULL; | 1135 | return NULL; |
1148 | } | 1136 | } |
1149 | r = omalloc(size, mopts.malloc_zero, CALLER); | 1137 | r = omalloc(size, 0, CALLER); |
1150 | malloc_active--; | 1138 | malloc_active--; |
1151 | _MALLOC_UNLOCK(); | 1139 | _MALLOC_UNLOCK(); |
1152 | if (r == NULL && mopts.malloc_xmalloc) { | 1140 | if (r == NULL && mopts.malloc_xmalloc) { |
@@ -1198,9 +1186,11 @@ ofree(void *p) | |||
1198 | } | 1186 | } |
1199 | malloc_guarded -= mopts.malloc_guard; | 1187 | malloc_guarded -= mopts.malloc_guard; |
1200 | } | 1188 | } |
1201 | if (mopts.malloc_junk && !mopts.malloc_freeunmap) | 1189 | if (mopts.malloc_junk && !mopts.malloc_freeunmap) { |
1202 | memset(p, SOME_FREEJUNK, | 1190 | size_t amt = mopts.malloc_junk == 1 ? MALLOC_MAXCHUNK : |
1203 | PAGEROUND(sz) - mopts.malloc_guard); | 1191 | PAGEROUND(sz) - mopts.malloc_guard; |
1192 | memset(p, SOME_FREEJUNK, amt); | ||
1193 | } | ||
1204 | unmap(g_pool, p, PAGEROUND(sz)); | 1194 | unmap(g_pool, p, PAGEROUND(sz)); |
1205 | delete(g_pool, r); | 1195 | delete(g_pool, r); |
1206 | } else { | 1196 | } else { |
@@ -1304,7 +1294,7 @@ orealloc(void *p, size_t newsz, void *f) | |||
1304 | q = MAP_FAILED; | 1294 | q = MAP_FAILED; |
1305 | if (q == hint) { | 1295 | if (q == hint) { |
1306 | malloc_used += needed; | 1296 | malloc_used += needed; |
1307 | if (mopts.malloc_junk) | 1297 | if (mopts.malloc_junk == 2) |
1308 | memset(q, SOME_JUNK, needed); | 1298 | memset(q, SOME_JUNK, needed); |
1309 | r->size = newsz; | 1299 | r->size = newsz; |
1310 | STATS_SETF(r, f); | 1300 | STATS_SETF(r, f); |
@@ -1331,7 +1321,7 @@ orealloc(void *p, size_t newsz, void *f) | |||
1331 | STATS_SETF(r, f); | 1321 | STATS_SETF(r, f); |
1332 | return p; | 1322 | return p; |
1333 | } else { | 1323 | } else { |
1334 | if (newsz > oldsz && mopts.malloc_junk) | 1324 | if (newsz > oldsz && mopts.malloc_junk == 2) |
1335 | memset((char *)p + newsz, SOME_JUNK, | 1325 | memset((char *)p + newsz, SOME_JUNK, |
1336 | rnewsz - mopts.malloc_guard - newsz); | 1326 | rnewsz - mopts.malloc_guard - newsz); |
1337 | r->size = gnewsz; | 1327 | r->size = gnewsz; |
@@ -1340,7 +1330,7 @@ orealloc(void *p, size_t newsz, void *f) | |||
1340 | } | 1330 | } |
1341 | } | 1331 | } |
1342 | if (newsz <= oldsz && newsz > oldsz / 2 && !mopts.malloc_realloc) { | 1332 | if (newsz <= oldsz && newsz > oldsz / 2 && !mopts.malloc_realloc) { |
1343 | if (mopts.malloc_junk && newsz > 0) | 1333 | if (mopts.malloc_junk == 2 && newsz > 0) |
1344 | memset((char *)p + newsz, SOME_JUNK, oldsz - newsz); | 1334 | memset((char *)p + newsz, SOME_JUNK, oldsz - newsz); |
1345 | STATS_SETF(r, f); | 1335 | STATS_SETF(r, f); |
1346 | return p; | 1336 | return p; |
@@ -1525,7 +1515,7 @@ omemalign(size_t alignment, size_t sz, int zero_fill, void *f) | |||
1525 | malloc_guarded += mopts.malloc_guard; | 1515 | malloc_guarded += mopts.malloc_guard; |
1526 | } | 1516 | } |
1527 | 1517 | ||
1528 | if (mopts.malloc_junk) { | 1518 | if (mopts.malloc_junk == 2) { |
1529 | if (zero_fill) | 1519 | if (zero_fill) |
1530 | memset((char *)p + sz - mopts.malloc_guard, | 1520 | memset((char *)p + sz - mopts.malloc_guard, |
1531 | SOME_JUNK, psz - sz); | 1521 | SOME_JUNK, psz - sz); |
@@ -1556,7 +1546,7 @@ posix_memalign(void **memptr, size_t alignment, size_t size) | |||
1556 | malloc_recurse(); | 1546 | malloc_recurse(); |
1557 | goto err; | 1547 | goto err; |
1558 | } | 1548 | } |
1559 | r = omemalign(alignment, size, mopts.malloc_zero, CALLER); | 1549 | r = omemalign(alignment, size, 0, CALLER); |
1560 | malloc_active--; | 1550 | malloc_active--; |
1561 | _MALLOC_UNLOCK(); | 1551 | _MALLOC_UNLOCK(); |
1562 | if (r == NULL) { | 1552 | if (r == NULL) { |