Optimize [nmd]alloc() fast paths.

Optimize [nmd]alloc() fast paths such that the (flags == 0) case is
streamlined, flags decoding only happens to the minimum degree
necessary, and no conditionals are repeated.
This commit is contained in:
Jason Evans 2014-09-07 14:40:19 -07:00
parent c21b05ea09
commit b718cf77e9
7 changed files with 172 additions and 131 deletions

View File

@ -577,7 +577,7 @@ small_bin2size_lookup(size_t binind)
assert(binind < NBINS); assert(binind < NBINS);
{ {
size_t ret = ((size_t)(small_bin2size_tab[binind])); size_t ret = (size_t)small_bin2size_tab[binind];
assert(ret == small_bin2size_compute(binind)); assert(ret == small_bin2size_compute(binind));
return (ret); return (ret);
} }
@ -615,7 +615,7 @@ small_s2u_compute(size_t size)
JEMALLOC_ALWAYS_INLINE size_t JEMALLOC_ALWAYS_INLINE size_t
small_s2u_lookup(size_t size) small_s2u_lookup(size_t size)
{ {
size_t ret = (small_bin2size(small_size2bin(size))); size_t ret = small_bin2size(small_size2bin(size));
assert(ret == small_s2u_compute(size)); assert(ret == small_s2u_compute(size));
return (ret); return (ret);

View File

@ -165,7 +165,17 @@ static const bool config_ivsalloc =
#include "jemalloc/internal/jemalloc_internal_macros.h" #include "jemalloc/internal/jemalloc_internal_macros.h"
#define MALLOCX_ARENA_MASK ((int)~0xff)
#define MALLOCX_LG_ALIGN_MASK ((int)0x3f) #define MALLOCX_LG_ALIGN_MASK ((int)0x3f)
/* Use MALLOCX_ALIGN_GET() if alignment may not be specified in flags. */
#define MALLOCX_ALIGN_GET_SPECIFIED(flags) \
(ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK))
#define MALLOCX_ALIGN_GET(flags) \
(MALLOCX_ALIGN_GET_SPECIFIED(flags) & (SIZE_T_MAX-1))
#define MALLOCX_ZERO_GET(flags) \
((bool)(flags & MALLOCX_ZERO))
#define MALLOCX_ARENA_GET(flags) \
(((unsigned)(flags >> 8)) - 1)
/* Smallest size class to support. */ /* Smallest size class to support. */
#define LG_TINY_MIN 3 #define LG_TINY_MIN 3
@ -625,15 +635,13 @@ size_t u2rz(size_t usize);
size_t p2rz(const void *ptr); size_t p2rz(const void *ptr);
void idalloct(void *ptr, bool try_tcache); void idalloct(void *ptr, bool try_tcache);
void idalloc(void *ptr); void idalloc(void *ptr);
void iqalloct(void *ptr, bool try_tcache); void iqalloc(void *ptr, bool try_tcache);
void iqalloc(void *ptr);
void *iralloct_realign(void *ptr, size_t oldsize, size_t size, size_t extra, void *iralloct_realign(void *ptr, size_t oldsize, size_t size, size_t extra,
size_t alignment, bool zero, bool try_tcache_alloc, bool try_tcache_dalloc, size_t alignment, bool zero, bool try_tcache_alloc, bool try_tcache_dalloc,
arena_t *arena); arena_t *arena);
void *iralloct(void *ptr, size_t size, size_t extra, size_t alignment, void *iralloct(void *ptr, size_t size, size_t alignment, bool zero,
bool zero, bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena); bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena);
void *iralloc(void *ptr, size_t size, size_t extra, size_t alignment, void *iralloc(void *ptr, size_t size, size_t alignment, bool zero);
bool zero);
bool ixalloc(void *ptr, size_t size, size_t extra, size_t alignment, bool ixalloc(void *ptr, size_t size, size_t extra, size_t alignment,
bool zero); bool zero);
malloc_tsd_protos(JEMALLOC_ATTR(unused), thread_allocated, thread_allocated_t) malloc_tsd_protos(JEMALLOC_ATTR(unused), thread_allocated, thread_allocated_t)
@ -787,7 +795,7 @@ idalloc(void *ptr)
} }
JEMALLOC_ALWAYS_INLINE void JEMALLOC_ALWAYS_INLINE void
iqalloct(void *ptr, bool try_tcache) iqalloc(void *ptr, bool try_tcache)
{ {
if (config_fill && opt_quarantine) if (config_fill && opt_quarantine)
@ -796,13 +804,6 @@ iqalloct(void *ptr, bool try_tcache)
idalloct(ptr, try_tcache); idalloct(ptr, try_tcache);
} }
JEMALLOC_ALWAYS_INLINE void
iqalloc(void *ptr)
{
iqalloct(ptr, true);
}
JEMALLOC_ALWAYS_INLINE void * JEMALLOC_ALWAYS_INLINE void *
iralloct_realign(void *ptr, size_t oldsize, size_t size, size_t extra, iralloct_realign(void *ptr, size_t oldsize, size_t size, size_t extra,
size_t alignment, bool zero, bool try_tcache_alloc, bool try_tcache_dalloc, size_t alignment, bool zero, bool try_tcache_alloc, bool try_tcache_dalloc,
@ -832,12 +833,12 @@ iralloct_realign(void *ptr, size_t oldsize, size_t size, size_t extra,
*/ */
copysize = (size < oldsize) ? size : oldsize; copysize = (size < oldsize) ? size : oldsize;
memcpy(p, ptr, copysize); memcpy(p, ptr, copysize);
iqalloct(ptr, try_tcache_dalloc); iqalloc(ptr, try_tcache_dalloc);
return (p); return (p);
} }
JEMALLOC_ALWAYS_INLINE void * JEMALLOC_ALWAYS_INLINE void *
iralloct(void *ptr, size_t size, size_t extra, size_t alignment, bool zero, iralloct(void *ptr, size_t size, size_t alignment, bool zero,
bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena) bool try_tcache_alloc, bool try_tcache_dalloc, arena_t *arena)
{ {
size_t oldsize; size_t oldsize;
@ -853,25 +854,24 @@ iralloct(void *ptr, size_t size, size_t extra, size_t alignment, bool zero,
* Existing object alignment is inadequate; allocate new space * Existing object alignment is inadequate; allocate new space
* and copy. * and copy.
*/ */
return (iralloct_realign(ptr, oldsize, size, extra, alignment, return (iralloct_realign(ptr, oldsize, size, 0, alignment, zero,
zero, try_tcache_alloc, try_tcache_dalloc, arena)); try_tcache_alloc, try_tcache_dalloc, arena));
} }
if (size + extra <= arena_maxclass) { if (size <= arena_maxclass) {
return (arena_ralloc(arena, ptr, oldsize, size, extra, return (arena_ralloc(arena, ptr, oldsize, size, 0, alignment,
alignment, zero, try_tcache_alloc, zero, try_tcache_alloc, try_tcache_dalloc));
try_tcache_dalloc));
} else { } else {
return (huge_ralloc(arena, ptr, oldsize, size, extra, return (huge_ralloc(arena, ptr, oldsize, size, 0, alignment,
alignment, zero, try_tcache_dalloc)); zero, try_tcache_dalloc));
} }
} }
JEMALLOC_ALWAYS_INLINE void * JEMALLOC_ALWAYS_INLINE void *
iralloc(void *ptr, size_t size, size_t extra, size_t alignment, bool zero) iralloc(void *ptr, size_t size, size_t alignment, bool zero)
{ {
return (iralloct(ptr, size, extra, alignment, zero, true, true, NULL)); return (iralloct(ptr, size, alignment, zero, true, true, NULL));
} }
JEMALLOC_ALWAYS_INLINE bool JEMALLOC_ALWAYS_INLINE bool

