From 66048cb6cc273dc8270f775209b01db0363ecee8 Mon Sep 17 00:00:00 2001 From: daan Date: Tue, 19 May 2020 13:31:24 -0700 Subject: [PATCH 1/3] fix return value for page_unreset --- src/segment.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/segment.c b/src/segment.c index b2521524..d53ecfd1 100644 --- a/src/segment.c +++ b/src/segment.c @@ -245,7 +245,7 @@ static bool mi_page_unreset(mi_segment_t* segment, mi_page_t* page, size_t size, mi_assert_internal(page->is_reset); mi_assert_internal(page->is_committed); mi_assert_internal(!segment->mem_is_fixed); - if (segment->mem_is_fixed || !page->is_committed || !page->is_reset) return; + if (segment->mem_is_fixed || !page->is_committed || !page->is_reset) return true; page->is_reset = false; size_t psize; uint8_t* start = mi_segment_raw_page_start(segment, page, &psize); From 32b360858177bd8d78a2b35b9ced3c2c676fb4ac Mon Sep 17 00:00:00 2001 From: daan Date: Wed, 17 Jun 2020 13:12:05 -0700 Subject: [PATCH 2/3] simplify initial main tld declaration --- src/init.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/init.c b/src/init.c index 132043e8..92115283 100644 --- a/src/init.c +++ b/src/init.c @@ -105,10 +105,6 @@ const mi_heap_t _mi_heap_empty = { // the thread-local default heap for allocation mi_decl_thread mi_heap_t* _mi_heap_default = (mi_heap_t*)&_mi_heap_empty; - -#define tld_main_stats ((mi_stats_t*)((uint8_t*)&tld_main + offsetof(mi_tld_t,stats))) -#define tld_main_os ((mi_os_tld_t*)((uint8_t*)&tld_main + offsetof(mi_tld_t,os))) - extern mi_heap_t _mi_heap_main; static mi_tld_t tld_main = { @@ -116,9 +112,9 @@ static mi_tld_t tld_main = { &_mi_heap_main, &_mi_heap_main, { { NULL, NULL }, {NULL ,NULL}, {NULL ,NULL, 0}, 0, 0, 0, 0, 0, 0, NULL, - tld_main_stats, tld_main_os + &tld_main.stats, &tld_main.os }, // segments - { 0, tld_main_stats }, // os + { 0, &tld_main.stats }, // os { MI_STATS_NULL } // stats }; From 5a6d9ba8079398fad3a1dd67bd749327fc977437 Mon Sep 17 00:00:00 2001 From: daan Date: Wed, 17 Jun 2020 19:07:32 -0700 Subject: [PATCH 3/3] fix handling of failing to allocate heap metadata on thread creation, issue #257 --- src/init.c | 14 ++++++++++---- src/page.c | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/init.c b/src/init.c index 92115283..622a5eb0 100644 --- a/src/init.c +++ b/src/init.c @@ -176,10 +176,15 @@ static bool _mi_heap_init(void) { } else { // use `_mi_os_alloc` to allocate directly from the OS - mi_thread_data_t* td = (mi_thread_data_t*)_mi_os_alloc(sizeof(mi_thread_data_t),&_mi_stats_main); // Todo: more efficient allocation? + mi_thread_data_t* td = (mi_thread_data_t*)_mi_os_alloc(sizeof(mi_thread_data_t), &_mi_stats_main); // Todo: more efficient allocation? if (td == NULL) { - _mi_error_message(ENOMEM, "failed to allocate thread local heap memory\n"); - return false; + // if this fails, try once more. (issue #257) + td = (mi_thread_data_t*)_mi_os_alloc(sizeof(mi_thread_data_t), &_mi_stats_main); + if (td == NULL) { + // really out of memory + _mi_error_message(ENOMEM, "unable to allocate thread local heap metadata (%zu bytes)\n", sizeof(mi_thread_data_t)); + return false; + } } // OS allocated so already zero initialized mi_tld_t* tld = &td->tld; @@ -341,7 +346,8 @@ void mi_thread_init(void) mi_attr_noexcept // don't further initialize for the main thread if (_mi_is_main_thread()) return; - _mi_stat_increase(&mi_get_default_heap()->tld->stats.threads, 1); + mi_heap_t* heap = mi_get_default_heap(); + if (mi_heap_is_initialized(heap)) { _mi_stat_increase(&mi_get_default_heap()->tld->stats.threads, 1); } //_mi_verbose_message("thread init: 0x%zx\n", _mi_thread_id()); } diff --git a/src/page.c b/src/page.c index 18f1812e..c8a4e54b 100644 --- a/src/page.c +++ b/src/page.c @@ -816,6 +816,7 @@ void* _mi_malloc_generic(mi_heap_t* heap, size_t size) mi_attr_noexcept if (mi_unlikely(!mi_heap_is_initialized(heap))) { mi_thread_init(); // calls `_mi_heap_init` in turn heap = mi_get_default_heap(); + if (mi_unlikely(!mi_heap_is_initialized(heap))) { return NULL; } } mi_assert_internal(mi_heap_is_initialized(heap));