[gtksourceview] utils: add helpers for aligned allocations



commit 154c78aee8a087cc57bb60e59220dcc9f7521447
Author: Christian Hergert <chergert redhat com>
Date:   Tue Dec 14 15:56:01 2021 -0800

    utils: add helpers for aligned allocations

 gtksourceview/gtksourceutils-private.h |   9 +++
 gtksourceview/gtksourceutils.c         | 138 ++++++++++++++++++++++++++++++++-
 meson.build                            |  16 ++++
 3 files changed, 160 insertions(+), 3 deletions(-)
---
diff --git a/gtksourceview/gtksourceutils-private.h b/gtksourceview/gtksourceutils-private.h
index 3658513b..671797a0 100644
--- a/gtksourceview/gtksourceutils-private.h
+++ b/gtksourceview/gtksourceutils-private.h
@@ -53,4 +53,13 @@ void     _gtk_source_view_jump_to_iter                   (GtkTextView
                                                           double                       xalign,
                                                           double                       yalign);
 
+G_GNUC_INTERNAL
+char    *_gtk_source_utils_aligned_alloc                 (gsize                        size,
+                                                          gsize                        number,
+                                                          gsize                        alignment);
+G_GNUC_INTERNAL
+void     _gtk_source_utils_aligned_free                  (gpointer                     data);
+G_GNUC_INTERNAL
+gsize    _gtk_source_utils_get_page_size                 (void) G_GNUC_CONST;
+
 G_END_DECLS
diff --git a/gtksourceview/gtksourceutils.c b/gtksourceview/gtksourceutils.c
index 6c617cc4..ecac43bc 100644
--- a/gtksourceview/gtksourceutils.c
+++ b/gtksourceview/gtksourceutils.c
@@ -30,13 +30,35 @@
 
 #include "config.h"
 
-#include "gtksourceutils.h"
-#include "gtksourceutils-private.h"
-#include <string.h>
+#if defined(HAVE_POSIX_MEMALIGN) && !defined(_XOPEN_SOURCE)
+# define _XOPEN_SOURCE 600
+#endif
+
+#if defined(HAVE_MEMALIGN) || defined(HAVE__ALIGNED_MALLOC)
+/* Required for _aligned_malloc() and _aligned_free() on Windows */
+#include <malloc.h>
+#endif
+
+#ifdef HAVE__ALIGNED_MALLOC
+/* _aligned_malloc() takes parameters of aligned_malloc() in reverse order */
+# define aligned_alloc(alignment, size) _aligned_malloc (size, alignment)
+
+/* _aligned_malloc()'ed memory must be freed by _align_free() on MSVC */
+# define aligned_free(x) _aligned_free (x)
+#else
+# define aligned_free(x) free (x)
+#endif
+
 #include <errno.h>
 #include <math.h>
+#include <string.h>
+
+#include <glib.h>
 #include <glib/gi18n-lib.h>
 
+#include "gtksourceutils.h"
+#include "gtksourceutils-private.h"
+
 /**
  * gtk_source_utils_unescape_search_text:
  * @text: the text to unescape.
@@ -778,3 +800,113 @@ _gtk_source_view_jump_to_iter (GtkTextView       *text_view,
   gtk_adjustment_set_value (hadj, xvalue);
   gtk_adjustment_set_value (vadj, yvalue + top_margin);
 }
+
+gsize
+_gtk_source_utils_get_page_size (void)
+{
+       static gsize page_size;
+
+       if (page_size == 0)
+       {
+#ifdef G_OS_WIN32
+               SYSTEM_INFO si;
+               GetSystemInfo (&si);
+               page_size = si.dwPageSize;
+
+#else
+               page_size = sysconf (_SC_PAGESIZE);
+#endif
+       }
+
+       return page_size;
+}
+
+/* _gtk_source_utils_aligned_alloc() and _gtk_source_utils_aligned_free() are
+ * based upon graphene_aligned_alloc() and graphene_aligned_free() whose
+ * original license is provided below.
+ *
+ * Copyright 2014  Emmanuele Bassi
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+char *
+_gtk_source_utils_aligned_alloc (gsize size,
+                                 gsize number,
+                                 gsize alignment)
+{
+       void *res = NULL;
+       size_t max_size = (size_t) -1;
+       size_t real_size;
+
+       if (size == 0 || number == 0)
+               return NULL;
+
+       if (size > 0 && number > max_size / size)
+       {
+               g_error ("Overflow in the allocation of (%lu x %lu) bytes",
+                        (unsigned long) size,
+                        (unsigned long) number);
+       }
+
+       real_size = size * number;
+
+       errno = 0;
+
+#if defined(HAVE_POSIX_MEMALIGN)
+       errno = posix_memalign (&res, alignment, real_size);
+#elif defined(HAVE_ALIGNED_ALLOC) || defined(HAVE__ALIGNED_MALLOC)
+       /* real_size must be a multiple of alignment */
+       if (real_size % alignment != 0)
+       {
+               size_t offset = real_size % alignment;
+               real_size += (alignment - offset);
+       }
+
+       res = aligned_alloc (alignment, real_size);
+#elif defined(HAVE_MEMALIGN)
+       res = memalign (alignment, real_size);
+#else
+       res = malloc (real_size);
+#endif
+
+       if (errno != 0 || res == NULL)
+       {
+               g_error ("Allocation error: %s", strerror (errno));
+       }
+
+#ifndef G_DISABLE_ASSERT
+       {
+               static gsize page_size;
+
+               if (page_size == 0)
+                       page_size = _gtk_source_utils_get_page_size ();
+
+               g_assert_cmpint (GPOINTER_TO_SIZE (res) % page_size, ==, 0);
+       }
+#endif
+
+       return res;
+}
+
+void
+_gtk_source_utils_aligned_free (gpointer data)
+{
+       aligned_free (data);
+}
diff --git a/meson.build b/meson.build
index 24d3d021..795a3626 100644
--- a/meson.build
+++ b/meson.build
@@ -152,6 +152,22 @@ foreach header: check_headers
   endif
 endforeach
 
+# Functions
+if cc.has_function('memalign', prefix: '#include <stdlib.h>\n#include <malloc.h>')
+  config_h.set10('HAVE_MEMALIGN', 1, description: 'Define if memalign() is available')
+elif cc.has_function('_aligned_malloc', prefix: '#include <malloc.h>')
+  config_h.set10('HAVE__ALIGNED_MALLOC', 1, description: 'Define if _aligned_malloc() is available')
+# Don't probe the ones below on Windows because when building with
+# MinGW-w64 on MSYS2, Meson<0.37.0 incorrectly detects those below as
+# being available even though they're not.
+elif cc.has_function('aligned_alloc', prefix: '#include <stdlib.h>') and not (host_system == 'windows')
+  config_h.set10('HAVE_ALIGNED_ALLOC', 1, description: 'Define if aligned_malloc() is available')
+elif cc.has_function('posix_memalign', prefix: '#include <stdlib.h>') and not (host_system == 'windows')
+  config_h.set10('HAVE_POSIX_MEMALIGN', 1, description: 'Define if posix_memalign() is available')
+else
+  error('No aligned malloc function could be found.')
+endif
+
 # libsysprof-capture support
 if get_option('sysprof')
   libsysprof_capture_dep = dependency('sysprof-capture-4',


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