+Wed Jan 25 16:39:18 2006 Tim Janik <timj@imendio.com>
+
+ * glib/gslice.c: honour g_mem_gc_friendly settings when freeing
+ slices, make sure g_mem_gc_friendly is properly initialized.
+
+ * gmem.[hc]: ensure g_mem_gc_friendly is initialized from G_DEBUG upon
+ the first allocation. applied some branching optimizations.
+
+ * docs/macros.txt: reflected --enable-gc-friendly change and
+ described ENABLE_GC_FRIENDLY_DEFAULT as well as G_DEBUG=gc-friendly.
+
+ * configure.in: changed --enable-gc-friendly=yes to define
+ ENABLE_GC_FRIENDLY_DEFAULT.
+
+ * glib/garray.c: changed ENABLE_GC_FRIENDLY macro #ifdef-s to
+ if (G_UNLIKELY (g_mem_gc_friendly)).
+
+ * glib/gtree.c:
+ * glib/ghash.c: removed ENABLE_GC_FRIENDLY code which is now taken
+ care of by g_slice_free1().
+
+ * tests/slice-test.c: fixed leaks, reported by Kjartan Maraas.
+
Tue Jan 24 17:49:36 2006 Tim Janik <timj@imendio.com>
* glib/gslice.c: only use posix_memalign() if it's known to work,
+Wed Jan 25 16:39:18 2006 Tim Janik <timj@imendio.com>
+
+ * glib/gslice.c: honour g_mem_gc_friendly settings when freeing
+ slices, make sure g_mem_gc_friendly is properly initialized.
+
+ * gmem.[hc]: ensure g_mem_gc_friendly is initialized from G_DEBUG upon
+ the first allocation. applied some branching optimizations.
+
+ * docs/macros.txt: reflected --enable-gc-friendly change and
+ described ENABLE_GC_FRIENDLY_DEFAULT as well as G_DEBUG=gc-friendly.
+
+ * configure.in: changed --enable-gc-friendly=yes to define
+ ENABLE_GC_FRIENDLY_DEFAULT.
+
+ * glib/garray.c: changed ENABLE_GC_FRIENDLY macro #ifdef-s to
+ if (G_UNLIKELY (g_mem_gc_friendly)).
+
+ * glib/gtree.c:
+ * glib/ghash.c: removed ENABLE_GC_FRIENDLY code which is now taken
+ care of by g_slice_free1().
+
+ * tests/slice-test.c: fixed leaks, reported by Kjartan Maraas.
+
Tue Jan 24 17:49:36 2006 Tim Janik <timj@imendio.com>
* glib/gslice.c: only use posix_memalign() if it's known to work,
+Wed Jan 25 16:39:18 2006 Tim Janik <timj@imendio.com>
+
+ * glib/gslice.c: honour g_mem_gc_friendly settings when freeing
+ slices, make sure g_mem_gc_friendly is properly initialized.
+
+ * gmem.[hc]: ensure g_mem_gc_friendly is initialized from G_DEBUG upon
+ the first allocation. applied some branching optimizations.
+
+ * docs/macros.txt: reflected --enable-gc-friendly change and
+ described ENABLE_GC_FRIENDLY_DEFAULT as well as G_DEBUG=gc-friendly.
+
+ * configure.in: changed --enable-gc-friendly=yes to define
+ ENABLE_GC_FRIENDLY_DEFAULT.
+
+ * glib/garray.c: changed ENABLE_GC_FRIENDLY macro #ifdef-s to
+ if (G_UNLIKELY (g_mem_gc_friendly)).
+
+ * glib/gtree.c:
+ * glib/ghash.c: removed ENABLE_GC_FRIENDLY code which is now taken
+ care of by g_slice_free1().
+
+ * tests/slice-test.c: fixed leaks, reported by Kjartan Maraas.
+
Tue Jan 24 17:49:36 2006 Tim Janik <timj@imendio.com>
* glib/gslice.c: only use posix_memalign() if it's known to work,
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)
+ AC_DEFINE(ENABLE_GC_FRIENDLY_DEFAULT, 1, [Whether to enable GC friendliness by default])
AC_MSG_RESULT([yes])
else
AC_MSG_RESULT([no])
--enable-debug=yes [default for development branches]
-DG_ENABLE_DEBUG -g
--enable-gc-friendly=yes
- #define ENABLE_GC_FRIENDLY 1
+ #define ENABLE_GC_FRIENDLY_DEFAULT 1
--disable-mem-pools=yes
#define DISABLE_MEM_POOLS 1
==========================================
-ENABLE_GC_FRIENDLY
+ENABLE_GC_FRIENDLY_DEFAULT
Newly allocated memory that isn't directly initialized, as well
as memory being freed should be reset to 0. The point here is to
allow memory checkers and similar programs that use bohem GC alike
algorithms to produce more accurate results.
+ This can also be accomplished by setting the environment variable
+ G_DEBUG=gc-friendly.
DISABLE_MEM_POOLS
Many small chunks of memory are often allocated via collective pools
in GLib and are cached after release to speed up reallocations.
if (array->clear)
g_array_elt_zero (array, array->len, length - array->len);
}
-#ifdef ENABLE_GC_FRIENDLY
- else if (length < array->len)
+ else if (G_UNLIKELY (g_mem_gc_friendly) && length < array->len)
g_array_elt_zero (array, length, array->len - length);
-#endif /* ENABLE_GC_FRIENDLY */
array->len = length;
array->len -= 1;
-#ifdef ENABLE_GC_FRIENDLY
- g_array_elt_zero (array, array->len, 1);
-#else /* !ENABLE_GC_FRIENDLY */
- g_array_zero_terminate (array);
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ g_array_elt_zero (array, array->len, 1);
+ else
+ g_array_zero_terminate (array);
return farray;
}
array->len -= 1;
-#ifdef ENABLE_GC_FRIENDLY
- g_array_elt_zero (array, array->len, 1);
-#else /* !ENABLE_GC_FRIENDLY */
- g_array_zero_terminate (array);
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ g_array_elt_zero (array, array->len, 1);
+ else
+ g_array_zero_terminate (array);
return farray;
}
(array->len - (index_ + length)) * array->elt_size);
array->len -= length;
-#ifdef ENABLE_GC_FRIENDLY
- g_array_elt_zero (array, array->len, length);
-#else /* !ENABLE_GC_FRIENDLY */
- g_array_zero_terminate (array);
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ g_array_elt_zero (array, array->len, length);
+ else
+ g_array_zero_terminate (array);
return farray;
}
array->data = g_realloc (array->data, want_alloc);
-#ifdef ENABLE_GC_FRIENDLY
- memset (array->data + array->alloc, 0, want_alloc - array->alloc);
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ memset (array->data + array->alloc, 0, want_alloc - array->alloc);
array->alloc = want_alloc;
}
{
if ((array->len + len) > array->alloc)
{
-#ifdef ENABLE_GC_FRIENDLY
guint old_alloc = array->alloc;
-#endif /* ENABLE_GC_FRIENDLY */
array->alloc = g_nearest_pow (array->len + len);
array->alloc = MAX (array->alloc, MIN_ARRAY_SIZE);
array->pdata = g_realloc (array->pdata, sizeof (gpointer) * array->alloc);
-#ifdef ENABLE_GC_FRIENDLY
- for ( ; old_alloc < array->alloc; old_alloc++)
- array->pdata [old_alloc] = NULL;
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ for ( ; old_alloc < array->alloc; old_alloc++)
+ array->pdata [old_alloc] = NULL;
}
}
for (i = array->len; i < length; i++)
array->pdata[i] = NULL;
}
-#ifdef ENABLE_GC_FRIENDLY
- else if (length < array->len)
+ if (G_UNLIKELY (g_mem_gc_friendly) && length < array->len)
{
int i;
for (i = length; i < array->len; i++)
array->pdata[i] = NULL;
}
-#endif /* ENABLE_GC_FRIENDLY */
array->len = length;
}
array->len -= 1;
-#ifdef ENABLE_GC_FRIENDLY
- array->pdata[array->len] = NULL;
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ array->pdata[array->len] = NULL;
return result;
}
array->len -= 1;
-#ifdef ENABLE_GC_FRIENDLY
- array->pdata[array->len] = NULL;
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ array->pdata[array->len] = NULL;
return result;
}
(array->len - (index_ + length)) * sizeof (gpointer));
array->len -= length;
-#ifdef ENABLE_GC_FRIENDLY
- {
- guint i;
- for (i = 0; i < length; i++)
- array->pdata[array->len + i] = NULL;
- }
-#endif /* ENABLE_GC_FRIENDLY */
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ {
+ guint i;
+ for (i = 0; i < length; i++)
+ array->pdata[array->len + i] = NULL;
+ }
}
gboolean
key_destroy_func (hash_node->key);
if (value_destroy_func)
value_destroy_func (hash_node->value);
-
-#ifdef ENABLE_GC_FRIENDLY
- hash_node->key = NULL;
- hash_node->value = NULL;
-#endif /* ENABLE_GC_FRIENDLY */
-
g_slice_free (GHashNode, hash_node);
}
#include "gthreadinit.h"
#include "galias.h"
+#define MEM_PROFILE_TABLE_SIZE 4096
+
+
/* notes on macros:
* having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
* g_mem_profile().
* REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
* SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
* match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
- * if ENABLE_GC_FRIENDLY is defined, freed memory should be 0-wiped.
+ * g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
*/
-#define MEM_PROFILE_TABLE_SIZE 4096
+/* --- prototypes --- */
+static gboolean g_mem_initialized = FALSE;
+static void g_mem_init_nomessage (void);
+
+/* --- malloc wrappers --- */
#ifndef REALLOC_0_WORKS
static gpointer
standard_realloc (gpointer mem,
gpointer
g_malloc (gulong n_bytes)
{
- if (n_bytes)
+ if (G_UNLIKELY (!g_mem_initialized))
+ g_mem_init_nomessage();
+ if (G_LIKELY (n_bytes))
{
gpointer mem;
gpointer
g_malloc0 (gulong n_bytes)
{
- if (n_bytes)
+ if (G_UNLIKELY (!g_mem_initialized))
+ g_mem_init_nomessage();
+ if (G_LIKELY (n_bytes))
{
gpointer mem;
g_realloc (gpointer mem,
gulong n_bytes)
{
- if (n_bytes)
+ if (G_UNLIKELY (!g_mem_initialized))
+ g_mem_init_nomessage();
+ if (G_LIKELY (n_bytes))
{
mem = glib_mem_vtable.realloc (mem, n_bytes);
if (mem)
void
g_free (gpointer mem)
{
- if (mem)
+ if (G_UNLIKELY (!g_mem_initialized))
+ g_mem_init_nomessage();
+ if (G_LIKELY (mem))
glib_mem_vtable.free (mem);
}
gpointer
g_try_malloc (gulong n_bytes)
{
- if (n_bytes)
+ if (G_UNLIKELY (!g_mem_initialized))
+ g_mem_init_nomessage();
+ if (G_LIKELY (n_bytes))
return glib_mem_vtable.try_malloc (n_bytes);
else
return NULL;
g_try_realloc (gpointer mem,
gulong n_bytes)
{
- if (n_bytes)
+ if (G_UNLIKELY (!g_mem_initialized))
+ g_mem_init_nomessage();
+ if (G_LIKELY (n_bytes))
return glib_mem_vtable.try_realloc (mem, n_bytes);
if (mem)
gulong local_zinit;
gulong local_frees;
+ if (G_UNLIKELY (!g_mem_initialized))
+ g_mem_init_nomessage();
+
g_mutex_lock (gmem_profile_mutex);
local_allocs = profile_allocs;
{
}
+#ifdef ENABLE_GC_FRIENDLY_DEFAULT
+gboolean g_mem_gc_friendly = TRUE;
+#else
+gboolean g_mem_gc_friendly = FALSE;
+#endif
+
+static void
+g_mem_init_nomessage (void)
+{
+ if (g_mem_initialized)
+ return;
+ /* don't use g_malloc/g_message here */
+ gchar buffer[1024];
+ const gchar *val = _g_getenv_nomalloc ("G_DEBUG", buffer);
+ static const GDebugKey keys[] = {
+ { "gc-friendly", 1 },
+ };
+ gint flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
+ if (flags & 1) /* gc-friendly */
+ {
+ g_mem_gc_friendly = TRUE;
+ }
+ g_mem_initialized = TRUE;
+}
+
void
_g_mem_thread_init_noprivate_nomessage (void)
{
- /* we may only create mutexes here, locking/unlocking itself
- * does not yet work.
+ /* we may only create mutexes here, locking/
+ * unlocking a mutex does not yet work.
*/
+ g_mem_init_nomessage();
#ifndef G_DISABLE_CHECKS
gmem_profile_mutex = g_mutex_new ();
#endif
void g_mem_set_vtable (GMemVTable *vtable);
gboolean g_mem_is_system_malloc (void);
+GLIB_VAR gboolean g_mem_gc_friendly;
+
/* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
*/
GLIB_VAR GMemVTable *glib_mem_profiler_table;
allocator->max_slab_chunk_size_for_magazine_cache = MAX_SLAB_CHUNK_SIZE (allocator);
if (allocator->config.always_malloc || allocator->config.bypass_magazines)
allocator->max_slab_chunk_size_for_magazine_cache = 0; /* non-optimized cases */
+ /* at this point, g_mem_gc_friendly() should be initialized, this
+ * should have been accomplished by the above g_malloc/g_new calls
+ */
}
static inline guint
gsize chunk_size = P2ALIGN (mem_size);
guint acat = allocator_categorize (chunk_size);
if (G_UNLIKELY (!mem_block))
- /* pass */;
- else if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
+ return;
+ if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
{
ThreadMemory *tmem = thread_memory_from_self();
guint ix = SLAB_INDEX (allocator, chunk_size);
if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
thread_memory_magazine2_unload (tmem, ix);
}
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ memset (mem_block, 0, chunk_size);
thread_memory_magazine2_free (tmem, ix, mem_block);
}
else if (acat == 2) /* allocate through slab allocator */
{
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ memset (mem_block, 0, chunk_size);
g_mutex_lock (allocator->slab_mutex);
slab_allocator_free_chunk (chunk_size, mem_block);
g_mutex_unlock (allocator->slab_mutex);
}
else /* delegate to system malloc */
- g_free (mem_block);
+ {
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ memset (mem_block, 0, mem_size);
+ g_free (mem_block);
+ }
}
void
if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
thread_memory_magazine2_unload (tmem, ix);
}
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ memset (current, 0, chunk_size);
thread_memory_magazine2_free (tmem, ix, current);
}
}
{
guint8 *current = slice;
slice = *(gpointer*) (current + next_offset);
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ memset (current, 0, chunk_size);
slab_allocator_free_chunk (chunk_size, current);
}
g_mutex_unlock (allocator->slab_mutex);
{
guint8 *current = slice;
slice = *(gpointer*) (current + next_offset);
+ if (G_UNLIKELY (g_mem_gc_friendly))
+ memset (current, 0, mem_size);
g_free (current);
}
}
tree->key_destroy_func (node->key);
if (tree->value_destroy_func)
tree->value_destroy_func (node->value);
-
-#ifdef ENABLE_GC_FRIENDLY
- node->left = NULL;
- node->right = NULL;
- node->key = NULL;
- node->value = NULL;
-#endif /* ENABLE_GC_FRIENDLY */
-
g_slice_free (GTreeNode, node);
node = next;
tree->value_destroy_func (node->value);
}
-#ifdef ENABLE_GC_FRIENDLY
- node->left = NULL;
- node->right = NULL;
- node->key = NULL;
- node->value = NULL;
-#endif /* ENABLE_GC_FRIENDLY */
-
g_slice_free (GTreeNode, node);
tree->nnodes--;
for (i = 0; i < prime_size; i++)
if (memchunks[i])
old_mem_chunk_destroy (memchunks[i]);
+ g_free (ps);
+ g_free (ss);
return NULL;
}
for (j = 0; j < k; j++)
g_slice_free1 (sz, ps[j]);
}
+ g_free (ps);
+ g_free (ss);
return NULL;
}