[glib/ebassi/object-allocator] gtype: Use the system allocator on UNIX




commit dd1b9fb3e581fad687c50fc69c7cca2bc1abc8fa
Author: Emmanuele Bassi <ebassi gnome org>
Date:   Wed Jul 6 20:43:45 2022 +0100

    gtype: Use the system allocator on UNIX
    
    Instead of replacing the slice allocator wholesale, we can start phasing
    it out by having GTypeInstance use the system allocator on operating
    systems where we know we have access to a decent allocator.

 gobject/gtype.c | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/gobject/gtype.c b/gobject/gtype.c
index 177e446918..682781bc70 100644
--- a/gobject/gtype.c
+++ b/gobject/gtype.c
@@ -1824,6 +1824,18 @@ type_iface_blow_holder_info_Wm (TypeNode *iface,
     }
 }
 
+/* We use the system allocator on UNIX-y systems, where we know we have
+ * access to a decent allocator. On other systems, we fall back to the
+ * slice allocator, as we know its performance profile
+ */
+#ifdef G_OS_UNIX
+# define instance_alloc(s)  g_malloc0((s))
+# define instance_free(s,p) g_free((p))
+#else
+# define instance_alloc(s)  g_slice_alloc0((s))
+# define instance_free(s,p) g_slice_free1((s),(p))
+#endif
+
 /**
  * g_type_create_instance: (skip)
  * @type: an instantiatable type to create an instance for
@@ -1900,7 +1912,7 @@ g_type_create_instance (GType type)
       private_size += ALIGN_STRUCT (1);
 
       /* Allocate one extra pointer size... */
-      allocated = g_slice_alloc0 (private_size + ivar_size + sizeof (gpointer));
+      allocated = instance_alloc (private_size + ivar_size + sizeof (gpointer));
       /* ... and point it back to the start of the private data. */
       *(gpointer *) (allocated + private_size + ivar_size) = allocated + ALIGN_STRUCT (1);
 
@@ -1910,7 +1922,7 @@ g_type_create_instance (GType type)
     }
   else
 #endif
-    allocated = g_slice_alloc0 (private_size + ivar_size);
+    allocated = instance_alloc (private_size + ivar_size);
 
   instance = (GTypeInstance *) (allocated + private_size);
 
@@ -2000,14 +2012,14 @@ g_type_free_instance (GTypeInstance *instance)
       /* Clear out the extra pointer... */
       *(gpointer *) (allocated + private_size + ivar_size) = NULL;
       /* ... and ensure we include it in the size we free. */
-      g_slice_free1 (private_size + ivar_size + sizeof (gpointer), allocated);
+      instance_free (private_size + ivar_size + sizeof (gpointer), allocated);
 
       VALGRIND_FREELIKE_BLOCK (allocated + ALIGN_STRUCT (1), 0);
       VALGRIND_FREELIKE_BLOCK (instance, 0);
     }
   else
 #endif
-    g_slice_free1 (private_size + ivar_size, allocated);
+    instance_free (private_size + ivar_size, allocated);
 
 #ifdef G_ENABLE_DEBUG
   IF_DEBUG (INSTANCE_COUNT)


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