Modify usize calculation and corresponding tests.

This commit is contained in:
guangli-dai 2024-04-18 15:42:14 -07:00 committed by Guangli Dai
parent 9ce475a135
commit a0d18fe5d7
8 changed files with 90 additions and 9 deletions

View file

@ -287,9 +287,14 @@ edata_szind_get(const edata_t *edata) {
return szind;
}
/*
* Calculating usize based on szind is unsafe because the result can be
* inaccurate when sz_limit_usize_gap_enabled() is true. The result should be
* compared against SC_LARGE_MINCLASS before usage.
*/
static inline size_t
edata_usize_get_from_ind_unsafe(const edata_t *edata) {
return sz_index2size(edata_szind_get(edata));
return sz_index2size_unsafe(edata_szind_get(edata));
}
static inline size_t

View file

@ -266,11 +266,33 @@ sz_index2size_lookup(szind_t index) {
}
JEMALLOC_ALWAYS_INLINE size_t
sz_index2size(szind_t index) {
sz_index2size_unsafe(szind_t index) {
assert(index < SC_NSIZES);
return sz_index2size_lookup(index);
}
JEMALLOC_ALWAYS_INLINE size_t
sz_index2size(szind_t index) {
size_t size = sz_index2size_unsafe(index);
if (sz_limit_usize_gap_enabled()) {
/*
* With limit_usize_gap enabled, the usize above
* SC_LARGE_MINCLASS should grow by PAGE. However, for sizes
* in [SC_LARGE_MINCLASS, USIZE_GROW_SLOW_THRESHOLD], the
* usize would not change because the size class gap in this
* range is just the same as PAGE. Although we use
* SC_LARGE_MINCLASS as the threshold in most places, we
* allow tcache and sec to cache up to
* USIZE_GROW_SLOW_THRESHOLD to minimize the side effect of
* not having size classes for larger sizes. Thus, we assert
* the size is no larger than USIZE_GROW_SLOW_THRESHOLD here
* instead of SC_LARGE_MINCLASS.
*/
assert(size <= USIZE_GROW_SLOW_THRESHOLD);
}
return size;
}
JEMALLOC_ALWAYS_INLINE void
sz_size2index_usize_fastpath(size_t size, szind_t *ind, size_t *usize) {
if (util_compile_time_const(size)) {
@ -305,7 +327,7 @@ sz_s2u_compute(size_t size) {
(ZU(1) << lg_ceil));
}
#endif
{
if (!sz_limit_usize_gap_enabled() || size <= SC_SMALL_MAXCLASS) {
size_t x = lg_floor((size<<1)-1);
size_t lg_delta = (x < SC_LG_NGROUP + LG_QUANTUM + 1)
? LG_QUANTUM : x - SC_LG_NGROUP - 1;
@ -313,11 +335,18 @@ sz_s2u_compute(size_t size) {
size_t delta_mask = delta - 1;
size_t usize = (size + delta_mask) & ~delta_mask;
return usize;
} else {
size_t usize = ((size + PAGE - 1) >> LG_PAGE) << LG_PAGE;
assert(usize - size < PAGE);
return usize;
}
}
JEMALLOC_ALWAYS_INLINE size_t
sz_s2u_lookup(size_t size) {
if (config_limit_usize_gap) {
assert(size < SC_LARGE_MINCLASS);
}
size_t ret = sz_index2size_lookup(sz_size2index_lookup(size));
assert(ret == sz_s2u_compute(size));