Use malloc for > 256 in FastAllocated, allocateFast, Arena

This commit is contained in:
Andrew Noyes 2022-03-21 17:05:25 -07:00
parent 8a35f03a18
commit 3b7389fbc5
2 changed files with 25 additions and 38 deletions

View File

@ -342,23 +342,23 @@ ArenaBlock* ArenaBlock::create(int dataSize, Reference<ArenaBlock>& next) {
b->bigSize = 256;
INSTRUMENT_ALLOCATE("Arena256");
} else if (reqSize <= 512) {
b = (ArenaBlock*)FastAllocator<512>::allocate();
b = (ArenaBlock*)new uint8_t[512];
b->bigSize = 512;
INSTRUMENT_ALLOCATE("Arena512");
} else if (reqSize <= 1024) {
b = (ArenaBlock*)FastAllocator<1024>::allocate();
b = (ArenaBlock*)new uint8_t[1024];
b->bigSize = 1024;
INSTRUMENT_ALLOCATE("Arena1024");
} else if (reqSize <= 2048) {
b = (ArenaBlock*)FastAllocator<2048>::allocate();
b = (ArenaBlock*)new uint8_t[2048];
b->bigSize = 2048;
INSTRUMENT_ALLOCATE("Arena2048");
} else if (reqSize <= 4096) {
b = (ArenaBlock*)FastAllocator<4096>::allocate();
b = (ArenaBlock*)new uint8_t[4096];
b->bigSize = 4096;
INSTRUMENT_ALLOCATE("Arena4096");
} else {
b = (ArenaBlock*)FastAllocator<8192>::allocate();
b = (ArenaBlock*)new uint8_t[8192];
b->bigSize = 8192;
INSTRUMENT_ALLOCATE("Arena8192");
}
@ -460,26 +460,26 @@ void ArenaBlock::destroyLeaf() {
FastAllocator<256>::release(this);
INSTRUMENT_RELEASE("Arena256");
} else if (bigSize <= 512) {
FastAllocator<512>::release(this);
delete[] reinterpret_cast<uint8_t*>(this);
INSTRUMENT_RELEASE("Arena512");
} else if (bigSize <= 1024) {
FastAllocator<1024>::release(this);
delete[] reinterpret_cast<uint8_t*>(this);
INSTRUMENT_RELEASE("Arena1024");
} else if (bigSize <= 2048) {
FastAllocator<2048>::release(this);
delete[] reinterpret_cast<uint8_t*>(this);
INSTRUMENT_RELEASE("Arena2048");
} else if (bigSize <= 4096) {
FastAllocator<4096>::release(this);
delete[] reinterpret_cast<uint8_t*>(this);
INSTRUMENT_RELEASE("Arena4096");
} else if (bigSize <= 8192) {
FastAllocator<8192>::release(this);
delete[] reinterpret_cast<uint8_t*>(this);
INSTRUMENT_RELEASE("Arena8192");
} else {
#ifdef ALLOC_INSTRUMENTATION
allocInstr["ArenaHugeKB"].dealloc((bigSize + 1023) >> 10);
#endif
g_hugeArenaMemory.fetch_sub(bigSize);
delete[](uint8_t*) this;
delete[] reinterpret_cast<uint8_t*>(this);
}
}
}

View File

@ -210,13 +210,24 @@ public:
if (s != sizeof(Object))
abort();
INSTRUMENT_ALLOCATE(typeid(Object).name());
void* p = FastAllocator < sizeof(Object) <= 64 ? 64 : nextFastAllocatedSize(sizeof(Object)) > ::allocate();
return p;
if constexpr (sizeof(Object) <= 256) {
void* p = FastAllocator < sizeof(Object) <= 64 ? 64 : nextFastAllocatedSize(sizeof(Object)) > ::allocate();
return p;
} else {
void* p = new uint8_t[nextFastAllocatedSize(sizeof(Object))];
return p;
}
}
static void operator delete(void* s) {
INSTRUMENT_RELEASE(typeid(Object).name());
FastAllocator<sizeof(Object) <= 64 ? 64 : nextFastAllocatedSize(sizeof(Object))>::release(s);
if constexpr (sizeof(Object) <= 256) {
FastAllocator<sizeof(Object) <= 64 ? 64 : nextFastAllocatedSize(sizeof(Object))>::release(s);
} else {
delete[] reinterpret_cast<uint8_t*>(s);
}
}
// Redefine placement new so you can still use it
static void* operator new(size_t, void* p) { return p; }
@ -236,18 +247,6 @@ public:
return FastAllocator<128>::allocate();
if (size <= 256)
return FastAllocator<256>::allocate();
if (size <= 512)
return FastAllocator<512>::allocate();
if (size <= 1024)
return FastAllocator<1024>::allocate();
if (size <= 2048)
return FastAllocator<2048>::allocate();
if (size <= 4096)
return FastAllocator<4096>::allocate();
if (size <= 8192)
return FastAllocator<8192>::allocate();
if (size <= 16384)
return FastAllocator<16384>::allocate();
return new uint8_t[size];
}
@ -264,18 +263,6 @@ inline void freeFast(int size, void* ptr) {
return FastAllocator<128>::release(ptr);
if (size <= 256)
return FastAllocator<256>::release(ptr);
if (size <= 512)
return FastAllocator<512>::release(ptr);
if (size <= 1024)
return FastAllocator<1024>::release(ptr);
if (size <= 2048)
return FastAllocator<2048>::release(ptr);
if (size <= 4096)
return FastAllocator<4096>::release(ptr);
if (size <= 8192)
return FastAllocator<8192>::release(ptr);
if (size <= 16384)
return FastAllocator<16384>::release(ptr);
delete[](uint8_t*) ptr;
}