View File

@ -224,7 +224,6 @@ in_valgrind
ipalloc ipalloc
ipalloct ipalloct
iqalloc iqalloc
iqalloct
iralloc iralloc
iralloct iralloct
iralloct_realign iralloct_realign

View File

@ -202,6 +202,7 @@ cat <<EOF
* LG_TINY_MAXCLASS: Lg of maximum tiny size class. * LG_TINY_MAXCLASS: Lg of maximum tiny size class.
* LOOKUP_MAXCLASS: Maximum size class included in lookup table. * LOOKUP_MAXCLASS: Maximum size class included in lookup table.
* SMALL_MAXCLASS: Maximum small size class. * SMALL_MAXCLASS: Maximum small size class.
* LARGE_MINCLASS: Minimum large size class.
*/ */
#define LG_SIZE_CLASS_GROUP ${lg_g} #define LG_SIZE_CLASS_GROUP ${lg_g}
@ -246,6 +247,8 @@ cat <<EOF
# error "Too many small size classes" # error "Too many small size classes"
#endif #endif
#define LARGE_MINCLASS (PAGE_CEILING(SMALL_MAXCLASS+1))
#endif /* JEMALLOC_H_TYPES */ #endif /* JEMALLOC_H_TYPES */
/******************************************************************************/ /******************************************************************************/
#ifdef JEMALLOC_H_STRUCTS #ifdef JEMALLOC_H_STRUCTS

