[gegl] buffer: move memory-related functions to gegl-memory



commit 9d6623ddbe341659b5f31e32a3da93dee76f17ec
Author: Ell <ell_se yahoo com>
Date:   Sun Jan 6 05:48:47 2019 -0500

    buffer: move memory-related functions to gegl-memory
    
    Move all the memory-related functions (i.e., the memory allocation
    and manipulation functions) to a new gegl-memory module.

 gegl/Makefile.am                  |   1 +
 gegl/buffer/Makefile.am           |   3 +
 gegl/buffer/gegl-buffer.h         |  68 +-------------
 gegl/buffer/gegl-memory-private.h |  24 +++++
 gegl/buffer/gegl-memory.c         | 184 ++++++++++++++++++++++++++++++++++++++
 gegl/buffer/gegl-memory.h         |  84 +++++++++++++++++
 gegl/buffer/gegl-rectangle.c      | 154 -------------------------------
 7 files changed, 299 insertions(+), 219 deletions(-)
---
diff --git a/gegl/Makefile.am b/gegl/Makefile.am
index b049270be..5866faf82 100644
--- a/gegl/Makefile.am
+++ b/gegl/Makefile.am
@@ -63,6 +63,7 @@ GEGL_introspectable_headers = \
        buffer/gegl-buffer-iterator.h   \
        buffer/gegl-buffer-backend.h    \
        buffer/gegl-buffer-swap.h       \
+       buffer/gegl-memory.h            \
        buffer/gegl-rectangle.h         \
        buffer/gegl-tile-backend.h              \
        buffer/gegl-tile-handler.h              \
diff --git a/gegl/buffer/Makefile.am b/gegl/buffer/Makefile.am
index 106f72cb3..f2b21c644 100644
--- a/gegl/buffer/Makefile.am
+++ b/gegl/buffer/Makefile.am
@@ -46,6 +46,7 @@ libbuffer_la_SOURCES = \
     gegl-compression-nop.c     \
     gegl-compression-rle.c     \
     gegl-compression-zlib.c    \
+    gegl-memory.c              \
     gegl-sampler.c             \
     gegl-sampler-cubic.c       \
     gegl-sampler-linear.c      \
@@ -81,6 +82,8 @@ libbuffer_la_SOURCES = \
     gegl-compression-nop.h     \
     gegl-compression-rle.h     \
     gegl-compression-zlib.h    \
+    gegl-memory.h              \
+    gegl-memory-private.h      \
     gegl-sampler.h             \
     gegl-sampler-cubic.h       \
     gegl-sampler-linear.h      \
diff --git a/gegl/buffer/gegl-buffer.h b/gegl/buffer/gegl-buffer.h
index b477a3493..f962bc1a8 100644
--- a/gegl/buffer/gegl-buffer.h
+++ b/gegl/buffer/gegl-buffer.h
@@ -719,69 +719,6 @@ glong gegl_buffer_signal_connect (GeglBuffer *buffer,
                                   gpointer    data);
 
 
-
-/***
- * Aligned memory:
- *
- * GEGL provides functions to allocate and free buffers that are guaranteed to
- * be on 16 byte aligned memory addresses.
- */
-
-/**
- * gegl_malloc: (skip)
- * @n_bytes: the number of bytes to allocte.
- *
- * Allocates @n_bytes of memory. If n_bytes is 0 it returns NULL.
- *
- * Returns a pointer to the allocated memory.
- */
-gpointer gegl_malloc                  (gsize    n_bytes) G_GNUC_MALLOC;
-
-/**
- * gegl_free: (skip)
- * @mem: the memory to free.
- *
- * Frees the memory pointed to by @mem, if @mem is NULL it will warn and abort.
- */
-void     gegl_free                    (gpointer mem);
-
-/**
- * gegl_calloc: (skip)
- * @size: size of items to allocate
- * @n_memb: number of members
- *
- * allocated 0'd memory.
- */
-gpointer gegl_calloc (gsize size, int n_memb) G_GNUC_MALLOC;
-
-/**
- * gegl_memeq_zero: (skip)
- * @ptr: pointer to the memory block
- * @size: block size
- *
- * Checks if all the bytes of the memory block @ptr, of size @size,
- * are equal to zero.
- *
- * Returns: TRUE if all the bytes are equal to zero.
- */
-gboolean gegl_memeq_zero (gconstpointer ptr,
-                          gsize         size);
-
-/**
- * gegl_memset_pattern: (skip)
- * @dst_ptr: pointer to copy to
- * @src_ptr: pointer to copy from
- * @pattern_size: the length of @src_ptr
- * @count: number of copies
- *
- * Fill @dst_ptr with @count copies of the bytes in @src_ptr.
- */
-void gegl_memset_pattern              (void *       dst_ptr,
-                                              const void * src_ptr,
-                                              gint         pattern_size,
-                                              gint         count);
-
-
 /**
  * gegl_buffer_flush_ext:
  * @buffer: a GeglBuffer
@@ -796,8 +733,9 @@ void gegl_memset_pattern              (void *       dst_ptr,
 void
 gegl_buffer_flush_ext (GeglBuffer *buffer, const GeglRectangle *rect);
 
-#include <gegl-buffer-iterator.h>
-#include <gegl-rectangle.h>
+#include "gegl-buffer-iterator.h"
+#include "gegl-rectangle.h"
+#include "gegl-memory.h"
 
 
 GType gegl_buffer_get_type  (void) G_GNUC_CONST;
diff --git a/gegl/buffer/gegl-memory-private.h b/gegl/buffer/gegl-memory-private.h
new file mode 100644
index 000000000..9621e1e12
--- /dev/null
+++ b/gegl/buffer/gegl-memory-private.h
@@ -0,0 +1,24 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GEGL_MEMORY_PRIVATE_H__
+#define __GEGL_MEMORY_PRIVATE_H__
+
+
+#define GEGL_ALIGN 16
+
+
+#endif  /* __GEGL_MEMORY_PRIVATE_H__ */
diff --git a/gegl/buffer/gegl-memory.c b/gegl/buffer/gegl-memory.c
new file mode 100644
index 000000000..56679dc70
--- /dev/null
+++ b/gegl/buffer/gegl-memory.c
@@ -0,0 +1,184 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <string.h>
+
+#include <glib-object.h>
+
+#include "gegl-memory.h"
+#include "gegl-memory-private.h"
+
+
+G_STATIC_ASSERT (GEGL_ALIGN <= G_MAXUINT8);
+
+
+/*  public functions  */
+
+/* utility call that makes sure allocations are 16 byte aligned.
+ * making RGBA float buffers have aligned access for pixels.
+ */
+gpointer
+gegl_malloc (gsize size)
+{
+  gchar *mem;
+  gchar *ret;
+  gint   offset;
+
+  mem    = g_malloc (size + GEGL_ALIGN);
+  offset = GEGL_ALIGN - GPOINTER_TO_UINT(mem) % GEGL_ALIGN;
+  ret    = (gpointer)(mem + offset);
+
+  /* store the offset to the real malloc one byte in front of this malloc */
+  *(guint8*)(ret-1)=offset;
+  return (gpointer) ret;
+}
+
+gpointer
+gegl_calloc (gsize size,
+             gint  n_memb)
+{
+  gchar *ret = gegl_malloc (size * n_memb);
+  memset (ret, 0, size * n_memb);
+  return ret;
+}
+
+void
+gegl_free (gpointer buf)
+{
+  g_assert (buf);
+  g_free ((gchar*)buf - *((guint8*)buf -1));
+}
+
+gboolean
+gegl_memeq_zero (gconstpointer ptr,
+                 gsize         size)
+{
+  const guint8 *p = ptr;
+
+  if (size >= 1 && (guintptr) p & 0x1)
+    {
+      if (*(const guint8 *) p)
+        return FALSE;
+
+      p    += 1;
+      size -= 1;
+    }
+
+  if (size >= 2 && (guintptr) p & 0x2)
+    {
+      if (*(const guint16 *) p)
+        return FALSE;
+
+      p    += 2;
+      size -= 2;
+    }
+
+  if (size >= 4 && (guintptr) p & 0x4)
+    {
+      if (*(const guint32 *) p)
+        return FALSE;
+
+      p    += 4;
+      size -= 4;
+    }
+
+  while (size >= 8)
+    {
+      if (*(const guint64 *) p)
+        return FALSE;
+
+      p    += 8;
+      size -= 8;
+    }
+
+  if (size >= 4)
+    {
+      if (*(const guint32 *) p)
+        return FALSE;
+
+      p    += 4;
+      size -= 4;
+    }
+
+  if (size >= 2)
+    {
+      if (*(const guint16 *) p)
+        return FALSE;
+
+      p    += 2;
+      size -= 2;
+    }
+
+  if (size >= 1)
+    {
+      if (*(const guint8 *) p)
+        return FALSE;
+
+      p    += 1;
+      size -= 1;
+    }
+
+  return TRUE;
+}
+
+void
+gegl_memset_pattern (gpointer      restrict dst_ptr,
+                     gconstpointer restrict src_ptr,
+                     gint                   pattern_size,
+                     gint                   count)
+{
+  guchar       *dst = dst_ptr;
+  const guchar *src = src_ptr;
+
+  /* g_assert (pattern_size > 0 && count >= 0); */
+
+  if (pattern_size == 1 || count == 0)
+    {
+      memset (dst, *src, count);
+    }
+  else
+    {
+      gsize block_size;
+      gsize remaining_size;
+
+      block_size = pattern_size,
+
+      memcpy (dst, src, block_size);
+      src  = dst;
+      dst += block_size;
+
+      remaining_size = (count - 1) * block_size;
+
+      while (block_size < remaining_size)
+        {
+          memcpy (dst, src, block_size);
+          dst += block_size;
+
+          remaining_size -= block_size;
+
+          /* limit the block size, so that we don't saturate the cache.
+           *
+           * FIXME: optimal limit could use more benchmarking.
+           */
+          if (block_size <= 2048)
+            block_size *= 2;
+        }
+
+      memcpy (dst, src, remaining_size);
+    }
+}
diff --git a/gegl/buffer/gegl-memory.h b/gegl/buffer/gegl-memory.h
new file mode 100644
index 000000000..44d3774c8
--- /dev/null
+++ b/gegl/buffer/gegl-memory.h
@@ -0,0 +1,84 @@
+/* This file is part of GEGL.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GEGL_MEMORY_H__
+#define __GEGL_MEMORY_H__
+
+
+/***
+ * Aligned memory:
+ *
+ * GEGL provides functions to allocate and free buffers that are guaranteed to
+ * be on 16 byte aligned memory addresses.
+ */
+
+/**
+ * gegl_malloc: (skip)
+ * @n_bytes: the number of bytes to allocte.
+ *
+ * Allocates @n_bytes of memory. If n_bytes is 0 it returns NULL.
+ *
+ * Returns a pointer to the allocated memory.
+ */
+gpointer   gegl_malloc         (gsize         n_bytes) G_GNUC_MALLOC;
+
+/**
+ * gegl_free: (skip)
+ * @mem: the memory to free.
+ *
+ * Frees the memory pointed to by @mem, if @mem is NULL it will warn and abort.
+ */
+void       gegl_free           (gpointer      mem);
+
+/**
+ * gegl_calloc: (skip)
+ * @size: size of items to allocate
+ * @n_memb: number of members
+ *
+ * allocated 0'd memory.
+ */
+gpointer   gegl_calloc         (gsize         size,
+                                gint          n_memb) G_GNUC_MALLOC;
+
+/**
+ * gegl_memeq_zero: (skip)
+ * @ptr: pointer to the memory block
+ * @size: block size
+ *
+ * Checks if all the bytes of the memory block @ptr, of size @size,
+ * are equal to zero.
+ *
+ * Returns: TRUE if all the bytes are equal to zero.
+ */
+gboolean   gegl_memeq_zero     (gconstpointer ptr,
+                                gsize         size);
+
+/**
+ * gegl_memset_pattern: (skip)
+ * @dst_ptr: pointer to copy to
+ * @src_ptr: pointer to copy from
+ * @pattern_size: the length of @src_ptr
+ * @count: number of copies
+ *
+ * Fill @dst_ptr with @count copies of the bytes in @src_ptr.
+ */
+void       gegl_memset_pattern (gpointer      dst_ptr,
+                                gconstpointer src_ptr,
+                                gint          pattern_size,
+                                gint          count);
+
+
+#endif  /* __GEGL_MEMORY_H__ */
diff --git a/gegl/buffer/gegl-rectangle.c b/gegl/buffer/gegl-rectangle.c
index 4c8b31afb..d2f8f709c 100644
--- a/gegl/buffer/gegl-rectangle.c
+++ b/gegl/buffer/gegl-rectangle.c
@@ -269,160 +269,6 @@ gegl_rectangle_get_type (void)
   return our_type;
 }
 
