1 /* GLIB sliced memory - fast concurrent memory chunk allocator
2 * Copyright (C) 2005 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 #if defined HAVE_POSIX_MEMALIGN && defined POSIX_MEMALIGN_WITH_COMPLIANT_ALLOCS
24 # define HAVE_COMPLIANT_POSIX_MEMALIGN 1
27 #ifdef HAVE_COMPLIANT_POSIX_MEMALIGN
28 #define _XOPEN_SOURCE 600 /* posix_memalign() */
30 #include <stdlib.h> /* posix_memalign() */
33 #include "gmem.h" /* gslice.h */
34 #include "gthreadprivate.h"
38 #include <unistd.h> /* sysconf() */
45 #include <stdio.h> /* fputs/fprintf */
48 /* the GSlice allocator is split up into 4 layers, roughly modelled after the slab
49 * allocator and magazine extensions as outlined in:
50 * + [Bonwick94] Jeff Bonwick, The slab allocator: An object-caching kernel
51 * memory allocator. USENIX 1994, http://citeseer.ist.psu.edu/bonwick94slab.html
52 * + [Bonwick01] Bonwick and Jonathan Adams, Magazines and vmem: Extending the
53 * slab allocator to many cpu's and arbitrary resources.
54 * USENIX 2001, http://citeseer.ist.psu.edu/bonwick01magazines.html
56 * - the thread magazines. for each (aligned) chunk size, a magazine (a list)
57 * of recently freed and soon to be allocated chunks is maintained per thread.
58 * this way, most alloc/free requests can be quickly satisfied from per-thread
59 * free lists which only require one g_private_get() call to retrive the
61 * - the magazine cache. allocating and freeing chunks to/from threads only
62 * occours at magazine sizes from a global depot of magazines. the depot
63 * maintaines a 15 second working set of allocated magazines, so full
64 * magazines are not allocated and released too often.
65 * the chunk size dependent magazine sizes automatically adapt (within limits,
66 * see [3]) to lock contention to properly scale performance across a variety
68 * - the slab allocator. this allocator allocates slabs (blocks of memory) close
69 * to the system page size or multiples thereof which have to be page aligned.
70 * the blocks are divided into smaller chunks which are used to satisfy
71 * allocations from the upper layers. the space provided by the reminder of
72 * the chunk size division is used for cache colorization (random distribution
73 * of chunk addresses) to improve processor cache utilization. multiple slabs
74 * with the same chunk size are kept in a partially sorted ring to allow O(1)
75 * freeing and allocation of chunks (as long as the allocation of an entirely
76 * new slab can be avoided).
77 * - the page allocator. on most modern systems, posix_memalign(3) or
78 * memalign(3) should be available, so this is used to allocate blocks with
79 * system page size based alignments and sizes or multiples thereof.
80 * if no memalign variant is provided, valloc() is used instead and
81 * block sizes are limited to the system page size (no multiples thereof).
82 * as a fallback, on system without even valloc(), a malloc(3)-based page
83 * allocator with alloc-only behaviour is used.
86 * [1] some systems memalign(3) implementations may rely on boundary tagging for
87 * the handed out memory chunks. to avoid excessive page-wise fragmentation,
88 * we reserve 2 * sizeof (void*) per block size for the systems memalign(3),
89 * specified in NATIVE_MALLOC_PADDING.
90 * [2] using the slab allocator alone already provides for a fast and efficient
91 * allocator, it doesn't properly scale beyond single-threaded uses though.
92 * also, the slab allocator implements eager free(3)-ing, i.e. does not
93 * provide any form of caching or working set maintenance. so if used alone,
94 * it's vulnerable to trashing for sequences of balanced (alloc, free) pairs
95 * at certain thresholds.
96 * [3] magazine sizes are bound by an implementation specific minimum size and
97 * a chunk size specific maximum to limit magazine storage sizes to roughly
99 * [4] allocating ca. 8 chunks per block/page keeps a good balance between
100 * external and internal fragmentation (<= 12.5%). [Bonwick94]
103 /* --- macros and constants --- */
104 #define LARGEALIGNMENT (256)
105 #define P2ALIGNMENT (2 * sizeof (gsize)) /* fits 2 pointers (assumed to be 2 * GLIB_SIZEOF_SIZE_T below) */
106 #define ALIGN(size, base) ((base) * (gsize) (((size) + (base) - 1) / (base)))
107 #define NATIVE_MALLOC_PADDING P2ALIGNMENT /* per-page padding left for native malloc(3) see [1] */
108 #define SLAB_INFO_SIZE P2ALIGN (sizeof (SlabInfo) + NATIVE_MALLOC_PADDING)
109 #define MAX_MAGAZINE_SIZE (256) /* see [3] and allocator_get_magazine_threshold() for this */
110 #define MIN_MAGAZINE_SIZE (4)
111 #define MAX_STAMP_COUNTER (7) /* distributes the load of gettimeofday() */
112 #define MAX_SLAB_CHUNK_SIZE(al) (((al)->max_page_size - SLAB_INFO_SIZE) / 8) /* we want at last 8 chunks per page, see [4] */
113 #define MAX_SLAB_INDEX(al) (SLAB_INDEX (al, MAX_SLAB_CHUNK_SIZE (al)) + 1)
114 #define SLAB_INDEX(al, asize) ((asize) / P2ALIGNMENT - 1) /* asize must be P2ALIGNMENT aligned */
115 #define SLAB_CHUNK_SIZE(al, ix) (((ix) + 1) * P2ALIGNMENT)
116 #define SLAB_BPAGE_SIZE(al,csz) (8 * (csz) + SLAB_INFO_SIZE)
118 /* optimized version of ALIGN (size, P2ALIGNMENT) */
119 #if GLIB_SIZEOF_SIZE_T * 2 == 8 /* P2ALIGNMENT */
120 #define P2ALIGN(size) (((size) + 0x7) & ~(gsize) 0x7)
121 #elif GLIB_SIZEOF_SIZE_T * 2 == 16 /* P2ALIGNMENT */
122 #define P2ALIGN(size) (((size) + 0xf) & ~(gsize) 0xf)
124 #define P2ALIGN(size) ALIGN (size, P2ALIGNMENT)
127 /* special helpers to avoid gmessage.c dependency */
128 static void mem_error (const char *format, ...) G_GNUC_PRINTF (1,2);
129 #define mem_assert(cond) do { if (G_LIKELY (cond)) ; else mem_error ("assertion failed: %s", #cond); } while (0)
131 /* --- structures --- */
132 typedef struct _ChunkLink ChunkLink;
133 typedef struct _SlabInfo SlabInfo;
134 typedef struct _CachedMagazine CachedMagazine;
142 SlabInfo *next, *prev;
146 gsize count; /* approximative chunks list length */
149 Magazine *magazine1; /* array of MAX_SLAB_INDEX (allocator) */
150 Magazine *magazine2; /* array of MAX_SLAB_INDEX (allocator) */
153 gboolean always_malloc;
154 gboolean bypass_magazines;
155 gboolean debug_blocks;
156 gsize working_set_msecs;
157 guint color_increment;
160 /* const after initialization */
161 gsize min_page_size, max_page_size;
163 gsize max_slab_chunk_size_for_magazine_cache;
165 GMutex *magazine_mutex;
166 ChunkLink **magazines; /* array of MAX_SLAB_INDEX (allocator) */
167 guint *contention_counters; /* array of MAX_SLAB_INDEX (allocator) */
173 SlabInfo **slab_stack; /* array of MAX_SLAB_INDEX (allocator) */
177 /* --- g-slice prototypes --- */
178 static gpointer slab_allocator_alloc_chunk (gsize chunk_size);
179 static void slab_allocator_free_chunk (gsize chunk_size,
181 static void private_thread_memory_cleanup (gpointer data);
182 static gpointer allocator_memalign (gsize alignment,
184 static void allocator_memfree (gsize memsize,
186 static inline void magazine_cache_update_stamp (void);
187 static inline gsize allocator_get_magazine_threshold (Allocator *allocator,
190 /* --- g-slice memory checker --- */
191 static void smc_notify_alloc (void *pointer,
193 static int smc_notify_free (void *pointer,
196 /* --- variables --- */
197 static GPrivate *private_thread_memory = NULL;
198 static gsize sys_page_size = 0;
199 static Allocator allocator[1] = { { 0, }, };
200 static SliceConfig slice_config = {
201 FALSE, /* always_malloc */
202 FALSE, /* bypass_magazines */
203 FALSE, /* debug_blocks */
204 15 * 1000, /* working_set_msecs */
205 1, /* color increment, alt: 0x7fffffff */
207 static GMutex *smc_tree_mutex = NULL; /* mutex for G_SLICE=debug-blocks */
209 /* --- auxillary funcitons --- */
211 g_slice_set_config (GSliceConfig ckey,
214 g_return_if_fail (sys_page_size == 0);
217 case G_SLICE_CONFIG_ALWAYS_MALLOC:
218 slice_config.always_malloc = value != 0;
220 case G_SLICE_CONFIG_BYPASS_MAGAZINES:
221 slice_config.bypass_magazines = value != 0;
223 case G_SLICE_CONFIG_WORKING_SET_MSECS:
224 slice_config.working_set_msecs = value;
226 case G_SLICE_CONFIG_COLOR_INCREMENT:
227 slice_config.color_increment = value;
233 g_slice_get_config (GSliceConfig ckey)
237 case G_SLICE_CONFIG_ALWAYS_MALLOC:
238 return slice_config.always_malloc;
239 case G_SLICE_CONFIG_BYPASS_MAGAZINES:
240 return slice_config.bypass_magazines;
241 case G_SLICE_CONFIG_WORKING_SET_MSECS:
242 return slice_config.working_set_msecs;
243 case G_SLICE_CONFIG_CHUNK_SIZES:
244 return MAX_SLAB_INDEX (allocator);
245 case G_SLICE_CONFIG_COLOR_INCREMENT:
246 return slice_config.color_increment;
253 g_slice_get_config_state (GSliceConfig ckey,
258 g_return_val_if_fail (n_values != NULL, NULL);
263 case G_SLICE_CONFIG_CONTENTION_COUNTER:
264 array[i++] = SLAB_CHUNK_SIZE (allocator, address);
265 array[i++] = allocator->contention_counters[address];
266 array[i++] = allocator_get_magazine_threshold (allocator, address);
268 return g_memdup (array, sizeof (array[0]) * *n_values);
275 slice_config_init (SliceConfig *config)
277 /* don't use g_malloc/g_message here */
279 const gchar *val = _g_getenv_nomalloc ("G_SLICE", buffer);
280 const GDebugKey keys[] = {
281 { "always-malloc", 1 << 0 },
282 { "debug-blocks", 1 << 1 },
284 gint flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
285 *config = slice_config;
286 if (flags & (1 << 0)) /* always-malloc */
287 config->always_malloc = TRUE;
288 if (flags & (1 << 1)) /* debug-blocks */
289 config->debug_blocks = TRUE;
293 g_slice_init_nomessage (void)
295 /* we may not use g_error() or friends here */
296 mem_assert (sys_page_size == 0);
297 mem_assert (MIN_MAGAZINE_SIZE >= 4);
301 SYSTEM_INFO system_info;
302 GetSystemInfo (&system_info);
303 sys_page_size = system_info.dwPageSize;
306 sys_page_size = sysconf (_SC_PAGESIZE); /* = sysconf (_SC_PAGE_SIZE); = getpagesize(); */
308 mem_assert (sys_page_size >= 2 * LARGEALIGNMENT);
309 mem_assert ((sys_page_size & (sys_page_size - 1)) == 0);
310 slice_config_init (&allocator->config);
311 allocator->min_page_size = sys_page_size;
312 #if HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN
313 /* allow allocation of pages up to 8KB (with 8KB alignment).
314 * this is useful because many medium to large sized structures
315 * fit less than 8 times (see [4]) into 4KB pages.
316 * we allow very small page sizes here, to reduce wastage in
317 * threads if only small allocations are required (this does
318 * bear the risk of incresing allocation times and fragmentation
321 allocator->min_page_size = MAX (allocator->min_page_size, 4096);
322 allocator->max_page_size = MAX (allocator->min_page_size, 8192);
323 allocator->min_page_size = MIN (allocator->min_page_size, 128);
325 /* we can only align to system page size */
326 allocator->max_page_size = sys_page_size;
328 if (allocator->config.always_malloc)
330 allocator->contention_counters = NULL;
331 allocator->magazines = NULL;
332 allocator->slab_stack = NULL;
336 allocator->contention_counters = g_new0 (guint, MAX_SLAB_INDEX (allocator));
337 allocator->magazines = g_new0 (ChunkLink*, MAX_SLAB_INDEX (allocator));
338 allocator->slab_stack = g_new0 (SlabInfo*, MAX_SLAB_INDEX (allocator));
341 allocator->magazine_mutex = NULL; /* _g_slice_thread_init_nomessage() */
342 allocator->mutex_counter = 0;
343 allocator->stamp_counter = MAX_STAMP_COUNTER; /* force initial update */
344 allocator->last_stamp = 0;
345 allocator->slab_mutex = NULL; /* _g_slice_thread_init_nomessage() */
346 allocator->color_accu = 0;
347 magazine_cache_update_stamp();
348 /* values cached for performance reasons */
349 allocator->max_slab_chunk_size_for_magazine_cache = MAX_SLAB_CHUNK_SIZE (allocator);
350 if (allocator->config.always_malloc || allocator->config.bypass_magazines)
351 allocator->max_slab_chunk_size_for_magazine_cache = 0; /* non-optimized cases */
352 /* at this point, g_mem_gc_friendly() should be initialized, this
353 * should have been accomplished by the above g_malloc/g_new calls
358 allocator_categorize (gsize aligned_chunk_size)
360 /* speed up the likely path */
361 if (G_LIKELY (aligned_chunk_size && aligned_chunk_size <= allocator->max_slab_chunk_size_for_magazine_cache))
362 return 1; /* use magazine cache */
364 /* the above will fail (max_slab_chunk_size_for_magazine_cache == 0) if the
365 * allocator is still uninitialized, or if we are not configured to use the
369 g_slice_init_nomessage ();
370 if (!allocator->config.always_malloc &&
371 aligned_chunk_size &&
372 aligned_chunk_size <= MAX_SLAB_CHUNK_SIZE (allocator))
374 if (allocator->config.bypass_magazines)
375 return 2; /* use slab allocator, see [2] */
376 return 1; /* use magazine cache */
378 return 0; /* use malloc() */
382 _g_slice_thread_init_nomessage (void)
384 /* we may not use g_error() or friends here */
386 g_slice_init_nomessage();
389 /* g_slice_init_nomessage() has been called already, probably due
390 * to a g_slice_alloc1() before g_thread_init().
393 private_thread_memory = g_private_new (private_thread_memory_cleanup);
394 allocator->magazine_mutex = g_mutex_new();
395 allocator->slab_mutex = g_mutex_new();
396 if (allocator->config.debug_blocks)
397 smc_tree_mutex = g_mutex_new();
401 g_mutex_lock_a (GMutex *mutex,
402 guint *contention_counter)
404 gboolean contention = FALSE;
405 if (!g_mutex_trylock (mutex))
407 g_mutex_lock (mutex);
412 allocator->mutex_counter++;
413 if (allocator->mutex_counter >= 1) /* quickly adapt to contention */
415 allocator->mutex_counter = 0;
416 *contention_counter = MIN (*contention_counter + 1, MAX_MAGAZINE_SIZE);
419 else /* !contention */
421 allocator->mutex_counter--;
422 if (allocator->mutex_counter < -11) /* moderately recover magazine sizes */
424 allocator->mutex_counter = 0;
425 *contention_counter = MAX (*contention_counter, 1) - 1;
430 static inline ThreadMemory*
431 thread_memory_from_self (void)
433 ThreadMemory *tmem = g_private_get (private_thread_memory);
434 if (G_UNLIKELY (!tmem))
436 static ThreadMemory *single_thread_memory = NULL; /* remember single-thread info for multi-threaded case */
437 if (single_thread_memory && g_thread_supported ())
439 g_mutex_lock (allocator->slab_mutex);
440 if (single_thread_memory)
442 /* GSlice has been used before g_thread_init(), and now
443 * we are running threaded. to cope with it, use the saved
444 * thread memory structure from when we weren't threaded.
446 tmem = single_thread_memory;
447 single_thread_memory = NULL; /* slab_mutex protected when multi-threaded */
449 g_mutex_unlock (allocator->slab_mutex);
453 const guint n_magazines = MAX_SLAB_INDEX (allocator);
454 tmem = g_malloc0 (sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines);
455 tmem->magazine1 = (Magazine*) (tmem + 1);
456 tmem->magazine2 = &tmem->magazine1[n_magazines];
458 /* g_private_get/g_private_set works in the single-threaded xor the multi-
459 * threaded case. but not *across* g_thread_init(), after multi-thread
460 * initialization it returns NULL for previously set single-thread data.
462 g_private_set (private_thread_memory, tmem);
463 /* save single-thread thread memory structure, in case we need to
464 * pick it up again after multi-thread initialization happened.
466 if (!single_thread_memory && !g_thread_supported ())
467 single_thread_memory = tmem; /* no slab_mutex created yet */
472 static inline ChunkLink*
473 magazine_chain_pop_head (ChunkLink **magazine_chunks)
475 /* magazine chains are linked via ChunkLink->next.
476 * each ChunkLink->data of the toplevel chain may point to a subchain,
477 * linked via ChunkLink->next. ChunkLink->data of the subchains just
478 * contains uninitialized junk.
480 ChunkLink *chunk = (*magazine_chunks)->data;
481 if (G_UNLIKELY (chunk))
483 /* allocating from freed list */
484 (*magazine_chunks)->data = chunk->next;
488 chunk = *magazine_chunks;
489 *magazine_chunks = chunk->next;
494 #if 0 /* useful for debugging */
496 magazine_count (ChunkLink *head)
503 ChunkLink *child = head->data;
505 for (child = head->data; child; child = child->next)
514 allocator_get_magazine_threshold (Allocator *allocator,
517 /* the magazine size calculated here has a lower bound of MIN_MAGAZINE_SIZE,
518 * which is required by the implementation. also, for moderately sized chunks
519 * (say >= 64 bytes), magazine sizes shouldn't be much smaller then the number
520 * of chunks available per page/2 to avoid excessive traffic in the magazine
521 * cache for small to medium sized structures.
522 * the upper bound of the magazine size is effectively provided by
523 * MAX_MAGAZINE_SIZE. for larger chunks, this number is scaled down so that
524 * the content of a single magazine doesn't exceed ca. 16KB.
526 gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
527 guint threshold = MAX (MIN_MAGAZINE_SIZE, allocator->max_page_size / MAX (5 * chunk_size, 5 * 32));
528 guint contention_counter = allocator->contention_counters[ix];
529 if (G_UNLIKELY (contention_counter)) /* single CPU bias */
531 /* adapt contention counter thresholds to chunk sizes */
532 contention_counter = contention_counter * 64 / chunk_size;
533 threshold = MAX (threshold, contention_counter);
538 /* --- magazine cache --- */
540 magazine_cache_update_stamp (void)
542 if (allocator->stamp_counter >= MAX_STAMP_COUNTER)
545 g_get_current_time (&tv);
546 allocator->last_stamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; /* milli seconds */
547 allocator->stamp_counter = 0;
550 allocator->stamp_counter++;
553 static inline ChunkLink*
554 magazine_chain_prepare_fields (ChunkLink *magazine_chunks)
560 /* checked upon initialization: mem_assert (MIN_MAGAZINE_SIZE >= 4); */
561 /* ensure a magazine with at least 4 unused data pointers */
562 chunk1 = magazine_chain_pop_head (&magazine_chunks);
563 chunk2 = magazine_chain_pop_head (&magazine_chunks);
564 chunk3 = magazine_chain_pop_head (&magazine_chunks);
565 chunk4 = magazine_chain_pop_head (&magazine_chunks);
566 chunk4->next = magazine_chunks;
567 chunk3->next = chunk4;
568 chunk2->next = chunk3;
569 chunk1->next = chunk2;
573 /* access the first 3 fields of a specially prepared magazine chain */
574 #define magazine_chain_prev(mc) ((mc)->data)
575 #define magazine_chain_stamp(mc) ((mc)->next->data)
576 #define magazine_chain_uint_stamp(mc) GPOINTER_TO_UINT ((mc)->next->data)
577 #define magazine_chain_next(mc) ((mc)->next->next->data)
578 #define magazine_chain_count(mc) ((mc)->next->next->next->data)
581 magazine_cache_trim (Allocator *allocator,
585 /* g_mutex_lock (allocator->mutex); done by caller */
586 /* trim magazine cache from tail */
587 ChunkLink *current = magazine_chain_prev (allocator->magazines[ix]);
588 ChunkLink *trash = NULL;
589 while (ABS (stamp - magazine_chain_uint_stamp (current)) >= allocator->config.working_set_msecs)
592 ChunkLink *prev = magazine_chain_prev (current);
593 ChunkLink *next = magazine_chain_next (current);
594 magazine_chain_next (prev) = next;
595 magazine_chain_prev (next) = prev;
596 /* clear special fields, put on trash stack */
597 magazine_chain_next (current) = NULL;
598 magazine_chain_count (current) = NULL;
599 magazine_chain_stamp (current) = NULL;
600 magazine_chain_prev (current) = trash;
602 /* fixup list head if required */
603 if (current == allocator->magazines[ix])
605 allocator->magazines[ix] = NULL;
610 g_mutex_unlock (allocator->magazine_mutex);
614 const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
615 g_mutex_lock (allocator->slab_mutex);
619 trash = magazine_chain_prev (current);
620 magazine_chain_prev (current) = NULL; /* clear special field */
623 ChunkLink *chunk = magazine_chain_pop_head (¤t);
624 slab_allocator_free_chunk (chunk_size, chunk);
627 g_mutex_unlock (allocator->slab_mutex);
632 magazine_cache_push_magazine (guint ix,
633 ChunkLink *magazine_chunks,
634 gsize count) /* must be >= MIN_MAGAZINE_SIZE */
636 ChunkLink *current = magazine_chain_prepare_fields (magazine_chunks);
637 ChunkLink *next, *prev;
638 g_mutex_lock (allocator->magazine_mutex);
639 /* add magazine at head */
640 next = allocator->magazines[ix];
642 prev = magazine_chain_prev (next);
644 next = prev = current;
645 magazine_chain_next (prev) = current;
646 magazine_chain_prev (next) = current;
647 magazine_chain_prev (current) = prev;
648 magazine_chain_next (current) = next;
649 magazine_chain_count (current) = (gpointer) count;
651 magazine_cache_update_stamp();
652 magazine_chain_stamp (current) = GUINT_TO_POINTER (allocator->last_stamp);
653 allocator->magazines[ix] = current;
654 /* free old magazines beyond a certain threshold */
655 magazine_cache_trim (allocator, ix, allocator->last_stamp);
656 /* g_mutex_unlock (allocator->mutex); was done by magazine_cache_trim() */
660 magazine_cache_pop_magazine (guint ix,
663 g_mutex_lock_a (allocator->magazine_mutex, &allocator->contention_counters[ix]);
664 if (!allocator->magazines[ix])
666 guint magazine_threshold = allocator_get_magazine_threshold (allocator, ix);
667 gsize i, chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
668 ChunkLink *chunk, *head;
669 g_mutex_unlock (allocator->magazine_mutex);
670 g_mutex_lock (allocator->slab_mutex);
671 head = slab_allocator_alloc_chunk (chunk_size);
674 for (i = 1; i < magazine_threshold; i++)
676 chunk->next = slab_allocator_alloc_chunk (chunk_size);
681 g_mutex_unlock (allocator->slab_mutex);
687 ChunkLink *current = allocator->magazines[ix];
688 ChunkLink *prev = magazine_chain_prev (current);
689 ChunkLink *next = magazine_chain_next (current);
691 magazine_chain_next (prev) = next;
692 magazine_chain_prev (next) = prev;
693 allocator->magazines[ix] = next == current ? NULL : next;
694 g_mutex_unlock (allocator->magazine_mutex);
695 /* clear special fields and hand out */
696 *countp = (gsize) magazine_chain_count (current);
697 magazine_chain_prev (current) = NULL;
698 magazine_chain_next (current) = NULL;
699 magazine_chain_count (current) = NULL;
700 magazine_chain_stamp (current) = NULL;
705 /* --- thread magazines --- */
707 private_thread_memory_cleanup (gpointer data)
709 ThreadMemory *tmem = data;
710 const guint n_magazines = MAX_SLAB_INDEX (allocator);
712 for (ix = 0; ix < n_magazines; ix++)
716 mags[0] = &tmem->magazine1[ix];
717 mags[1] = &tmem->magazine2[ix];
718 for (j = 0; j < 2; j++)
720 Magazine *mag = mags[j];
721 if (mag->count >= MIN_MAGAZINE_SIZE)
722 magazine_cache_push_magazine (ix, mag->chunks, mag->count);
725 const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
726 g_mutex_lock (allocator->slab_mutex);
729 ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
730 slab_allocator_free_chunk (chunk_size, chunk);
732 g_mutex_unlock (allocator->slab_mutex);
740 thread_memory_magazine1_reload (ThreadMemory *tmem,
743 Magazine *mag = &tmem->magazine1[ix];
744 mem_assert (mag->chunks == NULL); /* ensure that we may reset mag->count */
746 mag->chunks = magazine_cache_pop_magazine (ix, &mag->count);
750 thread_memory_magazine2_unload (ThreadMemory *tmem,
753 Magazine *mag = &tmem->magazine2[ix];
754 magazine_cache_push_magazine (ix, mag->chunks, mag->count);
760 thread_memory_swap_magazines (ThreadMemory *tmem,
763 Magazine xmag = tmem->magazine1[ix];
764 tmem->magazine1[ix] = tmem->magazine2[ix];
765 tmem->magazine2[ix] = xmag;
768 static inline gboolean
769 thread_memory_magazine1_is_empty (ThreadMemory *tmem,
772 return tmem->magazine1[ix].chunks == NULL;
775 static inline gboolean
776 thread_memory_magazine2_is_full (ThreadMemory *tmem,
779 return tmem->magazine2[ix].count >= allocator_get_magazine_threshold (allocator, ix);
782 static inline gpointer
783 thread_memory_magazine1_alloc (ThreadMemory *tmem,
786 Magazine *mag = &tmem->magazine1[ix];
787 ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
788 if (G_LIKELY (mag->count > 0))
794 thread_memory_magazine2_free (ThreadMemory *tmem,
798 Magazine *mag = &tmem->magazine2[ix];
799 ChunkLink *chunk = mem;
801 chunk->next = mag->chunks;
806 /* --- API functions --- */
808 g_slice_alloc (gsize mem_size)
813 chunk_size = P2ALIGN (mem_size);
814 acat = allocator_categorize (chunk_size);
815 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
817 ThreadMemory *tmem = thread_memory_from_self();
818 guint ix = SLAB_INDEX (allocator, chunk_size);
819 if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
821 thread_memory_swap_magazines (tmem, ix);
822 if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
823 thread_memory_magazine1_reload (tmem, ix);
825 mem = thread_memory_magazine1_alloc (tmem, ix);
827 else if (acat == 2) /* allocate through slab allocator */
829 g_mutex_lock (allocator->slab_mutex);
830 mem = slab_allocator_alloc_chunk (chunk_size);
831 g_mutex_unlock (allocator->slab_mutex);
833 else /* delegate to system malloc */
834 mem = g_malloc (mem_size);
835 if (G_UNLIKELY (allocator->config.debug_blocks))
836 smc_notify_alloc (mem, mem_size);
841 g_slice_alloc0 (gsize mem_size)
843 gpointer mem = g_slice_alloc (mem_size);
845 memset (mem, 0, mem_size);
850 g_slice_copy (gsize mem_size,
851 gconstpointer mem_block)
853 gpointer mem = g_slice_alloc (mem_size);
855 memcpy (mem, mem_block, mem_size);
860 g_slice_free1 (gsize mem_size,
863 gsize chunk_size = P2ALIGN (mem_size);
864 guint acat = allocator_categorize (chunk_size);
865 if (G_UNLIKELY (!mem_block))
867 if (G_UNLIKELY (allocator->config.debug_blocks) &&
868 !smc_notify_free (mem_block, mem_size))
870 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
872 ThreadMemory *tmem = thread_memory_from_self();
873 guint ix = SLAB_INDEX (allocator, chunk_size);
874 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
876 thread_memory_swap_magazines (tmem, ix);
877 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
878 thread_memory_magazine2_unload (tmem, ix);
880 if (G_UNLIKELY (g_mem_gc_friendly))
881 memset (mem_block, 0, chunk_size);
882 thread_memory_magazine2_free (tmem, ix, mem_block);
884 else if (acat == 2) /* allocate through slab allocator */
886 if (G_UNLIKELY (g_mem_gc_friendly))
887 memset (mem_block, 0, chunk_size);
888 g_mutex_lock (allocator->slab_mutex);
889 slab_allocator_free_chunk (chunk_size, mem_block);
890 g_mutex_unlock (allocator->slab_mutex);
892 else /* delegate to system malloc */
894 if (G_UNLIKELY (g_mem_gc_friendly))
895 memset (mem_block, 0, mem_size);
901 g_slice_free_chain_with_offset (gsize mem_size,
905 gpointer slice = mem_chain;
906 /* while the thread magazines and the magazine cache are implemented so that
907 * they can easily be extended to allow for free lists containing more free
908 * lists for the first level nodes, which would allow O(1) freeing in this
909 * function, the benefit of such an extension is questionable, because:
910 * - the magazine size counts will become mere lower bounds which confuses
911 * the code adapting to lock contention;
912 * - freeing a single node to the thread magazines is very fast, so this
913 * O(list_length) operation is multiplied by a fairly small factor;
914 * - memory usage histograms on larger applications seem to indicate that
915 * the amount of released multi node lists is negligible in comparison
916 * to single node releases.
917 * - the major performance bottle neck, namely g_private_get() or
918 * g_mutex_lock()/g_mutex_unlock() has already been moved out of the
919 * inner loop for freeing chained slices.
921 gsize chunk_size = P2ALIGN (mem_size);
922 guint acat = allocator_categorize (chunk_size);
923 if (G_LIKELY (acat == 1)) /* allocate through magazine layer */
925 ThreadMemory *tmem = thread_memory_from_self();
926 guint ix = SLAB_INDEX (allocator, chunk_size);
929 guint8 *current = slice;
930 slice = *(gpointer*) (current + next_offset);
931 if (G_UNLIKELY (allocator->config.debug_blocks) &&
932 !smc_notify_free (current, mem_size))
934 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
936 thread_memory_swap_magazines (tmem, ix);
937 if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
938 thread_memory_magazine2_unload (tmem, ix);
940 if (G_UNLIKELY (g_mem_gc_friendly))
941 memset (current, 0, chunk_size);
942 thread_memory_magazine2_free (tmem, ix, current);
945 else if (acat == 2) /* allocate through slab allocator */
947 g_mutex_lock (allocator->slab_mutex);
950 guint8 *current = slice;
951 slice = *(gpointer*) (current + next_offset);
952 if (G_UNLIKELY (allocator->config.debug_blocks) &&
953 !smc_notify_free (current, mem_size))
955 if (G_UNLIKELY (g_mem_gc_friendly))
956 memset (current, 0, chunk_size);
957 slab_allocator_free_chunk (chunk_size, current);
959 g_mutex_unlock (allocator->slab_mutex);
961 else /* delegate to system malloc */
964 guint8 *current = slice;
965 slice = *(gpointer*) (current + next_offset);
966 if (G_UNLIKELY (allocator->config.debug_blocks) &&
967 !smc_notify_free (current, mem_size))
969 if (G_UNLIKELY (g_mem_gc_friendly))
970 memset (current, 0, mem_size);
975 /* --- single page allocator --- */
977 allocator_slab_stack_push (Allocator *allocator,
981 /* insert slab at slab ring head */
982 if (!allocator->slab_stack[ix])
989 SlabInfo *next = allocator->slab_stack[ix], *prev = next->prev;
995 allocator->slab_stack[ix] = sinfo;
999 allocator_aligned_page_size (Allocator *allocator,
1002 gsize val = 1 << g_bit_storage (n_bytes - 1);
1003 val = MAX (val, allocator->min_page_size);
1008 allocator_add_slab (Allocator *allocator,
1014 gsize addr, padding, n_chunks, color = 0;
1015 gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1016 /* allocate 1 page for the chunks and the slab */
1017 gpointer aligned_memory = allocator_memalign (page_size, page_size - NATIVE_MALLOC_PADDING);
1018 guint8 *mem = aligned_memory;
1022 const gchar *syserr = "unknown error";
1024 syserr = strerror (errno);
1026 mem_error ("failed to allocate %u bytes (alignment: %u): %s\n",
1027 (guint) (page_size - NATIVE_MALLOC_PADDING), (guint) page_size, syserr);
1029 /* mask page adress */
1030 addr = ((gsize) mem / page_size) * page_size;
1031 /* assert alignment */
1032 mem_assert (aligned_memory == (gpointer) addr);
1033 /* basic slab info setup */
1034 sinfo = (SlabInfo*) (mem + page_size - SLAB_INFO_SIZE);
1035 sinfo->n_allocated = 0;
1036 sinfo->chunks = NULL;
1037 /* figure cache colorization */
1038 n_chunks = ((guint8*) sinfo - mem) / chunk_size;
1039 padding = ((guint8*) sinfo - mem) - n_chunks * chunk_size;
1042 color = (allocator->color_accu * P2ALIGNMENT) % padding;
1043 allocator->color_accu += allocator->config.color_increment;
1045 /* add chunks to free list */
1046 chunk = (ChunkLink*) (mem + color);
1047 sinfo->chunks = chunk;
1048 for (i = 0; i < n_chunks - 1; i++)
1050 chunk->next = (ChunkLink*) ((guint8*) chunk + chunk_size);
1051 chunk = chunk->next;
1053 chunk->next = NULL; /* last chunk */
1054 /* add slab to slab ring */
1055 allocator_slab_stack_push (allocator, ix, sinfo);
1059 slab_allocator_alloc_chunk (gsize chunk_size)
1062 guint ix = SLAB_INDEX (allocator, chunk_size);
1063 /* ensure non-empty slab */
1064 if (!allocator->slab_stack[ix] || !allocator->slab_stack[ix]->chunks)
1065 allocator_add_slab (allocator, ix, chunk_size);
1066 /* allocate chunk */
1067 chunk = allocator->slab_stack[ix]->chunks;
1068 allocator->slab_stack[ix]->chunks = chunk->next;
1069 allocator->slab_stack[ix]->n_allocated++;
1070 /* rotate empty slabs */
1071 if (!allocator->slab_stack[ix]->chunks)
1072 allocator->slab_stack[ix] = allocator->slab_stack[ix]->next;
1077 slab_allocator_free_chunk (gsize chunk_size,
1082 guint ix = SLAB_INDEX (allocator, chunk_size);
1083 gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1084 gsize addr = ((gsize) mem / page_size) * page_size;
1085 /* mask page adress */
1086 guint8 *page = (guint8*) addr;
1087 SlabInfo *sinfo = (SlabInfo*) (page + page_size - SLAB_INFO_SIZE);
1088 /* assert valid chunk count */
1089 mem_assert (sinfo->n_allocated > 0);
1090 /* add chunk to free list */
1091 was_empty = sinfo->chunks == NULL;
1092 chunk = (ChunkLink*) mem;
1093 chunk->next = sinfo->chunks;
1094 sinfo->chunks = chunk;
1095 sinfo->n_allocated--;
1096 /* keep slab ring partially sorted, empty slabs at end */
1100 SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1103 if (allocator->slab_stack[ix] == sinfo)
1104 allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1105 /* insert slab at head */
1106 allocator_slab_stack_push (allocator, ix, sinfo);
1108 /* eagerly free complete unused slabs */
1109 if (!sinfo->n_allocated)
1112 SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1115 if (allocator->slab_stack[ix] == sinfo)
1116 allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1118 allocator_memfree (page_size, page);
1122 /* --- memalign implementation --- */
1123 #ifdef HAVE_MALLOC_H
1124 #include <malloc.h> /* memalign() */
1128 * define HAVE_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works, <stdlib.h>
1129 * define HAVE_COMPLIANT_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works for sizes != 2^n, <stdlib.h>
1130 * define HAVE_MEMALIGN 1 // if free(memalign(3)) works, <malloc.h>
1131 * define HAVE_VALLOC 1 // if free(valloc(3)) works, <stdlib.h> or <malloc.h>
1132 * if none is provided, we implement malloc(3)-based alloc-only page alignment
1135 #if !(HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC)
1136 static GTrashStack *compat_valloc_trash = NULL;
1140 allocator_memalign (gsize alignment,
1143 gpointer aligned_memory = NULL;
1145 #if HAVE_COMPLIANT_POSIX_MEMALIGN
1146 err = posix_memalign (&aligned_memory, alignment, memsize);
1149 aligned_memory = memalign (alignment, memsize);
1153 aligned_memory = valloc (memsize);
1156 /* simplistic non-freeing page allocator */
1157 mem_assert (alignment == sys_page_size);
1158 mem_assert (memsize <= sys_page_size);
1159 if (!compat_valloc_trash)
1161 const guint n_pages = 16;
1162 guint8 *mem = malloc (n_pages * sys_page_size);
1167 guint8 *amem = (guint8*) ALIGN ((gsize) mem, sys_page_size);
1169 i--; /* mem wasn't page aligned */
1171 g_trash_stack_push (&compat_valloc_trash, amem + i * sys_page_size);
1174 aligned_memory = g_trash_stack_pop (&compat_valloc_trash);
1176 if (!aligned_memory)
1178 return aligned_memory;
1182 allocator_memfree (gsize memsize,
1185 #if HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC
1188 mem_assert (memsize <= sys_page_size);
1189 g_trash_stack_push (&compat_valloc_trash, mem);
1194 mem_error (const char *format,
1199 /* at least, put out "MEMORY-ERROR", in case we segfault during the rest of the function */
1200 fputs ("\n***MEMORY-ERROR***: ", stderr);
1201 pname = g_get_prgname();
1202 fprintf (stderr, "%s[%ld]: GSlice: ", pname ? pname : "", (long)getpid());
1203 va_start (args, format);
1204 vfprintf (stderr, format, args);
1206 fputs ("\n", stderr);
1211 /* --- g-slice memory checker tree --- */
1212 typedef size_t SmcKType; /* key type */
1213 typedef size_t SmcVType; /* value type */
1218 static void smc_tree_insert (SmcKType key,
1220 static gboolean smc_tree_lookup (SmcKType key,
1222 static gboolean smc_tree_remove (SmcKType key);
1225 /* --- g-slice memory checker implementation --- */
1227 smc_notify_alloc (void *pointer,
1230 size_t adress = (size_t) pointer;
1232 smc_tree_insert (adress, size);
1237 smc_notify_ignore (void *pointer)
1239 size_t adress = (size_t) pointer;
1241 smc_tree_remove (adress);
1246 smc_notify_free (void *pointer,
1249 size_t adress = (size_t) pointer;
1254 return 1; /* ignore */
1255 found_one = smc_tree_lookup (adress, &real_size);
1258 fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1261 if (real_size != size && (real_size || size))
1263 fprintf (stderr, "GSlice: MemChecker: attempt to release block with invalid size: %p size=%" G_GSIZE_FORMAT " invalid-size=%" G_GSIZE_FORMAT "\n", pointer, real_size, size);
1266 if (!smc_tree_remove (adress))
1268 fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1271 return 1; /* all fine */
1274 /* --- g-slice memory checker tree implementation --- */
1275 #define SMC_TRUNK_COUNT (4093 /* 16381 */) /* prime, to distribute trunk collisions (big, allocated just once) */
1276 #define SMC_BRANCH_COUNT (511) /* prime, to distribute branch collisions */
1277 #define SMC_TRUNK_EXTENT (SMC_BRANCH_COUNT * 2039) /* key adress space per trunk, should distribute uniformly across BRANCH_COUNT */
1278 #define SMC_TRUNK_HASH(k) ((k / SMC_TRUNK_EXTENT) % SMC_TRUNK_COUNT) /* generate new trunk hash per megabyte (roughly) */
1279 #define SMC_BRANCH_HASH(k) (k % SMC_BRANCH_COUNT)
1283 unsigned int n_entries;
1286 static SmcBranch **smc_tree_root = NULL;
1289 smc_tree_abort (int errval)
1291 const char *syserr = "unknown error";
1293 syserr = strerror (errval);
1295 mem_error ("MemChecker: failure in debugging tree: %s", syserr);
1298 static inline SmcEntry*
1299 smc_tree_branch_grow_L (SmcBranch *branch,
1302 unsigned int old_size = branch->n_entries * sizeof (branch->entries[0]);
1303 unsigned int new_size = old_size + sizeof (branch->entries[0]);
1305 mem_assert (index <= branch->n_entries);
1306 branch->entries = (SmcEntry*) realloc (branch->entries, new_size);
1307 if (!branch->entries)
1308 smc_tree_abort (errno);
1309 entry = branch->entries + index;
1310 g_memmove (entry + 1, entry, (branch->n_entries - index) * sizeof (entry[0]));
1311 branch->n_entries += 1;
1315 static inline SmcEntry*
1316 smc_tree_branch_lookup_nearest_L (SmcBranch *branch,
1319 unsigned int n_nodes = branch->n_entries, offs = 0;
1320 SmcEntry *check = branch->entries;
1322 while (offs < n_nodes)
1324 unsigned int i = (offs + n_nodes) >> 1;
1325 check = branch->entries + i;
1326 cmp = key < check->key ? -1 : key != check->key;
1328 return check; /* return exact match */
1331 else /* (cmp > 0) */
1334 /* check points at last mismatch, cmp > 0 indicates greater key */
1335 return cmp > 0 ? check + 1 : check; /* return insertion position for inexact match */
1339 smc_tree_insert (SmcKType key,
1342 unsigned int ix0, ix1;
1345 g_mutex_lock (smc_tree_mutex);
1346 ix0 = SMC_TRUNK_HASH (key);
1347 ix1 = SMC_BRANCH_HASH (key);
1350 smc_tree_root = calloc (SMC_TRUNK_COUNT, sizeof (smc_tree_root[0]));
1352 smc_tree_abort (errno);
1354 if (!smc_tree_root[ix0])
1356 smc_tree_root[ix0] = calloc (SMC_BRANCH_COUNT, sizeof (smc_tree_root[0][0]));
1357 if (!smc_tree_root[ix0])
1358 smc_tree_abort (errno);
1360 entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1361 if (!entry || /* need create */
1362 entry >= smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries || /* need append */
1363 entry->key != key) /* need insert */
1364 entry = smc_tree_branch_grow_L (&smc_tree_root[ix0][ix1], entry - smc_tree_root[ix0][ix1].entries);
1366 entry->value = value;
1367 g_mutex_unlock (smc_tree_mutex);
1371 smc_tree_lookup (SmcKType key,
1374 SmcEntry *entry = NULL;
1375 unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1376 gboolean found_one = FALSE;
1378 g_mutex_lock (smc_tree_mutex);
1379 if (smc_tree_root && smc_tree_root[ix0])
1381 entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1383 entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1387 *value_p = entry->value;
1390 g_mutex_unlock (smc_tree_mutex);
1395 smc_tree_remove (SmcKType key)
1397 unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1398 gboolean found_one = FALSE;
1399 g_mutex_lock (smc_tree_mutex);
1400 if (smc_tree_root && smc_tree_root[ix0])
1402 SmcEntry *entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1404 entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1407 unsigned int i = entry - smc_tree_root[ix0][ix1].entries;
1408 smc_tree_root[ix0][ix1].n_entries -= 1;
1409 g_memmove (entry, entry + 1, (smc_tree_root[ix0][ix1].n_entries - i) * sizeof (entry[0]));
1410 if (!smc_tree_root[ix0][ix1].n_entries)
1412 /* avoid useless pressure on the memory system */
1413 free (smc_tree_root[ix0][ix1].entries);
1414 smc_tree_root[ix0][ix1].entries = NULL;
1419 g_mutex_unlock (smc_tree_mutex);
1423 #ifdef G_ENABLE_DEBUG
1425 g_slice_debug_tree_statistics (void)
1427 g_mutex_lock (smc_tree_mutex);
1430 unsigned int i, j, t = 0, o = 0, b = 0, su = 0, ex = 0, en = 4294967295u;
1432 for (i = 0; i < SMC_TRUNK_COUNT; i++)
1433 if (smc_tree_root[i])
1436 for (j = 0; j < SMC_BRANCH_COUNT; j++)
1437 if (smc_tree_root[i][j].n_entries)
1440 su += smc_tree_root[i][j].n_entries;
1441 en = MIN (en, smc_tree_root[i][j].n_entries);
1442 ex = MAX (ex, smc_tree_root[i][j].n_entries);
1444 else if (smc_tree_root[i][j].entries)
1445 o++; /* formerly used, now empty */
1448 tf = MAX (t, 1.0); /* max(1) to be a valid divisor */
1449 bf = MAX (b, 1.0); /* max(1) to be a valid divisor */
1450 fprintf (stderr, "GSlice: MemChecker: %u trunks, %u branches, %u old branches\n", t, b, o);
1451 fprintf (stderr, "GSlice: MemChecker: %f branches per trunk, %.2f%% utilization\n",
1453 100.0 - (SMC_BRANCH_COUNT - b / tf) / (0.01 * SMC_BRANCH_COUNT));
1454 fprintf (stderr, "GSlice: MemChecker: %f entries per branch, %u minimum, %u maximum\n",
1458 fprintf (stderr, "GSlice: MemChecker: root=NULL\n");
1459 g_mutex_unlock (smc_tree_mutex);
1461 /* sample statistics (beast + GSLice + 24h scripted core & GUI activity):
1462 * PID %CPU %MEM VSZ RSS COMMAND
1463 * 8887 30.3 45.8 456068 414856 beast-0.7.1 empty.bse
1464 * $ cat /proc/8887/statm # total-program-size resident-set-size shared-pages text/code data/stack library dirty-pages
1465 * 114017 103714 2354 344 0 108676 0
1466 * $ cat /proc/8887/status
1477 * (gdb) print g_slice_debug_tree_statistics ()
1478 * GSlice: MemChecker: 422 trunks, 213068 branches, 0 old branches
1479 * GSlice: MemChecker: 504.900474 branches per trunk, 98.81% utilization
1480 * GSlice: MemChecker: 4.965039 entries per branch, 1 minimum, 37 maximum
1483 #endif /* G_ENABLE_DEBUG */
1485 #define __G_SLICE_C__
1486 #include "galiasdef.c"