mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] fixup! tlsf: support on-demand requesting memory from external store
@ 2025-10-15  8:26 Ahmad Fatoum
  2025-10-20 11:04 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2025-10-15  8:26 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

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 <a.fatoum@barebox.org>
---
 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




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-10-20 12:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-15  8:26 [PATCH] fixup! tlsf: support on-demand requesting memory from external store Ahmad Fatoum
2025-10-20 11:04 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox