From 75db5782889dde25bb9f0ee7d4f06db0fa7edf5b Mon Sep 17 00:00:00 2001 From: Tim Janik Date: Mon, 23 Jan 2006 15:51:06 +0000 Subject: [PATCH] initialize GSlice config from G_SLICE environemtn variable. we support Mon Jan 23 16:46:20 2006 Tim Janik * glib/gslice.c (slice_config_init): initialize GSlice config from G_SLICE environemtn variable. we support G_SLICE=always-malloc currently, which forces all g_slice_*() allocations to use the system malloc instead. * glib/gutils.c: g_parse_debug_string(): added a note about not using g_malloc() here. _g_getenv_nomalloc(): getenv() variant that doesn't use g_malloc or g_slice. contains only guesswork in the WIN32 branch. --- ChangeLog | 12 +++++++++ ChangeLog.pre-2-10 | 12 +++++++++ ChangeLog.pre-2-12 | 12 +++++++++ glib/gslice.c | 27 ++++++++++++++++---- glib/gutils.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++ glib/gutils.h | 2 ++ 6 files changed, 123 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 8acc30909..bfecd6a72 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +Mon Jan 23 16:46:20 2006 Tim Janik + + * glib/gslice.c (slice_config_init): initialize GSlice config from + G_SLICE environemtn variable. we support G_SLICE=always-malloc + currently, which forces all g_slice_*() allocations to use the system + malloc instead. + + * glib/gutils.c: + g_parse_debug_string(): added a note about not using g_malloc() here. + _g_getenv_nomalloc(): getenv() variant that doesn't use g_malloc or + g_slice. contains only guesswork in the WIN32 branch. + 2006-01-18 Matthias Clasen * Bump version diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10 index 8acc30909..bfecd6a72 100644 --- a/ChangeLog.pre-2-10 +++ b/ChangeLog.pre-2-10 @@ -1,3 +1,15 @@ +Mon Jan 23 16:46:20 2006 Tim Janik + + * glib/gslice.c (slice_config_init): initialize GSlice config from + G_SLICE environemtn variable. we support G_SLICE=always-malloc + currently, which forces all g_slice_*() allocations to use the system + malloc instead. + + * glib/gutils.c: + g_parse_debug_string(): added a note about not using g_malloc() here. + _g_getenv_nomalloc(): getenv() variant that doesn't use g_malloc or + g_slice. contains only guesswork in the WIN32 branch. + 2006-01-18 Matthias Clasen * Bump version diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12 index 8acc30909..bfecd6a72 100644 --- a/ChangeLog.pre-2-12 +++ b/ChangeLog.pre-2-12 @@ -1,3 +1,15 @@ +Mon Jan 23 16:46:20 2006 Tim Janik + + * glib/gslice.c (slice_config_init): initialize GSlice config from + G_SLICE environemtn variable. we support G_SLICE=always-malloc + currently, which forces all g_slice_*() allocations to use the system + malloc instead. + + * glib/gutils.c: + g_parse_debug_string(): added a note about not using g_malloc() here. + _g_getenv_nomalloc(): getenv() variant that doesn't use g_malloc or + g_slice. contains only guesswork in the WIN32 branch. + 2006-01-18 Matthias Clasen * Bump version diff --git a/glib/gslice.c b/glib/gslice.c index 46dd70b70..c9a4d0297 100644 --- a/glib/gslice.c +++ b/glib/gslice.c @@ -180,10 +180,10 @@ static inline gsize allocator_get_magazine_threshold (Allocator *allocator, guint ix); /* --- variables --- */ -static GPrivate *private_thread_memory = NULL; -static gsize sys_page_size = 0; -static Allocator allocator[1] = { { 0, }, }; -static SliceConfig slice_config = { +static GPrivate *private_thread_memory = NULL; +static gsize sys_page_size = 0; +static Allocator allocator[1] = { { 0, }, }; +static SliceConfig slice_config = { FALSE, /* always_malloc */ FALSE, /* bypass_magazines */ 15 * 1000, /* working_set_msecs */ @@ -255,6 +255,23 @@ g_slice_get_config_state (GSliceConfig ckey, } } +static void +slice_config_init (SliceConfig *config) +{ + /* don't use g_malloc/g_message here */ + gchar buffer[1024]; + const gchar *val = _g_getenv_nomalloc ("G_SLICE", buffer); + static const GDebugKey keys[] = { + { "always-malloc", 1 << 0 }, + }; + gint flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys)); + *config = slice_config; + if (flags & (1 << 0)) /* always-malloc */ + { + config->always_malloc = TRUE; + } +} + static void g_slice_init_nomessage (void) { @@ -273,7 +290,7 @@ g_slice_init_nomessage (void) #endif mem_assert (sys_page_size >= 2 * LARGEALIGNMENT); mem_assert ((sys_page_size & (sys_page_size - 1)) == 0); - allocator->config = slice_config; + slice_config_init (&allocator->config); allocator->min_page_size = sys_page_size; #if HAVE_POSIX_MEMALIGN || HAVE_MEMALIGN /* allow allocation of pages up to 8KB (with 8KB alignment). diff --git a/glib/gutils.c b/glib/gutils.c index faf61f684..7bcd0bc45 100644 --- a/glib/gutils.c +++ b/glib/gutils.c @@ -576,6 +576,11 @@ g_parse_debug_string (const gchar *string, guint result = 0; g_return_val_if_fail (string != NULL, 0); + + /* this function is used by gmem.c/gslice.c initialization code, + * so introducing malloc dependencies here would require adaptions + * of those code portions. + */ if (!g_ascii_strcasecmp (string, "all")) { @@ -1155,6 +1160,64 @@ g_getenv (const gchar *variable) #endif /* G_OS_WIN32 */ } +/* _g_getenv_nomalloc + * this function does a getenv() without doing any kind of allocation + * through glib. it's suitable for chars <= 127 only (both, for the + * variable name and the contents) and for contents < 1024 chars in + * length. also, it aliases "" to a NULL return value. + **/ +const gchar* +_g_getenv_nomalloc (const gchar *variable, + gchar buffer[1024]) +{ +#ifndef G_OS_WIN32 + const gchar *retval = getenv (variable); + if (retval && retval[0]) + { + gint l = strlen (retval); + if (l < 1024) + { + strncpy (buffer, retval, l); + buffer[l] = 0; + return buffer; + } + } + return NULL; +#else /* G_OS_WIN32 */ + if (G_WIN32_HAVE_WIDECHAR_API ()) + { + /* convert variable name to wchar_t without malloc */ + wchar_t wname[256], result[1024]; + gint i, l = strlen (variable); + if (l >= 256) + return NULL; + for (i = 0; variable[i]; i++) + wname[i] = variable[i]; + wname[i] = 0; + l = GetEnvironmentVariableW (wname, result, 1024); + if (l > 0 && l < 1024 && result[0]) + { + /* convert variable contents from wchar_t without malloc */ + for (i = 0; i < l; i++) + buffer[i] = result[i]; + buffer[i] = 0; + return buffer; + } + return NULL; + } + else + { + gint l = GetEnvironmentVariableW (variable, buffer, 1024); + if (l > 0 && l < 1024 && buffer[0]) + { + buffer[l] = 0; + return buffer; + } + return NULL; + } +#endif /* G_OS_WIN32 */ +} + /** * g_setenv: * @variable: the environment variable to set, must not contain '='. diff --git a/glib/gutils.h b/glib/gutils.h index 5350b9c1d..6ef90cb32 100644 --- a/glib/gutils.h +++ b/glib/gutils.h @@ -211,6 +211,8 @@ gboolean g_setenv (const gchar *variable, gboolean overwrite); void g_unsetenv (const gchar *variable); gchar** g_listenv (void); +const gchar* _g_getenv_nomalloc (const gchar *variable, + gchar buffer[1024]); /* we try to provide a usefull equivalent for ATEXIT if it is * not defined, but use is actually abandoned. people should