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.
diff --git a/src/sec.c b/src/sec.c
index 0c4e703..6fffaf1 100644
--- a/src/sec.c
+++ b/src/sec.c
@@ -23,11 +23,11 @@
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 @@
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 @@
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 e98bdc9..f3ec403 100644
--- a/test/unit/sec.c
+++ b/test/unit/sec.c
@@ -46,6 +46,7 @@
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 *