Commit b8c6ff42 authored by Philip Withnall's avatar Philip Withnall

Merge branch 'private-set-alloc0' into 'master'

gthread: Add g_private_set_alloc0() convenience API

See merge request !516
parents 990da71e 140c2a22
Pipeline #49711 failed with stages
in 8 minutes and 21 seconds
...@@ -657,3 +657,22 @@ ...@@ -657,3 +657,22 @@
fun:g_dbus_error_register_error_domain fun:g_dbus_error_register_error_domain
fun:g_dbus_error_quark fun:g_dbus_error_quark
} }
# Thread-private data allocated once per thread
{
g_private_set_alloc0
Memcheck:Leak
fun:malloc
...
fun:g_private_set_alloc0
}
# Thread-private GMainContext stack
{
g_main_context_push_thread_default
Memcheck:Leak
fun:malloc
...
fun:g_queue_new
fun:g_main_context_push_thread_default
}
\ No newline at end of file
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "gmessages.h" #include "gmessages.h"
#include "gstrfuncs.h" #include "gstrfuncs.h"
#include "gthread.h" #include "gthread.h"
#include "gthreadprivate.h"
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
#include "gwin32.h" #include "gwin32.h"
#endif #endif
...@@ -187,10 +188,7 @@ g_get_charset (const char **charset) ...@@ -187,10 +188,7 @@ g_get_charset (const char **charset)
const gchar *raw; const gchar *raw;
if (!cache) if (!cache)
{ cache = g_private_set_alloc0 (&cache_private, sizeof (GCharsetCache));
cache = g_new0 (GCharsetCache, 1);
g_private_set (&cache_private, cache);
}
G_LOCK (aliases); G_LOCK (aliases);
raw = _g_locale_charset_raw (); raw = _g_locale_charset_raw ();
......
...@@ -46,6 +46,7 @@ ...@@ -46,6 +46,7 @@
#include "gstrfuncs.h" #include "gstrfuncs.h"
#include "gtestutils.h" #include "gtestutils.h"
#include "gthread.h" #include "gthread.h"
#include "gthreadprivate.h"
#include "gunicode.h" #include "gunicode.h"
#include "gfileutils.h" #include "gfileutils.h"
...@@ -1131,10 +1132,7 @@ g_get_filename_charsets (const gchar ***filename_charsets) ...@@ -1131,10 +1132,7 @@ g_get_filename_charsets (const gchar ***filename_charsets)
const gchar *charset; const gchar *charset;
if (!cache) if (!cache)
{ cache = g_private_set_alloc0 (&cache_private, sizeof (GFilenameCharsetCache));
cache = g_new0 (GFilenameCharsetCache, 1);
g_private_set (&cache_private, cache);
}
g_get_charset (&charset); g_get_charset (&charset);
......
...@@ -87,6 +87,7 @@ ...@@ -87,6 +87,7 @@
#include "gqueue.h" #include "gqueue.h"
#include "gstrfuncs.h" #include "gstrfuncs.h"
#include "gtestutils.h" #include "gtestutils.h"
#include "gthreadprivate.h"
#ifdef G_OS_WIN32 #ifdef G_OS_WIN32
#include "gwin32.h" #include "gwin32.h"
...@@ -2828,7 +2829,7 @@ g_get_monotonic_time (void) ...@@ -2828,7 +2829,7 @@ g_get_monotonic_time (void)
static void static void
g_main_dispatch_free (gpointer dispatch) g_main_dispatch_free (gpointer dispatch)
{ {
g_slice_free (GMainDispatch, dispatch); g_free (dispatch);
} }
/* Running the main loop */ /* Running the main loop */
...@@ -2842,10 +2843,7 @@ get_dispatch (void) ...@@ -2842,10 +2843,7 @@ get_dispatch (void)
dispatch = g_private_get (&depth_private); dispatch = g_private_get (&depth_private);
if (!dispatch) if (!dispatch)
{ dispatch = g_private_set_alloc0 (&depth_private, sizeof (GMainDispatch));
dispatch = g_slice_new0 (GMainDispatch);
g_private_set (&depth_private, dispatch);
}
return dispatch; return dispatch;
} }
......
...@@ -45,6 +45,7 @@ ...@@ -45,6 +45,7 @@
#include "gtrashstack.h" #include "gtrashstack.h"
#include "gtestutils.h" #include "gtestutils.h"
#include "gthread.h" #include "gthread.h"
#include "gthreadprivate.h"
#include "glib_trace.h" #include "glib_trace.h"
#include "gprintf.h" #include "gprintf.h"
...@@ -515,10 +516,9 @@ thread_memory_from_self (void) ...@@ -515,10 +516,9 @@ thread_memory_from_self (void)
g_mutex_unlock (&init_mutex); g_mutex_unlock (&init_mutex);
n_magazines = MAX_SLAB_INDEX (allocator); n_magazines = MAX_SLAB_INDEX (allocator);
tmem = g_malloc0 (sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines); tmem = g_private_set_alloc0 (&private_thread_memory, sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines);
tmem->magazine1 = (Magazine*) (tmem + 1); tmem->magazine1 = (Magazine*) (tmem + 1);
tmem->magazine2 = &tmem->magazine1[n_magazines]; tmem->magazine2 = &tmem->magazine1[n_magazines];
g_private_set (&private_thread_memory, tmem);
} }
return tmem; return tmem;
} }
......
...@@ -517,6 +517,33 @@ static GPrivate g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup ...@@ -517,6 +517,33 @@ static GPrivate g_thread_specific_private = G_PRIVATE_INIT (g_thread_cleanup
G_LOCK_DEFINE_STATIC (g_thread_new); G_LOCK_DEFINE_STATIC (g_thread_new);
/*
* g_private_set_alloc0:
* @key: a #GPrivate
* @size: size of the allocation, in bytes
*
* Sets the thread local variable @key to have a newly-allocated and zero-filled
* value of given @size, and returns a pointer to that memory. Allocations made
* using this API will be suppressed in valgrind: it is intended to be used for
* one-time allocations which are known to be leaked, such as those for
* per-thread initialisation data. Otherwise, this function behaves the same as
* g_private_set().
*
* Returns: (transfer full): new thread-local heap allocation of size @size
* Since: 2.60
*/
/*< private >*/
gpointer
g_private_set_alloc0 (GPrivate *key,
gsize size)
{
gpointer allocated = g_malloc0 (size);
g_private_set (key, allocated);
return g_steal_pointer (&allocated);
}
/* GOnce {{{1 ------------------------------------------------------------- */ /* GOnce {{{1 ------------------------------------------------------------- */
/** /**
......
...@@ -56,4 +56,7 @@ GThread * g_thread_new_internal (const gchar *name, ...@@ -56,4 +56,7 @@ GThread * g_thread_new_internal (const gchar *name,
gpointer g_thread_proxy (gpointer thread); gpointer g_thread_proxy (gpointer thread);
gpointer g_private_set_alloc0 (GPrivate *key,
gsize size);
#endif /* __G_THREADPRIVATE_H__ */ #endif /* __G_THREADPRIVATE_H__ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment