[libdazzle] counters: try to be more flexible for win32



commit b314be8b117f49680966d19a8831fbea7c3fd672
Author: Christian Hergert <chergert redhat com>
Date:   Sun Sep 3 14:33:21 2017 -0700

    counters: try to be more flexible for win32
    
    This uses _aligned_malloc() as the fallback for win32.

 src/util/dzl-counter.c |   35 ++++++++++++++++++++++++++++++-----
 1 files changed, 30 insertions(+), 5 deletions(-)
---
diff --git a/src/util/dzl-counter.c b/src/util/dzl-counter.c
index c923196..db559ff 100644
--- a/src/util/dzl-counter.c
+++ b/src/util/dzl-counter.c
@@ -26,7 +26,6 @@
 
 #include <glib/gprintf.h>
 #include <gmodule.h>
-#include <sys/mman.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <fcntl.h>
@@ -35,6 +34,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#ifdef G_OS_UNIX
+# include <sys/mman.h>
+#endif
+
 #include "dzl-counter.h"
 
 G_DEFINE_BOXED_TYPE (DzlCounterArena, dzl_counter_arena, dzl_counter_arena_ref, dzl_counter_arena_unref)
@@ -156,13 +159,15 @@ _dzl_counter_arena_atexit (void)
 static void
 _dzl_counter_arena_init_local (DzlCounterArena *arena)
 {
+  gsize size;
+  gint page_size;
   ShmHeader *header;
+#ifndef G_OS_WIN32
   gpointer mem;
   unsigned pid;
-  gsize size;
-  gint page_size;
   gint fd;
   gchar name [32];
+#endif
 
   page_size = sysconf (_SC_PAGE_SIZE);
 
@@ -174,6 +179,8 @@ _dzl_counter_arena_init_local (DzlCounterArena *arena)
       goto use_malloc;
     }
 
+#ifndef G_OS_WIN32
+
   /*
    * FIXME: https://bugzilla.gnome.org/show_bug.cgi?id=749280
    *
@@ -233,26 +240,39 @@ _dzl_counter_arena_init_local (DzlCounterArena *arena)
 failure:
   shm_unlink (name);
   close (fd);
+#endif
 
 use_malloc:
   g_warning ("Failed to allocate shared memory for counters. "
              "Counters will not be available to external processes.");
 
+  /*
+   * Ask for double memory than required so that we can be certain
+   * that the memalign will keep us within valid memory ranges.
+   */
+  if (size < page_size)
+    size = page_size;
   arena->data_is_mmapped = FALSE;
-  arena->cells = g_malloc0 (size << 1);
   arena->n_cells = (size / DATA_CELL_SIZE);
   arena->data_length = size;
+#ifdef G_OS_WIN32
+  arena->cells = _aligned_malloc (size, page_size);
+#else
+  arena->cells = g_malloc0 (size << 1);
+#endif
 
+#ifndef G_OS_WIN32
   /*
    * Make sure that we have a properly aligned allocation back from
    * malloc. Since we are at least a page size, we should pretty much
    * be guaranteed this, but better to check with posix_memalign().
    */
-  if (posix_memalign ((void *)&arena->cells, page_size, size << 1) != 0)
+  if (posix_memalign ((gpointer)&arena->cells, page_size, size << 1) != 0)
     {
       perror ("posix_memalign()");
       abort ();
     }
+#endif
 
   header = (void *)arena->cells;
   header->magic = MAGIC;
@@ -392,7 +412,12 @@ _dzl_counter_arena_destroy (DzlCounterArena *arena)
   if (arena->data_is_mmapped)
     munmap (arena->cells, arena->data_length);
   else
+#ifdef G_OS_WIN32
+    /* Allocated with _aligned_malloc() */
+    _aligned_free (arena->cells);
+#else
     g_free (arena->cells);
+#endif
 
   g_clear_pointer (&arena->counters, g_list_free);
 


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