aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2015-08-30 19:46:52 +0200
committerMattias Andrée <maandree@operamail.com>2015-08-30 19:46:52 +0200
commit3305a21cd0fa4e3b43b1a4495dfc711297ac0cc4 (patch)
tree1dde2c8ceb5e0b02ec3533331a57098893d065b8
parentadd memory.h (diff)
downloadslibc-3305a21cd0fa4e3b43b1a4495dfc711297ac0cc4.tar.gz
slibc-3305a21cd0fa4e3b43b1a4495dfc711297ac0cc4.tar.bz2
slibc-3305a21cd0fa4e3b43b1a4495dfc711297ac0cc4.tar.xz
add malloc_usable_size
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--include/malloc.h15
-rw-r--r--src/malloc.c15
2 files changed, 30 insertions, 0 deletions
diff --git a/include/malloc.h b/include/malloc.h
index 0e17817..956a5e5 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -241,6 +241,21 @@ void* aligned_alloc(size_t, size_t)
#endif
+#if defined(_GNU_SOURCE) && !defined(_PORTABLE_SOURCE)
+/**
+ * This function returns the allocation size of
+ * a memory segment.
+ *
+ * `p = malloc(n), malloc_usable_size(p)` will return `n`.
+ *
+ * @param segment The memory segment.
+ * @return The size of the memory segment, 0 if `segment` is `NULL`.
+ */
+size_t malloc_usable_size(void*)
+ __GCC_ONLY(__attribute__((warn_unused_result)));
+#endif
+
+
#endif
diff --git a/src/malloc.c b/src/malloc.c
index f7a2e95..e09cc85 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -307,3 +307,18 @@ void* aligned_alloc(size_t boundary, size_t size)
return memalign(boundary, size);
}
+
+/**
+ * This function returns the allocation size of
+ * a memory segment.
+ *
+ * `p = malloc(n), malloc_usable_size(p)` will return `n`.
+ *
+ * @param segment The memory segment.
+ * @return The size of the memory segment, 0 if `segment` is `NULL`.
+ */
+size_t malloc_usable_size(void* segment)
+{
+ return allocsize(segment);
+}
+