Coverage Report

Created: 2026-10-31 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Include/internal/mimalloc/mimalloc/prim.h
Line
Count
Source
1
/* ----------------------------------------------------------------------------
2
Copyright (c) 2018-2024, Microsoft Research, Daan Leijen
3
This is free software; you can redistribute it and/or modify it under the
4
terms of the MIT license. A copy of the license can be found in the file
5
"LICENSE" at the root of this distribution.
6
-----------------------------------------------------------------------------*/
7
#pragma once
8
#ifndef MIMALLOC_PRIM_H
9
#define MIMALLOC_PRIM_H
10
#include "internal.h"             // mi_decl_hidden
11
12
// --------------------------------------------------------------------------
13
// This file specifies the primitive portability API.
14
// Each OS/host needs to implement these primitives, see `src/prim`
15
// for implementations on Window, macOS, WASI, and Linux/Unix.
16
//
17
// note: on all primitive functions, we always have result parameters != NULL, and:
18
//  addr != NULL and page aligned
19
//  size > 0     and page aligned
20
//  the return value is an error code as an `int` where 0 is success
21
// --------------------------------------------------------------------------
22
23
// OS memory configuration
24
typedef struct mi_os_mem_config_s {
25
  size_t  page_size;              // default to 4KiB
26
  size_t  large_page_size;        // 0 if not supported, usually 2MiB
27
  size_t  alloc_granularity;      // smallest allocation size (usually 4KiB, on Windows 64KiB)
28
  size_t  physical_memory_in_kib; // physical memory size in KiB
29
  size_t  virtual_address_bits;   // usually 48 or 56 bits on 64-bit systems. (used to determine secure randomization)
30
  bool    has_overcommit;         // can we reserve more memory than can be actually committed?
31
  bool    has_partial_free;       // can allocated blocks be freed partially? (true for mmap, false for VirtualAlloc)
32
  bool    has_virtual_reserve;    // supports virtual address space reservation? (if true we can reserve virtual address space without using commit or physical memory)
33
} mi_os_mem_config_t;
34
35
// Initialize
36
void _mi_prim_mem_init( mi_os_mem_config_t* config );
37
38
// Free OS memory
39
int _mi_prim_free(void* addr, size_t size );
40
41
// Allocate OS memory. Return NULL on error.
42
// The `try_alignment` is just a hint and the returned pointer does not have to be aligned.
43
// If `commit` is false, the virtual memory range only needs to be reserved (with no access)
44
// which will later be committed explicitly using `_mi_prim_commit`.
45
// `is_zero` is set to true if the memory was zero initialized (as on most OS's)
46
// The `hint_addr` address is either `NULL` or a preferred allocation address but can be ignored.
47
// pre: !commit => !allow_large
48
//      try_alignment >= _mi_os_page_size() and a power of 2
49
int _mi_prim_alloc(void* hint_addr, size_t size, size_t try_alignment, bool commit, bool allow_large, bool* is_large, bool* is_zero, void** addr);
50
51
// Commit memory. Returns error code or 0 on success.
52
// For example, on Linux this would make the memory PROT_READ|PROT_WRITE.
53
// `is_zero` is set to true if the memory was zero initialized (e.g. on Windows)
54
int _mi_prim_commit(void* addr, size_t size, bool* is_zero);
55
56
// Decommit memory. Returns error code or 0 on success. The `needs_recommit` result is true
57
// if the memory would need to be re-committed. For example, on Windows this is always true,
58
// but on Linux we could use MADV_DONTNEED to decommit which does not need a recommit.
59
// pre: needs_recommit != NULL
60
int _mi_prim_decommit(void* addr, size_t size, bool* needs_recommit);
61
62
// Reset memory. The range keeps being accessible but the content might be reset to zero at any moment.
63
// Returns error code or 0 on success.
64
int _mi_prim_reset(void* addr, size_t size);
65
66
// Reuse memory. This is called for memory that is already committed but
67
// may have been reset (`_mi_prim_reset`) or decommitted (`_mi_prim_decommit`) where `needs_recommit` was false.
68
// Returns error code or 0 on success. On most platforms this is a no-op.
69
int _mi_prim_reuse(void* addr, size_t size);
70
71
// Protect memory. Returns error code or 0 on success.
72
int _mi_prim_protect(void* addr, size_t size, bool protect);
73
74
// Allocate huge (1GiB) pages possibly associated with a NUMA node.
75
// `is_zero` is set to true if the memory was zero initialized (as on most OS's)
76
// pre: size > 0  and a multiple of 1GiB.
77
//      numa_node is either negative (don't care), or a numa node number.
78
int _mi_prim_alloc_huge_os_pages(void* hint_addr, size_t size, int numa_node, bool* is_zero, void** addr);
79
80
// Return the current NUMA node
81
size_t _mi_prim_numa_node(void);
82
83
// Return the number of logical NUMA nodes
84
size_t _mi_prim_numa_node_count(void);
85
86
// Clock ticks
87
mi_msecs_t _mi_prim_clock_now(void);
88
89
// Return process information (only for statistics)
90
typedef struct mi_process_info_s {
91
  mi_msecs_t  elapsed;
92
  mi_msecs_t  utime;
93
  mi_msecs_t  stime;
94
  size_t      current_rss;
95
  size_t      peak_rss;
96
  size_t      current_commit;
97
  size_t      peak_commit;
98
  size_t      page_faults;
99
} mi_process_info_t;
100
101
void _mi_prim_process_info(mi_process_info_t* pinfo);
102
103
// Default stderr output. (only for warnings etc. with verbose enabled)
104
// msg != NULL && _mi_strlen(msg) > 0
105
void _mi_prim_out_stderr( const char* msg );
106
107
// Get an environment variable. (only for options)
108
// name != NULL, result != NULL, result_size >= 64
109
// Return 1 for success, 0 if not found,
110
// and -1 on error (for example, if `getenv` cannot be called yet during preloading).
111
int _mi_prim_getenv(const char* name, char* result, size_t result_size);
112
113
114
// Fill a buffer with strong randomness; return `false` on error or if
115
// there is no strong randomization available.
116
bool _mi_prim_random_buf(void* buf, size_t buf_len);
117
118
// Called on the first thread start, and should ensure `_mi_thread_done` is called on thread termination.
119
void _mi_prim_thread_init_auto_done(void);
120
121
// Called on process exit and may take action to clean up resources associated with the thread auto done.
122
void _mi_prim_thread_done_auto_done(void);
123
124
// Called when the default heap for a thread changes
125
void _mi_prim_thread_associate_default_heap(mi_heap_t* heap);
126
127
128
//-------------------------------------------------------------------
129
// Access to TLS (thread local storage) slots.
130
// We need fast access to both a unique thread id (in `free.c:mi_free`) and
131
// to a thread-local heap pointer (in `alloc.c:mi_malloc`).
132
// To achieve this we use specialized code for various platforms.
133
//-------------------------------------------------------------------
134
135
// On some libc + platform combinations we can directly access a thread-local storage (TLS) slot.
136
// The TLS layout depends on both the OS and libc implementation so we use specific tests for each main platform.
137
// If you test on another platform and it works please send a PR :-)
138
// see also https://akkadia.org/drepper/tls.pdf for more info on the TLS register.
139
//
140
// Note: we would like to prefer `__builtin_thread_pointer()` nowadays instead of using assembly,
141
// but unfortunately we can not detect support reliably (see issue #883)
142
// We also use it on Apple OS as we use a TLS slot for the default heap there.
143
#if defined(__GNUC__) && ( \
144
           (defined(__GLIBC__)   && (defined(__x86_64__) || defined(__i386__) || (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__) || defined(__riscv))) \
