@@ -1663,13 +1663,8 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
16631663 szind_t ind = 0 ;
16641664 size_t usize = 0 ;
16651665
1666- /*
1667- * For reentrancy checking, we get the old reentrancy level from tsd and
1668- * reset it once we're done. In case of early bailout though, we never
1669- * bother getting the old level, so we shouldn't try to reset it. This
1670- * is indicated by leaving the pointer as NULL.
1671- */
1672- int8_t * reentrancy_level = NULL ;
1666+ /* Reentrancy is only checked on slow path. */
1667+ int8_t reentrancy_level ;
16731668
16741669 /* Compute the amount of memory the user wants. */
16751670 if (unlikely (compute_size_with_overflow (sopts -> may_overflow , dopts ,
@@ -1716,12 +1711,11 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
17161711 * If we need to handle reentrancy, we can do it out of a
17171712 * known-initialized arena (i.e. arena 0).
17181713 */
1719- reentrancy_level = tsd_reentrancy_levelp_get (tsd );
1720- ++ * reentrancy_level ;
1721- if (* reentrancy_level == 1 ) {
1714+ reentrancy_level = tsd_reentrancy_level_get (tsd );
1715+ if (reentrancy_level == 0 ) {
17221716 witness_assert_lockless (tsd_tsdn (tsd ));
17231717 }
1724- if (unlikely (* reentrancy_level > 1 )) {
1718+ if (sopts -> slow && unlikely (reentrancy_level > 0 )) {
17251719 /*
17261720 * We should never specify particular arenas or tcaches from
17271721 * within our internal allocations.
@@ -1795,14 +1789,9 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
17951789 }
17961790
17971791 /* Success! */
1798- if (* reentrancy_level == 1 ) {
1792+ if (reentrancy_level == 0 ) {
17991793 witness_assert_lockless (tsd_tsdn (tsd ));
18001794 }
1801- /*
1802- * If we got here, we never bailed out on a failure path, so
1803- * reentrancy_level is non-null.
1804- */
1805- -- * reentrancy_level ;
18061795 * dopts -> result = allocation ;
18071796 return 0 ;
18081797
@@ -1826,10 +1815,6 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
18261815 * dopts -> result = NULL ;
18271816 }
18281817
1829- if (reentrancy_level != NULL ) {
1830- -- * reentrancy_level ;
1831- }
1832-
18331818 return ENOMEM ;
18341819
18351820 /*
@@ -1857,10 +1842,6 @@ imalloc_body(static_opts_t *sopts, dynamic_opts_t *dopts, tsd_t *tsd) {
18571842 * dopts -> result = NULL ;
18581843 }
18591844
1860- if (reentrancy_level != NULL ) {
1861- -- * reentrancy_level ;
1862- }
1863-
18641845 return EINVAL ;
18651846}
18661847
@@ -2053,8 +2034,11 @@ irealloc_prof(tsd_t *tsd, void *old_ptr, size_t old_usize, size_t usize,
20532034
20542035JEMALLOC_ALWAYS_INLINE_C void
20552036ifree (tsd_t * tsd , void * ptr , tcache_t * tcache , bool slow_path ) {
2056- if (* tsd_reentrancy_levelp_get (tsd ) == 0 ) {
2037+ assert (slow_path || tsd_assert_fast (tsd ));
2038+ if (tsd_reentrancy_level_get (tsd ) == 0 ) {
20572039 witness_assert_lockless (tsd_tsdn (tsd ));
2040+ } else {
2041+ assert (slow_path );
20582042 }
20592043
20602044 assert (ptr != NULL );
@@ -2088,8 +2072,11 @@ ifree(tsd_t *tsd, void *ptr, tcache_t *tcache, bool slow_path) {
20882072
20892073JEMALLOC_ALWAYS_INLINE_C void
20902074isfree (tsd_t * tsd , void * ptr , size_t usize , tcache_t * tcache , bool slow_path ) {
2091- if (* tsd_reentrancy_levelp_get (tsd ) == 0 ) {
2075+ assert (slow_path || tsd_assert_fast (tsd ));
2076+ if (tsd_reentrancy_level_get (tsd ) == 0 ) {
20922077 witness_assert_lockless (tsd_tsdn (tsd ));
2078+ } else {
2079+ assert (slow_path );
20932080 }
20942081
20952082 assert (ptr != NULL );
@@ -2129,14 +2116,14 @@ je_realloc(void *ptr, size_t size) {
21292116
21302117 if (unlikely (size == 0 )) {
21312118 if (ptr != NULL ) {
2132- tsd_t * tsd ;
2133-
21342119 /* realloc(ptr, 0) is equivalent to free(ptr). */
21352120 UTRACE (ptr , 0 , 0 );
2136- tsd = tsd_fetch () ;
2137- tcache_t * tcache = NULL ;
2138- if (likely ( * tsd_reentrancy_levelp_get ( tsd ) == 0 ) ) {
2121+ tcache_t * tcache ;
2122+ tsd_t * tsd = tsd_fetch () ;
2123+ if (tsd_reentrancy_level_get ( tsd ) == 0 ) {
21392124 tcache = tcache_get (tsd );
2125+ } else {
2126+ tcache = NULL ;
21402127 }
21412128 ifree (tsd , ptr , tcache , true);
21422129 return NULL ;
@@ -2200,29 +2187,25 @@ je_free(void *ptr) {
22002187 UTRACE (ptr , 0 , 0 );
22012188 if (likely (ptr != NULL )) {
22022189 tsd_t * tsd = tsd_fetch ();
2203- if (* tsd_reentrancy_levelp_get (tsd ) == 0 ) {
2190+ if (tsd_reentrancy_level_get (tsd ) == 0 ) {
22042191 witness_assert_lockless (tsd_tsdn (tsd ));
22052192 }
2193+
22062194 tcache_t * tcache ;
22072195 if (likely (tsd_fast (tsd ))) {
22082196 tsd_assert_fast (tsd );
2209- if (likely (* tsd_reentrancy_levelp_get (tsd ) == 0 )) {
2210- /* Getting tcache ptr unconditionally. */
2211- tcache = tsd_tcachep_get (tsd );
2212- assert (tcache == tcache_get (tsd ));
2213- } else {
2214- tcache = NULL ;
2215- }
2197+ /* Unconditionally get tcache ptr on fast path. */
2198+ tcache = tsd_tcachep_get (tsd );
22162199 ifree (tsd , ptr , tcache , false);
22172200 } else {
2218- if (likely (* tsd_reentrancy_levelp_get (tsd ) == 0 )) {
2201+ if (likely (tsd_reentrancy_level_get (tsd ) == 0 )) {
22192202 tcache = tcache_get (tsd );
22202203 } else {
22212204 tcache = NULL ;
22222205 }
22232206 ifree (tsd , ptr , tcache , true);
22242207 }
2225- if (* tsd_reentrancy_levelp_get (tsd ) == 0 ) {
2208+ if (tsd_reentrancy_level_get (tsd ) == 0 ) {
22262209 witness_assert_lockless (tsd_tsdn (tsd ));
22272210 }
22282211 }
@@ -2707,33 +2690,32 @@ je_sallocx(const void *ptr, int flags) {
27072690
27082691JEMALLOC_EXPORT void JEMALLOC_NOTHROW
27092692je_dallocx (void * ptr , int flags ) {
2710- tsd_t * tsd ;
2711- tcache_t * tcache ;
2712-
27132693 assert (ptr != NULL );
27142694 assert (malloc_initialized () || IS_INITIALIZER );
27152695
2716- tsd = tsd_fetch ();
2696+ tsd_t * tsd = tsd_fetch ();
27172697 bool fast = tsd_fast (tsd );
27182698 witness_assert_lockless (tsd_tsdn (tsd ));
2699+
2700+ tcache_t * tcache ;
27192701 if (unlikely ((flags & MALLOCX_TCACHE_MASK ) != 0 )) {
27202702 /* Not allowed to be reentrant and specify a custom tcache. */
2721- assert (* tsd_reentrancy_levelp_get (tsd ) == 0 );
2703+ assert (tsd_reentrancy_level_get (tsd ) == 0 );
27222704 if ((flags & MALLOCX_TCACHE_MASK ) == MALLOCX_TCACHE_NONE ) {
27232705 tcache = NULL ;
27242706 } else {
27252707 tcache = tcaches_get (tsd , MALLOCX_TCACHE_GET (flags ));
27262708 }
27272709 } else {
2728- if (likely (* tsd_reentrancy_levelp_get ( tsd ) == 0 )) {
2729- if ( likely ( fast )) {
2730- tcache = tsd_tcachep_get (tsd );
2731- assert ( tcache == tcache_get ( tsd ));
2732- } else {
2710+ if (likely (fast )) {
2711+ tcache = tsd_tcachep_get ( tsd );
2712+ assert ( tcache == tcache_get (tsd ) );
2713+ } else {
2714+ if ( likely ( tsd_reentrancy_level_get ( tsd ) == 0 )) {
27332715 tcache = tcache_get (tsd );
2716+ } else {
2717+ tcache = NULL ;
27342718 }
2735- } else {
2736- tcache = NULL ;
27372719 }
27382720 }
27392721
@@ -2749,10 +2731,9 @@ je_dallocx(void *ptr, int flags) {
27492731
27502732JEMALLOC_ALWAYS_INLINE_C size_t
27512733inallocx (tsdn_t * tsdn , size_t size , int flags ) {
2752- size_t usize ;
2753-
27542734 witness_assert_lockless (tsdn );
27552735
2736+ size_t usize ;
27562737 if (likely ((flags & MALLOCX_LG_ALIGN_MASK ) == 0 )) {
27572738 usize = s2u (size );
27582739 } else {
@@ -2764,36 +2745,34 @@ inallocx(tsdn_t *tsdn, size_t size, int flags) {
27642745
27652746JEMALLOC_EXPORT void JEMALLOC_NOTHROW
27662747je_sdallocx (void * ptr , size_t size , int flags ) {
2767- tsd_t * tsd ;
2768- size_t usize ;
2769- tcache_t * tcache ;
2770-
27712748 assert (ptr != NULL );
27722749 assert (malloc_initialized () || IS_INITIALIZER );
2773- tsd = tsd_fetch ();
2750+
2751+ tsd_t * tsd = tsd_fetch ();
27742752 bool fast = tsd_fast (tsd );
2775- usize = inallocx (tsd_tsdn (tsd ), size , flags );
2753+ size_t usize = inallocx (tsd_tsdn (tsd ), size , flags );
27762754 assert (usize == isalloc (tsd_tsdn (tsd ), ptr ));
2777-
27782755 witness_assert_lockless (tsd_tsdn (tsd ));
2756+
2757+ tcache_t * tcache ;
27792758 if (unlikely ((flags & MALLOCX_TCACHE_MASK ) != 0 )) {
27802759 /* Not allowed to be reentrant and specify a custom tcache. */
2781- assert (* tsd_reentrancy_levelp_get (tsd ) == 0 );
2760+ assert (tsd_reentrancy_level_get (tsd ) == 0 );
27822761 if ((flags & MALLOCX_TCACHE_MASK ) == MALLOCX_TCACHE_NONE ) {
27832762 tcache = NULL ;
27842763 } else {
27852764 tcache = tcaches_get (tsd , MALLOCX_TCACHE_GET (flags ));
27862765 }
27872766 } else {
2788- if (likely (* tsd_reentrancy_levelp_get ( tsd ) == 0 )) {
2789- if ( likely ( fast )) {
2790- tcache = tsd_tcachep_get (tsd );
2791- assert ( tcache == tcache_get ( tsd ));
2792- } else {
2767+ if (likely (fast )) {
2768+ tcache = tsd_tcachep_get ( tsd );
2769+ assert ( tcache == tcache_get (tsd ) );
2770+ } else {
2771+ if ( likely ( tsd_reentrancy_level_get ( tsd ) == 0 )) {
27932772 tcache = tcache_get (tsd );
2773+ } else {
2774+ tcache = NULL ;
27942775 }
2795- } else {
2796- tcache = NULL ;
27972776 }
27982777 }
27992778
0 commit comments