mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH 1/2] malloc: implement malloc_usable_size()
Date: Tue,  6 Aug 2024 12:54:28 +0200	[thread overview]
Message-ID: <20240806105429.2278660-1-s.hauer@pengutronix.de> (raw)

malloc_usable_size() is a standard libc function. Implement it for the
barebox allocators, dlmalloc and tlsf_malloc.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/dlmalloc.c    | 12 ++++++++++++
 common/tlsf_malloc.c | 11 +++++++++++
 include/malloc.h     |  1 +
 3 files changed, 24 insertions(+)

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index c41487d54b..99f666aada 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -1431,6 +1431,18 @@ void free(void *mem)
 		frontlink(p, sz, idx, bck, fwd);
 }
 
+size_t malloc_usable_size(void *mem)
+{
+	mchunkptr p;
+	size_t size;
+
+	if (!mem)
+		return 0;
+
+	p = mem2chunk(mem);
+	return chunksize(p);
+}
+
 /*
   Realloc algorithm:
 
diff --git a/common/tlsf_malloc.c b/common/tlsf_malloc.c
index 981f09de41..f07ef5645b 100644
--- a/common/tlsf_malloc.c
+++ b/common/tlsf_malloc.c
@@ -38,6 +38,17 @@ void free(void *mem)
 }
 EXPORT_SYMBOL(free);
 
+size_t malloc_usable_size(void *mem)
+{
+	size_t size;
+
+	if (!mem)
+		return 0;
+
+	return tlsf_block_size(mem);
+}
+EXPORT_SYMBOL(malloc_usable_size);
+
 void *realloc(void *oldmem, size_t bytes)
 {
 	void *mem = tlsf_realloc(tlsf_mem_pool, oldmem, bytes);
diff --git a/include/malloc.h b/include/malloc.h
index d63853b91e..5cdff0a4f9 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -6,6 +6,7 @@
 #include <types.h>
 
 void *malloc(size_t) __alloc_size(1);
+size_t malloc_usable_size(void *);
 void free(void *);
 void *realloc(void *, size_t) __realloc_size(2);
 void *memalign(size_t, size_t) __alloc_size(2);
-- 
2.39.2




             reply	other threads:[~2024-08-06 10:55 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-08-06 10:54 Sascha Hauer [this message]
2024-08-06 10:54 ` [PATCH 2/2] malloc: implement free_sensitive() Sascha Hauer
2024-08-06 12:56 ` [PATCH] fixup! malloc: implement malloc_usable_size() Ahmad Fatoum
2024-08-07  6:58   ` (subset) " Sascha Hauer
2024-08-07  6:58 ` [PATCH 1/2] " Sascha Hauer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240806105429.2278660-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox