[glib/wip/mutexes] gmem: move to glib-ctor
- From: Ryan Lortie <ryanl src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [glib/wip/mutexes] gmem: move to glib-ctor
- Date: Sun, 18 Sep 2011 16:03:00 +0000 (UTC)
commit 7dd937f96b19f41d4eda4890dbbb57c6451cf4b6
Author: Ryan Lortie <desrt desrt ca>
Date: Sun Sep 18 02:27:03 2011 -0400
gmem: move to glib-ctor
glib/gmem.c | 114 ++++++++++++++++++++-----------------------------
glib/gthread.c | 3 -
glib/gthreadprivate.h | 2 -
3 files changed, 47 insertions(+), 72 deletions(-)
---
diff --git a/glib/gmem.c b/glib/gmem.c
index 59813f5..b7b9b8d 100644
--- a/glib/gmem.c
+++ b/glib/gmem.c
@@ -40,6 +40,7 @@
#include "gtestutils.h"
#include "gthread.h"
#include "glib_trace.h"
+#include "glib-ctor.h"
#define MEM_PROFILE_TABLE_SIZE 4096
@@ -54,10 +55,35 @@
* g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
*/
-/* --- prototypes --- */
-static gboolean g_mem_initialized = FALSE;
-static void g_mem_init_nomessage (void);
+#ifdef ENABLE_GC_FRIENDLY_DEFAULT
+gboolean g_mem_gc_friendly = TRUE;
+#else
+/**
+ * g_mem_gc_friendly:
+ *
+ * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
+ * includes the key <link linkend="G_DEBUG">gc-friendly</link>.
+ */
+gboolean g_mem_gc_friendly = FALSE;
+#endif
+
+GLIB_CTOR (g_mem_init_nomessage)
+{
+ gchar buffer[1024];
+ const gchar *val;
+ const GDebugKey keys[] = {
+ { "gc-friendly", 1 },
+ };
+ gint flags;
+ /* don't use g_malloc/g_message here */
+ val = _g_getenv_nomalloc ("G_DEBUG", buffer);
+ flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
+ if (flags & 1) /* gc-friendly */
+ {
+ g_mem_gc_friendly = TRUE;
+ }
+}
/* --- malloc wrappers --- */
#ifndef REALLOC_0_WORKS
@@ -155,8 +181,8 @@ static GMemVTable glib_mem_vtable = {
gpointer
g_malloc (gsize n_bytes)
{
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
+
if (G_LIKELY (n_bytes))
{
gpointer mem;
@@ -187,8 +213,8 @@ g_malloc (gsize n_bytes)
gpointer
g_malloc0 (gsize n_bytes)
{
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
+
if (G_LIKELY (n_bytes))
{
gpointer mem;
@@ -226,8 +252,8 @@ g_realloc (gpointer mem,
{
gpointer newmem;
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
+
if (G_LIKELY (n_bytes))
{
newmem = glib_mem_vtable.realloc (mem, n_bytes);
@@ -257,8 +283,8 @@ g_realloc (gpointer mem,
void
g_free (gpointer mem)
{
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
+
if (G_LIKELY (mem))
glib_mem_vtable.free (mem);
TRACE(GLIB_MEM_FREE((void*) mem));
@@ -278,8 +304,8 @@ g_try_malloc (gsize n_bytes)
{
gpointer mem;
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
+
if (G_LIKELY (n_bytes))
mem = glib_mem_vtable.try_malloc (n_bytes);
else
@@ -305,8 +331,8 @@ g_try_malloc0 (gsize n_bytes)
{
gpointer mem;
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
+
if (G_LIKELY (n_bytes))
mem = glib_mem_vtable.try_malloc (n_bytes);
else
@@ -335,8 +361,8 @@ g_try_realloc (gpointer mem,
{
gpointer newmem;
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
+
if (G_LIKELY (n_bytes))
newmem = glib_mem_vtable.try_realloc (mem, n_bytes);
else
@@ -371,8 +397,7 @@ g_malloc_n (gsize n_blocks,
{
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
{
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
G_STRLOC, n_blocks, n_block_bytes);
@@ -398,8 +423,7 @@ g_malloc0_n (gsize n_blocks,
{
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
{
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
G_STRLOC, n_blocks, n_block_bytes);
@@ -427,8 +451,7 @@ g_realloc_n (gpointer mem,
{
if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
{
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
G_STRLOC, n_blocks, n_block_bytes);
@@ -707,8 +730,7 @@ g_mem_profile (void)
gsize local_zinit;
gsize local_frees;
- if (G_UNLIKELY (!g_mem_initialized))
- g_mem_init_nomessage();
+ GLIB_ENSURE_CTOR (g_mem_init_nomessage);
g_mutex_lock (&gmem_profile_mutex);
@@ -1350,45 +1372,3 @@ void
g_allocator_free (GAllocator *allocator)
{
}
-
-#ifdef ENABLE_GC_FRIENDLY_DEFAULT
-gboolean g_mem_gc_friendly = TRUE;
-#else
-/**
- * g_mem_gc_friendly:
- *
- * This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
- * includes the key <link linkend="G_DEBUG">gc-friendly</link>.
- */
-gboolean g_mem_gc_friendly = FALSE;
-#endif
-
-static void
-g_mem_init_nomessage (void)
-{
- gchar buffer[1024];
- const gchar *val;
- const GDebugKey keys[] = {
- { "gc-friendly", 1 },
- };
- gint flags;
- if (g_mem_initialized)
- return;
- /* don't use g_malloc/g_message here */
- val = _g_getenv_nomalloc ("G_DEBUG", buffer);
- 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 a mutex does not yet work.
- */
- g_mem_init_nomessage();
-}
diff --git a/glib/gthread.c b/glib/gthread.c
index 8b561ed..1b784b2 100644
--- a/glib/gthread.c
+++ b/glib/gthread.c
@@ -939,9 +939,6 @@ g_thread_init_glib (void)
*/
GRealThread* main_thread = (GRealThread*) g_thread_self ();
- /* we may only create mutex and cond in here */
- _g_mem_thread_init_noprivate_nomessage ();
-
/* setup the basic threading system */
g_threads_got_initialized = TRUE;
g_private_init (&g_thread_specific_private, g_thread_cleanup);
diff --git a/glib/gthreadprivate.h b/glib/gthreadprivate.h
index 981c01e..fa64468 100644
--- a/glib/gthreadprivate.h
+++ b/glib/gthreadprivate.h
@@ -46,8 +46,6 @@ G_BEGIN_DECLS
/* Is called from gthread/gthread-impl.c */
void g_thread_init_glib (void);
-/* base initializers, may only use g_mutex_new(), g_cond_new() */
-G_GNUC_INTERNAL void _g_mem_thread_init_noprivate_nomessage (void);
/* initializers that may also use g_private_new() */
G_GNUC_INTERNAL void _g_messages_thread_init_nomessage (void);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]