[PATCH] Add a malloc_usable_size_in_advance function

Mike Hommey mh+jemalloc at glandium.org
Tue Feb 21 08:39:10 PST 2012


From: Mike Hommey <mh at glandium.org>

---
 doc/jemalloc.xml.in            |   13 +++++++++++++
 include/jemalloc/jemalloc.h.in |    2 ++
 src/jemalloc.c                 |    7 +++++++
 3 files changed, 22 insertions(+), 0 deletions(-)

diff --git a/doc/jemalloc.xml.in b/doc/jemalloc.xml.in
index 1e8c800..c125738 100644
--- a/doc/jemalloc.xml.in
+++ b/doc/jemalloc.xml.in
@@ -33,6 +33,7 @@
     <refname>realloc</refname>
     <refname>free</refname>
     <refname>malloc_usable_size</refname>
+    <refname>malloc_usable_size_in_advance</refname>
     <refname>malloc_stats_print</refname>
     <refname>mallctl</refname>
     <refname>mallctlnametomib</refname>
@@ -89,6 +90,11 @@
           <paramdef>const void *<parameter>ptr</parameter></paramdef>
         </funcprototype>
         <funcprototype>
+          <funcdef>size_t <function>malloc_usable_size_in_advance</function></funcdef>
+          <paramdef>size_t <parameter>size</parameter></paramdef>
+          <paramdef>size_t <parameter>alignment</parameter></paramdef>
+        </funcprototype>
+        <funcprototype>
           <funcdef>void <function>malloc_stats_print</function></funcdef>
           <paramdef>void <parameter>(*write_cb)</parameter>
             <funcparams>void *, const char *</funcparams>
@@ -217,6 +223,13 @@
       depended on, since such behavior is entirely implementation-dependent.
       </para>
 
+      <para>The <function>malloc_usable_size_in_advance<parameter/></function>
+      function returns the usable size that would be allocated if the given
+      <parameter>size</parameter> and <parameter>alignment</parameter> were
+      used with <function>malloc<parameter/></function>,
+      <function>memalign<parameter/></function>, or other allocation function.
+      </para>
+
       <para>The <function>malloc_stats_print<parameter/></function> function
       writes human-readable summary statistics via the
       <parameter>write_cb</parameter> callback function pointer and
diff --git a/include/jemalloc/jemalloc.h.in b/include/jemalloc/jemalloc.h.in
index 580a5ec..e43a060 100644
--- a/include/jemalloc/jemalloc.h.in
+++ b/include/jemalloc/jemalloc.h.in
@@ -43,6 +43,8 @@ void	*JEMALLOC_P(realloc)(void *ptr, size_t size);
 void	JEMALLOC_P(free)(void *ptr);
 
 size_t	JEMALLOC_P(malloc_usable_size)(const void *ptr);
+size_t	JEMALLOC_P(malloc_usable_size_in_advance)(size_t size,
+    size_t alignment);
 void	JEMALLOC_P(malloc_stats_print)(void (*write_cb)(void *, const char *),
     void *cbopaque, const char *opts);
 int	JEMALLOC_P(mallctl)(const char *name, void *oldp, size_t *oldlenp,
diff --git a/src/jemalloc.c b/src/jemalloc.c
index e819b7e..64961c5 100644
--- a/src/jemalloc.c
+++ b/src/jemalloc.c
@@ -1357,6 +1357,13 @@ JEMALLOC_P(malloc_usable_size)(const void *ptr)
 }
 
 JEMALLOC_ATTR(visibility("default"))
+size_t
+JEMALLOC_P(malloc_usable_size_in_advance)(size_t size, size_t alignment)
+{
+	return (alignment == 0) ? s2u(size) : sa2u(size, alignment, NULL);
+}
+
+JEMALLOC_ATTR(visibility("default"))
 void
 JEMALLOC_P(malloc_stats_print)(void (*write_cb)(void *, const char *),
     void *cbopaque, const char *opts)
-- 
1.7.9



More information about the jemalloc-discuss mailing list