-#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.
- */
-gpointer gegl_malloc (gsize size)
-{
-  gchar *mem;
-  gchar *ret;
-  gint   offset;
-
-  mem    = g_malloc (size + GEGL_ALIGN);
-  offset = GEGL_ALIGN - GPOINTER_TO_UINT(mem) % GEGL_ALIGN;
-  ret    = (gpointer)(mem + offset);
-
-  /* store the offset to the real malloc one byte in front of this malloc */
-  *(guint8*)(ret-1)=offset;
-  return (gpointer) ret;
-}
-
-gpointer gegl_calloc (gsize size, int n_memb)
-{
-  gchar *ret = gegl_malloc (size * n_memb);
-  memset (ret, 0, size * n_memb);
-  return ret;
-}
-
-void
-gegl_free (gpointer buf)
-{
-  g_assert (buf);
-  g_free ((gchar*)buf - *((guint8*)buf -1));
-}
-
-gboolean
-gegl_memeq_zero (gconstpointer ptr,
-                 gsize         size)
-{
-  const guint8 *p = ptr;
-
-  if (size >= 1 && (guintptr) p & 0x1)
-    {
-      if (*(const guint8 *) p)
-        return FALSE;
-
-      p    += 1;
-      size -= 1;
-    }
-
-  if (size >= 2 && (guintptr) p & 0x2)
-    {
-      if (*(const guint16 *) p)
-        return FALSE;
-
-      p    += 2;
-      size -= 2;
-    }
-
-  if (size >= 4 && (guintptr) p & 0x4)
-    {
-      if (*(const guint32 *) p)
-        return FALSE;
-
-      p    += 4;
-      size -= 4;
-    }
-
-  while (size >= 8)
-    {
-      if (*(const guint64 *) p)
-        return FALSE;
-
-      p    += 8;
-      size -= 8;
-    }
-
-  if (size >= 4)
-    {
-      if (*(const guint32 *) p)
-        return FALSE;
-
-      p    += 4;
-      size -= 4;
-    }
-
-  if (size >= 2)
-    {
-      if (*(const guint16 *) p)
-        return FALSE;
-
-      p    += 2;
-      size -= 2;
-    }
-
-  if (size >= 1)
-    {
-      if (*(const guint8 *) p)
-        return FALSE;
-
-      p    += 1;
-      size -= 1;
-    }
-
-  return TRUE;
-}
-
-void
-gegl_memset_pattern (void * restrict       dst_ptr,
-                            const void * restrict src_ptr,
-                            gint                  pattern_size,
-                            gint                  count)
-{
-  guchar       *dst = dst_ptr;
-  const guchar *src = src_ptr;
-
-  /* g_assert (pattern_size > 0 && count >= 0); */
-
-  if (pattern_size == 1 || count == 0)
-    {
-      memset (dst, *src, count);
-    }
-  else
-    {
-      gsize block_size;
-      gsize remaining_size;
-
-      block_size = pattern_size,
-
-      memcpy (dst, src, block_size);
-      src  = dst;
-      dst += block_size;
-
-      remaining_size = (count - 1) * block_size;
-
-      while (block_size < remaining_size)
-        {
-          memcpy (dst, src, block_size);
-          dst += block_size;
-
-          remaining_size -= block_size;
-
-          /* limit the block size, so that we don't saturate the cache.
-           * 
-           * FIXME: optimal limit could use more benchmarking.
-           */
-          if (block_size <= 2048)
-            block_size *= 2;
-        }
-
-      memcpy (dst, src, remaining_size);
-    }
-}
-
 gint
 _gegl_float_epsilon_zero (float value)
 {


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