View File

@ -2108,7 +2108,7 @@ arena_ralloc(arena_t *arena, void *ptr, size_t oldsize, size_t size,
copysize = (size < oldsize) ? size : oldsize; copysize = (size < oldsize) ? size : oldsize;
JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, copysize); JEMALLOC_VALGRIND_MAKE_MEM_UNDEFINED(ret, copysize);
memcpy(ret, ptr, copysize); memcpy(ret, ptr, copysize);
iqalloct(ptr, try_tcache_dalloc); iqalloc(ptr, try_tcache_dalloc);
return (ret); return (ret);
} }

View File

@ -129,7 +129,7 @@ huge_ralloc(arena_t *arena, void *ptr, size_t oldsize, size_t size,
*/ */
copysize = (size < oldsize) ? size : oldsize; copysize = (size < oldsize) ? size : oldsize;
memcpy(ret, ptr, copysize); memcpy(ret, ptr, copysize);
iqalloct(ptr, try_tcache_dalloc); iqalloc(ptr, try_tcache_dalloc);
return (ret); return (ret);
} }

View File

@ -870,7 +870,7 @@ imalloc_prof_sample(size_t usize, prof_tctx_t *tctx)
if (tctx == NULL) if (tctx == NULL)
return (NULL); return (NULL);
if (usize <= SMALL_MAXCLASS) { if (usize <= SMALL_MAXCLASS) {
p = imalloc(SMALL_MAXCLASS+1); p = imalloc(LARGE_MINCLASS);
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
arena_prof_promoted(p, usize); arena_prof_promoted(p, usize);
@ -950,9 +950,8 @@ imemalign_prof_sample(size_t alignment, size_t usize, prof_tctx_t *tctx)
if (tctx == NULL) if (tctx == NULL)
return (NULL); return (NULL);
if (usize <= SMALL_MAXCLASS) { if (usize <= SMALL_MAXCLASS) {
assert(sa2u(SMALL_MAXCLASS+1, alignment) != 0); assert(sa2u(LARGE_MINCLASS, alignment) == LARGE_MINCLASS);
p = ipalloc(sa2u(SMALL_MAXCLASS+1, alignment), alignment, p = imalloc(LARGE_MINCLASS);
false);
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
arena_prof_promoted(p, usize); arena_prof_promoted(p, usize);
@ -1077,7 +1076,7 @@ icalloc_prof_sample(size_t usize, prof_tctx_t *tctx)
if (tctx == NULL) if (tctx == NULL)
return (NULL); return (NULL);
if (usize <= SMALL_MAXCLASS) { if (usize <= SMALL_MAXCLASS) {
p = icalloc(SMALL_MAXCLASS+1); p = icalloc(LARGE_MINCLASS);
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
arena_prof_promoted(p, usize); arena_prof_promoted(p, usize);
@ -1174,12 +1173,12 @@ irealloc_prof_sample(void *oldptr, size_t usize, prof_tctx_t *tctx)
if (tctx == NULL) if (tctx == NULL)
return (NULL); return (NULL);
if (usize <= SMALL_MAXCLASS) { if (usize <= SMALL_MAXCLASS) {
p = iralloc(oldptr, SMALL_MAXCLASS+1, 0, 0, false); p = iralloc(oldptr, LARGE_MINCLASS, 0, false);
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
arena_prof_promoted(p, usize); arena_prof_promoted(p, usize);
} else } else
p = iralloc(oldptr, usize, 0, 0, false); p = iralloc(oldptr, usize, 0, false);
return (p); return (p);
} }
@ -1194,7 +1193,7 @@ irealloc_prof(void *oldptr, size_t old_usize, size_t usize, prof_tctx_t *tctx)
if ((uintptr_t)tctx != (uintptr_t)1U) if ((uintptr_t)tctx != (uintptr_t)1U)
p = irealloc_prof_sample(oldptr, usize, tctx); p = irealloc_prof_sample(oldptr, usize, tctx);
else else
p = iralloc(oldptr, usize, 0, 0, false); p = iralloc(oldptr, usize, 0, false);
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
prof_realloc(p, usize, tctx, old_usize, old_tctx); prof_realloc(p, usize, tctx, old_usize, old_tctx);
@ -1203,7 +1202,7 @@ irealloc_prof(void *oldptr, size_t old_usize, size_t usize, prof_tctx_t *tctx)
} }
JEMALLOC_INLINE_C void JEMALLOC_INLINE_C void
ifree(void *ptr) ifree(void *ptr, bool try_tcache)
{ {
size_t usize; size_t usize;
UNUSED size_t rzsize JEMALLOC_CC_SILENCE_INIT(0); UNUSED size_t rzsize JEMALLOC_CC_SILENCE_INIT(0);
@ -1220,7 +1219,7 @@ ifree(void *ptr)
thread_allocated_tsd_get()->deallocated += usize; thread_allocated_tsd_get()->deallocated += usize;
if (config_valgrind && in_valgrind) if (config_valgrind && in_valgrind)
rzsize = p2rz(ptr); rzsize = p2rz(ptr);
iqalloc(ptr); iqalloc(ptr, try_tcache);
JEMALLOC_VALGRIND_FREE(ptr, rzsize); JEMALLOC_VALGRIND_FREE(ptr, rzsize);
} }
@ -1236,7 +1235,7 @@ je_realloc(void *ptr, size_t size)
if (ptr != NULL) { if (ptr != NULL) {
/* realloc(ptr, 0) is equivalent to free(ptr). */ /* realloc(ptr, 0) is equivalent to free(ptr). */
UTRACE(ptr, 0, 0); UTRACE(ptr, 0, 0);
ifree(ptr); ifree(ptr, true);
return (NULL); return (NULL);
} }
size = 1; size = 1;
@ -1261,7 +1260,7 @@ je_realloc(void *ptr, size_t size)
} else { } else {
if (config_stats || (config_valgrind && in_valgrind)) if (config_stats || (config_valgrind && in_valgrind))
usize = s2u(size); usize = s2u(size);
ret = iralloc(ptr, size, 0, 0, false); ret = iralloc(ptr, size, 0, false);
} }
} else { } else {
/* realloc(NULL, size) is equivalent to malloc(size). */ /* realloc(NULL, size) is equivalent to malloc(size). */
@ -1295,7 +1294,7 @@ je_free(void *ptr)
UTRACE(ptr, 0, 0); UTRACE(ptr, 0, 0);
if (ptr != NULL) if (ptr != NULL)
ifree(ptr); ifree(ptr, true);
} }
/* /*
@ -1363,99 +1362,153 @@ JEMALLOC_EXPORT void *(*__memalign_hook)(size_t alignment, size_t size) =
* Begin non-standard functions. * Begin non-standard functions.
*/ */
JEMALLOC_ALWAYS_INLINE_C void
imallocx_flags_decode_hard(size_t size, int flags, size_t *usize,
size_t *alignment, bool *zero, bool *try_tcache, arena_t **arena)
{
if ((flags & MALLOCX_LG_ALIGN_MASK) == 0) {
*alignment = 0;
*usize = s2u(size);
} else {
*alignment = MALLOCX_ALIGN_GET_SPECIFIED(flags);
*usize = sa2u(size, *alignment);
}
*zero = MALLOCX_ZERO_GET(flags);
if ((flags & MALLOCX_ARENA_MASK) != 0) {
unsigned arena_ind = MALLOCX_ARENA_GET(flags);
*try_tcache = false;
*arena = arenas[arena_ind];
} else {
*try_tcache = true;
*arena = NULL;
}
}
JEMALLOC_ALWAYS_INLINE_C void
imallocx_flags_decode(size_t size, int flags, size_t *usize, size_t *alignment,
bool *zero, bool *try_tcache, arena_t **arena)
{
if (flags == 0) {
*usize = s2u(size);
assert(usize != 0);
*alignment = 0;
*zero = false;
*try_tcache = true;
*arena = NULL;
} else {
imallocx_flags_decode_hard(size, flags, usize, alignment, zero,
try_tcache, arena);
}
}
JEMALLOC_ALWAYS_INLINE_C void * JEMALLOC_ALWAYS_INLINE_C void *
imallocx(size_t usize, size_t alignment, bool zero, bool try_tcache, imallocx_flags(size_t usize, size_t alignment, bool zero, bool try_tcache,
arena_t *arena) arena_t *arena)
{ {
assert(usize == ((alignment == 0) ? s2u(usize) : sa2u(usize,
alignment)));
if (alignment != 0) if (alignment != 0)
return (ipalloct(usize, alignment, zero, try_tcache, arena)); return (ipalloct(usize, alignment, zero, try_tcache, arena));
else if (zero) if (zero)
return (icalloct(usize, try_tcache, arena)); return (icalloct(usize, try_tcache, arena));
else
return (imalloct(usize, try_tcache, arena)); return (imalloct(usize, try_tcache, arena));
} }
JEMALLOC_ALWAYS_INLINE_C void *
imallocx_maybe_flags(size_t size, int flags, size_t usize, size_t alignment,
bool zero, bool try_tcache, arena_t *arena)
{
if (flags == 0)
return (imalloc(size));
return (imallocx_flags(usize, alignment, zero, try_tcache, arena));
}
static void * static void *
imallocx_prof_sample(size_t usize, size_t alignment, bool zero, bool try_tcache, imallocx_prof_sample(size_t size, int flags, size_t usize, size_t alignment,
arena_t *arena, prof_tctx_t *tctx) bool zero, bool try_tcache, arena_t *arena)
{ {
void *p; void *p;
if (tctx == NULL)
return (NULL);
if (usize <= SMALL_MAXCLASS) { if (usize <= SMALL_MAXCLASS) {
size_t usize_promoted = (alignment == 0) ? assert(((alignment == 0) ? s2u(LARGE_MINCLASS) :
s2u(SMALL_MAXCLASS+1) : sa2u(SMALL_MAXCLASS+1, alignment); sa2u(LARGE_MINCLASS, alignment)) == LARGE_MINCLASS);
assert(usize_promoted != 0); p = imalloc(LARGE_MINCLASS);
p = imallocx(usize_promoted, alignment, zero, try_tcache,
arena);
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
arena_prof_promoted(p, usize); arena_prof_promoted(p, usize);
} else } else {
p = imallocx(usize, alignment, zero, try_tcache, arena); p = imallocx_maybe_flags(size, flags, usize, alignment, zero,
try_tcache, arena);
}
return (p); return (p);
} }
JEMALLOC_ALWAYS_INLINE_C void * JEMALLOC_ALWAYS_INLINE_C void *
imallocx_prof(size_t usize, size_t alignment, bool zero, bool try_tcache, imallocx_prof(size_t size, int flags, size_t *usize)
arena_t *arena, prof_tctx_t *tctx)
{ {
void *p; void *p;
size_t alignment;
bool zero;
bool try_tcache;
arena_t *arena;
prof_tctx_t *tctx;
if ((uintptr_t)tctx != (uintptr_t)1U) { imallocx_flags_decode(size, flags, usize, &alignment, &zero,
p = imallocx_prof_sample(usize, alignment, zero, try_tcache, &try_tcache, &arena);
arena, tctx); tctx = prof_alloc_prep(*usize);
if ((uintptr_t)tctx == (uintptr_t)1U) {
p = imallocx_maybe_flags(size, flags, *usize, alignment, zero,
try_tcache, arena);
} else if ((uintptr_t)tctx > (uintptr_t)1U) {
p = imallocx_prof_sample(size, flags, *usize, alignment, zero,
try_tcache, arena);
} else } else
p = imallocx(usize, alignment, zero, try_tcache, arena); p = NULL;
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
prof_malloc(p, usize, tctx); prof_malloc(p, *usize, tctx);
return (p); return (p);
} }
JEMALLOC_ALWAYS_INLINE_C void *
imallocx_no_prof(size_t size, int flags, size_t *usize)
{
size_t alignment;
bool zero;
bool try_tcache;
arena_t *arena;
if (flags == 0) {
if (config_stats || (config_valgrind && in_valgrind))
*usize = s2u(size);
return (imalloc(size));
}
imallocx_flags_decode_hard(size, flags, usize, &alignment, &zero,
&try_tcache, &arena);
return (imallocx_flags(*usize, alignment, zero, try_tcache, arena));
}
void * void *
je_mallocx(size_t size, int flags) je_mallocx(size_t size, int flags)
{ {
void *p; void *p;
size_t usize; size_t usize;
size_t alignment = (ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK)
& (SIZE_T_MAX-1));
bool zero = flags & MALLOCX_ZERO;
unsigned arena_ind = ((unsigned)(flags >> 8)) - 1;
arena_t *arena;
bool try_tcache;
assert(size != 0); assert(size != 0);
if (malloc_init()) if (malloc_init())
goto label_oom; goto label_oom;
if (arena_ind != UINT_MAX) { if (config_prof && opt_prof)
arena = arenas[arena_ind]; p = imallocx_prof(size, flags, &usize);
try_tcache = false; else
} else { p = imallocx_no_prof(size, flags, &usize);
arena = NULL;
try_tcache = true;
}
usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment);
assert(usize != 0);
if (config_prof && opt_prof) {
prof_tctx_t *tctx;
tctx = prof_alloc_prep(usize);
p = imallocx_prof(usize, alignment, zero, try_tcache, arena,
tctx);
} else
p = imallocx(usize, alignment, zero, try_tcache, arena);
if (p == NULL) if (p == NULL)
goto label_oom; goto label_oom;
@ -1464,7 +1517,7 @@ je_mallocx(size_t size, int flags)
thread_allocated_tsd_get()->allocated += usize; thread_allocated_tsd_get()->allocated += usize;
} }
UTRACE(0, size, p); UTRACE(0, size, p);
JEMALLOC_VALGRIND_MALLOC(true, p, usize, zero); JEMALLOC_VALGRIND_MALLOC(true, p, usize, MALLOCX_ZERO_GET(flags));
return (p); return (p);
label_oom: label_oom:
if (config_xmalloc && opt_xmalloc) { if (config_xmalloc && opt_xmalloc) {
@ -1485,15 +1538,14 @@ irallocx_prof_sample(void *oldptr, size_t size, size_t alignment, size_t usize,
if (tctx == NULL) if (tctx == NULL)
return (NULL); return (NULL);
if (usize <= SMALL_MAXCLASS) { if (usize <= SMALL_MAXCLASS) {
p = iralloct(oldptr, SMALL_MAXCLASS+1, (SMALL_MAXCLASS+1 >= p = iralloct(oldptr, LARGE_MINCLASS, alignment, zero,
size) ? 0 : size - (SMALL_MAXCLASS+1), alignment, zero,
try_tcache_alloc, try_tcache_dalloc, arena); try_tcache_alloc, try_tcache_dalloc, arena);
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
arena_prof_promoted(p, usize); arena_prof_promoted(p, usize);
} else { } else {
p = iralloct(oldptr, size, 0, alignment, zero, p = iralloct(oldptr, size, alignment, zero, try_tcache_alloc,
try_tcache_alloc, try_tcache_dalloc, arena); try_tcache_dalloc, arena);
} }
return (p); return (p);
@ -1512,8 +1564,8 @@ irallocx_prof(void *oldptr, size_t old_usize, size_t size, size_t alignment,
p = irallocx_prof_sample(oldptr, size, alignment, *usize, zero, p = irallocx_prof_sample(oldptr, size, alignment, *usize, zero,
try_tcache_alloc, try_tcache_dalloc, arena, tctx); try_tcache_alloc, try_tcache_dalloc, arena, tctx);
else { else {
p = iralloct(oldptr, size, 0, alignment, zero, p = iralloct(oldptr, size, alignment, zero, try_tcache_alloc,
try_tcache_alloc, try_tcache_dalloc, arena); try_tcache_dalloc, arena);
} }
if (p == NULL) if (p == NULL)
return (NULL); return (NULL);
@ -1540,10 +1592,8 @@ je_rallocx(void *ptr, size_t size, int flags)
void *p; void *p;
size_t usize, old_usize; size_t usize, old_usize;
UNUSED size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0); UNUSED size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0);
size_t alignment = (ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK) size_t alignment = MALLOCX_ALIGN_GET(flags);
& (SIZE_T_MAX-1));
bool zero = flags & MALLOCX_ZERO; bool zero = flags & MALLOCX_ZERO;
unsigned arena_ind = ((unsigned)(flags >> 8)) - 1;
bool try_tcache_alloc, try_tcache_dalloc; bool try_tcache_alloc, try_tcache_dalloc;
arena_t *arena; arena_t *arena;
@ -1552,7 +1602,8 @@ je_rallocx(void *ptr, size_t size, int flags)
assert(malloc_initialized || IS_INITIALIZER); assert(malloc_initialized || IS_INITIALIZER);
malloc_thread_init(); malloc_thread_init();
if (arena_ind != UINT_MAX) { if ((flags & MALLOCX_ARENA_MASK) != 0) {
unsigned arena_ind = MALLOCX_ARENA_GET(flags);
arena_chunk_t *chunk; arena_chunk_t *chunk;
try_tcache_alloc = false; try_tcache_alloc = false;
chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
@ -1582,7 +1633,7 @@ je_rallocx(void *ptr, size_t size, int flags)
if (p == NULL) if (p == NULL)
goto label_oom; goto label_oom;
} else { } else {
p = iralloct(ptr, size, 0, alignment, zero, try_tcache_alloc, p = iralloct(ptr, size, alignment, zero, try_tcache_alloc,
try_tcache_dalloc, arena); try_tcache_dalloc, arena);
if (p == NULL) if (p == NULL)
goto label_oom; goto label_oom;
@ -1677,10 +1728,8 @@ je_xallocx(void *ptr, size_t size, size_t extra, int flags)
{ {
size_t usize, old_usize; size_t usize, old_usize;
UNUSED size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0); UNUSED size_t old_rzsize JEMALLOC_CC_SILENCE_INIT(0);
size_t alignment = (ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK) size_t alignment = MALLOCX_ALIGN_GET(flags);
& (SIZE_T_MAX-1));
bool zero = flags & MALLOCX_ZERO; bool zero = flags & MALLOCX_ZERO;
unsigned arena_ind = ((unsigned)(flags >> 8)) - 1;
arena_t *arena; arena_t *arena;
assert(ptr != NULL); assert(ptr != NULL);
@ -1689,9 +1738,10 @@ je_xallocx(void *ptr, size_t size, size_t extra, int flags)
assert(malloc_initialized || IS_INITIALIZER); assert(malloc_initialized || IS_INITIALIZER);
malloc_thread_init(); malloc_thread_init();
if (arena_ind != UINT_MAX) if ((flags & MALLOCX_ARENA_MASK) != 0) {
unsigned arena_ind = MALLOCX_ARENA_GET(flags);
arena = arenas[arena_ind]; arena = arenas[arena_ind];
else } else
arena = NULL; arena = NULL;
old_usize = isalloc(ptr, config_prof); old_usize = isalloc(ptr, config_prof);
@ -1753,15 +1803,13 @@ je_sallocx(const void *ptr, int flags)
void void
je_dallocx(void *ptr, int flags) je_dallocx(void *ptr, int flags)
{ {
size_t usize;
UNUSED size_t rzsize JEMALLOC_CC_SILENCE_INIT(0);
unsigned arena_ind = ((unsigned)(flags >> 8)) - 1;
bool try_tcache; bool try_tcache;
assert(ptr != NULL); assert(ptr != NULL);
assert(malloc_initialized || IS_INITIALIZER); assert(malloc_initialized || IS_INITIALIZER);
if (arena_ind != UINT_MAX) { if ((flags & MALLOCX_ARENA_MASK) != 0) {
unsigned arena_ind = MALLOCX_ARENA_GET(flags);
arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr); arena_chunk_t *chunk = (arena_chunk_t *)CHUNK_ADDR2BASE(ptr);
try_tcache = (chunk == ptr || chunk->arena != try_tcache = (chunk == ptr || chunk->arena !=
arenas[arena_ind]); arenas[arena_ind]);
@ -1769,34 +1817,25 @@ je_dallocx(void *ptr, int flags)
try_tcache = true; try_tcache = true;
UTRACE(ptr, 0, 0); UTRACE(ptr, 0, 0);
if (config_stats || config_valgrind) ifree(ptr, try_tcache);
usize = isalloc(ptr, config_prof);
if (config_prof && opt_prof) {
if (config_stats == false && config_valgrind == false)
usize = isalloc(ptr, config_prof);
prof_free(ptr, usize);
}
if (config_stats)
thread_allocated_tsd_get()->deallocated += usize;
if (config_valgrind && in_valgrind)
rzsize = p2rz(ptr);
iqalloct(ptr, try_tcache);
JEMALLOC_VALGRIND_FREE(ptr, rzsize);
} }
size_t size_t
je_nallocx(size_t size, int flags) je_nallocx(size_t size, int flags)
{ {
size_t usize; size_t usize;
size_t alignment = (ZU(1) << (flags & MALLOCX_LG_ALIGN_MASK)
& (SIZE_T_MAX-1));
assert(size != 0); assert(size != 0);
if (malloc_init()) if (malloc_init())
return (0); return (0);
usize = (alignment == 0) ? s2u(size) : sa2u(size, alignment); if ((flags & MALLOCX_LG_ALIGN_MASK) == 0)
usize = s2u(size);
else {
size_t alignment = MALLOCX_ALIGN_GET_SPECIFIED(flags);
usize = sa2u(size, alignment);
}
assert(usize != 0); assert(usize != 0);
return (usize); return (usize);
} }