[gegl] utils: reduce size overhead of gegl_malloc()



commit 18ab54ed6686bb126e3a8b359991e35192e014a5
Author: Ell <ell_se yahoo com>
Date:   Wed Sep 6 11:24:48 2017 -0400

    utils: reduce size overhead of gegl_malloc()
    
    gegl_malloc() stores a pointer to the real malloc-ed buffer in front
    of the returned buffer, requiring sizeof(gpointer) bytes on top of
    GEGL_ALIGN.  Since the value of GEGL_ALIGN (16) easily fits in a
    single byte, we can replace this pointer with a single-byte offset
    to the real start of the buffer.  Since the offset from the real
    start of the buffer to the returned buffer is at least 1, we don't
    need to allocate any additional space on top of GEGL_ALIGN.

 gegl/gegl-utils.c |   13 +++++++------
 1 files changed, 7 insertions(+), 6 deletions(-)
---
diff --git a/gegl/gegl-utils.c b/gegl/gegl-utils.c
index 4e77879..2dcfc27 100644
--- a/gegl/gegl-utils.c
+++ b/gegl/gegl-utils.c
@@ -243,6 +243,7 @@ gegl_rectangle_get_type (void)
 }
 
 #define GEGL_ALIGN 16
+G_STATIC_ASSERT (GEGL_ALIGN <= G_MAXUINT8);
 
 /* utility call that makes sure allocations are 16 byte aligned.
  * making RGBA float buffers have aligned access for pixels.
@@ -253,12 +254,12 @@ gpointer gegl_malloc (gsize size)
   gchar *ret;
   gint   offset;
 
-  mem    = g_malloc (size + GEGL_ALIGN + sizeof(gpointer));
-  offset = GEGL_ALIGN - (GPOINTER_TO_UINT(mem) + sizeof(gpointer)) % GEGL_ALIGN;
-  ret    = (gpointer)(mem + sizeof(gpointer) + offset);
+  mem    = g_malloc (size + GEGL_ALIGN);
+  offset = GEGL_ALIGN - GPOINTER_TO_UINT(mem) % GEGL_ALIGN;
+  ret    = (gpointer)(mem + offset);
 
-  /* store the real malloc one pointer in front of this malloc */
-  *(gpointer*)(ret-sizeof(gpointer))=mem;
+  /* store the offset to the real malloc one byte in front of this malloc */
+  *(guint8*)(ret-1)=offset;
   return (gpointer) ret;
 }
 
@@ -273,7 +274,7 @@ void
 gegl_free (gpointer buf)
 {
   g_assert (buf);
-  g_free (*((gpointer*)buf -1));
+  g_free ((gchar*)buf - *((guint8*)buf -1));
 }
 
 void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]