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

@@ -11,16 +11,6 @@ extern "C" void _malloc_thread_cleanup();
namespace snmalloc
{
/**
* A global fake allocator object. This never allocates memory and, as a
* result, never owns any slabs. On the slow paths, where it would fetch
* slabs to allocate from, it will discover that it is the placeholder and
* replace itself with the thread-local allocator, allocating one if
* required. This avoids a branch on the fast path.
*/
inline Alloc GlobalPlaceHolder(
default_memory_provider, SNMALLOC_DEFAULT_CHUNKMAP(), nullptr, true);
#ifdef SNMALLOC_EXTERNAL_THREAD_ALLOC
/**
* Version of the `ThreadAlloc` interface that does no management of thread
@@ -39,15 +29,39 @@ namespace snmalloc
public:
static SNMALLOC_FAST_PATH Alloc* get_noncachable()
{
return (Alloc*&)ThreadAllocUntyped::get();
return (Alloc*)ThreadAllocUntyped::get();
}
static SNMALLOC_FAST_PATH Alloc* get()
{
return (Alloc*&)ThreadAllocUntyped::get();
return (Alloc*)ThreadAllocUntyped::get();
}
};
#endif
/**
* Function passed as a template parameter to `Allocator` to allow lazy
* replacement. In this case we are assuming the underlying external thread
* alloc is performing initialization, so this is not required, and just
* always returns nullptr to specify no new allocator is required.
*/
SNMALLOC_FAST_PATH void* lazy_replacement(void* existing)
{
UNUSED(existing);
return nullptr;
}
using ThreadAlloc = ThreadAllocUntypedWrapper;
#else
/**
* A global fake allocator object. This never allocates memory and, as a
* result, never owns any slabs. On the slow paths, where it would fetch
* slabs to allocate from, it will discover that it is the placeholder and
* replace itself with the thread-local allocator, allocating one if
* required. This avoids a branch on the fast path.
*/
inline GlobalVirtual dummy_memory_provider;
inline Alloc GlobalPlaceHolder(
dummy_memory_provider, SNMALLOC_DEFAULT_CHUNKMAP(), nullptr, true);
/**
* Common aspects of thread local allocator. Subclasses handle how releasing
@@ -196,8 +210,6 @@ namespace snmalloc
ThreadAllocLibcCleanup::inner_release();
}
using ThreadAlloc = ThreadAllocLibcCleanup;
#elif defined(SNMALLOC_EXTERNAL_THREAD_ALLOC)
using ThreadAlloc = ThreadAllocUntypedWrapper;
#else
using ThreadAlloc = ThreadAllocThreadDestructor;
#endif
@@ -233,5 +245,5 @@ namespace snmalloc
}
return lazy_replacement_slow();
}
#endif
} // namespace snmalloc