diff options
author | otto <> | 2018-12-10 07:57:49 +0000 |
---|---|---|
committer | otto <> | 2018-12-10 07:57:49 +0000 |
commit | 527de2024d889cce4bfe4045c0b2282d809afe3e (patch) | |
tree | 2eee06e3fb07d172a1a798f9f7caaca16ed84b74 | |
parent | bf087b2e1ea3295afdd913d8d55a0b73fe87003b (diff) | |
download | openbsd-527de2024d889cce4bfe4045c0b2282d809afe3e.tar.gz openbsd-527de2024d889cce4bfe4045c0b2282d809afe3e.tar.bz2 openbsd-527de2024d889cce4bfe4045c0b2282d809afe3e.zip |
Improve speed for the multi-threaded case by reducing lock contention.
tested by many; ok florian@
-rw-r--r-- | src/lib/libc/stdlib/malloc.c | 51 |
1 files changed, 21 insertions, 30 deletions
diff --git a/src/lib/libc/stdlib/malloc.c b/src/lib/libc/stdlib/malloc.c index 5f5bd5d45b..de504cdd8b 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.256 2018/12/09 11:32:02 florian Exp $ */ | 1 | /* $OpenBSD: malloc.c,v 1.257 2018/12/10 07:57:49 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> |
@@ -1309,14 +1309,14 @@ findpool(void *p, struct dir_info *argpool, struct dir_info **foundpool, | |||
1309 | } | 1309 | } |
1310 | 1310 | ||
1311 | static void | 1311 | static void |
1312 | ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | 1312 | ofree(struct dir_info **argpool, void *p, int clear, int check, size_t argsz) |
1313 | { | 1313 | { |
1314 | struct region_info *r; | 1314 | struct region_info *r; |
1315 | struct dir_info *pool; | 1315 | struct dir_info *pool; |
1316 | char *saved_function; | 1316 | char *saved_function; |
1317 | size_t sz; | 1317 | size_t sz; |
1318 | 1318 | ||
1319 | r = findpool(p, argpool, &pool, &saved_function); | 1319 | r = findpool(p, *argpool, &pool, &saved_function); |
1320 | 1320 | ||
1321 | REALSIZE(sz, r); | 1321 | REALSIZE(sz, r); |
1322 | if (check) { | 1322 | if (check) { |
@@ -1405,12 +1405,9 @@ ofree(struct dir_info *argpool, void *p, int clear, int check, size_t argsz) | |||
1405 | } | 1405 | } |
1406 | } | 1406 | } |
1407 | 1407 | ||
1408 | if (argpool != pool) { | 1408 | if (*argpool != pool) { |
1409 | pool->active--; | ||
1410 | pool->func = saved_function; | 1409 | pool->func = saved_function; |
1411 | _MALLOC_UNLOCK(pool->mutex); | 1410 | *argpool = pool; |
1412 | _MALLOC_LOCK(argpool->mutex); | ||
1413 | argpool->active++; | ||
1414 | } | 1411 | } |
1415 | } | 1412 | } |
1416 | 1413 | ||
@@ -1433,7 +1430,7 @@ free(void *ptr) | |||
1433 | malloc_recurse(d); | 1430 | malloc_recurse(d); |
1434 | return; | 1431 | return; |
1435 | } | 1432 | } |
1436 | ofree(d, ptr, 0, 0, 0); | 1433 | ofree(&d, ptr, 0, 0, 0); |
1437 | d->active--; | 1434 | d->active--; |
1438 | _MALLOC_UNLOCK(d->mutex); | 1435 | _MALLOC_UNLOCK(d->mutex); |
1439 | errno = saved_errno; | 1436 | errno = saved_errno; |
@@ -1471,7 +1468,7 @@ freezero(void *ptr, size_t sz) | |||
1471 | malloc_recurse(d); | 1468 | malloc_recurse(d); |
1472 | return; | 1469 | return; |
1473 | } | 1470 | } |
1474 | ofree(d, ptr, 1, 1, sz); | 1471 | ofree(&d, ptr, 1, 1, sz); |
1475 | d->active--; | 1472 | d->active--; |
1476 | _MALLOC_UNLOCK(d->mutex); | 1473 | _MALLOC_UNLOCK(d->mutex); |
1477 | errno = saved_errno; | 1474 | errno = saved_errno; |
@@ -1479,7 +1476,7 @@ freezero(void *ptr, size_t sz) | |||
1479 | DEF_WEAK(freezero); | 1476 | DEF_WEAK(freezero); |
1480 | 1477 | ||
1481 | static void * | 1478 | static void * |
1482 | orealloc(struct dir_info *argpool, void *p, size_t newsz, void *f) | 1479 | orealloc(struct dir_info **argpool, void *p, size_t newsz, void *f) |
1483 | { | 1480 | { |
1484 | struct region_info *r; | 1481 | struct region_info *r; |
1485 | struct dir_info *pool; | 1482 | struct dir_info *pool; |
@@ -1490,14 +1487,14 @@ orealloc(struct dir_info *argpool, void *p, size_t newsz, void *f) | |||
1490 | uint32_t chunknum; | 1487 | uint32_t chunknum; |
1491 | 1488 | ||
1492 | if (p == NULL) | 1489 | if (p == NULL) |
1493 | return omalloc(argpool, newsz, 0, f); | 1490 | return omalloc(*argpool, newsz, 0, f); |
1494 | 1491 | ||
1495 | if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { | 1492 | if (newsz >= SIZE_MAX - mopts.malloc_guard - MALLOC_PAGESIZE) { |
1496 | errno = ENOMEM; | 1493 | errno = ENOMEM; |
1497 | return NULL; | 1494 | return NULL; |
1498 | } | 1495 | } |
1499 | 1496 | ||
1500 | r = findpool(p, argpool, &pool, &saved_function); | 1497 | r = findpool(p, *argpool, &pool, &saved_function); |
1501 | 1498 | ||
1502 | REALSIZE(oldsz, r); | 1499 | REALSIZE(oldsz, r); |
1503 | if (mopts.chunk_canaries && oldsz <= MALLOC_MAXCHUNK) { | 1500 | if (mopts.chunk_canaries && oldsz <= MALLOC_MAXCHUNK) { |
@@ -1631,7 +1628,7 @@ gotit: | |||
1631 | } | 1628 | } |
1632 | if (newsz != 0 && oldsz != 0) | 1629 | if (newsz != 0 && oldsz != 0) |
1633 | memcpy(q, p, oldsz < newsz ? oldsz : newsz); | 1630 | memcpy(q, p, oldsz < newsz ? oldsz : newsz); |
1634 | ofree(pool, p, 0, 0, 0); | 1631 | ofree(&pool, p, 0, 0, 0); |
1635 | ret = q; | 1632 | ret = q; |
1636 | } else { | 1633 | } else { |
1637 | /* oldsz == newsz */ | 1634 | /* oldsz == newsz */ |
@@ -1641,12 +1638,9 @@ gotit: | |||
1641 | ret = p; | 1638 | ret = p; |
1642 | } | 1639 | } |
1643 | done: | 1640 | done: |
1644 | if (argpool != pool) { | 1641 | if (*argpool != pool) { |
1645 | pool->active--; | ||
1646 | pool->func = saved_function; | 1642 | pool->func = saved_function; |
1647 | _MALLOC_UNLOCK(pool->mutex); | 1643 | *argpool = pool; |
1648 | _MALLOC_LOCK(argpool->mutex); | ||
1649 | argpool->active++; | ||
1650 | } | 1644 | } |
1651 | return ret; | 1645 | return ret; |
1652 | } | 1646 | } |
@@ -1669,7 +1663,7 @@ realloc(void *ptr, size_t size) | |||
1669 | malloc_recurse(d); | 1663 | malloc_recurse(d); |
1670 | return NULL; | 1664 | return NULL; |
1671 | } | 1665 | } |
1672 | r = orealloc(d, ptr, size, CALLER); | 1666 | r = orealloc(&d, ptr, size, CALLER); |
1673 | 1667 | ||
1674 | d->active--; | 1668 | d->active--; |
1675 | _MALLOC_UNLOCK(d->mutex); | 1669 | _MALLOC_UNLOCK(d->mutex); |
@@ -1730,7 +1724,7 @@ calloc(size_t nmemb, size_t size) | |||
1730 | /*DEF_STRONG(calloc);*/ | 1724 | /*DEF_STRONG(calloc);*/ |
1731 | 1725 | ||
1732 | static void * | 1726 | static void * |
1733 | orecallocarray(struct dir_info *argpool, void *p, size_t oldsize, | 1727 | orecallocarray(struct dir_info **argpool, void *p, size_t oldsize, |
1734 | size_t newsize, void *f) | 1728 | size_t newsize, void *f) |
1735 | { | 1729 | { |
1736 | struct region_info *r; | 1730 | struct region_info *r; |
@@ -1740,12 +1734,12 @@ orecallocarray(struct dir_info *argpool, void *p, size_t oldsize, | |||
1740 | size_t sz; | 1734 | size_t sz; |
1741 | 1735 | ||
1742 | if (p == NULL) | 1736 | if (p == NULL) |
1743 | return omalloc(argpool, newsize, 1, f); | 1737 | return omalloc(*argpool, newsize, 1, f); |
1744 | 1738 | ||
1745 | if (oldsize == newsize) | 1739 | if (oldsize == newsize) |
1746 | return p; | 1740 | return p; |
1747 | 1741 | ||
1748 | r = findpool(p, argpool, &pool, &saved_function); | 1742 | r = findpool(p, *argpool, &pool, &saved_function); |
1749 | 1743 | ||
1750 | REALSIZE(sz, r); | 1744 | REALSIZE(sz, r); |
1751 | if (sz <= MALLOC_MAXCHUNK) { | 1745 | if (sz <= MALLOC_MAXCHUNK) { |
@@ -1772,15 +1766,12 @@ orecallocarray(struct dir_info *argpool, void *p, size_t oldsize, | |||
1772 | } else | 1766 | } else |
1773 | memcpy(newptr, p, newsize); | 1767 | memcpy(newptr, p, newsize); |
1774 | 1768 | ||
1775 | ofree(pool, p, 1, 0, oldsize); | 1769 | ofree(&pool, p, 1, 0, oldsize); |
1776 | 1770 | ||
1777 | done: | 1771 | done: |
1778 | if (argpool != pool) { | 1772 | if (*argpool != pool) { |
1779 | pool->active--; | ||
1780 | pool->func = saved_function; | 1773 | pool->func = saved_function; |
1781 | _MALLOC_UNLOCK(pool->mutex); | 1774 | *argpool = pool; |
1782 | _MALLOC_LOCK(argpool->mutex); | ||
1783 | argpool->active++; | ||
1784 | } | 1775 | } |
1785 | 1776 | ||
1786 | return newptr; | 1777 | return newptr; |
@@ -1883,7 +1874,7 @@ recallocarray(void *ptr, size_t oldnmemb, size_t newnmemb, size_t size) | |||
1883 | return NULL; | 1874 | return NULL; |
1884 | } | 1875 | } |
1885 | 1876 | ||
1886 | r = orecallocarray(d, ptr, oldsize, newsize, CALLER); | 1877 | r = orecallocarray(&d, ptr, oldsize, newsize, CALLER); |
1887 | 1878 | ||
1888 | d->active--; | 1879 | d->active--; |
1889 | _MALLOC_UNLOCK(d->mutex); | 1880 | _MALLOC_UNLOCK(d->mutex); |