Convert external links to markdown syntax
[platform/upstream/glib.git] / glib / gslice.c
1 /* GLIB sliced memory - fast concurrent memory chunk allocator
2  * Copyright (C) 2005 Tim Janik
3  *
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.
8  *
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.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 /* MT safe */
18
19 #include "config.h"
20 #include "glibconfig.h"
21
22 #if     defined HAVE_POSIX_MEMALIGN && defined POSIX_MEMALIGN_WITH_COMPLIANT_ALLOCS
23 #  define HAVE_COMPLIANT_POSIX_MEMALIGN 1
24 #endif
25
26 #if defined(HAVE_COMPLIANT_POSIX_MEMALIGN) && !defined(_XOPEN_SOURCE)
27 #define _XOPEN_SOURCE 600       /* posix_memalign() */
28 #endif
29 #include <stdlib.h>             /* posix_memalign() */
30 #include <string.h>
31 #include <errno.h>
32
33 #ifdef G_OS_UNIX
34 #include <unistd.h>             /* sysconf() */
35 #endif
36 #ifdef G_OS_WIN32
37 #include <windows.h>
38 #include <process.h>
39 #endif
40
41 #include <stdio.h>              /* fputs/fprintf */
42
43 #include "gslice.h"
44
45 #include "gmain.h"
46 #include "gmem.h"               /* gslice.h */
47 #include "gstrfuncs.h"
48 #include "gutils.h"
49 #include "gtrashstack.h"
50 #include "gtestutils.h"
51 #include "gthread.h"
52 #include "glib_trace.h"
53
54 #include "valgrind.h"
55
56 /**
57  * SECTION:memory_slices
58  * @title: Memory Slices
59  * @short_description: efficient way to allocate groups of equal-sized
60  *     chunks of memory
61  *
62  * Memory slices provide a space-efficient and multi-processing scalable
63  * way to allocate equal-sized pieces of memory, just like the original
64  * #GMemChunks (from GLib 2.8), while avoiding their excessive
65  * memory-waste, scalability and performance problems.
66  *
67  * To achieve these goals, the slice allocator uses a sophisticated,
68  * layered design that has been inspired by Bonwick's slab allocator
69  * ([Bonwick94](http://citeseer.ist.psu.edu/bonwick94slab.html)
70  * Jeff Bonwick, The slab allocator: An object-caching kernel
71  * memory allocator. USENIX 1994, and
72  * [Bonwick01](http://citeseer.ist.psu.edu/bonwick01magazines.html)
73  * Bonwick and Jonathan Adams, Magazines and vmem: Extending the
74  * slab allocator to many cpu's and arbitrary resources. USENIX 2001)
75  *
76  * It uses posix_memalign() to optimize allocations of many equally-sized
77  * chunks, and has per-thread free lists (the so-called magazine layer)
78  * to quickly satisfy allocation requests of already known structure sizes.
79  * This is accompanied by extra caching logic to keep freed memory around
80  * for some time before returning it to the system. Memory that is unused
81  * due to alignment constraints is used for cache colorization (random
82  * distribution of chunk addresses) to improve CPU cache utilization. The
83  * caching layer of the slice allocator adapts itself to high lock contention
84  * to improve scalability.
85  *
86  * The slice allocator can allocate blocks as small as two pointers, and
87  * unlike malloc(), it does not reserve extra space per block. For large block
88  * sizes, g_slice_new() and g_slice_alloc() will automatically delegate to the
89  * system malloc() implementation. For newly written code it is recommended
90  * to use the new <literal>g_slice</literal> API instead of g_malloc() and
91  * friends, as long as objects are not resized during their lifetime and the
92  * object size used at allocation time is still available when freeing.
93  *
94  * Here is an example for using the slice allocator:
95  * |[<!-- language="C" --> 
96  * gchar *mem[10000];
97  * gint i;
98  *
99  * /&ast; Allocate 10000 blocks. &ast;/
100  * for (i = 0; i < 10000; i++)
101  *   {
102  *     mem[i] = g_slice_alloc (50);
103  *
104  *     /&ast; Fill in the memory with some junk. &ast;/
105  *     for (j = 0; j < 50; j++)
106  *       mem[i][j] = i * j;
107  *   }
108  *
109  * /&ast; Now free all of the blocks. &ast;/
110  * for (i = 0; i < 10000; i++)
111  *   g_slice_free1 (50, mem[i]);
112  * ]|
113  *
114  * And here is an example for using the using the slice allocator
115  * with data structures:
116  * |[<!-- language="C" --> 
117  * GRealArray *array;
118  *
119  * /&ast; Allocate one block, using the g_slice_new() macro. &ast;/
120  * array = g_slice_new (GRealArray);
121
122  * /&ast; We can now use array just like a normal pointer to a structure. &ast;/
123  * array->data            = NULL;
124  * array->len             = 0;
125  * array->alloc           = 0;
126  * array->zero_terminated = (zero_terminated ? 1 : 0);
127  * array->clear           = (clear ? 1 : 0);
128  * array->elt_size        = elt_size;
129  *
130  * /&ast; We can free the block, so it can be reused. &ast;/
131  * g_slice_free (GRealArray, array);
132  * ]|
133  */
134
135 /* the GSlice allocator is split up into 4 layers, roughly modelled after the slab
136  * allocator and magazine extensions as outlined in:
137  * + [Bonwick94] Jeff Bonwick, The slab allocator: An object-caching kernel
138  *   memory allocator. USENIX 1994, http://citeseer.ist.psu.edu/bonwick94slab.html
139  * + [Bonwick01] Bonwick and Jonathan Adams, Magazines and vmem: Extending the
140  *   slab allocator to many cpu's and arbitrary resources.
141  *   USENIX 2001, http://citeseer.ist.psu.edu/bonwick01magazines.html
142  * the layers are:
143  * - the thread magazines. for each (aligned) chunk size, a magazine (a list)
144  *   of recently freed and soon to be allocated chunks is maintained per thread.
145  *   this way, most alloc/free requests can be quickly satisfied from per-thread
146  *   free lists which only require one g_private_get() call to retrive the
147  *   thread handle.
148  * - the magazine cache. allocating and freeing chunks to/from threads only
149  *   occours at magazine sizes from a global depot of magazines. the depot
150  *   maintaines a 15 second working set of allocated magazines, so full
151  *   magazines are not allocated and released too often.
152  *   the chunk size dependent magazine sizes automatically adapt (within limits,
153  *   see [3]) to lock contention to properly scale performance across a variety
154  *   of SMP systems.
155  * - the slab allocator. this allocator allocates slabs (blocks of memory) close
156  *   to the system page size or multiples thereof which have to be page aligned.
157  *   the blocks are divided into smaller chunks which are used to satisfy
158  *   allocations from the upper layers. the space provided by the reminder of
159  *   the chunk size division is used for cache colorization (random distribution
160  *   of chunk addresses) to improve processor cache utilization. multiple slabs
161  *   with the same chunk size are kept in a partially sorted ring to allow O(1)
162  *   freeing and allocation of chunks (as long as the allocation of an entirely
163  *   new slab can be avoided).
164  * - the page allocator. on most modern systems, posix_memalign(3) or
165  *   memalign(3) should be available, so this is used to allocate blocks with
166  *   system page size based alignments and sizes or multiples thereof.
167  *   if no memalign variant is provided, valloc() is used instead and
168  *   block sizes are limited to the system page size (no multiples thereof).
169  *   as a fallback, on system without even valloc(), a malloc(3)-based page
170  *   allocator with alloc-only behaviour is used.
171  *
172  * NOTES:
173  * [1] some systems memalign(3) implementations may rely on boundary tagging for
174  *     the handed out memory chunks. to avoid excessive page-wise fragmentation,
175  *     we reserve 2 * sizeof (void*) per block size for the systems memalign(3),
176  *     specified in NATIVE_MALLOC_PADDING.
177  * [2] using the slab allocator alone already provides for a fast and efficient
178  *     allocator, it doesn't properly scale beyond single-threaded uses though.
179  *     also, the slab allocator implements eager free(3)-ing, i.e. does not
180  *     provide any form of caching or working set maintenance. so if used alone,
181  *     it's vulnerable to trashing for sequences of balanced (alloc, free) pairs
182  *     at certain thresholds.
183  * [3] magazine sizes are bound by an implementation specific minimum size and
184  *     a chunk size specific maximum to limit magazine storage sizes to roughly
185  *     16KB.
186  * [4] allocating ca. 8 chunks per block/page keeps a good balance between
187  *     external and internal fragmentation (<= 12.5%). [Bonwick94]
188  */
189
190 /* --- macros and constants --- */
191 #define LARGEALIGNMENT          (256)
192 #define P2ALIGNMENT             (2 * sizeof (gsize))                            /* fits 2 pointers (assumed to be 2 * GLIB_SIZEOF_SIZE_T below) */
193 #define ALIGN(size, base)       ((base) * (gsize) (((size) + (base) - 1) / (base)))
194 #define NATIVE_MALLOC_PADDING   P2ALIGNMENT                                     /* per-page padding left for native malloc(3) see [1] */
195 #define SLAB_INFO_SIZE          P2ALIGN (sizeof (SlabInfo) + NATIVE_MALLOC_PADDING)
196 #define MAX_MAGAZINE_SIZE       (256)                                           /* see [3] and allocator_get_magazine_threshold() for this */
197 #define MIN_MAGAZINE_SIZE       (4)
198 #define MAX_STAMP_COUNTER       (7)                                             /* distributes the load of gettimeofday() */
199 #define MAX_SLAB_CHUNK_SIZE(al) (((al)->max_page_size - SLAB_INFO_SIZE) / 8)    /* we want at last 8 chunks per page, see [4] */
200 #define MAX_SLAB_INDEX(al)      (SLAB_INDEX (al, MAX_SLAB_CHUNK_SIZE (al)) + 1)
201 #define SLAB_INDEX(al, asize)   ((asize) / P2ALIGNMENT - 1)                     /* asize must be P2ALIGNMENT aligned */
202 #define SLAB_CHUNK_SIZE(al, ix) (((ix) + 1) * P2ALIGNMENT)
203 #define SLAB_BPAGE_SIZE(al,csz) (8 * (csz) + SLAB_INFO_SIZE)
204
205 /* optimized version of ALIGN (size, P2ALIGNMENT) */
206 #if     GLIB_SIZEOF_SIZE_T * 2 == 8  /* P2ALIGNMENT */
207 #define P2ALIGN(size)   (((size) + 0x7) & ~(gsize) 0x7)
208 #elif   GLIB_SIZEOF_SIZE_T * 2 == 16 /* P2ALIGNMENT */
209 #define P2ALIGN(size)   (((size) + 0xf) & ~(gsize) 0xf)
210 #else
211 #define P2ALIGN(size)   ALIGN (size, P2ALIGNMENT)
212 #endif
213
214 /* special helpers to avoid gmessage.c dependency */
215 static void mem_error (const char *format, ...) G_GNUC_PRINTF (1,2);
216 #define mem_assert(cond)    do { if (G_LIKELY (cond)) ; else mem_error ("assertion failed: %s", #cond); } while (0)
217
218 /* --- structures --- */
219 typedef struct _ChunkLink      ChunkLink;
220 typedef struct _SlabInfo       SlabInfo;
221 typedef struct _CachedMagazine CachedMagazine;
222 struct _ChunkLink {
223   ChunkLink *next;
224   ChunkLink *data;
225 };
226 struct _SlabInfo {
227   ChunkLink *chunks;
228   guint n_allocated;
229   SlabInfo *next, *prev;
230 };
231 typedef struct {
232   ChunkLink *chunks;
233   gsize      count;                     /* approximative chunks list length */
234 } Magazine;
235 typedef struct {
236   Magazine   *magazine1;                /* array of MAX_SLAB_INDEX (allocator) */
237   Magazine   *magazine2;                /* array of MAX_SLAB_INDEX (allocator) */
238 } ThreadMemory;
239 typedef struct {
240   gboolean always_malloc;
241   gboolean bypass_magazines;
242   gboolean debug_blocks;
243   gsize    working_set_msecs;
244   guint    color_increment;
245 } SliceConfig;
246 typedef struct {
247   /* const after initialization */
248   gsize         min_page_size, max_page_size;
249   SliceConfig   config;
250   gsize         max_slab_chunk_size_for_magazine_cache;
251   /* magazine cache */
252   GMutex        magazine_mutex;
253   ChunkLink   **magazines;                /* array of MAX_SLAB_INDEX (allocator) */
254   guint        *contention_counters;      /* array of MAX_SLAB_INDEX (allocator) */
255   gint          mutex_counter;
256   guint         stamp_counter;
257   guint         last_stamp;
258   /* slab allocator */
259   GMutex        slab_mutex;
260   SlabInfo    **slab_stack;                /* array of MAX_SLAB_INDEX (allocator) */
261   guint        color_accu;
262 } Allocator;
263
264 /* --- g-slice prototypes --- */
265 static gpointer     slab_allocator_alloc_chunk       (gsize      chunk_size);
266 static void         slab_allocator_free_chunk        (gsize      chunk_size,
267                                                       gpointer   mem);
268 static void         private_thread_memory_cleanup    (gpointer   data);
269 static gpointer     allocator_memalign               (gsize      alignment,
270                                                       gsize      memsize);
271 static void         allocator_memfree                (gsize      memsize,
272                                                       gpointer   mem);
273 static inline void  magazine_cache_update_stamp      (void);
274 static inline gsize allocator_get_magazine_threshold (Allocator *allocator,
275                                                       guint      ix);
276
277 /* --- g-slice memory checker --- */
278 static void     smc_notify_alloc  (void   *pointer,
279                                    size_t  size);
280 static int      smc_notify_free   (void   *pointer,
281                                    size_t  size);
282
283 /* --- variables --- */
284 static GPrivate    private_thread_memory = G_PRIVATE_INIT (private_thread_memory_cleanup);
285 static gsize       sys_page_size = 0;
286 static Allocator   allocator[1] = { { 0, }, };
287 static SliceConfig slice_config = {
288   FALSE,        /* always_malloc */
289   FALSE,        /* bypass_magazines */
290   FALSE,        /* debug_blocks */
291   15 * 1000,    /* working_set_msecs */
292   1,            /* color increment, alt: 0x7fffffff */
293 };
294 static GMutex      smc_tree_mutex; /* mutex for G_SLICE=debug-blocks */
295
296 /* --- auxiliary funcitons --- */
297 void
298 g_slice_set_config (GSliceConfig ckey,
299                     gint64       value)
300 {
301   g_return_if_fail (sys_page_size == 0);
302   switch (ckey)
303     {
304     case G_SLICE_CONFIG_ALWAYS_MALLOC:
305       slice_config.always_malloc = value != 0;
306       break;
307     case G_SLICE_CONFIG_BYPASS_MAGAZINES:
308       slice_config.bypass_magazines = value != 0;
309       break;
310     case G_SLICE_CONFIG_WORKING_SET_MSECS:
311       slice_config.working_set_msecs = value;
312       break;
313     case G_SLICE_CONFIG_COLOR_INCREMENT:
314       slice_config.color_increment = value;
315     default: ;
316     }
317 }
318
319 gint64
320 g_slice_get_config (GSliceConfig ckey)
321 {
322   switch (ckey)
323     {
324     case G_SLICE_CONFIG_ALWAYS_MALLOC:
325       return slice_config.always_malloc;
326     case G_SLICE_CONFIG_BYPASS_MAGAZINES:
327       return slice_config.bypass_magazines;
328     case G_SLICE_CONFIG_WORKING_SET_MSECS:
329       return slice_config.working_set_msecs;
330     case G_SLICE_CONFIG_CHUNK_SIZES:
331       return MAX_SLAB_INDEX (allocator);
332     case G_SLICE_CONFIG_COLOR_INCREMENT:
333       return slice_config.color_increment;
334     default:
335       return 0;
336     }
337 }
338
339 gint64*
340 g_slice_get_config_state (GSliceConfig ckey,
341                           gint64       address,
342                           guint       *n_values)
343 {
344   guint i = 0;
345   g_return_val_if_fail (n_values != NULL, NULL);
346   *n_values = 0;
347   switch (ckey)
348     {
349       gint64 array[64];
350     case G_SLICE_CONFIG_CONTENTION_COUNTER:
351       array[i++] = SLAB_CHUNK_SIZE (allocator, address);
352       array[i++] = allocator->contention_counters[address];
353       array[i++] = allocator_get_magazine_threshold (allocator, address);
354       *n_values = i;
355       return g_memdup (array, sizeof (array[0]) * *n_values);
356     default:
357       return NULL;
358     }
359 }
360
361 static void
362 slice_config_init (SliceConfig *config)
363 {
364   const gchar *val;
365
366   *config = slice_config;
367
368   val = getenv ("G_SLICE");
369   if (val != NULL)
370     {
371       gint flags;
372       const GDebugKey keys[] = {
373         { "always-malloc", 1 << 0 },
374         { "debug-blocks",  1 << 1 },
375       };
376
377       flags = g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
378       if (flags & (1 << 0))
379         config->always_malloc = TRUE;
380       if (flags & (1 << 1))
381         config->debug_blocks = TRUE;
382     }
383   else
384     {
385       /* G_SLICE was not specified, so check if valgrind is running and
386        * disable ourselves if it is.
387        *
388        * This way it's possible to force gslice to be enabled under
389        * valgrind just by setting G_SLICE to the empty string.
390        */
391       if (RUNNING_ON_VALGRIND)
392         config->always_malloc = TRUE;
393     }
394 }
395
396 static void
397 g_slice_init_nomessage (void)
398 {
399   /* we may not use g_error() or friends here */
400   mem_assert (sys_page_size == 0);
401   mem_assert (MIN_MAGAZINE_SIZE >= 4);
402
403 #ifdef G_OS_WIN32
404   {
405     SYSTEM_INFO system_info;
406     GetSystemInfo (&system_info);
407     sys_page_size = system_info.dwPageSize;
408   }
409 #else
410   sys_page_size = sysconf (_SC_PAGESIZE); /* = sysconf (_SC_PAGE_SIZE); = getpagesize(); */
411 #endif
412   mem_assert (sys_page_size >= 2 * LARGEALIGNMENT);
413   mem_assert ((sys_page_size & (sys_page_size - 1)) == 0);
414   slice_config_init (&allocator->config);
415   allocator->min_page_size = sys_page_size;
416 #if HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN
417   /* allow allocation of pages up to 8KB (with 8KB alignment).
418    * this is useful because many medium to large sized structures
419    * fit less than 8 times (see [4]) into 4KB pages.
420    * we allow very small page sizes here, to reduce wastage in
421    * threads if only small allocations are required (this does
422    * bear the risk of increasing allocation times and fragmentation
423    * though).
424    */
425   allocator->min_page_size = MAX (allocator->min_page_size, 4096);
426   allocator->max_page_size = MAX (allocator->min_page_size, 8192);
427   allocator->min_page_size = MIN (allocator->min_page_size, 128);
428 #else
429   /* we can only align to system page size */
430   allocator->max_page_size = sys_page_size;
431 #endif
432   if (allocator->config.always_malloc)
433     {
434       allocator->contention_counters = NULL;
435       allocator->magazines = NULL;
436       allocator->slab_stack = NULL;
437     }
438   else
439     {
440       allocator->contention_counters = g_new0 (guint, MAX_SLAB_INDEX (allocator));
441       allocator->magazines = g_new0 (ChunkLink*, MAX_SLAB_INDEX (allocator));
442       allocator->slab_stack = g_new0 (SlabInfo*, MAX_SLAB_INDEX (allocator));
443     }
444
445   allocator->mutex_counter = 0;
446   allocator->stamp_counter = MAX_STAMP_COUNTER; /* force initial update */
447   allocator->last_stamp = 0;
448   allocator->color_accu = 0;
449   magazine_cache_update_stamp();
450   /* values cached for performance reasons */
451   allocator->max_slab_chunk_size_for_magazine_cache = MAX_SLAB_CHUNK_SIZE (allocator);
452   if (allocator->config.always_malloc || allocator->config.bypass_magazines)
453     allocator->max_slab_chunk_size_for_magazine_cache = 0;      /* non-optimized cases */
454 }
455
456 static inline guint
457 allocator_categorize (gsize aligned_chunk_size)
458 {
459   /* speed up the likely path */
460   if (G_LIKELY (aligned_chunk_size && aligned_chunk_size <= allocator->max_slab_chunk_size_for_magazine_cache))
461     return 1;           /* use magazine cache */
462
463   if (!allocator->config.always_malloc &&
464       aligned_chunk_size &&
465       aligned_chunk_size <= MAX_SLAB_CHUNK_SIZE (allocator))
466     {
467       if (allocator->config.bypass_magazines)
468         return 2;       /* use slab allocator, see [2] */
469       return 1;         /* use magazine cache */
470     }
471   return 0;             /* use malloc() */
472 }
473
474 static inline void
475 g_mutex_lock_a (GMutex *mutex,
476                 guint  *contention_counter)
477 {
478   gboolean contention = FALSE;
479   if (!g_mutex_trylock (mutex))
480     {
481       g_mutex_lock (mutex);
482       contention = TRUE;
483     }
484   if (contention)
485     {
486       allocator->mutex_counter++;
487       if (allocator->mutex_counter >= 1)        /* quickly adapt to contention */
488         {
489           allocator->mutex_counter = 0;
490           *contention_counter = MIN (*contention_counter + 1, MAX_MAGAZINE_SIZE);
491         }
492     }
493   else /* !contention */
494     {
495       allocator->mutex_counter--;
496       if (allocator->mutex_counter < -11)       /* moderately recover magazine sizes */
497         {
498           allocator->mutex_counter = 0;
499           *contention_counter = MAX (*contention_counter, 1) - 1;
500         }
501     }
502 }
503
504 static inline ThreadMemory*
505 thread_memory_from_self (void)
506 {
507   ThreadMemory *tmem = g_private_get (&private_thread_memory);
508   if (G_UNLIKELY (!tmem))
509     {
510       static GMutex init_mutex;
511       guint n_magazines;
512
513       g_mutex_lock (&init_mutex);
514       if G_UNLIKELY (sys_page_size == 0)
515         g_slice_init_nomessage ();
516       g_mutex_unlock (&init_mutex);
517
518       n_magazines = MAX_SLAB_INDEX (allocator);
519       tmem = g_malloc0 (sizeof (ThreadMemory) + sizeof (Magazine) * 2 * n_magazines);
520       tmem->magazine1 = (Magazine*) (tmem + 1);
521       tmem->magazine2 = &tmem->magazine1[n_magazines];
522       g_private_set (&private_thread_memory, tmem);
523     }
524   return tmem;
525 }
526
527 static inline ChunkLink*
528 magazine_chain_pop_head (ChunkLink **magazine_chunks)
529 {
530   /* magazine chains are linked via ChunkLink->next.
531    * each ChunkLink->data of the toplevel chain may point to a subchain,
532    * linked via ChunkLink->next. ChunkLink->data of the subchains just
533    * contains uninitialized junk.
534    */
535   ChunkLink *chunk = (*magazine_chunks)->data;
536   if (G_UNLIKELY (chunk))
537     {
538       /* allocating from freed list */
539       (*magazine_chunks)->data = chunk->next;
540     }
541   else
542     {
543       chunk = *magazine_chunks;
544       *magazine_chunks = chunk->next;
545     }
546   return chunk;
547 }
548
549 #if 0 /* useful for debugging */
550 static guint
551 magazine_count (ChunkLink *head)
552 {
553   guint count = 0;
554   if (!head)
555     return 0;
556   while (head)
557     {
558       ChunkLink *child = head->data;
559       count += 1;
560       for (child = head->data; child; child = child->next)
561         count += 1;
562       head = head->next;
563     }
564   return count;
565 }
566 #endif
567
568 static inline gsize
569 allocator_get_magazine_threshold (Allocator *allocator,
570                                   guint      ix)
571 {
572   /* the magazine size calculated here has a lower bound of MIN_MAGAZINE_SIZE,
573    * which is required by the implementation. also, for moderately sized chunks
574    * (say >= 64 bytes), magazine sizes shouldn't be much smaller then the number
575    * of chunks available per page/2 to avoid excessive traffic in the magazine
576    * cache for small to medium sized structures.
577    * the upper bound of the magazine size is effectively provided by
578    * MAX_MAGAZINE_SIZE. for larger chunks, this number is scaled down so that
579    * the content of a single magazine doesn't exceed ca. 16KB.
580    */
581   gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
582   guint threshold = MAX (MIN_MAGAZINE_SIZE, allocator->max_page_size / MAX (5 * chunk_size, 5 * 32));
583   guint contention_counter = allocator->contention_counters[ix];
584   if (G_UNLIKELY (contention_counter))  /* single CPU bias */
585     {
586       /* adapt contention counter thresholds to chunk sizes */
587       contention_counter = contention_counter * 64 / chunk_size;
588       threshold = MAX (threshold, contention_counter);
589     }
590   return threshold;
591 }
592
593 /* --- magazine cache --- */
594 static inline void
595 magazine_cache_update_stamp (void)
596 {
597   if (allocator->stamp_counter >= MAX_STAMP_COUNTER)
598     {
599       GTimeVal tv;
600       g_get_current_time (&tv);
601       allocator->last_stamp = tv.tv_sec * 1000 + tv.tv_usec / 1000; /* milli seconds */
602       allocator->stamp_counter = 0;
603     }
604   else
605     allocator->stamp_counter++;
606 }
607
608 static inline ChunkLink*
609 magazine_chain_prepare_fields (ChunkLink *magazine_chunks)
610 {
611   ChunkLink *chunk1;
612   ChunkLink *chunk2;
613   ChunkLink *chunk3;
614   ChunkLink *chunk4;
615   /* checked upon initialization: mem_assert (MIN_MAGAZINE_SIZE >= 4); */
616   /* ensure a magazine with at least 4 unused data pointers */
617   chunk1 = magazine_chain_pop_head (&magazine_chunks);
618   chunk2 = magazine_chain_pop_head (&magazine_chunks);
619   chunk3 = magazine_chain_pop_head (&magazine_chunks);
620   chunk4 = magazine_chain_pop_head (&magazine_chunks);
621   chunk4->next = magazine_chunks;
622   chunk3->next = chunk4;
623   chunk2->next = chunk3;
624   chunk1->next = chunk2;
625   return chunk1;
626 }
627
628 /* access the first 3 fields of a specially prepared magazine chain */
629 #define magazine_chain_prev(mc)         ((mc)->data)
630 #define magazine_chain_stamp(mc)        ((mc)->next->data)
631 #define magazine_chain_uint_stamp(mc)   GPOINTER_TO_UINT ((mc)->next->data)
632 #define magazine_chain_next(mc)         ((mc)->next->next->data)
633 #define magazine_chain_count(mc)        ((mc)->next->next->next->data)
634
635 static void
636 magazine_cache_trim (Allocator *allocator,
637                      guint      ix,
638                      guint      stamp)
639 {
640   /* g_mutex_lock (allocator->mutex); done by caller */
641   /* trim magazine cache from tail */
642   ChunkLink *current = magazine_chain_prev (allocator->magazines[ix]);
643   ChunkLink *trash = NULL;
644   while (ABS (stamp - magazine_chain_uint_stamp (current)) >= allocator->config.working_set_msecs)
645     {
646       /* unlink */
647       ChunkLink *prev = magazine_chain_prev (current);
648       ChunkLink *next = magazine_chain_next (current);
649       magazine_chain_next (prev) = next;
650       magazine_chain_prev (next) = prev;
651       /* clear special fields, put on trash stack */
652       magazine_chain_next (current) = NULL;
653       magazine_chain_count (current) = NULL;
654       magazine_chain_stamp (current) = NULL;
655       magazine_chain_prev (current) = trash;
656       trash = current;
657       /* fixup list head if required */
658       if (current == allocator->magazines[ix])
659         {
660           allocator->magazines[ix] = NULL;
661           break;
662         }
663       current = prev;
664     }
665   g_mutex_unlock (&allocator->magazine_mutex);
666   /* free trash */
667   if (trash)
668     {
669       const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
670       g_mutex_lock (&allocator->slab_mutex);
671       while (trash)
672         {
673           current = trash;
674           trash = magazine_chain_prev (current);
675           magazine_chain_prev (current) = NULL; /* clear special field */
676           while (current)
677             {
678               ChunkLink *chunk = magazine_chain_pop_head (&current);
679               slab_allocator_free_chunk (chunk_size, chunk);
680             }
681         }
682       g_mutex_unlock (&allocator->slab_mutex);
683     }
684 }
685
686 static void
687 magazine_cache_push_magazine (guint      ix,
688                               ChunkLink *magazine_chunks,
689                               gsize      count) /* must be >= MIN_MAGAZINE_SIZE */
690 {
691   ChunkLink *current = magazine_chain_prepare_fields (magazine_chunks);
692   ChunkLink *next, *prev;
693   g_mutex_lock (&allocator->magazine_mutex);
694   /* add magazine at head */
695   next = allocator->magazines[ix];
696   if (next)
697     prev = magazine_chain_prev (next);
698   else
699     next = prev = current;
700   magazine_chain_next (prev) = current;
701   magazine_chain_prev (next) = current;
702   magazine_chain_prev (current) = prev;
703   magazine_chain_next (current) = next;
704   magazine_chain_count (current) = (gpointer) count;
705   /* stamp magazine */
706   magazine_cache_update_stamp();
707   magazine_chain_stamp (current) = GUINT_TO_POINTER (allocator->last_stamp);
708   allocator->magazines[ix] = current;
709   /* free old magazines beyond a certain threshold */
710   magazine_cache_trim (allocator, ix, allocator->last_stamp);
711   /* g_mutex_unlock (allocator->mutex); was done by magazine_cache_trim() */
712 }
713
714 static ChunkLink*
715 magazine_cache_pop_magazine (guint  ix,
716                              gsize *countp)
717 {
718   g_mutex_lock_a (&allocator->magazine_mutex, &allocator->contention_counters[ix]);
719   if (!allocator->magazines[ix])
720     {
721       guint magazine_threshold = allocator_get_magazine_threshold (allocator, ix);
722       gsize i, chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
723       ChunkLink *chunk, *head;
724       g_mutex_unlock (&allocator->magazine_mutex);
725       g_mutex_lock (&allocator->slab_mutex);
726       head = slab_allocator_alloc_chunk (chunk_size);
727       head->data = NULL;
728       chunk = head;
729       for (i = 1; i < magazine_threshold; i++)
730         {
731           chunk->next = slab_allocator_alloc_chunk (chunk_size);
732           chunk = chunk->next;
733           chunk->data = NULL;
734         }
735       chunk->next = NULL;
736       g_mutex_unlock (&allocator->slab_mutex);
737       *countp = i;
738       return head;
739     }
740   else
741     {
742       ChunkLink *current = allocator->magazines[ix];
743       ChunkLink *prev = magazine_chain_prev (current);
744       ChunkLink *next = magazine_chain_next (current);
745       /* unlink */
746       magazine_chain_next (prev) = next;
747       magazine_chain_prev (next) = prev;
748       allocator->magazines[ix] = next == current ? NULL : next;
749       g_mutex_unlock (&allocator->magazine_mutex);
750       /* clear special fields and hand out */
751       *countp = (gsize) magazine_chain_count (current);
752       magazine_chain_prev (current) = NULL;
753       magazine_chain_next (current) = NULL;
754       magazine_chain_count (current) = NULL;
755       magazine_chain_stamp (current) = NULL;
756       return current;
757     }
758 }
759
760 /* --- thread magazines --- */
761 static void
762 private_thread_memory_cleanup (gpointer data)
763 {
764   ThreadMemory *tmem = data;
765   const guint n_magazines = MAX_SLAB_INDEX (allocator);
766   guint ix;
767   for (ix = 0; ix < n_magazines; ix++)
768     {
769       Magazine *mags[2];
770       guint j;
771       mags[0] = &tmem->magazine1[ix];
772       mags[1] = &tmem->magazine2[ix];
773       for (j = 0; j < 2; j++)
774         {
775           Magazine *mag = mags[j];
776           if (mag->count >= MIN_MAGAZINE_SIZE)
777             magazine_cache_push_magazine (ix, mag->chunks, mag->count);
778           else
779             {
780               const gsize chunk_size = SLAB_CHUNK_SIZE (allocator, ix);
781               g_mutex_lock (&allocator->slab_mutex);
782               while (mag->chunks)
783                 {
784                   ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
785                   slab_allocator_free_chunk (chunk_size, chunk);
786                 }
787               g_mutex_unlock (&allocator->slab_mutex);
788             }
789         }
790     }
791   g_free (tmem);
792 }
793
794 static void
795 thread_memory_magazine1_reload (ThreadMemory *tmem,
796                                 guint         ix)
797 {
798   Magazine *mag = &tmem->magazine1[ix];
799   mem_assert (mag->chunks == NULL); /* ensure that we may reset mag->count */
800   mag->count = 0;
801   mag->chunks = magazine_cache_pop_magazine (ix, &mag->count);
802 }
803
804 static void
805 thread_memory_magazine2_unload (ThreadMemory *tmem,
806                                 guint         ix)
807 {
808   Magazine *mag = &tmem->magazine2[ix];
809   magazine_cache_push_magazine (ix, mag->chunks, mag->count);
810   mag->chunks = NULL;
811   mag->count = 0;
812 }
813
814 static inline void
815 thread_memory_swap_magazines (ThreadMemory *tmem,
816                               guint         ix)
817 {
818   Magazine xmag = tmem->magazine1[ix];
819   tmem->magazine1[ix] = tmem->magazine2[ix];
820   tmem->magazine2[ix] = xmag;
821 }
822
823 static inline gboolean
824 thread_memory_magazine1_is_empty (ThreadMemory *tmem,
825                                   guint         ix)
826 {
827   return tmem->magazine1[ix].chunks == NULL;
828 }
829
830 static inline gboolean
831 thread_memory_magazine2_is_full (ThreadMemory *tmem,
832                                  guint         ix)
833 {
834   return tmem->magazine2[ix].count >= allocator_get_magazine_threshold (allocator, ix);
835 }
836
837 static inline gpointer
838 thread_memory_magazine1_alloc (ThreadMemory *tmem,
839                                guint         ix)
840 {
841   Magazine *mag = &tmem->magazine1[ix];
842   ChunkLink *chunk = magazine_chain_pop_head (&mag->chunks);
843   if (G_LIKELY (mag->count > 0))
844     mag->count--;
845   return chunk;
846 }
847
848 static inline void
849 thread_memory_magazine2_free (ThreadMemory *tmem,
850                               guint         ix,
851                               gpointer      mem)
852 {
853   Magazine *mag = &tmem->magazine2[ix];
854   ChunkLink *chunk = mem;
855   chunk->data = NULL;
856   chunk->next = mag->chunks;
857   mag->chunks = chunk;
858   mag->count++;
859 }
860
861 /* --- API functions --- */
862
863 /**
864  * g_slice_new:
865  * @type: the type to allocate, typically a structure name
866  *
867  * A convenience macro to allocate a block of memory from the
868  * slice allocator.
869  *
870  * It calls g_slice_alloc() with <literal>sizeof (@type)</literal>
871  * and casts the returned pointer to a pointer of the given type,
872  * avoiding a type cast in the source code.
873  * Note that the underlying slice allocation mechanism can
874  * be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
875  * environment variable.
876  *
877  * Returns: a pointer to the allocated block, cast to a pointer to @type
878  *
879  * Since: 2.10
880  */
881
882 /**
883  * g_slice_new0:
884  * @type: the type to allocate, typically a structure name
885  *
886  * A convenience macro to allocate a block of memory from the
887  * slice allocator and set the memory to 0.
888  *
889  * It calls g_slice_alloc0() with <literal>sizeof (@type)</literal>
890  * and casts the returned pointer to a pointer of the given type,
891  * avoiding a type cast in the source code.
892  * Note that the underlying slice allocation mechanism can
893  * be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
894  * environment variable.
895  *
896  * Since: 2.10
897  */
898
899 /**
900  * g_slice_dup:
901  * @type: the type to duplicate, typically a structure name
902  * @mem: the memory to copy into the allocated block
903  *
904  * A convenience macro to duplicate a block of memory using
905  * the slice allocator.
906  *
907  * It calls g_slice_copy() with <literal>sizeof (@type)</literal>
908  * and casts the returned pointer to a pointer of the given type,
909  * avoiding a type cast in the source code.
910  * Note that the underlying slice allocation mechanism can
911  * be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
912  * environment variable.
913  *
914  * Returns: a pointer to the allocated block, cast to a pointer to @type
915  *
916  * Since: 2.14
917  */
918
919 /**
920  * g_slice_free:
921  * @type: the type of the block to free, typically a structure name
922  * @mem: a pointer to the block to free
923  *
924  * A convenience macro to free a block of memory that has
925  * been allocated from the slice allocator.
926  *
927  * It calls g_slice_free1() using <literal>sizeof (type)</literal>
928  * as the block size.
929  * Note that the exact release behaviour can be changed with the
930  * <link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment
931  * variable, also see <link linkend="G_SLICE">G_SLICE</link> for
932  * related debugging options.
933  *
934  * Since: 2.10
935  */
936
937 /**
938  * g_slice_free_chain:
939  * @type: the type of the @mem_chain blocks
940  * @mem_chain: a pointer to the first block of the chain
941  * @next: the field name of the next pointer in @type
942  *
943  * Frees a linked list of memory blocks of structure type @type.
944  * The memory blocks must be equal-sized, allocated via
945  * g_slice_alloc() or g_slice_alloc0() and linked together by
946  * a @next pointer (similar to #GSList). The name of the
947  * @next field in @type is passed as third argument.
948  * Note that the exact release behaviour can be changed with the
949  * <link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment
950  * variable, also see <link linkend="G_SLICE">G_SLICE</link> for
951  * related debugging options.
952  *
953  * Since: 2.10
954  */
955
956 /**
957  * g_slice_alloc:
958  * @block_size: the number of bytes to allocate
959  *
960  * Allocates a block of memory from the slice allocator.
961  * The block adress handed out can be expected to be aligned
962  * to at least <literal>1 * sizeof (void*)</literal>,
963  * though in general slices are 2 * sizeof (void*) bytes aligned,
964  * if a malloc() fallback implementation is used instead,
965  * the alignment may be reduced in a libc dependent fashion.
966  * Note that the underlying slice allocation mechanism can
967  * be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
968  * environment variable.
969  *
970  * Returns: a pointer to the allocated memory block
971  *
972  * Since: 2.10
973  */
974 gpointer
975 g_slice_alloc (gsize mem_size)
976 {
977   ThreadMemory *tmem;
978   gsize chunk_size;
979   gpointer mem;
980   guint acat;
981
982   /* This gets the private structure for this thread.  If the private
983    * structure does not yet exist, it is created.
984    *
985    * This has a side effect of causing GSlice to be initialised, so it
986    * must come first.
987    */
988   tmem = thread_memory_from_self ();
989
990   chunk_size = P2ALIGN (mem_size);
991   acat = allocator_categorize (chunk_size);
992   if (G_LIKELY (acat == 1))     /* allocate through magazine layer */
993     {
994       guint ix = SLAB_INDEX (allocator, chunk_size);
995       if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
996         {
997           thread_memory_swap_magazines (tmem, ix);
998           if (G_UNLIKELY (thread_memory_magazine1_is_empty (tmem, ix)))
999             thread_memory_magazine1_reload (tmem, ix);
1000         }
1001       mem = thread_memory_magazine1_alloc (tmem, ix);
1002     }
1003   else if (acat == 2)           /* allocate through slab allocator */
1004     {
1005       g_mutex_lock (&allocator->slab_mutex);
1006       mem = slab_allocator_alloc_chunk (chunk_size);
1007       g_mutex_unlock (&allocator->slab_mutex);
1008     }
1009   else                          /* delegate to system malloc */
1010     mem = g_malloc (mem_size);
1011   if (G_UNLIKELY (allocator->config.debug_blocks))
1012     smc_notify_alloc (mem, mem_size);
1013
1014   TRACE (GLIB_SLICE_ALLOC((void*)mem, mem_size));
1015
1016   return mem;
1017 }
1018
1019 /**
1020  * g_slice_alloc0:
1021  * @block_size: the number of bytes to allocate
1022  *
1023  * Allocates a block of memory via g_slice_alloc() and initializes
1024  * the returned memory to 0. Note that the underlying slice allocation
1025  * mechanism can be changed with the
1026  * <link linkend="G_SLICE">G_SLICE=always-malloc</link>
1027  * environment variable.
1028  *
1029  * Returns: a pointer to the allocated block
1030  *
1031  * Since: 2.10
1032  */
1033 gpointer
1034 g_slice_alloc0 (gsize mem_size)
1035 {
1036   gpointer mem = g_slice_alloc (mem_size);
1037   if (mem)
1038     memset (mem, 0, mem_size);
1039   return mem;
1040 }
1041
1042 /**
1043  * g_slice_copy:
1044  * @block_size: the number of bytes to allocate
1045  * @mem_block: the memory to copy
1046  *
1047  * Allocates a block of memory from the slice allocator
1048  * and copies @block_size bytes into it from @mem_block.
1049  *
1050  * Returns: a pointer to the allocated memory block
1051  *
1052  * Since: 2.14
1053  */
1054 gpointer
1055 g_slice_copy (gsize         mem_size,
1056               gconstpointer mem_block)
1057 {
1058   gpointer mem = g_slice_alloc (mem_size);
1059   if (mem)
1060     memcpy (mem, mem_block, mem_size);
1061   return mem;
1062 }
1063
1064 /**
1065  * g_slice_free1:
1066  * @block_size: the size of the block
1067  * @mem_block: a pointer to the block to free
1068  *
1069  * Frees a block of memory.
1070  *
1071  * The memory must have been allocated via g_slice_alloc() or
1072  * g_slice_alloc0() and the @block_size has to match the size
1073  * specified upon allocation. Note that the exact release behaviour
1074  * can be changed with the
1075  * <link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment
1076  * variable, also see <link linkend="G_SLICE">G_SLICE</link> for
1077  * related debugging options.
1078  *
1079  * Since: 2.10
1080  */
1081 void
1082 g_slice_free1 (gsize    mem_size,
1083                gpointer mem_block)
1084 {
1085   gsize chunk_size = P2ALIGN (mem_size);
1086   guint acat = allocator_categorize (chunk_size);
1087   if (G_UNLIKELY (!mem_block))
1088     return;
1089   if (G_UNLIKELY (allocator->config.debug_blocks) &&
1090       !smc_notify_free (mem_block, mem_size))
1091     abort();
1092   if (G_LIKELY (acat == 1))             /* allocate through magazine layer */
1093     {
1094       ThreadMemory *tmem = thread_memory_from_self();
1095       guint ix = SLAB_INDEX (allocator, chunk_size);
1096       if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
1097         {
1098           thread_memory_swap_magazines (tmem, ix);
1099           if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
1100             thread_memory_magazine2_unload (tmem, ix);
1101         }
1102       if (G_UNLIKELY (g_mem_gc_friendly))
1103         memset (mem_block, 0, chunk_size);
1104       thread_memory_magazine2_free (tmem, ix, mem_block);
1105     }
1106   else if (acat == 2)                   /* allocate through slab allocator */
1107     {
1108       if (G_UNLIKELY (g_mem_gc_friendly))
1109         memset (mem_block, 0, chunk_size);
1110       g_mutex_lock (&allocator->slab_mutex);
1111       slab_allocator_free_chunk (chunk_size, mem_block);
1112       g_mutex_unlock (&allocator->slab_mutex);
1113     }
1114   else                                  /* delegate to system malloc */
1115     {
1116       if (G_UNLIKELY (g_mem_gc_friendly))
1117         memset (mem_block, 0, mem_size);
1118       g_free (mem_block);
1119     }
1120   TRACE (GLIB_SLICE_FREE((void*)mem_block, mem_size));
1121 }
1122
1123 /**
1124  * g_slice_free_chain_with_offset:
1125  * @block_size: the size of the blocks
1126  * @mem_chain:  a pointer to the first block of the chain
1127  * @next_offset: the offset of the @next field in the blocks
1128  *
1129  * Frees a linked list of memory blocks of structure type @type.
1130  *
1131  * The memory blocks must be equal-sized, allocated via
1132  * g_slice_alloc() or g_slice_alloc0() and linked together by a
1133  * @next pointer (similar to #GSList). The offset of the @next
1134  * field in each block is passed as third argument.
1135  * Note that the exact release behaviour can be changed with the
1136  * <link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment
1137  * variable, also see <link linkend="G_SLICE">G_SLICE</link> for
1138  * related debugging options.
1139  *
1140  * Since: 2.10
1141  */
1142 void
1143 g_slice_free_chain_with_offset (gsize    mem_size,
1144                                 gpointer mem_chain,
1145                                 gsize    next_offset)
1146 {
1147   gpointer slice = mem_chain;
1148   /* while the thread magazines and the magazine cache are implemented so that
1149    * they can easily be extended to allow for free lists containing more free
1150    * lists for the first level nodes, which would allow O(1) freeing in this
1151    * function, the benefit of such an extension is questionable, because:
1152    * - the magazine size counts will become mere lower bounds which confuses
1153    *   the code adapting to lock contention;
1154    * - freeing a single node to the thread magazines is very fast, so this
1155    *   O(list_length) operation is multiplied by a fairly small factor;
1156    * - memory usage histograms on larger applications seem to indicate that
1157    *   the amount of released multi node lists is negligible in comparison
1158    *   to single node releases.
1159    * - the major performance bottle neck, namely g_private_get() or
1160    *   g_mutex_lock()/g_mutex_unlock() has already been moved out of the
1161    *   inner loop for freeing chained slices.
1162    */
1163   gsize chunk_size = P2ALIGN (mem_size);
1164   guint acat = allocator_categorize (chunk_size);
1165   if (G_LIKELY (acat == 1))             /* allocate through magazine layer */
1166     {
1167       ThreadMemory *tmem = thread_memory_from_self();
1168       guint ix = SLAB_INDEX (allocator, chunk_size);
1169       while (slice)
1170         {
1171           guint8 *current = slice;
1172           slice = *(gpointer*) (current + next_offset);
1173           if (G_UNLIKELY (allocator->config.debug_blocks) &&
1174               !smc_notify_free (current, mem_size))
1175             abort();
1176           if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
1177             {
1178               thread_memory_swap_magazines (tmem, ix);
1179               if (G_UNLIKELY (thread_memory_magazine2_is_full (tmem, ix)))
1180                 thread_memory_magazine2_unload (tmem, ix);
1181             }
1182           if (G_UNLIKELY (g_mem_gc_friendly))
1183             memset (current, 0, chunk_size);
1184           thread_memory_magazine2_free (tmem, ix, current);
1185         }
1186     }
1187   else if (acat == 2)                   /* allocate through slab allocator */
1188     {
1189       g_mutex_lock (&allocator->slab_mutex);
1190       while (slice)
1191         {
1192           guint8 *current = slice;
1193           slice = *(gpointer*) (current + next_offset);
1194           if (G_UNLIKELY (allocator->config.debug_blocks) &&
1195               !smc_notify_free (current, mem_size))
1196             abort();
1197           if (G_UNLIKELY (g_mem_gc_friendly))
1198             memset (current, 0, chunk_size);
1199           slab_allocator_free_chunk (chunk_size, current);
1200         }
1201       g_mutex_unlock (&allocator->slab_mutex);
1202     }
1203   else                                  /* delegate to system malloc */
1204     while (slice)
1205       {
1206         guint8 *current = slice;
1207         slice = *(gpointer*) (current + next_offset);
1208         if (G_UNLIKELY (allocator->config.debug_blocks) &&
1209             !smc_notify_free (current, mem_size))
1210           abort();
1211         if (G_UNLIKELY (g_mem_gc_friendly))
1212           memset (current, 0, mem_size);
1213         g_free (current);
1214       }
1215 }
1216
1217 /* --- single page allocator --- */
1218 static void
1219 allocator_slab_stack_push (Allocator *allocator,
1220                            guint      ix,
1221                            SlabInfo  *sinfo)
1222 {
1223   /* insert slab at slab ring head */
1224   if (!allocator->slab_stack[ix])
1225     {
1226       sinfo->next = sinfo;
1227       sinfo->prev = sinfo;
1228     }
1229   else
1230     {
1231       SlabInfo *next = allocator->slab_stack[ix], *prev = next->prev;
1232       next->prev = sinfo;
1233       prev->next = sinfo;
1234       sinfo->next = next;
1235       sinfo->prev = prev;
1236     }
1237   allocator->slab_stack[ix] = sinfo;
1238 }
1239
1240 static gsize
1241 allocator_aligned_page_size (Allocator *allocator,
1242                              gsize      n_bytes)
1243 {
1244   gsize val = 1 << g_bit_storage (n_bytes - 1);
1245   val = MAX (val, allocator->min_page_size);
1246   return val;
1247 }
1248
1249 static void
1250 allocator_add_slab (Allocator *allocator,
1251                     guint      ix,
1252                     gsize      chunk_size)
1253 {
1254   ChunkLink *chunk;
1255   SlabInfo *sinfo;
1256   gsize addr, padding, n_chunks, color = 0;
1257   gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1258   /* allocate 1 page for the chunks and the slab */
1259   gpointer aligned_memory = allocator_memalign (page_size, page_size - NATIVE_MALLOC_PADDING);
1260   guint8 *mem = aligned_memory;
1261   guint i;
1262   if (!mem)
1263     {
1264       const gchar *syserr = strerror (errno);
1265       mem_error ("failed to allocate %u bytes (alignment: %u): %s\n",
1266                  (guint) (page_size - NATIVE_MALLOC_PADDING), (guint) page_size, syserr);
1267     }
1268   /* mask page address */
1269   addr = ((gsize) mem / page_size) * page_size;
1270   /* assert alignment */
1271   mem_assert (aligned_memory == (gpointer) addr);
1272   /* basic slab info setup */
1273   sinfo = (SlabInfo*) (mem + page_size - SLAB_INFO_SIZE);
1274   sinfo->n_allocated = 0;
1275   sinfo->chunks = NULL;
1276   /* figure cache colorization */
1277   n_chunks = ((guint8*) sinfo - mem) / chunk_size;
1278   padding = ((guint8*) sinfo - mem) - n_chunks * chunk_size;
1279   if (padding)
1280     {
1281       color = (allocator->color_accu * P2ALIGNMENT) % padding;
1282       allocator->color_accu += allocator->config.color_increment;
1283     }
1284   /* add chunks to free list */
1285   chunk = (ChunkLink*) (mem + color);
1286   sinfo->chunks = chunk;
1287   for (i = 0; i < n_chunks - 1; i++)
1288     {
1289       chunk->next = (ChunkLink*) ((guint8*) chunk + chunk_size);
1290       chunk = chunk->next;
1291     }
1292   chunk->next = NULL;   /* last chunk */
1293   /* add slab to slab ring */
1294   allocator_slab_stack_push (allocator, ix, sinfo);
1295 }
1296
1297 static gpointer
1298 slab_allocator_alloc_chunk (gsize chunk_size)
1299 {
1300   ChunkLink *chunk;
1301   guint ix = SLAB_INDEX (allocator, chunk_size);
1302   /* ensure non-empty slab */
1303   if (!allocator->slab_stack[ix] || !allocator->slab_stack[ix]->chunks)
1304     allocator_add_slab (allocator, ix, chunk_size);
1305   /* allocate chunk */
1306   chunk = allocator->slab_stack[ix]->chunks;
1307   allocator->slab_stack[ix]->chunks = chunk->next;
1308   allocator->slab_stack[ix]->n_allocated++;
1309   /* rotate empty slabs */
1310   if (!allocator->slab_stack[ix]->chunks)
1311     allocator->slab_stack[ix] = allocator->slab_stack[ix]->next;
1312   return chunk;
1313 }
1314
1315 static void
1316 slab_allocator_free_chunk (gsize    chunk_size,
1317                            gpointer mem)
1318 {
1319   ChunkLink *chunk;
1320   gboolean was_empty;
1321   guint ix = SLAB_INDEX (allocator, chunk_size);
1322   gsize page_size = allocator_aligned_page_size (allocator, SLAB_BPAGE_SIZE (allocator, chunk_size));
1323   gsize addr = ((gsize) mem / page_size) * page_size;
1324   /* mask page address */
1325   guint8 *page = (guint8*) addr;
1326   SlabInfo *sinfo = (SlabInfo*) (page + page_size - SLAB_INFO_SIZE);
1327   /* assert valid chunk count */
1328   mem_assert (sinfo->n_allocated > 0);
1329   /* add chunk to free list */
1330   was_empty = sinfo->chunks == NULL;
1331   chunk = (ChunkLink*) mem;
1332   chunk->next = sinfo->chunks;
1333   sinfo->chunks = chunk;
1334   sinfo->n_allocated--;
1335   /* keep slab ring partially sorted, empty slabs at end */
1336   if (was_empty)
1337     {
1338       /* unlink slab */
1339       SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1340       next->prev = prev;
1341       prev->next = next;
1342       if (allocator->slab_stack[ix] == sinfo)
1343         allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1344       /* insert slab at head */
1345       allocator_slab_stack_push (allocator, ix, sinfo);
1346     }
1347   /* eagerly free complete unused slabs */
1348   if (!sinfo->n_allocated)
1349     {
1350       /* unlink slab */
1351       SlabInfo *next = sinfo->next, *prev = sinfo->prev;
1352       next->prev = prev;
1353       prev->next = next;
1354       if (allocator->slab_stack[ix] == sinfo)
1355         allocator->slab_stack[ix] = next == sinfo ? NULL : next;
1356       /* free slab */
1357       allocator_memfree (page_size, page);
1358     }
1359 }
1360
1361 /* --- memalign implementation --- */
1362 #ifdef HAVE_MALLOC_H
1363 #include <malloc.h>             /* memalign() */
1364 #endif
1365
1366 /* from config.h:
1367  * define HAVE_POSIX_MEMALIGN           1 // if free(posix_memalign(3)) works, <stdlib.h>
1368  * define HAVE_COMPLIANT_POSIX_MEMALIGN 1 // if free(posix_memalign(3)) works for sizes != 2^n, <stdlib.h>
1369  * define HAVE_MEMALIGN                 1 // if free(memalign(3)) works, <malloc.h>
1370  * define HAVE_VALLOC                   1 // if free(valloc(3)) works, <stdlib.h> or <malloc.h>
1371  * if none is provided, we implement malloc(3)-based alloc-only page alignment
1372  */
1373
1374 #if !(HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC)
1375 static GTrashStack *compat_valloc_trash = NULL;
1376 #endif
1377
1378 static gpointer
1379 allocator_memalign (gsize alignment,
1380                     gsize memsize)
1381 {
1382   gpointer aligned_memory = NULL;
1383   gint err = ENOMEM;
1384 #if     HAVE_COMPLIANT_POSIX_MEMALIGN
1385   err = posix_memalign (&aligned_memory, alignment, memsize);
1386 #elif   HAVE_MEMALIGN
1387   errno = 0;
1388   aligned_memory = memalign (alignment, memsize);
1389   err = errno;
1390 #elif   HAVE_VALLOC
1391   errno = 0;
1392   aligned_memory = valloc (memsize);
1393   err = errno;
1394 #else
1395   /* simplistic non-freeing page allocator */
1396   mem_assert (alignment == sys_page_size);
1397   mem_assert (memsize <= sys_page_size);
1398   if (!compat_valloc_trash)
1399     {
1400       const guint n_pages = 16;
1401       guint8 *mem = malloc (n_pages * sys_page_size);
1402       err = errno;
1403       if (mem)
1404         {
1405           gint i = n_pages;
1406           guint8 *amem = (guint8*) ALIGN ((gsize) mem, sys_page_size);
1407           if (amem != mem)
1408             i--;        /* mem wasn't page aligned */
1409           while (--i >= 0)
1410             g_trash_stack_push (&compat_valloc_trash, amem + i * sys_page_size);
1411         }
1412     }
1413   aligned_memory = g_trash_stack_pop (&compat_valloc_trash);
1414 #endif
1415   if (!aligned_memory)
1416     errno = err;
1417   return aligned_memory;
1418 }
1419
1420 static void
1421 allocator_memfree (gsize    memsize,
1422                    gpointer mem)
1423 {
1424 #if     HAVE_COMPLIANT_POSIX_MEMALIGN || HAVE_MEMALIGN || HAVE_VALLOC
1425   free (mem);
1426 #else
1427   mem_assert (memsize <= sys_page_size);
1428   g_trash_stack_push (&compat_valloc_trash, mem);
1429 #endif
1430 }
1431
1432 static void
1433 mem_error (const char *format,
1434            ...)
1435 {
1436   const char *pname;
1437   va_list args;
1438   /* at least, put out "MEMORY-ERROR", in case we segfault during the rest of the function */
1439   fputs ("\n***MEMORY-ERROR***: ", stderr);
1440   pname = g_get_prgname();
1441   fprintf (stderr, "%s[%ld]: GSlice: ", pname ? pname : "", (long)getpid());
1442   va_start (args, format);
1443   vfprintf (stderr, format, args);
1444   va_end (args);
1445   fputs ("\n", stderr);
1446   abort();
1447   _exit (1);
1448 }
1449
1450 /* --- g-slice memory checker tree --- */
1451 typedef size_t SmcKType;                /* key type */
1452 typedef size_t SmcVType;                /* value type */
1453 typedef struct {
1454   SmcKType key;
1455   SmcVType value;
1456 } SmcEntry;
1457 static void             smc_tree_insert      (SmcKType  key,
1458                                               SmcVType  value);
1459 static gboolean         smc_tree_lookup      (SmcKType  key,
1460                                               SmcVType *value_p);
1461 static gboolean         smc_tree_remove      (SmcKType  key);
1462
1463
1464 /* --- g-slice memory checker implementation --- */
1465 static void
1466 smc_notify_alloc (void   *pointer,
1467                   size_t  size)
1468 {
1469   size_t adress = (size_t) pointer;
1470   if (pointer)
1471     smc_tree_insert (adress, size);
1472 }
1473
1474 #if 0
1475 static void
1476 smc_notify_ignore (void *pointer)
1477 {
1478   size_t adress = (size_t) pointer;
1479   if (pointer)
1480     smc_tree_remove (adress);
1481 }
1482 #endif
1483
1484 static int
1485 smc_notify_free (void   *pointer,
1486                  size_t  size)
1487 {
1488   size_t adress = (size_t) pointer;
1489   SmcVType real_size;
1490   gboolean found_one;
1491
1492   if (!pointer)
1493     return 1; /* ignore */
1494   found_one = smc_tree_lookup (adress, &real_size);
1495   if (!found_one)
1496     {
1497       fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1498       return 0;
1499     }
1500   if (real_size != size && (real_size || size))
1501     {
1502       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);
1503       return 0;
1504     }
1505   if (!smc_tree_remove (adress))
1506     {
1507       fprintf (stderr, "GSlice: MemChecker: attempt to release non-allocated block: %p size=%" G_GSIZE_FORMAT "\n", pointer, size);
1508       return 0;
1509     }
1510   return 1; /* all fine */
1511 }
1512
1513 /* --- g-slice memory checker tree implementation --- */
1514 #define SMC_TRUNK_COUNT     (4093 /* 16381 */)          /* prime, to distribute trunk collisions (big, allocated just once) */
1515 #define SMC_BRANCH_COUNT    (511)                       /* prime, to distribute branch collisions */
1516 #define SMC_TRUNK_EXTENT    (SMC_BRANCH_COUNT * 2039)   /* key address space per trunk, should distribute uniformly across BRANCH_COUNT */
1517 #define SMC_TRUNK_HASH(k)   ((k / SMC_TRUNK_EXTENT) % SMC_TRUNK_COUNT)  /* generate new trunk hash per megabyte (roughly) */
1518 #define SMC_BRANCH_HASH(k)  (k % SMC_BRANCH_COUNT)
1519
1520 typedef struct {
1521   SmcEntry    *entries;
1522   unsigned int n_entries;
1523 } SmcBranch;
1524
1525 static SmcBranch     **smc_tree_root = NULL;
1526
1527 static void
1528 smc_tree_abort (int errval)
1529 {
1530   const char *syserr = strerror (errval);
1531   mem_error ("MemChecker: failure in debugging tree: %s", syserr);
1532 }
1533
1534 static inline SmcEntry*
1535 smc_tree_branch_grow_L (SmcBranch   *branch,
1536                         unsigned int index)
1537 {
1538   unsigned int old_size = branch->n_entries * sizeof (branch->entries[0]);
1539   unsigned int new_size = old_size + sizeof (branch->entries[0]);
1540   SmcEntry *entry;
1541   mem_assert (index <= branch->n_entries);
1542   branch->entries = (SmcEntry*) realloc (branch->entries, new_size);
1543   if (!branch->entries)
1544     smc_tree_abort (errno);
1545   entry = branch->entries + index;
1546   memmove (entry + 1, entry, (branch->n_entries - index) * sizeof (entry[0]));
1547   branch->n_entries += 1;
1548   return entry;
1549 }
1550
1551 static inline SmcEntry*
1552 smc_tree_branch_lookup_nearest_L (SmcBranch *branch,
1553                                   SmcKType   key)
1554 {
1555   unsigned int n_nodes = branch->n_entries, offs = 0;
1556   SmcEntry *check = branch->entries;
1557   int cmp = 0;
1558   while (offs < n_nodes)
1559     {
1560       unsigned int i = (offs + n_nodes) >> 1;
1561       check = branch->entries + i;
1562       cmp = key < check->key ? -1 : key != check->key;
1563       if (cmp == 0)
1564         return check;                   /* return exact match */
1565       else if (cmp < 0)
1566         n_nodes = i;
1567       else /* (cmp > 0) */
1568         offs = i + 1;
1569     }
1570   /* check points at last mismatch, cmp > 0 indicates greater key */
1571   return cmp > 0 ? check + 1 : check;   /* return insertion position for inexact match */
1572 }
1573
1574 static void
1575 smc_tree_insert (SmcKType key,
1576                  SmcVType value)
1577 {
1578   unsigned int ix0, ix1;
1579   SmcEntry *entry;
1580
1581   g_mutex_lock (&smc_tree_mutex);
1582   ix0 = SMC_TRUNK_HASH (key);
1583   ix1 = SMC_BRANCH_HASH (key);
1584   if (!smc_tree_root)
1585     {
1586       smc_tree_root = calloc (SMC_TRUNK_COUNT, sizeof (smc_tree_root[0]));
1587       if (!smc_tree_root)
1588         smc_tree_abort (errno);
1589     }
1590   if (!smc_tree_root[ix0])
1591     {
1592       smc_tree_root[ix0] = calloc (SMC_BRANCH_COUNT, sizeof (smc_tree_root[0][0]));
1593       if (!smc_tree_root[ix0])
1594         smc_tree_abort (errno);
1595     }
1596   entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1597   if (!entry ||                                                                         /* need create */
1598       entry >= smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries ||   /* need append */
1599       entry->key != key)                                                                /* need insert */
1600     entry = smc_tree_branch_grow_L (&smc_tree_root[ix0][ix1], entry - smc_tree_root[ix0][ix1].entries);
1601   entry->key = key;
1602   entry->value = value;
1603   g_mutex_unlock (&smc_tree_mutex);
1604 }
1605
1606 static gboolean
1607 smc_tree_lookup (SmcKType  key,
1608                  SmcVType *value_p)
1609 {
1610   SmcEntry *entry = NULL;
1611   unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1612   gboolean found_one = FALSE;
1613   *value_p = 0;
1614   g_mutex_lock (&smc_tree_mutex);
1615   if (smc_tree_root && smc_tree_root[ix0])
1616     {
1617       entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1618       if (entry &&
1619           entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1620           entry->key == key)
1621         {
1622           found_one = TRUE;
1623           *value_p = entry->value;
1624         }
1625     }
1626   g_mutex_unlock (&smc_tree_mutex);
1627   return found_one;
1628 }
1629
1630 static gboolean
1631 smc_tree_remove (SmcKType key)
1632 {
1633   unsigned int ix0 = SMC_TRUNK_HASH (key), ix1 = SMC_BRANCH_HASH (key);
1634   gboolean found_one = FALSE;
1635   g_mutex_lock (&smc_tree_mutex);
1636   if (smc_tree_root && smc_tree_root[ix0])
1637     {
1638       SmcEntry *entry = smc_tree_branch_lookup_nearest_L (&smc_tree_root[ix0][ix1], key);
1639       if (entry &&
1640           entry < smc_tree_root[ix0][ix1].entries + smc_tree_root[ix0][ix1].n_entries &&
1641           entry->key == key)
1642         {
1643           unsigned int i = entry - smc_tree_root[ix0][ix1].entries;
1644           smc_tree_root[ix0][ix1].n_entries -= 1;
1645           memmove (entry, entry + 1, (smc_tree_root[ix0][ix1].n_entries - i) * sizeof (entry[0]));
1646           if (!smc_tree_root[ix0][ix1].n_entries)
1647             {
1648               /* avoid useless pressure on the memory system */
1649               free (smc_tree_root[ix0][ix1].entries);
1650               smc_tree_root[ix0][ix1].entries = NULL;
1651             }
1652           found_one = TRUE;
1653         }
1654     }
1655   g_mutex_unlock (&smc_tree_mutex);
1656   return found_one;
1657 }
1658
1659 #ifdef G_ENABLE_DEBUG
1660 void
1661 g_slice_debug_tree_statistics (void)
1662 {
1663   g_mutex_lock (&smc_tree_mutex);
1664   if (smc_tree_root)
1665     {
1666       unsigned int i, j, t = 0, o = 0, b = 0, su = 0, ex = 0, en = 4294967295u;
1667       double tf, bf;
1668       for (i = 0; i < SMC_TRUNK_COUNT; i++)
1669         if (smc_tree_root[i])
1670           {
1671             t++;
1672             for (j = 0; j < SMC_BRANCH_COUNT; j++)
1673               if (smc_tree_root[i][j].n_entries)
1674                 {
1675                   b++;
1676                   su += smc_tree_root[i][j].n_entries;
1677                   en = MIN (en, smc_tree_root[i][j].n_entries);
1678                   ex = MAX (ex, smc_tree_root[i][j].n_entries);
1679                 }
1680               else if (smc_tree_root[i][j].entries)
1681                 o++; /* formerly used, now empty */
1682           }
1683       en = b ? en : 0;
1684       tf = MAX (t, 1.0); /* max(1) to be a valid divisor */
1685       bf = MAX (b, 1.0); /* max(1) to be a valid divisor */
1686       fprintf (stderr, "GSlice: MemChecker: %u trunks, %u branches, %u old branches\n", t, b, o);
1687       fprintf (stderr, "GSlice: MemChecker: %f branches per trunk, %.2f%% utilization\n",
1688                b / tf,
1689                100.0 - (SMC_BRANCH_COUNT - b / tf) / (0.01 * SMC_BRANCH_COUNT));
1690       fprintf (stderr, "GSlice: MemChecker: %f entries per branch, %u minimum, %u maximum\n",
1691                su / bf, en, ex);
1692     }
1693   else
1694     fprintf (stderr, "GSlice: MemChecker: root=NULL\n");
1695   g_mutex_unlock (&smc_tree_mutex);
1696   
1697   /* sample statistics (beast + GSLice + 24h scripted core & GUI activity):
1698    *  PID %CPU %MEM   VSZ  RSS      COMMAND
1699    * 8887 30.3 45.8 456068 414856   beast-0.7.1 empty.bse
1700    * $ cat /proc/8887/statm # total-program-size resident-set-size shared-pages text/code data/stack library dirty-pages
1701    * 114017 103714 2354 344 0 108676 0
1702    * $ cat /proc/8887/status 
1703    * Name:   beast-0.7.1
1704    * VmSize:   456068 kB
1705    * VmLck:         0 kB
1706    * VmRSS:    414856 kB
1707    * VmData:   434620 kB
1708    * VmStk:        84 kB
1709    * VmExe:      1376 kB
1710    * VmLib:     13036 kB
1711    * VmPTE:       456 kB
1712    * Threads:        3
1713    * (gdb) print g_slice_debug_tree_statistics ()
1714    * GSlice: MemChecker: 422 trunks, 213068 branches, 0 old branches
1715    * GSlice: MemChecker: 504.900474 branches per trunk, 98.81% utilization
1716    * GSlice: MemChecker: 4.965039 entries per branch, 1 minimum, 37 maximum
1717    */
1718 }
1719 #endif /* G_ENABLE_DEBUG */