tlsf: fix misaccounting in request_store feature

A TLSF pool itself has overhead in the form of tlsf_pool_overhead()
that's distinct from the struct pool_entry. Adding a helper that returns
this total overhead allows us also to use roundup_pow_of_two(), which
previously issues a warning on clang about sizeof(sizeof(...)) looking
strange.

Another issues is that request_store was not explicitly zeroed leading
to issues on OOM situations, which is remedied as well.

Signed-off-by: Ahmad Fatoum <[email protected]>
---
 common/tlsf.c        | 1 +
 common/tlsf_malloc.c | 7 ++++++-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/common/tlsf.c b/common/tlsf.c
index 3c72b41c0385..e7f1b8354f02 100644
--- a/common/tlsf.c
+++ b/common/tlsf.c
@@ -654,6 +654,7 @@ static void control_construct(control_t* control)
        control->block_null.next_free = &control->block_null;
        control->block_null.prev_free = &control->block_null;
 
+       control->request_store = NULL;
        control->fl_bitmap = 0;
        for (i = 0; i < FL_INDEX_COUNT; ++i)
        {
diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c
index d3b665a2fc40..9d55014117f0 100644
--- a/common/tlsf_malloc.c
+++ b/common/tlsf_malloc.c
@@ -27,6 +27,11 @@ struct pool_entry {
 
 static LIST_HEAD(mem_pool_list);
 
+static inline size_t mem_pool_overhead(void)
+{
+       return sizeof(struct pool_entry) + tlsf_pool_overhead();
+}
+
 void *malloc(size_t bytes)
 {
        void *mem;
@@ -132,7 +137,7 @@ static void tlsf_request_store(tlsf_t tlsf, size_t bytes)
 {
        size_t size;
 
-       size = __roundup_pow_of_two(bytes + sizeof(struct pool_entry));
+       size = roundup_pow_of_two(bytes + mem_pool_overhead());
 
        malloc_request_store(max_t(size_t, SZ_8M, size));
 }
-- 
2.47.3


Reply via email to