Use allocateFast4kAligned instead of duplicating its logic

This commit is contained in:
Andrew Noyes 2022-03-21 16:57:38 -07:00
parent 6390d93efd
commit 8a35f03a18
3 changed files with 9 additions and 32 deletions

View File

@ -29,14 +29,7 @@ static std::map<NetworkAddress, std::pair<Reference<EvictablePageCache>, Referen
EvictablePage::~EvictablePage() {
if (data) {
#if defined(USE_JEMALLOC)
aligned_free(data);
#else
if (pageCache->pageSize == 4096)
FastAllocator<4096>::release(data);
else
aligned_free(data);
#endif
freeFast4kAligned(pageCache->pageSize, data);
}
if (EvictablePageCache::RANDOM == pageCache->cacheEvictionType) {
if (index > -1) {
@ -173,14 +166,7 @@ void AsyncFileCached::releaseZeroCopy(void* data, int length, int64_t offset) {
if (o != orphanedPages.end()) {
if (o->second == 1) {
if (data) {
#if defined(USE_JEMALLOC)
aligned_free(data);
#else
if (length == 4096)
FastAllocator<4096>::release(data);
else
aligned_free(data);
#endif
freeFast4kAligned(length, data);
}
} else {
--o->second;

View File

@ -79,14 +79,9 @@ struct EvictablePageCache : ReferenceCounted<EvictablePageCache> {
void allocate(EvictablePage* page) {
try_evict();
try_evict();
#if defined(USE_JEMALLOC)
page->data = aligned_alloc(4096, pageSize);
#else
page->data = pageSize == 4096 ? FastAllocator<4096>::allocate() : aligned_alloc(4096, pageSize);
#endif
if (page->data == nullptr) {
platform::outOfMemory();
}
page->data = allocateFast4kAligned(pageSize);
if (RANDOM == cacheEvictionType) {
page->index = pages.size();
pages.push_back(page);
@ -394,14 +389,7 @@ struct AFCPage : public EvictablePage, public FastAllocated<AFCPage> {
owner->orphanedPages[data] = zeroCopyRefCount;
zeroCopyRefCount = 0;
notReading = Void();
#if defined(USE_JEMALLOC)
data = aligned_alloc(4096, pageCache->pageSize);
#else
data = pageCache->pageSize == 4096 ? FastAllocator<4096>::allocate() : aligned_alloc(4096, pageCache->pageSize);
#endif
if (data == nullptr) {
platform::outOfMemory();
}
data = allocateFast4kAligned(pageCache->pageSize);
}
Future<Void> write(void const* data, int length, int offset) {

View File

@ -279,6 +279,8 @@ inline void freeFast(int size, void* ptr) {
delete[](uint8_t*) ptr;
}
// Allocate a block of memory aligned to 4096 bytes. Size must be a multiple of
// 4096. Guaranteed not to return null. Use freeFast4kAligned to free.
[[nodiscard]] inline void* allocateFast4kAligned(int size) {
#if !defined(USE_JEMALLOC)
// Use FastAllocator for sizes it supports to avoid internal fragmentation in some implementations of aligned_alloc
@ -296,6 +298,7 @@ inline void freeFast(int size, void* ptr) {
return result;
}
// Free a pointer returned from allocateFast4kAligned(size)
inline void freeFast4kAligned(int size, void* ptr) {
#if !defined(USE_JEMALLOC)
// Sizes supported by FastAllocator must be release via FastAllocator