Rizin
unix-like reverse engineering framework and cli tools
tcache.h
Go to the documentation of this file.
1 /******************************************************************************/
2 #ifdef JEMALLOC_H_TYPES
3 
4 typedef struct tcache_bin_info_s tcache_bin_info_t;
5 typedef struct tcache_bin_s tcache_bin_t;
6 typedef struct tcache_s tcache_t;
7 typedef struct tcaches_s tcaches_t;
8 
9 /*
10  * tcache pointers close to NULL are used to encode state information that is
11  * used for two purposes: preventing thread caching on a per thread basis and
12  * cleaning up during thread shutdown.
13  */
14 #define TCACHE_STATE_DISABLED ((tcache_t *)(uintptr_t)1)
15 #define TCACHE_STATE_REINCARNATED ((tcache_t *)(uintptr_t)2)
16 #define TCACHE_STATE_PURGATORY ((tcache_t *)(uintptr_t)3)
17 #define TCACHE_STATE_MAX TCACHE_STATE_PURGATORY
18 
19 /*
20  * Absolute minimum number of cache slots for each small bin.
21  */
22 #define TCACHE_NSLOTS_SMALL_MIN 20
23 
24 /*
25  * Absolute maximum number of cache slots for each small bin in the thread
26  * cache. This is an additional constraint beyond that imposed as: twice the
27  * number of regions per run for this size class.
28  *
29  * This constant must be an even number.
30  */
31 #define TCACHE_NSLOTS_SMALL_MAX 200
32 
33 /* Number of cache slots for large size classes. */
34 #define TCACHE_NSLOTS_LARGE 20
35 
36 /* (1U << opt_lg_tcache_max) is used to compute tcache_maxclass. */
37 #define LG_TCACHE_MAXCLASS_DEFAULT 15
38 
39 /*
40  * TCACHE_GC_SWEEP is the approximate number of allocation events between
41  * full GC sweeps. Integer rounding may cause the actual number to be
42  * slightly higher, since GC is performed incrementally.
43  */
44 #define TCACHE_GC_SWEEP 8192
45 
46 /* Number of tcache allocation/deallocation events between incremental GCs. */
47 #define TCACHE_GC_INCR \
48  ((TCACHE_GC_SWEEP / JM_NBINS) + ((TCACHE_GC_SWEEP / JM_NBINS == 0) ? 0 : 1))
49 
50 #endif /* JEMALLOC_H_TYPES */
51 /******************************************************************************/
52 #ifdef JEMALLOC_H_STRUCTS
53 
54 typedef enum {
55  tcache_enabled_false = 0, /* Enable cast to/from bool. */
56  tcache_enabled_true = 1,
57  tcache_enabled_default = 2
58 } tcache_enabled_t;
59 
60 /*
61  * Read-only information associated with each element of tcache_t's tbins array
62  * is stored separately, mainly to reduce memory usage.
63  */
64 struct tcache_bin_info_s {
65  unsigned ncached_max; /* Upper limit on ncached. */
66 };
67 
68 struct tcache_bin_s {
69  tcache_bin_stats_t tstats;
70  int low_water; /* Min # cached since last GC. */
71  unsigned lg_fill_div; /* Fill (ncached_max >> lg_fill_div). */
72  unsigned ncached; /* # of cached objects. */
73  /*
74  * To make use of adjacent cacheline prefetch, the items in the avail
75  * stack goes to higher address for newer allocations. avail points
76  * just above the available space, which means that
77  * avail[-ncached, ... -1] are available items and the lowest item will
78  * be allocated first.
79  */
80  void **avail; /* Stack of available objects. */
81 };
82 
83 struct tcache_s {
84  ql_elm(tcache_t) link; /* Used for aggregating stats. */
85  uint64_t prof_accumbytes;/* Cleared after arena_prof_accum(). */
86  ticker_t gc_ticker; /* Drives incremental GC. */
87  szind_t next_gc_bin; /* Next bin to GC. */
88  tcache_bin_t tbins[1]; /* Dynamically sized. */
89  /*
90  * The pointer stacks associated with tbins follow as a contiguous
91  * array. During tcache initialization, the avail pointer in each
92  * element of tbins is initialized to point to the proper offset within
93  * this array.
94  */
95 };
96 
97 /* Linkage for list of available (previously used) explicit tcache IDs. */
98 struct tcaches_s {
99  union {
100  tcache_t *tcache;
101  tcaches_t *next;
102  };
103 };
104 
105 #endif /* JEMALLOC_H_STRUCTS */
106 /******************************************************************************/
107 #ifdef JEMALLOC_H_EXTERNS
108 
109 extern bool opt_tcache;
111 
112 extern tcache_bin_info_t *tcache_bin_info;
113 
114 /*
115  * Number of tcache bins. There are JM_NBINS small-object bins, plus 0 or more
116  * large-object bins.
117  */
118 extern unsigned nhbins;
119 
120 /* Maximum cached size class. */
121 extern size_t tcache_maxclass;
122 
123 /*
124  * Explicit tcaches, managed via the tcache.{create,flush,destroy} mallctls and
125  * usable via the MALLOCX_TCACHE() flag. The automatic per thread tcaches are
126  * completely disjoint from this data structure. tcaches starts off as a sparse
127  * array, so it has no physical memory footprint until individual pages are
128  * touched. This allows the entire array to be allocated the first time an
129  * explicit tcache is created without a disproportionate impact on memory usage.
130  */
131 extern tcaches_t *tcaches;
132 
133 size_t tcache_salloc(tsdn_t *tsdn, const void *ptr);
134 void tcache_event_hard(tsd_t *tsd, tcache_t *tcache);
135 void *tcache_alloc_small_hard(tsdn_t *tsdn, arena_t *arena, tcache_t *tcache,
136  tcache_bin_t *tbin, szind_t binind, bool *tcache_success);
137 void tcache_bin_flush_small(tsd_t *tsd, tcache_t *tcache, tcache_bin_t *tbin,
138  szind_t binind, unsigned rem);
139 void tcache_bin_flush_large(tsd_t *tsd, tcache_bin_t *tbin, szind_t binind,
140  unsigned rem, tcache_t *tcache);
141 void tcache_arena_reassociate(tsdn_t *tsdn, tcache_t *tcache,
142  arena_t *oldarena, arena_t *newarena);
143 tcache_t *tcache_get_hard(tsd_t *tsd);
144 tcache_t *tcache_create(tsdn_t *tsdn, arena_t *arena);
145 void tcache_cleanup(tsd_t *tsd);
146 void tcache_enabled_cleanup(tsd_t *tsd);
147 void tcache_stats_merge(tsdn_t *tsdn, tcache_t *tcache, arena_t *arena);
148 bool tcaches_create(tsd_t *tsd, unsigned *rz_ind);
149 void tcaches_flush(tsd_t *tsd, unsigned ind);
150 void tcaches_destroy(tsd_t *tsd, unsigned ind);
151 bool tcache_boot(tsdn_t *tsdn);
152 void tcache_prefork(tsdn_t *tsdn);
153 void tcache_postfork_parent(tsdn_t *tsdn);
154 void tcache_postfork_child(tsdn_t *tsdn);
155 
156 #endif /* JEMALLOC_H_EXTERNS */
157 /******************************************************************************/
158 #ifdef JEMALLOC_H_INLINES
159 
160 #ifndef JEMALLOC_ENABLE_INLINE
161 void tcache_event(tsd_t *tsd, tcache_t *tcache);
162 void tcache_flush(void);
163 bool tcache_enabled_get(void);
164 tcache_t *tcache_get(tsd_t *tsd, bool create);
165 void tcache_enabled_set(bool enabled);
166 void *tcache_alloc_easy(tcache_bin_t *tbin, bool *tcache_success);
167 void *tcache_alloc_small(tsd_t *tsd, arena_t *arena, tcache_t *tcache,
168  size_t size, szind_t ind, bool zero, bool slow_path);
169 void *tcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache,
170  size_t size, szind_t ind, bool zero, bool slow_path);
171 void tcache_dalloc_small(tsd_t *tsd, tcache_t *tcache, void *ptr,
172  szind_t binind, bool slow_path);
173 void tcache_dalloc_large(tsd_t *tsd, tcache_t *tcache, void *ptr,
174  size_t size, bool slow_path);
175 tcache_t *tcaches_get(tsd_t *tsd, unsigned ind);
176 #endif
177 
178 #if (defined(JEMALLOC_ENABLE_INLINE) || defined(JEMALLOC_TCACHE_C_))
179 JEMALLOC_INLINE void
180 tcache_flush(void)
181 {
182  tsd_t *tsd;
183 
184  cassert(config_tcache);
185 
186  tsd = tsd_fetch();
187  tcache_cleanup(tsd);
188 }
189 
190 JEMALLOC_INLINE bool
191 tcache_enabled_get(void)
192 {
193  tsd_t *tsd;
194  tcache_enabled_t tcache_enabled;
195 
196  if(unlikely(!config_tcache))
197  return false;
198  tsd = tsd_fetch();
199  tcache_enabled = tsd_tcache_enabled_get(tsd);
200  if (tcache_enabled == tcache_enabled_default) {
201  tcache_enabled = (tcache_enabled_t)opt_tcache;
202  tsd_tcache_enabled_set(tsd, tcache_enabled);
203  }
204 
205  return ((bool)tcache_enabled);
206 }
207 
208 JEMALLOC_INLINE void
209 tcache_enabled_set(bool enabled)
210 {
211  tsd_t *tsd;
212  tcache_enabled_t tcache_enabled;
213 
214  cassert(config_tcache);
215 
216  tsd = tsd_fetch();
217 
218  tcache_enabled = (tcache_enabled_t)enabled;
219  tsd_tcache_enabled_set(tsd, tcache_enabled);
220 
221  if (!enabled)
222  tcache_cleanup(tsd);
223 }
224 
225 JEMALLOC_ALWAYS_INLINE tcache_t *
226 tcache_get(tsd_t *tsd, bool create)
227 {
228  tcache_t *tcache;
229 
230  if (!config_tcache)
231  return (NULL);
232 
233  tcache = tsd_tcache_get(tsd);
234  if (!create)
235  return (tcache);
236  if (unlikely(tcache == NULL) && tsd_nominal(tsd)) {
237  tcache = tcache_get_hard(tsd);
238  tsd_tcache_set(tsd, tcache);
239  }
240 
241  return (tcache);
242 }
243 
245 tcache_event(tsd_t *tsd, tcache_t *tcache)
246 {
247 
248  if (TCACHE_GC_INCR == 0)
249  return;
250 
251  if (unlikely(ticker_tick(&tcache->gc_ticker)))
252  tcache_event_hard(tsd, tcache);
253 }
254 
256 tcache_alloc_easy(tcache_bin_t *tbin, bool *tcache_success)
257 {
258  void *ret;
259 
260  if (unlikely(tbin->ncached == 0)) {
261  tbin->low_water = -1;
262  *tcache_success = false;
263  return (NULL);
264  }
265  /*
266  * tcache_success (instead of ret) should be checked upon the return of
267  * this function. We avoid checking (ret == NULL) because there is
268  * never a null stored on the avail stack (which is unknown to the
269  * compiler), and eagerly checking ret would cause pipeline stall
270  * (waiting for the cacheline).
271  */
272  *tcache_success = true;
273  ret = *(tbin->avail - tbin->ncached);
274  tbin->ncached--;
275 
276  if (unlikely((int)tbin->ncached < tbin->low_water))
277  tbin->low_water = tbin->ncached;
278 
279  return (ret);
280 }
281 
283 tcache_alloc_small(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,
284  szind_t binind, bool zero, bool slow_path)
285 {
286  void *ret;
287  tcache_bin_t *tbin;
288  bool tcache_success;
289  size_t usize JEMALLOC_CC_SILENCE_INIT(0);
290 
291  if (unlikely(binind > JM_NBINS))
292  return (NULL);
293 
294  tbin = &tcache->tbins[binind];
295  ret = tcache_alloc_easy(tbin, &tcache_success);
296 
297  if (unlikely(!(tcache_success == (ret != NULL))))
298  return (NULL);
299 
300  if (unlikely(!tcache_success)) {
301  bool tcache_hard_success;
302  arena = arena_choose(tsd, arena);
303  if (unlikely(arena == NULL))
304  return (NULL);
305 
306  ret = tcache_alloc_small_hard(tsd_tsdn(tsd), arena, tcache,
307  tbin, binind, &tcache_hard_success);
308  if (tcache_hard_success == false)
309  return (NULL);
310  }
311 
312  if (unlikely(!ret))
313  return (NULL);
314 
315  /*
316  * Only compute usize if required. The checks in the following if
317  * statement are all static.
318  */
319  if (config_prof || (slow_path && config_fill) || unlikely(zero)) {
320  usize = index2size(binind);
321  assert(tcache_salloc(tsd_tsdn(tsd), ret) == usize);
322  }
323 
324  if (likely(!zero)) {
325  if (slow_path && config_fill) {
326  if (unlikely(opt_junk_alloc)) {
328  &arena_bin_info[binind], false);
329  } else if (unlikely(opt_zero))
330  memset(ret, 0, usize);
331  }
332  } else {
333  if (slow_path && config_fill && unlikely(opt_junk_alloc)) {
335  true);
336  }
337  memset(ret, 0, usize);
338  }
339 
340  if (config_stats)
341  tbin->tstats.nrequests++;
342  if (config_prof)
343  tcache->prof_accumbytes += usize;
344  tcache_event(tsd, tcache);
345  return (ret);
346 }
347 
349 tcache_alloc_large(tsd_t *tsd, arena_t *arena, tcache_t *tcache, size_t size,
350  szind_t binind, bool zero, bool slow_path)
351 {
352  void *ret;
353  tcache_bin_t *tbin;
354  bool tcache_success;
355 
356  if (unlikely(binind > nhbins))
357  return (NULL);
358  tbin = &tcache->tbins[binind];
359  ret = tcache_alloc_easy(tbin, &tcache_success);
360 
361  if (unlikely(!(tcache_success == (ret != NULL))))
362  return (NULL);
363 
364  if (unlikely(!tcache_success)) {
365  /*
366  * Only allocate one large object at a time, because it's quite
367  * expensive to create one and not use it.
368  */
369  arena = arena_choose(tsd, arena);
370  if (unlikely(arena == NULL))
371  return (NULL);
372 
373  ret = arena_malloc_large(tsd_tsdn(tsd), arena, binind, zero);
374  if (ret == NULL)
375  return (NULL);
376  } else {
377  size_t usize JEMALLOC_CC_SILENCE_INIT(0);
378 
379  /* Only compute usize on demand */
380  if (config_prof || (slow_path && config_fill) ||
381  unlikely(zero)) {
382  usize = index2size(binind);
383  assert(usize <= tcache_maxclass);
384  }
385 
386  if (config_prof && usize == LARGE_MINCLASS) {
387  arena_chunk_t *chunk =
388  (arena_chunk_t *)CHUNK_ADDR2BASE(ret);
389  size_t pageind = (((uintptr_t)ret - (uintptr_t)chunk) >>
390  LG_PAGE);
392  BININD_INVALID);
393  }
394  if (likely(!zero)) {
395  if (slow_path && config_fill) {
396  if (unlikely(opt_junk_alloc)) {
397  memset(ret, JEMALLOC_ALLOC_JUNK,
398  usize);
399  } else if (unlikely(opt_zero))
400  memset(ret, 0, usize);
401  }
402  } else
403  memset(ret, 0, usize);
404 
405  if (config_stats)
406  tbin->tstats.nrequests++;
407  if (config_prof)
408  tcache->prof_accumbytes += usize;
409  }
410 
411  tcache_event(tsd, tcache);
412  return (ret);
413 }
414 
416 tcache_dalloc_small(tsd_t *tsd, tcache_t *tcache, void *ptr, szind_t binind,
417  bool slow_path)
418 {
419  tcache_bin_t *tbin;
420  tcache_bin_info_t *tbin_info;
421 
422  assert(tcache_salloc(tsd_tsdn(tsd), ptr) <= SMALL_MAXCLASS);
423 
424  if (slow_path && config_fill && unlikely(opt_junk_free))
426 
427  tbin = &tcache->tbins[binind];
428  tbin_info = &tcache_bin_info[binind];
429  if (unlikely(tbin->ncached == tbin_info->ncached_max)) {
430  tcache_bin_flush_small(tsd, tcache, tbin, binind,
431  (tbin_info->ncached_max >> 1));
432  }
433  assert(tbin->ncached < tbin_info->ncached_max);
434  tbin->ncached++;
435  *(tbin->avail - tbin->ncached) = ptr;
436 
437  tcache_event(tsd, tcache);
438 }
439 
441 tcache_dalloc_large(tsd_t *tsd, tcache_t *tcache, void *ptr, size_t size,
442  bool slow_path)
443 {
444  szind_t binind;
445  tcache_bin_t *tbin;
446  tcache_bin_info_t *tbin_info;
447 
448  assert((size & PAGE_MASK) == 0);
449  assert(tcache_salloc(tsd_tsdn(tsd), ptr) > SMALL_MAXCLASS);
451 
452  binind = size2index(size);
453 
454  if (slow_path && config_fill && unlikely(opt_junk_free))
456 
457  tbin = &tcache->tbins[binind];
458  tbin_info = &tcache_bin_info[binind];
459  if (unlikely(tbin->ncached == tbin_info->ncached_max)) {
460  tcache_bin_flush_large(tsd, tbin, binind,
461  (tbin_info->ncached_max >> 1), tcache);
462  }
463 
464  assert(tbin->ncached < tbin_info->ncached_max);
465  tbin->ncached++;
466  *(tbin->avail - tbin->ncached) = ptr;
467 
468  tcache_event(tsd, tcache);
469 }
470 
471 JEMALLOC_ALWAYS_INLINE tcache_t *
472 tcaches_get(tsd_t *tsd, unsigned ind)
473 {
474  tcaches_t *elm = &tcaches[ind];
475  if (unlikely(elm->tcache == NULL)) {
476  elm->tcache = tcache_create(tsd_tsdn(tsd), arena_choose(tsd,
477  NULL));
478  }
479  return (elm->tcache);
480 }
481 #endif
482 
483 #endif /* JEMALLOC_H_INLINES */
484 /******************************************************************************/
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_cgc.c:16
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd link
Definition: sflib.h:33
voidpf void uLong size
Definition: ioapi.h:138
unsigned szind_t
static const bool config_stats
arena_t * arena_choose(tsd_t *tsd, arena_t *arena)
szind_t size2index(size_t size)
bool opt_zero
static const bool config_prof
static const bool config_tcache
bool opt_junk_alloc
size_t index2size(szind_t index)
#define PAGE_MASK
static const bool config_fill
bool opt_junk_free
#define LG_PAGE
#define JEMALLOC_INLINE
#define JEMALLOC_ALWAYS_INLINE
return memset(p, 0, total)
#define likely(expr)
Definition: lz4.c:174
#define unlikely(expr)
Definition: lz4.c:177
assert(limit<=UINT32_MAX/2)
#define arena_mapbits_large_binind_set
#define arena_alloc_junk_small
#define tcache_alloc_small
#define tcache_event_hard
#define tcache_cleanup
#define opt_tcache
#define tcache_get
#define arena_dalloc_junk_large
#define tcaches_create
#define tcache_dalloc_large
#define arena_bin_info
#define tcache_postfork_child
#define tcaches_get
#define nhbins
#define arena_malloc_large
#define tcache_stats_merge
#define tcache_salloc
#define tcache_get_hard
#define tcache_alloc_large
#define tcaches_destroy
#define tsd_tcache_get
#define arena_dalloc_junk_small
#define tcache_bin_flush_large
#define tcache_create
#define tcache_postfork_parent
#define tcache_maxclass
#define tsd_tcache_enabled_get
#define opt_lg_tcache_max
#define tcache_alloc_easy
#define ticker_tick
#define tsd_tsdn
#define tsd_fetch
#define tcache_event
#define tcaches_flush
#define tcache_enabled_set
#define tsd_nominal
#define tcache_bin_flush_small
#define tcache_enabled_cleanup
#define tcaches
#define tcache_prefork
#define tcache_alloc_small_hard
#define tsd_tcache_enabled_set
#define tcache_dalloc_small
#define tcache_flush
#define tcache_bin_info
#define tsd_tcache_set
#define tcache_arena_reassociate
#define tcache_boot
#define tcache_enabled_get
#define ql_elm(a_type)
Definition: ql.h:9
unsigned long uint64_t
Definition: sftypes.h:28
int ssize_t
Definition: sftypes.h:39
_W64 unsigned int uintptr_t
Definition: malloc.c:21
Definition: gun.c:81