Disabling memory chunks and free list in glib



Here is a preliminary patch that adds a configure option that disables all
memory chunks and free lists in glib.

I don't know if this is the correct way to to this, as by disabling the
memchunk code the application developer cannot use mem chunks either. On
the other hand it is a lot cleaner that going through all GMemChunk users
in glib and gtk+.

Comments?

/ Alex

Index: configure.in
===================================================================
RCS file: /cvs/gnome/glib/configure.in,v
retrieving revision 1.162
diff -u -p -r1.162 configure.in
--- configure.in	2000/12/12 07:31:56	1.162
+++ configure.in	2000/12/18 16:00:28
@@ -105,6 +105,7 @@ AC_ARG_ENABLE(msg-prefix, [  --enable-ms
 AC_ARG_ENABLE(mem_check, [  --enable-mem-check      turn on malloc/free sanity checking [default=no]],,enable_mem_check=no)
 AC_ARG_ENABLE(mem_profile, [  --enable-mem-profile    turn on malloc profiling atexit [default=no]],,enable_mem_profile=no)
 AC_ARG_ENABLE(gc_friendly, [  --enable-gc-friendly    turn on garbage collector friendliness [default=no]],,enable_gc_friendly=no)
+AC_ARG_ENABLE(mem_pools, [  --disable-mem-pools     disable all glib internal memory pools [default=no]],,disable_mem_pools=no)
 AC_ARG_ENABLE(ansi, [  --enable-ansi           turn on strict ansi [default=no]],
 		    , enable_ansi=no)
 AC_ARG_ENABLE(threads, [  --enable-threads        turn on basic thread support [default=yes]
@@ -134,13 +135,23 @@ else
 fi

 AC_MSG_CHECKING(whether to enable garbage collector friendliness)
-if test "x$enable_gc_friendly" = "xyes"; then
-  AC_DEFINE(ENABLE_GC_FRIENDLY, 1, [Whether to enable GC friendliness])
-  AC_SUBST(ENABLE_GC_FRIENDLY)
+if test "disable_mem_pools" = "xyes"; then
+  AC_DEFINE(DISABLE_MEM_POOLS, 1, [Whether to disable memory pools])
+  AC_SUBST(DISABLE_MEM_POOLS)
   AC_MSG_RESULT(yes)
 else
   AC_MSG_RESULT(no)
 fi
+
+AC_MSG_CHECKING(whether to disable memory pools)
+if test "x$enable_mem_profile" = "xyes"; then
+  AC_DEFINE(ENABLE_MEM_PROFILE, 1, [Whether to enable memory profiling])
+  AC_SUBST(ENABLE_MEM_PROFILE)
+  AC_MSG_RESULT(yes)
+else
+  AC_MSG_RESULT(no)
+fi
+

 if test "x$enable_debug" = "xyes"; then
   test "$cflags_set" = set || CFLAGS="$CFLAGS -g"
Index: glist.c
===================================================================
RCS file: /cvs/gnome/glib/glist.c,v
retrieving revision 1.18
diff -u -p -r1.18 glist.c
--- glist.c	2000/11/20 23:59:32	1.18
+++ glist.c	2000/12/18 16:00:28
@@ -31,6 +31,7 @@
 #include "glib.h"


+#ifndef DISABLE_MEM_POOLS
 struct _GAllocator /* from gmem.c */
 {
   gchar         *name;
@@ -196,6 +197,42 @@ g_list_free_1 (GList *list)
 {
   _g_list_free_1 (list);
 }
+
+#else /* DISABLE_MEM_POOLS */
+
+GList*
+g_list_alloc (void)
+{
+  GList *list;
+
+  list = g_new (GList, 1);
+
+  list->data = NULL;
+  list->next = NULL;
+  list->prev = NULL;
+
+  return list;
+}
+
+void
+g_list_free (GList *list)
+{
+  GList *last;
+  while (list)
+    {
+      last = list;
+      list = list->next;
+      g_free (last);
+    }
+}
+
+void
+g_list_free_1 (GList *list)
+{
+  g_free (list);
+}
+
+#endif

 GList*
 g_list_append (GList	*list,
Index: gmem.c
===================================================================
RCS file: /cvs/gnome/glib/gmem.c,v
retrieving revision 1.23
diff -u -p -r1.23 gmem.c
--- gmem.c	2000/09/25 21:28:14	1.23
+++ gmem.c	2000/12/18 16:00:28
@@ -132,10 +132,12 @@ static gint   g_mem_chunk_area_search  (
 					gchar    *addr);


+#ifndef DISABLE_MEM_POOLS
 /* here we can't use StaticMutexes, as they depend upon a working
  * g_malloc, the same holds true for StaticPrivate */
 static GMutex* mem_chunks_lock = NULL;
 static GRealMemChunk *mem_chunks = NULL;
+#endif

 #ifdef ENABLE_MEM_PROFILE
 static GMutex* mem_profile_lock;
@@ -460,6 +462,7 @@ g_mem_check (gpointer mem)
 #endif /* ENABLE_MEM_CHECK */
 }

+#ifndef DISABLE_MEM_POOLS
 GMemChunk*
 g_mem_chunk_new (gchar  *name,
 		 gint    atom_size,
@@ -960,6 +963,84 @@ g_mem_chunk_area_search (GMemArea *a,
     }
   return -1;
 }
+#else /* DISABLE_MEM_POOLS */
+
+typedef struct
+{
+  guint alloc_size;           /* the size of an atom */
+}  GMinimalMemChunk;
+
+GMemChunk*
+g_mem_chunk_new (gchar  *name,
+		 gint    atom_size,
+		 gulong  area_size,
+		 gint    type)
+{
+  GMinimalMemChunk *mem_chunk;
+
+  g_return_val_if_fail (atom_size > 0, NULL);
+
+  mem_chunk = g_new (struct _GMinimalMemChunk, 1);
+  mem_chunk->alloc_size = atom_size;
+
+  return ((GMemChunk*) mem_chunk);
+}
+
+void
+g_mem_chunk_destroy (GMemChunk *mem_chunk)
+{
+  g_free (mem_chunk);
+}
+
+gpointer
+g_mem_chunk_alloc (GMemChunk *mem_chunk)
+{
+  GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
+
+  return g_malloc (minimal->alloc_size);
+}
+
+gpointer
+g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
+{
+  GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
+
+  return g_malloc0 (minimal->alloc_size);
+}
+
+void
+g_mem_chunk_free (GMemChunk *mem_chunk,
+		  gpointer   mem)
+{
+  g_free (mem);
+}
+
+void
+g_mem_chunk_clean (GMemChunk *mem_chunk)
+{
+}
+
+void
+g_mem_chunk_reset (GMemChunk *mem_chunk)
+{
+}
+
+void
+g_mem_chunk_print (GMemChunk *mem_chunk)
+{
+}
+
+void
+g_mem_chunk_info (void)
+{
+}
+
+void
+g_blow_chunks (void)
+{
+}
+
+#endif /* DISABLE_MEM_POOLS */

 /* generic allocators
  */
Index: gnode.c
===================================================================
RCS file: /cvs/gnome/glib/gnode.c,v
retrieving revision 1.15
diff -u -p -r1.15 gnode.c
--- gnode.c	2000/09/29 23:12:58	1.15
+++ gnode.c	2000/12/18 16:00:28
@@ -33,6 +33,7 @@

 #include "glib.h"

+#ifndef DISABLE_MEM_POOLS
 /* node allocation
  */
 struct _GAllocator /* from gmem.c */
@@ -167,6 +168,30 @@ g_nodes_free (GNode *node)
   current_allocator->free_nodes = node;
   G_UNLOCK (current_allocator);
 }
+#else /* DISABLE_MEM_POOLS */
+
+GNode*
+g_node_new (gpointer data)
+{
+  GNode *node;
+
+  node = g_new (GNode, 1);
+
+  node->data = data;
+  node->next = NULL;
+  node->prev = NULL;
+  node->parent = NULL;
+  node->children = NULL;
+
+  return node;
+}
+
+static void
+g_nodes_free (GNode *node)
+{
+  g_free (node);
+}
+#endif

 void
 g_node_destroy (GNode *root)
Index: gslist.c
===================================================================
RCS file: /cvs/gnome/glib/gslist.c,v
retrieving revision 1.18
diff -u -p -r1.18 gslist.c
--- gslist.c	2000/11/20 23:59:32	1.18
+++ gslist.c	2000/12/18 16:00:28
@@ -31,6 +31,7 @@
 #include "glib.h"


+#ifndef DISABLE_MEM_POOLS
 struct _GAllocator /* from gmem.c */
 {
   gchar         *name;
@@ -189,6 +190,40 @@ g_slist_free_1 (GSList *list)
 {
   _g_slist_free_1 (list);
 }
+#else /* DISABLE_MEM_POOLS */
+
+GSList*
+g_slist_alloc (void)
+{
+  GSList *list;
+
+  list = g_new (GSList, 1);
+
+  list->data = NULL;
+  list->next = NULL;
+
+  return list;
+}
+
+void
+g_slist_free (GSList *list)
+{
+  GSList *last;
+  while (list)
+    {
+      last = list;
+      list = list->next;
+      g_free (last);
+    }
+}
+
+void
+g_slist_free_1 (GSList *list)
+{
+  g_free (list);
+}
+
+#endif

 GSList*
 g_slist_append (GSList   *list,





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