/* we want 64 entries */
#define MAX_TCACHE_SIZE (MALLOC_ALIGNMENT * 63)
#define TCACHE_IDX ((MAX_TCACHE_SIZE / MALLOC_ALIGNMENT) + 1)
-#define size2tidx(bytes) (((bytes) + MALLOC_ALIGNMENT - 1) / MALLOC_ALIGNMENT)
+#define size2tidx_(bytes) (((bytes) + MALLOC_ALIGNMENT - 1) / MALLOC_ALIGNMENT)
+
+#define tidx2csize(idx) ((idx)*MALLOC_ALIGNMENT + SIZE_SZ)
+#define tidx2usize(idx) ((idx)*MALLOC_ALIGNMENT)
+
+/* When "x" is a user-provided size. */
+#define usize2tidx(x) size2tidx_(x,__FUNCTION__)
+/* When "x" is from chunksize(). */
+#define csize2tidx(x) size2tidx_((x)-SIZE_SZ,__FUNCTION__)
/* Rounds up, so...
idx 0 bytes 0
#if USE_TCACHE
/* Maximum number of buckets to use. */
size_t tcache_max;
+ size_t tcache_max_bytes;
/* Maximum number of chunks in each bucket. */
size_t tcache_count;
/* Maximum number of chunks to remove from the unsorted list, which
#if USE_TCACHE
,
.tcache_count = TCACHE_FILL_COUNT,
- .tcache_max = TCACHE_IDX-1,
+ .tcache_max = TCACHE_IDX,
+ .tcache_max_bytes = tidx2usize(TCACHE_IDX),
.tcache_unsorted_limit = 0 /* No limit */
#endif
};
#if USE_TCACHE
/* int_free also calls request2size, be careful to not pad twice. */
size_t tbytes = request2size(bytes);
- size_t tc_idx = size2tidx (tbytes);
+ size_t tc_idx = csize2tidx (tbytes);
if (tcache.initted == 0)
{
#if USE_TCACHE
/* While we're here, if we see other chunk of the same size,
stash them in the tcache. */
- size_t tc_idx = size2tidx (nb-SIZE_SZ);
+ size_t tc_idx = csize2tidx (nb);
if (tc_idx < mp_.tcache_max)
{
mchunkptr tc_victim;
#if USE_TCACHE
/* While we're here, if we see other chunk of the same size,
stash them in the tcache. */
- size_t tc_idx = size2tidx (nb-SIZE_SZ);
+ size_t tc_idx = csize2tidx (nb);
if (tc_idx < mp_.tcache_max)
{
mchunkptr tc_victim;
#if USE_TCACHE
INTERNAL_SIZE_T tcache_nb = 0;
- if (size2tidx (nb-SIZE_SZ) <= mp_.tcache_max)
+ if (csize2tidx (nb) <= mp_.tcache_max)
tcache_nb = nb;
- size_t tc_idx = size2tidx (nb-SIZE_SZ);
+ size_t tc_idx = csize2tidx (nb);
int return_cached = 0;
tcache_unsorted_count = 0;
#if USE_TCACHE
{
- size_t tc_idx = size2tidx (size - SIZE_SZ);
+ size_t tc_idx = csize2tidx (size);
if (tc_idx < mp_.tcache_max
&& tcache.counts[tc_idx] < mp_.tcache_count
__always_inline
do_set_tcache_max (size_t value)
{
- LIBC_PROBE (memory_mallopt_tcache_max, 2, value, mp_.tcache_max);
- mp_.tcache_max = value;
+ LIBC_PROBE (memory_mallopt_tcache_max_bytes, 2, value, mp_.tcache_max_bytes);
+ if (value >= 0 && value <= MAX_TCACHE_SIZE)
+ {
+ mp_.tcache_max_bytes = value;
+ mp_.tcache_max = usize2tidx (value) + 1;
+ }
return 1;
}
#if USE_TCACHE
case M_TCACHE_COUNT:
if (value >= 0)
- do_set_tcache_max (value);
+ do_set_tcache_count (value);
break;
case M_TCACHE_MAX:
if (value >= 0)
- {
- value = size2tidx (value);
- if (value < TCACHE_IDX)
- do_set_tcache_max (value);
- }
+ do_set_tcache_max (value);
break;
case M_TCACHE_UNSORTED_LIMIT:
if (value >= 0)