From 4777ee0c8591986380f8e6c161f15536c3d0af21 Mon Sep 17 00:00:00 2001 From: Daniel Micay Date: Sat, 25 Aug 2018 15:21:50 -0400 Subject: [PATCH] special case division for page size slabs --- malloc.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/malloc.c b/malloc.c index a3225bf..d516f25 100644 --- a/malloc.c +++ b/malloc.c @@ -292,7 +292,12 @@ static void *get_slab(struct size_class *c, size_t slab_size, struct slab_metada static struct slab_metadata *get_metadata(struct size_class *c, size_t slab_size, void *p) { size_t offset = (char *)p - (char *)c->class_region_start; - size_t index = offset / slab_size; + size_t index; + if (slab_size == PAGE_SIZE) { + index = offset >> PAGE_SHIFT; + } else { + index = offset / slab_size; + } // still caught without this check either as a read access violation or "double free" if (index >= c->metadata_allocated) { fatal_error("invalid free within a slab yet to be used");