mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-11 17:59:31 +03:00
fix concurrent mi_tld access bug
This commit is contained in:
parent
4aeb2e1005
commit
13ee94cef6
3 changed files with 21 additions and 12 deletions
|
@ -93,7 +93,7 @@ static mi_meta_page_t* mi_meta_page_zalloc(void) {
|
||||||
|
|
||||||
|
|
||||||
// allocate meta-data
|
// allocate meta-data
|
||||||
void* _mi_meta_zalloc( size_t size, mi_memid_t* pmemid )
|
mi_decl_noinline void* _mi_meta_zalloc( size_t size, mi_memid_t* pmemid )
|
||||||
{
|
{
|
||||||
mi_assert_internal(pmemid != NULL);
|
mi_assert_internal(pmemid != NULL);
|
||||||
size = _mi_align_up(size,MI_META_BLOCK_SIZE);
|
size = _mi_align_up(size,MI_META_BLOCK_SIZE);
|
||||||
|
@ -133,7 +133,7 @@ void* _mi_meta_zalloc( size_t size, mi_memid_t* pmemid )
|
||||||
}
|
}
|
||||||
|
|
||||||
// free meta-data
|
// free meta-data
|
||||||
void _mi_meta_free(void* p, size_t size, mi_memid_t memid) {
|
mi_decl_noinline void _mi_meta_free(void* p, size_t size, mi_memid_t memid) {
|
||||||
if (p==NULL) return;
|
if (p==NULL) return;
|
||||||
if (memid.memkind == MI_MEM_META) {
|
if (memid.memkind == MI_MEM_META) {
|
||||||
mi_assert_internal(_mi_divide_up(size, MI_META_BLOCK_SIZE) == memid.mem.meta.block_count);
|
mi_assert_internal(_mi_divide_up(size, MI_META_BLOCK_SIZE) == memid.mem.meta.block_count);
|
||||||
|
|
|
@ -551,6 +551,7 @@ static mi_page_t* mi_arena_page_try_find_abandoned(size_t slice_count, size_t bl
|
||||||
|
|
||||||
// any abandoned in our size class?
|
// any abandoned in our size class?
|
||||||
mi_subproc_t* const subproc = tld->subproc;
|
mi_subproc_t* const subproc = tld->subproc;
|
||||||
|
mi_assert_internal(subproc != NULL);
|
||||||
if (mi_atomic_load_relaxed(&subproc->abandoned_count[bin]) == 0) return NULL;
|
if (mi_atomic_load_relaxed(&subproc->abandoned_count[bin]) == 0) return NULL;
|
||||||
|
|
||||||
// search arena's
|
// search arena's
|
||||||
|
|
26
src/init.c
26
src/init.c
|
@ -134,7 +134,8 @@ static mi_decl_cache_align mi_subproc_t mi_subproc_default;
|
||||||
|
|
||||||
static mi_decl_cache_align mi_tld_t tld_main = {
|
static mi_decl_cache_align mi_tld_t tld_main = {
|
||||||
0,
|
0,
|
||||||
&_mi_heap_main, &_mi_heap_main,
|
&_mi_heap_main, // heap_backing
|
||||||
|
&_mi_heap_main, // heaps list
|
||||||
&mi_subproc_default, // subproc
|
&mi_subproc_default, // subproc
|
||||||
0, // tseq
|
0, // tseq
|
||||||
MI_MEMID_STATIC, // memid
|
MI_MEMID_STATIC, // memid
|
||||||
|
@ -271,7 +272,20 @@ static mi_tld_t* mi_tld_alloc(void) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mi_tld_t* _mi_tld(void) {
|
#define MI_TLD_INVALID ((mi_tld_t*)1)
|
||||||
|
|
||||||
|
static mi_decl_noinline void mi_tld_free(void) {
|
||||||
|
mi_tld_t* tld = _mi_tld();
|
||||||
|
mi_tld = MI_TLD_INVALID;
|
||||||
|
_mi_meta_free(tld, sizeof(mi_tld_t), tld->memid);
|
||||||
|
}
|
||||||
|
|
||||||
|
mi_tld_t* mi_decl_noinline _mi_tld(void) {
|
||||||
|
if (mi_tld == MI_TLD_INVALID) {
|
||||||
|
_mi_error_message(EFAULT, "internal error: tld accessed after the thread terminated\n");
|
||||||
|
abort();
|
||||||
|
mi_tld = NULL;
|
||||||
|
}
|
||||||
if (mi_tld==NULL) {
|
if (mi_tld==NULL) {
|
||||||
mi_tld = mi_tld_alloc();
|
mi_tld = mi_tld_alloc();
|
||||||
}
|
}
|
||||||
|
@ -409,9 +423,6 @@ static bool _mi_thread_heap_done(mi_heap_t* heap) {
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
// free the tld
|
|
||||||
mi_tld_t* tld = _mi_tld();
|
|
||||||
_mi_meta_free(_mi_tld(), sizeof(mi_tld_t), tld->memid);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -497,10 +508,7 @@ void _mi_thread_done(mi_heap_t* heap)
|
||||||
_mi_thread_heap_done(heap); // returns true if already ran
|
_mi_thread_heap_done(heap); // returns true if already ran
|
||||||
|
|
||||||
// free thread local data
|
// free thread local data
|
||||||
if (mi_tld != NULL) {
|
mi_tld_free();
|
||||||
_mi_meta_free(mi_tld, sizeof(mi_tld_t), mi_tld->memid);
|
|
||||||
mi_tld = NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void _mi_heap_set_default_direct(mi_heap_t* heap) {
|
void _mi_heap_set_default_direct(mi_heap_t* heap) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue