sz: Support 0 size in size2index lookup/compute

This commit is contained in:
Dave Watson 2018-10-08 10:11:04 -07:00
parent 2b112ea593
commit 4edbb7c64c
2 changed files with 14 additions and 6 deletions

View File

@ -122,6 +122,10 @@ sz_size2index_compute(size_t size) {
if (unlikely(size > SC_LARGE_MAXCLASS)) { if (unlikely(size > SC_LARGE_MAXCLASS)) {
return SC_NSIZES; return SC_NSIZES;
} }
if (size == 0) {
return 0;
}
#if (SC_NTINY != 0) #if (SC_NTINY != 0)
if (size <= (ZU(1) << SC_LG_TINY_MAXCLASS)) { if (size <= (ZU(1) << SC_LG_TINY_MAXCLASS)) {
szind_t lg_tmin = SC_LG_TINY_MAXCLASS - SC_NTINY + 1; szind_t lg_tmin = SC_LG_TINY_MAXCLASS - SC_NTINY + 1;
@ -150,14 +154,14 @@ sz_size2index_compute(size_t size) {
JEMALLOC_ALWAYS_INLINE szind_t JEMALLOC_ALWAYS_INLINE szind_t
sz_size2index_lookup(size_t size) { sz_size2index_lookup(size_t size) {
assert(size <= SC_LOOKUP_MAXCLASS); assert(size <= SC_LOOKUP_MAXCLASS);
szind_t ret = (sz_size2index_tab[(size-1) >> SC_LG_TINY_MIN]); szind_t ret = (sz_size2index_tab[(size + (ZU(1) << SC_LG_TINY_MIN) - 1)
>> SC_LG_TINY_MIN]);
assert(ret == sz_size2index_compute(size)); assert(ret == sz_size2index_compute(size));
return ret; return ret;
} }
JEMALLOC_ALWAYS_INLINE szind_t JEMALLOC_ALWAYS_INLINE szind_t
sz_size2index(size_t size) { sz_size2index(size_t size) {
assert(size > 0);
if (likely(size <= SC_LOOKUP_MAXCLASS)) { if (likely(size <= SC_LOOKUP_MAXCLASS)) {
return sz_size2index_lookup(size); return sz_size2index_lookup(size);
} }
@ -208,6 +212,10 @@ sz_s2u_compute(size_t size) {
if (unlikely(size > SC_LARGE_MAXCLASS)) { if (unlikely(size > SC_LARGE_MAXCLASS)) {
return 0; return 0;
} }
if (size == 0) {
size++;
}
#if (SC_NTINY > 0) #if (SC_NTINY > 0)
if (size <= (ZU(1) << SC_LG_TINY_MAXCLASS)) { if (size <= (ZU(1) << SC_LG_TINY_MAXCLASS)) {
size_t lg_tmin = SC_LG_TINY_MAXCLASS - SC_NTINY + 1; size_t lg_tmin = SC_LG_TINY_MAXCLASS - SC_NTINY + 1;
@ -241,7 +249,6 @@ sz_s2u_lookup(size_t size) {
*/ */
JEMALLOC_ALWAYS_INLINE size_t JEMALLOC_ALWAYS_INLINE size_t
sz_s2u(size_t size) { sz_s2u(size_t size) {
assert(size > 0);
if (likely(size <= SC_LOOKUP_MAXCLASS)) { if (likely(size <= SC_LOOKUP_MAXCLASS)) {
return sz_s2u_lookup(size); return sz_s2u_lookup(size);
} }

View File

@ -37,18 +37,19 @@ sz_boot_index2size_tab(const sc_data_t *sc_data) {
* the smallest interval for which the result can change. * the smallest interval for which the result can change.
*/ */
JEMALLOC_ALIGNED(CACHELINE) JEMALLOC_ALIGNED(CACHELINE)
uint8_t sz_size2index_tab[SC_LOOKUP_MAXCLASS >> SC_LG_TINY_MIN]; uint8_t sz_size2index_tab[(SC_LOOKUP_MAXCLASS >> SC_LG_TINY_MIN) + 1];
static void static void
sz_boot_size2index_tab(const sc_data_t *sc_data) { sz_boot_size2index_tab(const sc_data_t *sc_data) {
size_t dst_max = (SC_LOOKUP_MAXCLASS >> SC_LG_TINY_MIN); size_t dst_max = (SC_LOOKUP_MAXCLASS >> SC_LG_TINY_MIN) + 1;
size_t dst_ind = 0; size_t dst_ind = 0;
for (unsigned sc_ind = 0; sc_ind < SC_NSIZES && dst_ind < dst_max; for (unsigned sc_ind = 0; sc_ind < SC_NSIZES && dst_ind < dst_max;
sc_ind++) { sc_ind++) {
const sc_t *sc = &sc_data->sc[sc_ind]; const sc_t *sc = &sc_data->sc[sc_ind];
size_t sz = (ZU(1) << sc->lg_base) size_t sz = (ZU(1) << sc->lg_base)
+ (ZU(sc->ndelta) << sc->lg_delta); + (ZU(sc->ndelta) << sc->lg_delta);
size_t max_ind = ((sz - 1) >> SC_LG_TINY_MIN); size_t max_ind = ((sz + (ZU(1) << SC_LG_TINY_MIN) - 1)
>> SC_LG_TINY_MIN);
for (; dst_ind <= max_ind && dst_ind < dst_max; dst_ind++) { for (; dst_ind <= max_ind && dst_ind < dst_max; dst_ind++) {
sz_size2index_tab[dst_ind] = sc_ind; sz_size2index_tab[dst_ind] = sc_ind;
} }