fix compilation on macOS

This commit is contained in:
Daan 2022-02-03 19:15:10 -08:00
parent 636931874f
commit 95a8196490
3 changed files with 42 additions and 24 deletions

View file

@ -65,7 +65,7 @@ terms of the MIT license. A copy of the license can be found in the file
#endif
#if !defined(MI_DEBUG_TRACE_LEN)
#define MI_DEBUG_TRACE_LEN (7) // store up to N frames if tracing is enabled
#define MI_DEBUG_TRACE_LEN (8) // store up to N frames if tracing is enabled
#endif
#if !defined(MI_PADDING_EXTRA) // use extra padding bytes? (so a stack trace can be preserved or next block corruption prevented)

View file

@ -434,7 +434,7 @@ static void mi_allocator_done(void) {
// Called once by the process loader
static void mi_process_load(void) {
mi_heap_main_init();
#if defined(MI_TLS_RECURSE_GUARD)
#if defined(__APPLE__) || defined(MI_TLS_RECURSE_GUARD)
volatile mi_heap_t* dummy = _mi_heap_default; // access TLS to allocate it before setting tls_initialized to true;
MI_UNUSED(dummy);
#endif

View file

@ -375,6 +375,14 @@ void _mi_stack_trace_capture(void** strace, size_t len, size_t skip) {
void _mi_stack_trace_print(const char* msg, void** strace, size_t len, const mi_block_t* block, size_t bsize, size_t avail) {
_mi_fprintf(NULL, NULL, "trace %s at %p of size %zu (%zub usable), allocated at:\n",
(msg==NULL ? "block" : msg), block, avail, bsize);
uintptr_t uninit = 0;
for( size_t i = 0; i < MI_INTPTR_SIZE; i++ ) {
uninit = (uninit << 8) | MI_DEBUG_UNINIT;
}
if (strace == NULL || uninit == (uintptr_t)strace[0]) {
_mi_fprintf(NULL, NULL, " (uninitialized trace)\n");
}
else {
PSYMBOL_INFO info = (PSYMBOL_INFO)_malloca(sizeof(SYMBOL_INFO) + 256 * sizeof(TCHAR));
if (info==NULL) return;
memset(info, 0, sizeof(info));
@ -392,6 +400,7 @@ void _mi_stack_trace_print(const char* msg, void** strace, size_t len, const mi_
}
SymCleanup(current_process);
}
}
#elif (MI_DEBUG_TRACE > 0) && (defined(__linux__) || defined(__FreeBSD__) || defined(__APPLE__))
#include <execinfo.h>
#define MI_MAX_TRACE_LEN (64)
@ -413,6 +422,14 @@ void _mi_stack_trace_capture(void** strace, size_t len, size_t skip) {
void _mi_stack_trace_print(const char* msg, void** strace, size_t len, const mi_block_t* block, size_t bsize, size_t avail) {
_mi_fprintf(NULL, NULL, "trace %s at %p of size %zu (%zub usable), allocated at:\n",
(msg==NULL ? "block" : msg), block, avail, bsize);
uintptr_t uninit = 0;
for( size_t i = 0; i < MI_INTPTR_SIZE; i++ ) {
uninit = (uninit << 8) | MI_DEBUG_UNINIT;
}
if (strace == NULL || uninit == (uintptr_t)strace[0]) {
_mi_fprintf(NULL, NULL, " (uninitialized trace)\n");
}
else {
while( len > 0 && strace[len-1] == NULL) { len--; }
if (len == 0) return;
char** names = backtrace_symbols(strace, len);
@ -421,6 +438,7 @@ void _mi_stack_trace_print(const char* msg, void** strace, size_t len, const mi_
}
// free(names); // avoid potential recursion and leak the trace
}
}
#else
void _mi_stack_trace_capture(void** strace, size_t len, size_t skip) {
MI_UNUSED(strace); MI_UNUSED(len); MI_UNUSED(skip);