Introduce DefaultAllocator

This commit is contained in:
Alexander A. Klimov 2023-08-29 16:04:10 +02:00
parent 97eda30f86
commit 4fd8c92a13
2 changed files with 29 additions and 2 deletions

View File

@ -23,6 +23,7 @@ struct DAPage
static thread_local struct {
unsigned int InUse = 0;
unsigned int Paused = 0;
DAPage* TopPage = nullptr;
} l_DefragAllocator;
@ -31,7 +32,7 @@ extern "C" void* malloc(size_t bytes)
static const auto libcMalloc = (void*(*)(size_t))dlsym(RTLD_NEXT, "malloc");
static const size_t pageSize = std::max(128L * 1024L, sysconf(_SC_PAGESIZE));
if (BOOST_LIKELY(!l_DefragAllocator.InUse)) {
if (BOOST_LIKELY(!l_DefragAllocator.InUse || l_DefragAllocator.Paused)) {
return libcMalloc(bytes);
}
@ -75,7 +76,7 @@ extern "C" void free(void* memory)
{
static const auto libcFree = (void(*)(void*))dlsym(RTLD_NEXT, "free");
if (BOOST_LIKELY(!l_DefragAllocator.InUse)) {
if (BOOST_LIKELY(!l_DefragAllocator.InUse || l_DefragAllocator.Paused)) {
libcFree(memory);
}
}
@ -97,4 +98,14 @@ DefragAllocator::~DefragAllocator()
}
}
DefaultAllocator::DefaultAllocator()
{
++l_DefragAllocator.Paused;
}
DefaultAllocator::~DefaultAllocator()
{
--l_DefragAllocator.Paused;
}
#endif /* _WIN32 */

View File

@ -21,4 +21,20 @@ public:
~DefragAllocator();
};
/**
* TODO
*
* @ingroup base
*/
class DefaultAllocator
{
public:
DefaultAllocator();
DefaultAllocator(const DefaultAllocator&) = delete;
DefaultAllocator(DefaultAllocator&&) = delete;
DefaultAllocator& operator=(const DefaultAllocator&) = delete;
DefaultAllocator& operator=(DefaultAllocator&&) = delete;
~DefaultAllocator();
};
}