diff options
author | otto <> | 2018-11-27 17:29:55 +0000 |
---|---|---|
committer | otto <> | 2018-11-27 17:29:55 +0000 |
commit | ecfc33691b63d1fdd37618b90422f8704805acaa (patch) | |
tree | 64885cac4ac4a69f6831f3b7648be49b32c0f38d /src | |
parent | f55b2d4f7bfab5f5f8deb77d7950d65a60c3084c (diff) | |
download | openbsd-ecfc33691b63d1fdd37618b90422f8704805acaa.tar.gz openbsd-ecfc33691b63d1fdd37618b90422f8704805acaa.tar.bz2 openbsd-ecfc33691b63d1fdd37618b90422f8704805acaa.zip |
Refactor "find the right pool" code into a function. ok djm@ tb@
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 99 |
1 files changed, 34 insertions, 65 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 0912b904b8..7d5affbc2d 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.254 2018/11/21 06:57:04 otto Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.255 2018/11/27 17:29:55 otto Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> | 3 | * Copyright (c) 2008, 2010, 2011, 2016 Otto Moerbeek <otto@drijf.net> |
4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> | 4 | * Copyright (c) 2012 Matthew Dempsky <matthew@openbsd.org> |
@@ -1273,19 +1273,18 @@ validate_junk(struct dir_info *pool, void *p) | |||
1273 | } | 1273 | } |
1274 | } | 1274 | } |
1275 | 1275 | ||
1276 | static void | 1276 | |
1277 | ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | 1277 | static struct region_info * |
1278 | findpool(void *p, struct dir_info *argpool, struct dir_info **foundpool, | ||
1279 | char **saved_function) | ||
1278 | { | 1280 | { |
1279 | struct dir_info *pool; | 1281 | struct dir_info *pool = argpool; |
1280 | struct region_info *r; | 1282 | struct region_info *r = find(pool, p); |
1281 | char *saved_function; | ||
1282 | size_t sz; | ||
1283 | int i; | ||
1284 | 1283 | ||
1285 | pool = argpool; | ||
1286 | r = find(pool, p); | ||
1287 | if (r == NULL) { | 1284 | if (r == NULL) { |
1288 | if (mopts.malloc_mt) { | 1285 | if (mopts.malloc_mt) { |
1286 | int i; | ||
1287 | |||
1289 | for (i = 0; i < _MALLOC_MUTEXES; i++) { | 1288 | for (i = 0; i < _MALLOC_MUTEXES; i++) { |
1290 | if (i == argpool->mutex) | 1289 | if (i == argpool->mutex) |
1291 | continue; | 1290 | continue; |
@@ -1296,7 +1295,7 @@ ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | |||
1296 | pool->active++; | 1295 | pool->active++; |
1297 | r = find(pool, p); | 1296 | r = find(pool, p); |
1298 | if (r != NULL) { | 1297 | if (r != NULL) { |
1299 | saved_function = pool->func; | 1298 | *saved_function = pool->func; |
1300 | pool->func = argpool->func; | 1299 | pool->func = argpool->func; |
1301 | break; | 1300 | break; |
1302 | } | 1301 | } |
@@ -1305,6 +1304,19 @@ ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | |||
1305 | if (r == NULL) | 1304 | if (r == NULL) |
1306 | wrterror(argpool, "bogus pointer (double free?) %p", p); | 1305 | wrterror(argpool, "bogus pointer (double free?) %p", p); |
1307 | } | 1306 | } |
1307 | *foundpool = pool; | ||
1308 | return r; | ||
1309 | } | ||
1310 | |||
1311 | static void | ||
1312 | ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | ||
1313 | { | ||
1314 | struct region_info *r; | ||
1315 | struct dir_info *pool; | ||
1316 | char *saved_function; | ||
1317 | size_t sz; | ||
1318 | |||
1319 | r = findpool(p, argpool, &pool, &saved_function); | ||
1308 | 1320 | ||
1309 | REALSIZE(sz, r); | 1321 | REALSIZE(sz, r); |
1310 | if (check) { | 1322 | if (check) { |
@@ -1469,48 +1481,24 @@ DEF_WEAK(freezero); | |||
1469 | static void * | 1481 | static void * |
1470 | orealloc(struct dir_info *argpool, void *p, size_t newsz, void *f) | 1482 | orealloc(struct dir_info *argpool, void *p, size_t newsz, void *f) |
1471 | { | 1483 | { |
1472 | struct dir_info *pool; | ||
1473 | struct region_info *r; | 1484 | struct region_info *r; |
1485 | struct dir_info *pool; | ||
1486 | char *saved_function; | ||
1474 | struct chunk_info *info; | 1487 | struct chunk_info *info; |
1475 | size_t oldsz, goldsz, gnewsz; | 1488 | size_t oldsz, goldsz, gnewsz; |
1476 | void *q, *ret; | 1489 | void *q, *ret; |
1477 | char *saved_function; | ||
1478 | int i; | ||
1479 | uint32_t chunknum; | 1490 | uint32_t chunknum; |
1480 | 1491 | ||
1481 | pool = argpool; | ||
1482 | |||
1483 | if (p == NULL) | 1492 | if (p == NULL) |
1484 | return omalloc(pool, newsz, 0, f); | 1493 | return omalloc(argpool, newsz, 0, f); |
1485 | 1494 | ||
1486 | r = find(pool, p); | ||
1487 | if (r == NULL) { | ||
1488 | if (mopts.malloc_mt) { | ||
1489 | for (i = 0; i < _MALLOC_MUTEXES; i++) { | ||
1490 | if (i == argpool->mutex) | ||
1491 | continue; | ||
1492 | pool->active--; | ||
1493 | _MALLOC_UNLOCK(pool->mutex); | ||
1494 | pool = mopts.malloc_pool[i]; | ||
1495 | _MALLOC_LOCK(pool->mutex); | ||
1496 | pool->active++; | ||
1497 | r = find(pool, p); | ||
1498 | if (r != NULL) { | ||
1499 | saved_function = pool->func; | ||
1500 | pool->func = argpool->func; | ||
1501 | break; | ||
1502 | } | ||
1503 | } | ||
1504 | } | ||
1505 | if (r == NULL) | ||
1506 | wrterror(argpool, "bogus pointer (double free?) %p", p); | ||
1507 | } | ||
1508 | if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { | 1495 | if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { |
1509 | errno = ENOMEM; | 1496 | errno = ENOMEM; |
1510 | ret = NULL; | 1497 | return NULL; |
1511 | goto done; | ||
1512 | } | 1498 | } |
1513 | 1499 | ||
1500 | r = findpool(p, argpool, &pool, &saved_function); | ||
1501 | |||
1514 | REALSIZE(oldsz, r); | 1502 | REALSIZE(oldsz, r); |
1515 | if (mopts.chunk_canaries && oldsz <= MALLOC_MAXCHUNK) { | 1503 | if (mopts.chunk_canaries && oldsz <= MALLOC_MAXCHUNK) { |
1516 | info = (struct chunk_info *)r->size; | 1504 | info = (struct chunk_info *)r->size; |
@@ -1745,39 +1733,19 @@ static void * | |||
1745 | orecallocarray(struct dir_info *argpool, void *p, size_t oldsize, | 1733 | orecallocarray(struct dir_info *argpool, void *p, size_t oldsize, |
1746 | size_t newsize, void *f) | 1734 | size_t newsize, void *f) |
1747 | { | 1735 | { |
1748 | struct dir_info *pool; | ||
1749 | struct region_info *r; | 1736 | struct region_info *r; |
1737 | struct dir_info *pool; | ||
1738 | char *saved_function; | ||
1750 | void *newptr; | 1739 | void *newptr; |
1751 | size_t sz; | 1740 | size_t sz; |
1752 | int i; | ||
1753 | |||
1754 | pool = argpool; | ||
1755 | 1741 | ||
1756 | if (p == NULL) | 1742 | if (p == NULL) |
1757 | return omalloc(pool, newsize, 1, f); | 1743 | return omalloc(argpool, newsize, 1, f); |
1758 | 1744 | ||
1759 | if (oldsize == newsize) | 1745 | if (oldsize == newsize) |
1760 | return p; | 1746 | return p; |
1761 | 1747 | ||
1762 | r = find(pool, p); | 1748 | r = findpool(p, argpool, &pool, &saved_function); |
1763 | if (r == NULL) { | ||
1764 | if (mopts.malloc_mt) { | ||
1765 | for (i = 0; i < _MALLOC_MUTEXES; i++) { | ||
1766 | if (i == argpool->mutex) | ||
1767 | continue; | ||
1768 | pool->active--; | ||
1769 | _MALLOC_UNLOCK(pool->mutex); | ||
1770 | pool = mopts.malloc_pool[i]; | ||
1771 | _MALLOC_LOCK(pool->mutex); | ||
1772 | pool->active++; | ||
1773 | r = find(pool, p); | ||
1774 | if (r != NULL) | ||
1775 | break; | ||
1776 | } | ||
1777 | } | ||
1778 | if (r == NULL) | ||
1779 | wrterror(pool, "bogus pointer (double free?) %p", p); | ||
1780 | } | ||
1781 | 1749 | ||
1782 | REALSIZE(sz, r); | 1750 | REALSIZE(sz, r); |
1783 | if (sz <= MALLOC_MAXCHUNK) { | 1751 | if (sz <= MALLOC_MAXCHUNK) { |
@@ -1809,6 +1777,7 @@ orecallocarray(struct dir_info *argpool, void *p, size_t oldsize, | |||
1809 | done: | 1777 | done: |
1810 | if (argpool != pool) { | 1778 | if (argpool != pool) { |
1811 | pool->active--; | 1779 | pool->active--; |
1780 | pool->func = saved_function; | ||
1812 | _MALLOC_UNLOCK(pool->mutex); | 1781 | _MALLOC_UNLOCK(pool->mutex); |
1813 | _MALLOC_LOCK(argpool->mutex); | 1782 | _MALLOC_LOCK(argpool->mutex); |
1814 | argpool->active++; | 1783 | argpool->active++; |