#include "Allocator.h"
#include <cinttypes>
#include <cstdint>
#include "Base/ImmortalWrapper.h"
#include "Common/BaseObject.h"
#include "Heap/Allocator/RegionSpace.h"
#include "Mutator/ThreadLocal.h"
#include "ObjectModel/MObject.h"
namespace MapleRuntime {
using namespace std;
Allocator::Allocator()
{
allocBufferManager = new (std::nothrow) AllocBufferManager();
CHECK_DETAIL(allocBufferManager != nullptr, "new alloc buffer manager failed");
asyncAllocationInitSwitch = InitAyncAllocation();
isAsyncAllocationEnable.store(asyncAllocationInitSwitch);
}
bool Allocator::InitAyncAllocation()
{
auto enableAsyncAllocation = std::getenv("cjEnableAsyncAllocation");
if (enableAsyncAllocation == nullptr) {
#if defined(__OHOS__) || defined(__ANDROID__)
return true;
#else
return false;
#endif
}
if (strlen(enableAsyncAllocation) != 1) {
LOG(RTLOG_ERROR, "Unsupported cjEnableAsyncAllocation, cjEnableAsyncAllocation should be 0 or 1.\n");
#if defined(__OHOS__) || defined(__ANDROID__)
return true;
#else
return false;
#endif
}
switch (enableAsyncAllocation[0]) {
case '0':
return false;
case '1':
return true;
default:
LOG(RTLOG_ERROR, "Unsupported cjEnableAsyncAllocation, cjEnableAsyncAllocation should be 0 or 1.\n");
}
return true;
}
AggregateAllocator& AggregateAllocator::Instance(AllocationTag tag)
{
static ImmortalWrapper<AggregateAllocator> instance[MAX_ALLOCATION_TAG];
return *(instance[tag]);
}
PagePool& PagePool::Instance() noexcept
{
static ImmortalWrapper<PagePool> instance("PagePool");
return *instance;
}
Allocator* Allocator::NewAllocator()
{
RegionSpace* regionSpace = new (std::nothrow) RegionSpace();
CHECK_DETAIL(regionSpace != nullptr, "New RegionSpace failed");
return regionSpace;
}
}