summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/malloc.h2
-rw-r--r--libc/stdlib/malloc-standard/malloc_usable_size.c14
2 files changed, 16 insertions, 0 deletions
diff --git a/include/malloc.h b/include/malloc.h
index b16a1105a..e52792381 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -162,6 +162,8 @@ extern int malloc_trim(size_t pad);
/* Prints brief summary statistics on the stderr. */
extern void malloc_stats(void);
+extern size_t malloc_usable_size(void *);
+
/* SVID2/XPG mallopt options */
#ifndef M_MXFAST
# define M_MXFAST 1 /* UNUSED in this malloc */
diff --git a/libc/stdlib/malloc-standard/malloc_usable_size.c b/libc/stdlib/malloc-standard/malloc_usable_size.c
new file mode 100644
index 000000000..c78c6a867
--- /dev/null
+++ b/libc/stdlib/malloc-standard/malloc_usable_size.c
@@ -0,0 +1,14 @@
+/*
+ malloc_usable_size - fully inspired by musl implementation
+*/
+
+#include "malloc.h"
+
+/* for malloc_usable_size */
+#define OVERHEAD (2*sizeof(size_t))
+#define CHUNK_SIZE(c) ((c)->size & -2)
+#define MEM_TO_CHUNK(p) (struct malloc_chunk *)((char *)(p) - OVERHEAD)
+
+size_t malloc_usable_size(void *p) {
+ return p ? CHUNK_SIZE(MEM_TO_CHUNK(p)) - OVERHEAD : 0;
+}