145
        || (defined(__APPLE__)   && (defined(__x86_64__) || defined(__aarch64__) || defined(__POWERPC__))) \
146
        || (defined(__BIONIC__)  && (defined(__x86_64__) || defined(__i386__) || (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__))) \
147
        || (defined(__FreeBSD__) && (defined(__x86_64__) || defined(__i386__) || defined(__aarch64__))) \
148
        || (defined(__OpenBSD__) && (defined(__x86_64__) || defined(__i386__) || defined(__aarch64__))) \
149
      )
150
151
#define MI_HAS_TLS_SLOT    1
152
153
0
static inline void* mi_prim_tls_slot(size_t slot) mi_attr_noexcept {
154
0
  void* res;
155
0
  const size_t ofs = (slot*sizeof(void*));
156
0
  #if defined(__i386__)
157
0
    __asm__("movl %%gs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : );  // x86 32-bit always uses GS
158
0
  #elif defined(__APPLE__) && defined(__x86_64__)
159
0
    __asm__("movq %%gs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : );  // x86_64 macOSX uses GS
160
0
  #elif defined(__x86_64__) && (MI_INTPTR_SIZE==4)
161
0
    __asm__("movl %%fs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : );  // x32 ABI
162
0
  #elif defined(__x86_64__)
163
0
    __asm__("movq %%fs:%1, %0" : "=r" (res) : "m" (*((void**)ofs)) : );  // x86_64 Linux, BSD uses FS
164
0
  #elif defined(__arm__)
165
0
    void** tcb; MI_UNUSED(ofs);
166
0
    __asm__ volatile ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tcb));
167
0
    res = tcb[slot];
168
0
  #elif defined(__aarch64__)
169
0
    // CPython: patched for watchOS support.
170
0
    // Submitted upstream as microsoft/mimalloc#1444
171
0
    // Use a 64-bit temporary: on arm64_32 (ILP32), pointers are 32 bits
172
0
    // but system registers are 64 bits.
173
0
    uint64_t tp; MI_UNUSED(ofs);
174
0
    #if defined(__APPLE__) // M1, issue #343
175
0
    __asm__ volatile ("mrs %0, tpidrro_el0\nbic %0, %0, #7" : "=r" (tp));
176
0
    #else
177
0
    __asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tp));
