Reduce dependence on C++ runtime

If the external thread statics are used, then
we don't need to include some C++ runtime
concepts. This refactoring moves some global initialization under
conditional compilation.
This commit is contained in:
Matthew Parkinson
2020-02-26 17:55:29 +00:00
parent 3775a625a4
commit 9f53ec0ef8
10 changed files with 114 additions and 36 deletions

View File

@@ -0,0 +1,40 @@
#define SNMALLOC_EXTERNAL_THREAD_ALLOC
#include <mem/globalalloc.h>
using namespace snmalloc;
class ThreadAllocUntyped
{
public:
static void* get()
{
static thread_local void* alloc = nullptr;
if (alloc != nullptr)
{
return alloc;
}
alloc = current_alloc_pool()->acquire();
return alloc;
}
};
#include <override/malloc.cc>
int main()
{
MemoryProviderStateMixin<DefaultPal> mp;
// 28 is large enough to produce a nested allocator.
// It is also large enough for the example to run in.
// For 1MiB superslabs, SUPERSLAB_BITS + 4 is not big enough for the example.
auto a = ThreadAlloc::get();
for (size_t i = 0; i < 1000; i++)
{
auto r1 = a->alloc(100);
free(r1);
}
}