summaryrefslogtreecommitdiff
path: root/include/mimalloc-internal.h
diff options
context:
space:
mode:
authordaan <daan@microsoft.com>2020-01-29 23:08:12 -0800
committerdaan <daan@microsoft.com>2020-01-29 23:08:52 -0800
commited1c8a203ab0ce9df97919767d01bc3f180ec2f1 (patch)
tree6b958276531754629e47bd252cec9ec0fcee480a /include/mimalloc-internal.h
parent03b363a1c289ad4461c219050466a9f7de0b8432 (diff)
improve performance with tls recursion counter
Diffstat (limited to 'include/mimalloc-internal.h')
-rw-r--r--include/mimalloc-internal.h19
1 files changed, 11 insertions, 8 deletions
diff --git a/include/mimalloc-internal.h b/include/mimalloc-internal.h
index f4b578f..b2e57ae 100644
--- a/include/mimalloc-internal.h
+++ b/include/mimalloc-internal.h
@@ -275,24 +275,27 @@ extern const mi_heap_t _mi_heap_empty; // read-only empty heap, initial value o
extern mi_heap_t _mi_heap_main; // statically allocated main backing heap
extern bool _mi_process_is_initialized;
+extern mi_decl_thread mi_heap_t* _mi_heap_default; // default heap to allocate from
#ifdef MI_TLS_RECURSE_GUARD
extern mi_heap_t* _mi_get_default_heap_tls_safe(void);
+extern size_t _mi_tls_recurse;
+#endif
+
static inline mi_heap_t* mi_get_default_heap(void) {
+ #ifdef MI_TLS_RECURSE_GUARD
+ if (_mi_tls_recurse++>100) {
// on some BSD platforms, like macOS, the dynamic loader calls `malloc`
// to initialize thread local data. To avoid recursion, we need to avoid
// accessing the thread local `_mi_default_heap` until our module is loaded
// and use the statically allocated main heap until that time.
// TODO: patch ourselves dynamically to avoid this check every time?
- return _mi_get_default_heap_tls_safe();
-#else
-
-extern mi_decl_thread mi_heap_t* _mi_heap_default; // default heap to allocate from
-
-static inline mi_heap_t* mi_get_default_heap(void) {
+ mi_heap_t* heap = _mi_get_default_heap_tls_safe();
+ _mi_tls_recurse = 0;
+ return heap;
+ }
+ #endif
return _mi_heap_default;
-
-#endif
}
static inline bool mi_heap_is_default(const mi_heap_t* heap) {