178
0
    #endif
179
0
    void** tcb = (void**)(uintptr_t)tp;
180
0
    res = tcb[slot];
181
0
  #elif defined(__riscv)
182
0
    void** tcb; MI_UNUSED(ofs);
183
0
    __asm__ volatile ("mv %0, tp" : "=r" (tcb));
184
0
    res = tcb[slot];
185
0
  #elif defined(__APPLE__) && defined(__POWERPC__) // ppc, issue #781
186
0
    MI_UNUSED(ofs);
187
0
    res = pthread_getspecific(slot);
188
0
  #endif
189
0
  return res;
190
0
}
191
192
// setting a tls slot is only used on macOS for now
193
0
static inline void mi_prim_tls_slot_set(size_t slot, void* value) mi_attr_noexcept {
194
0
  const size_t ofs = (slot*sizeof(void*));
195
0
  #if defined(__i386__)
196
0
    __asm__("movl %1,%%gs:%0" : "=m" (*((void**)ofs)) : "rn" (value) : );  // 32-bit always uses GS
197
0
  #elif defined(__APPLE__) && defined(__x86_64__)
198
0
    __asm__("movq %1,%%gs:%0" : "=m" (*((void**)ofs)) : "rn" (value) : );  // x86_64 macOS uses GS
199
0
  #elif defined(__x86_64__) && (MI_INTPTR_SIZE==4)
200
0
    __asm__("movl %1,%%fs:%0" : "=m" (*((void**)ofs)) : "rn" (value) : );  // x32 ABI
201
0
  #elif defined(__x86_64__)
202
0
    __asm__("movq %1,%%fs:%0" : "=m" (*((void**)ofs)) : "rn" (value) : );  // x86_64 Linux, BSD uses FS
203
0
  #elif defined(__arm__)
204
0
    void** tcb; MI_UNUSED(ofs);
205
0
    __asm__ volatile ("mrc p15, 0, %0, c13, c0, 3\nbic %0, %0, #3" : "=r" (tcb));
206
0
    tcb[slot] = value;
207
0
  #elif defined(__aarch64__)
208
0
    // Use a 64-bit temporary: on arm64_32 (ILP32), pointers are 32 bits
209
0
    // but system registers are 64 bits.
210
0
    uint64_t tp; MI_UNUSED(ofs);
211
0
    #if defined(__APPLE__) // M1, issue #343
212
0
    __asm__ volatile ("mrs %0, tpidrro_el0\nbic %0, %0, #7" : "=r" (tp));
213
0
    #else
214
0
    __asm__ volatile ("mrs %0, tpidr_el0" : "=r" (tp));
215
0
    #endif
216
0
    void** tcb = (void**)(uintptr_t)tp;
217
0
    tcb[slot] = value;
218
0
  #elif defined(__riscv)
219
0
    void** tcb; MI_UNUSED(ofs);
220
0
    __asm__ volatile ("mv %0, tp" : "=r" (tcb));
221
0
    tcb[slot] = value;
222
0
  #elif defined(__APPLE__) && defined(__POWERPC__) // ppc, issue #781
223
0
    MI_UNUSED(ofs);
224
0
    pthread_setspecific(slot, value);
225
0
  #endif
226
0
}
227
228
#elif _WIN32 && MI_WIN_USE_FIXED_TLS && !defined(MI_WIN_USE_FLS)
229
230
// On windows we can store the thread-local heap at a fixed TLS slot to avoid
231
// thread-local initialization checks in the fast path.
232
// We allocate a user TLS slot at process initialization (see `windows/prim.c`)
233
// and store the offset `_mi_win_tls_offset`.
234
#define MI_HAS_TLS_SLOT  1              // 2 = we can reliably initialize the slot (saving a test on each malloc)
235
236
extern mi_decl_hidden size_t _mi_win_tls_offset;
237
238
#if MI_WIN_USE_FIXED_TLS > 1
239
#define MI_TLS_SLOT     (MI_WIN_USE_FIXED_TLS)
240
#elif MI_SIZE_SIZE == 4
241
#define MI_TLS_SLOT     (0x0E10 + _mi_win_tls_offset)  // User TLS slots <https://en.wikipedia.org/wiki/Win32_Thread_Information_Block>
242
#else
243
#define MI_TLS_SLOT     (0x1480 + _mi_win_tls_offset)  // User TLS slots <https://en.wikipedia.org/wiki/Win32_Thread_Information_Block>
244
#endif
245
246
static inline void* mi_prim_tls_slot(size_t slot) mi_attr_noexcept {
247
  #if (_M_X64 || _M_AMD64) && !defined(_M_ARM64EC)
248
  return (void*)__readgsqword((unsigned long)slot);   // direct load at offset from gs
249
  #elif _M_IX86 && !defined(_M_ARM64EC)
250
  return (void*)__readfsdword((unsigned long)slot);   // direct load at offset from fs
251
  #else
252
  return ((void**)NtCurrentTeb())[slot / sizeof(void*)];
253
  #endif
254
}
255
static inline void mi_prim_tls_slot_set(size_t slot, void* value) mi_attr_noexcept {
256
  ((void**)NtCurrentTeb())[slot / sizeof(void*)] = value;
257
}
258
259
#endif
260
261
262
263
//-------------------------------------------------------------------
264
// Get a fast unique thread id.
265
//
266
// Getting the thread id should be performant as it is called in the
267
// fast path of `_mi_free` and we specialize for various platforms as
268
// inlined definitions. Regular code should call `init.c:_mi_thread_id()`.
269
// We only require _mi_prim_thread_id() to return a unique id
270
// for each thread (unequal to zero).
271
//-------------------------------------------------------------------
272
273
274
// Do we have __builtin_thread_pointer? This would be the preferred way to get a unique thread id
275
// but unfortunately, it seems we cannot test for this reliably at this time (see issue #883)
276
// Nevertheless, it seems needed on older graviton platforms (see issue #851).
277
// For now, we only enable this for specific platforms.
278
#if !defined(MI_USE_BUILTIN_THREAD_POINTER)   /* allow user override */
279
  #if !defined(__APPLE__)  /* on apple (M1) the wrong register is read (tpidr_el0 instead of tpidrro_el0) so fall back to TLS slot assembly (<https://github.com/microsoft/mimalloc/issues/343#issuecomment-763272369>)*/ \
