0001-gslice-remove-slice-allocator.patch 60 KB

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