summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lapenkou <lapenkov@fb.com>2022-03-14 20:17:14 -0700
committerAlexander Lapenkov <lapenkov.a@yandex.ru>2022-03-22 17:45:55 -0700
commit52631c90f664ded0a5106a7d5fd906d46a7c1f81 (patch)
tree30ac880f9599dd512fa1621996df6ebfdbacd3b4
parent7ae0f15c598258610dd3cfd9633301ffa8661c45 (diff)
downloadjemalloc-52631c90f664ded0a5106a7d5fd906d46a7c1f81.tar.gz
Fix size class calculation for sec
Due to a bug in sec initialization, the number of cached size classes was equal to 198. The bug caused the creation of more than a hundred of unused bins, although it didn't affect the caching logic.
-rw-r--r--src/sec.c13
-rw-r--r--test/unit/sec.c1
2 files changed, 9 insertions, 5 deletions
diff --git a/src/sec.c b/src/sec.c
index 0c4e7032..6fffaf1e 100644
--- a/src/sec.c
+++ b/src/sec.c
@@ -23,11 +23,11 @@ sec_bin_init(sec_bin_t *bin) {
bool
sec_init(tsdn_t *tsdn, sec_t *sec, base_t *base, pai_t *fallback,
const sec_opts_t *opts) {
- size_t max_alloc = opts->max_alloc & PAGE_MASK;
- pszind_t npsizes = sz_psz2ind(max_alloc);
- if (sz_pind2sz(npsizes) > opts->max_alloc) {
- npsizes--;
- }
+ assert(opts->max_alloc > 0);
+
+ size_t max_alloc = opts->max_alloc & ~PAGE_MASK;
+ pszind_t npsizes = sz_psz2ind(max_alloc) + 1;
+
size_t sz_shards = opts->nshards * sizeof(sec_shard_t);
size_t sz_bins = opts->nshards * (size_t)npsizes * sizeof(sec_bin_t);
size_t sz_alloc = sz_shards + sz_bins;
@@ -232,6 +232,8 @@ sec_alloc(tsdn_t *tsdn, pai_t *self, size_t size, size_t alignment, bool zero,
deferred_work_generated);
}
pszind_t pszind = sz_psz2ind(size);
+ assert(pszind < sec->npsizes);
+
sec_shard_t *shard = sec_shard_pick(tsdn, sec);
sec_bin_t *bin = &shard->bins[pszind];
bool do_batch_fill = false;
@@ -305,6 +307,7 @@ sec_shard_dalloc_and_unlock(tsdn_t *tsdn, sec_t *sec, sec_shard_t *shard,
assert(shard->bytes_cur <= sec->opts.max_bytes);
size_t size = edata_size_get(edata);
pszind_t pszind = sz_psz2ind(size);
+ assert(pszind < sec->npsizes);
/*
* Prepending here results in LIFO allocation per bin, which seems
* reasonable.
diff --git a/test/unit/sec.c b/test/unit/sec.c
index e98bdc92..f3ec403d 100644
--- a/test/unit/sec.c
+++ b/test/unit/sec.c
@@ -46,6 +46,7 @@ test_sec_init(sec_t *sec, pai_t *fallback, size_t nshards, size_t max_alloc,
bool err = sec_init(TSDN_NULL, sec, base, fallback, &opts);
assert_false(err, "Unexpected initialization failure");
+ assert_u_ge(sec->npsizes, 0, "Zero size classes allowed for caching");
}
static inline edata_t *