From ba3a8b0058462765184a4e239cbe7b16ef12c56c Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Fri, 7 Sep 2018 00:25:02 -0400 Subject: [PATCH] add slot randomization to configuration header --- config.h | 1 + malloc.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/config.h b/config.h index dd1832e..c19a5de 100644 --- a/config.h +++ b/config.h @@ -3,5 +3,6 @@ #define GUARD_SLABS true #define WRITE_AFTER_FREE_CHECK true +#define SLOT_RANDOMIZE true #endif diff --git a/malloc.c b/malloc.c index 04e2c77..1d27dc1 100644 --- a/malloc.c +++ b/malloc.c @@ -204,13 +204,16 @@ static size_t get_free_slot(struct random_state *rng, size_t slots, struct slab_ fatal_error("no zero bits"); } - // randomize start location for linear search (uniform random choice is too slow) - uint64_t random_split = ~(~0UL << get_random_u16_uniform(rng, slots)); + if (SLOT_RANDOMIZE) { + // randomize start location for linear search (uniform random choice is too slow) + uint64_t random_split = ~(~0UL << get_random_u16_uniform(rng, slots)); - size_t slot = ffzl(masked | random_split); - if (slot) { - return slot - 1; + size_t slot = ffzl(masked | random_split); + if (slot) { + return slot - 1; + } } + return ffzl(masked) - 1; }