280
      && !defined(__CYGWIN__) \
281
      && !defined(MI_LIBC_MUSL) \
282
      && (!defined(__clang_major__) || __clang_major__ >= 14)  /* older clang versions emit bad code; fall back to using the TLS slot (<https://lore.kernel.org/linux-arm-kernel/202110280952.352F66D8@keescook/T/>) */
283
    #if    (defined(__GNUC__) && (__GNUC__ >= 7)  && defined(__aarch64__)) /* aarch64 for older gcc versions (issue #851) */ \
284
        || (defined(__GNUC__) && (__GNUC__ >= 7)  && defined(__riscv)) \
285
        || (defined(__GNUC__) && (__GNUC__ >= 11) && defined(__x86_64__)) \
286
        || (defined(__clang_major__) && (__clang_major__ >= 14) && (defined(__aarch64__) || defined(__x86_64__) || defined(__riscv))) /* pr #1363 */
287
      #define MI_USE_BUILTIN_THREAD_POINTER  1
288
    #endif
289
  #endif
290
#endif
291
292
293
294
// defined in `init.c`; do not use these directly
295
extern mi_decl_hidden mi_decl_thread mi_heap_t* _mi_heap_default;  // default heap to allocate from
296
extern mi_decl_hidden bool _mi_process_is_initialized;             // has mi_process_init been called?
297
298
static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept;
299
300
// Get a unique id for the current thread.
301
#if defined(MI_PRIM_THREAD_ID)
302
303
static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept {
304
  const mi_threadid_t tid = MI_PRIM_THREAD_ID();  // used for example by CPython for a free threaded build (see python/cpython#115488)
305
  mi_assert_internal( (tid & 0x03) == 0 );        // mimalloc reserves the bottom 2 bits
306
  return tid;
307
}
308
309
#elif defined(_WIN32)
310
311
static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept {
312
  // Windows: works on Intel and ARM in both 32- and 64-bit
313
  return (uintptr_t)NtCurrentTeb();
314
}
315
316
#elif MI_USE_BUILTIN_THREAD_POINTER
317
318
222
static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept {
319
  // Works on most Unix based platforms with recent compilers
320
222
  return (uintptr_t)__builtin_thread_pointer();
321
222
}
322
323
#elif MI_HAS_TLS_SLOT
324
325
static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept {
326
  #if defined(__BIONIC__)
327
    // issue #384, #495: on the Bionic libc (Android), slot 1 is the thread id
328
    // see: https://github.com/aosp-mirror/platform_bionic/blob/c44b1d0676ded732df4b3b21c5f798eacae93228/libc/platform/bionic/tls_defines.h#L86
329
    return (uintptr_t)mi_prim_tls_slot(1);
330
  #else
331
    // in all our other targets, slot 0 is the thread id
332
    // glibc: https://sourceware.org/git/?p=glibc.git;a=blob_plain;f=sysdeps/x86_64/nptl/tls.h
333
    // apple: https://github.com/apple/darwin-xnu/blob/main/libsyscall/os/tsd.h#L36
334
    return (uintptr_t)mi_prim_tls_slot(0);
335
  #endif
336
}
337
338
#else
339
340
// otherwise use portable C, taking the address of a thread local variable (this is still very fast on most platforms).
341
static inline mi_threadid_t _mi_prim_thread_id(void) mi_attr_noexcept {
342
  return (uintptr_t)&_mi_heap_default;
343
}
344
345
#endif
346
347
348
349
/* ----------------------------------------------------------------------------------------
350
Get the thread local default heap: `_mi_prim_get_default_heap()`
351
352
This is inlined here as it is on the fast path for allocation functions.
353
354
On most platforms (Windows, Linux, FreeBSD, NetBSD, etc), this just returns a
355
__thread local variable (`_mi_heap_default`). With the initial-exec TLS model this ensures
356
that the storage will always be available (allocated on the thread stacks).
357
358
On some platforms though we cannot use that when overriding `malloc` since the underlying
359
TLS implementation (or the loader) will call itself `malloc` on a first access and recurse.
360
We try to circumvent this in an efficient way:
361
- macOSX : we use an unused TLS slot from the OS allocated slots (MI_TLS_SLOT). On OSX, the
362
           loader itself calls `malloc` even before the modules are initialized.
363
- OpenBSD: we use an unused slot from the pthread block (MI_TLS_PTHREAD_SLOT_OFS).
364
- DragonFly: defaults are working but seem slow compared to freeBSD (see PR #323)
365
------------------------------------------------------------------------------------------- */
366
367
static inline mi_heap_t* mi_prim_get_default_heap(void);
368
369
#if defined(MI_MALLOC_OVERRIDE)
370
#if defined(__APPLE__) // macOS
371
  #define MI_TLS_SLOT               89  // seems unused?
