summaryrefslogtreecommitdiff
path: root/libc/stdlib
diff options
context:
space:
mode:
authorWaldemar Brodkorb <wbx@uclibc-ng.org>2018-01-31 05:27:54 +0000
committerWaldemar Brodkorb <wbx@uclibc-ng.org>2018-01-31 18:48:29 +0000
commit1bc015efddaf7dc4ddd1b6b6831c43e7ad4582c0 (patch)
tree9850117d2b9182ce36354cb4b0ec1163df7aa374 /libc/stdlib
parentf20179d6931df17c9310fd911dd4a348141ba72c (diff)
malloc: add malloc_usable_size()
Diffstat (limited to 'libc/stdlib')
-rw-r--r--libc/stdlib/malloc-standard/malloc_usable_size.c14
1 files changed, 14 insertions, 0 deletions
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;
+}