From 911b4c0b10660a377c967a368eabbb05c04a225b Mon Sep 17 00:00:00 2001 From: "Justin C. Miller" Date: Sun, 2 Aug 2020 16:54:06 -0700 Subject: [PATCH] [kutil] Fix a bug with order ignoring header size When moving heap allocation order calculation to use __builtin_clz, we based that calculation off of the length requested, not the totla length including the memory header size. --- src/libraries/kutil/heap_allocator.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libraries/kutil/heap_allocator.cpp b/src/libraries/kutil/heap_allocator.cpp index b77a663..4c3ca3d 100644 --- a/src/libraries/kutil/heap_allocator.cpp +++ b/src/libraries/kutil/heap_allocator.cpp @@ -77,7 +77,9 @@ heap_allocator::allocate(size_t length) if (length == 0) return nullptr; - unsigned order = 64 - __builtin_clz(length); + const unsigned clz = __builtin_clzll(total); + const unsigned order = 64 - clz; + kassert(order <= max_order, "Tried to allocate a block bigger than max_order"); if (order > max_order) return nullptr;