372
  // other possible unused ones are 9, 29, __PTK_FRAMEWORK_JAVASCRIPTCORE_KEY4 (94), __PTK_FRAMEWORK_GC_KEY9 (112) and __PTK_FRAMEWORK_OLDGC_KEY9 (89)
373
  // see <https://github.com/rweichler/substrate/blob/master/include/pthread_machdep.h>
374
#elif defined(__OpenBSD__)
375
  // use end bytes of a name; goes wrong if anyone uses names > 23 characters (ptrhread specifies 16)
376
  // see <https://github.com/openbsd/src/blob/master/lib/libc/include/thread_private.h#L371>
377
  #define MI_TLS_PTHREAD_SLOT_OFS   (6*sizeof(int) + 4*sizeof(void*) + 24)
378
  // #elif defined(__DragonFly__)
379
  // #warning "mimalloc is not working correctly on DragonFly yet."
380
  // #define MI_TLS_PTHREAD_SLOT_OFS   (4 + 1*sizeof(void*))  // offset `uniqueid` (also used by gdb?) <https://github.com/DragonFlyBSD/DragonFlyBSD/blob/master/lib/libthread_xu/thread/thr_private.h#L458>
381
#elif defined(__ANDROID__)
382
  // See issue #381
383
  #define MI_TLS_PTHREAD
384
#endif
385
#endif
386
387
388
#if MI_TLS_SLOT
389
# if !defined(MI_HAS_TLS_SLOT)
390
#  error "trying to use a TLS slot for the default heap, but the mi_prim_tls_slot primitives are not defined"
391
# endif
392
393
static inline mi_heap_t* mi_prim_get_default_heap(void) {
394
  mi_heap_t* heap = (mi_heap_t*)mi_prim_tls_slot(MI_TLS_SLOT);
395
  #if MI_HAS_TLS_SLOT == 1   // check if the TLS slot is initialized
396
  if mi_unlikely(heap == NULL) {
397
    #ifdef __GNUC__
398
    __asm(""); // prevent conditional load of the address of _mi_heap_empty
399
    #endif
400
    heap = (mi_heap_t*)&_mi_heap_empty;
401
  }
402
  #endif
403
  return heap;
404
}
405
406
#elif defined(MI_TLS_PTHREAD_SLOT_OFS)
407
408
static inline mi_heap_t** mi_prim_tls_pthread_heap_slot(void) {
409
  pthread_t self = pthread_self();
410
  #if defined(__DragonFly__)
411
  if (self==NULL) return NULL;
412
  #endif
413
  return (mi_heap_t**)((uint8_t*)self + MI_TLS_PTHREAD_SLOT_OFS);
414
}
415
416
static inline mi_heap_t* mi_prim_get_default_heap(void) {
417
  mi_heap_t** pheap = mi_prim_tls_pthread_heap_slot();
418
  if mi_unlikely(pheap == NULL) return _mi_heap_main_get();
419
  mi_heap_t* heap = *pheap;
420
  if mi_unlikely(heap == NULL) return (mi_heap_t*)&_mi_heap_empty;
421
  return heap;
422
}
423
424
#elif defined(MI_TLS_PTHREAD)
425
426
extern mi_decl_hidden pthread_key_t _mi_heap_default_key;
427
static inline mi_heap_t* mi_prim_get_default_heap(void) {
428
  mi_heap_t* heap = (mi_unlikely(_mi_heap_default_key == (pthread_key_t)(-1)) ? _mi_heap_main_get() : (mi_heap_t*)pthread_getspecific(_mi_heap_default_key));
429
  return (mi_unlikely(heap == NULL) ? (mi_heap_t*)&_mi_heap_empty : heap);
430
}
431
432
#else // default using a thread local variable; used on most platforms.
433
434
111
static inline mi_heap_t* mi_prim_get_default_heap(void) {
435
  #if defined(MI_TLS_RECURSE_GUARD)
436
  if (mi_unlikely(!_mi_process_is_initialized)) return _mi_heap_main_get();
437
  #endif
438
111
  return _mi_heap_default;
439
111
}
440
441
#endif  // mi_prim_get_default_heap()
442
443
444
#endif  // MIMALLOC_PRIM_H