Skip to content

Commit

Permalink
avoid static obj init order issues with mem mgmt routines
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrush committed Jul 6, 2021
1 parent f693a2c commit 4ae81e8
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/libs/conduit/conduit_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,23 +135,31 @@ set_memset_handler(void(*conduit_hnd_memset)(void*,
conduit_handle_memset = conduit_hnd_memset;
}


//-----------------------------------------------------------------------------
static std::map<index_t,void*(*)(size_t, size_t)> allocator_map
= {{0, &default_alloc_handler}};
static std::map<index_t,void*(*)(size_t, size_t)> &allocator_map()
{
static std::map<index_t,void*(*)(size_t, size_t)> _allocator_map
= {{0, &default_alloc_handler}};
return _allocator_map;
}

//-----------------------------------------------------------------------------
static std::map<index_t,void(*)(void*)> free_map
= {{0, default_free_handler}};
static std::map<index_t,void(*)(void*)> &free_map()
{
//-----------------------------------------------------------------------------
static std::map<index_t,void(*)(void*)> _free_map
= {{0, default_free_handler}};
return _free_map;
}

//-----------------------------------------------------------------------------
index_t
register_allocator(void*(*conduit_hnd_allocate) (size_t, size_t),
void(*conduit_hnd_free)(void *))
{
static index_t allocator_id = 1;
allocator_map[allocator_id] = conduit_hnd_allocate;
free_map[allocator_id] = conduit_hnd_free;
allocator_map()[allocator_id] = conduit_hnd_allocate;
free_map()[allocator_id] = conduit_hnd_free;
return allocator_id++;
}

Expand All @@ -162,15 +170,15 @@ conduit_allocate(size_t n_items,
size_t item_size,
index_t allocator_id)
{
return allocator_map[allocator_id](n_items, item_size);
return allocator_map()[allocator_id](n_items, item_size);
}

//-----------------------------------------------------------------------------
void
conduit_free(void *ptr,
index_t allocator_id)
{
free_map[allocator_id](ptr);
free_map()[allocator_id](ptr);
}

//-----------------------------------------------------------------------------
Expand Down

0 comments on commit 4ae81e8

Please sign in to comment.