From 8a35f03a18d0784c7cd87010be2d635c1022a2d9 Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Mon, 21 Mar 2022 16:57:38 -0700 Subject: [PATCH] Use allocateFast4kAligned instead of duplicating its logic --- fdbrpc/AsyncFileCached.actor.cpp | 18 ++---------------- fdbrpc/AsyncFileCached.actor.h | 20 ++++---------------- flow/FastAlloc.h | 3 +++ 3 files changed, 9 insertions(+), 32 deletions(-) diff --git a/fdbrpc/AsyncFileCached.actor.cpp b/fdbrpc/AsyncFileCached.actor.cpp index 1ea7c36aa4..93bf9cfc70 100644 --- a/fdbrpc/AsyncFileCached.actor.cpp +++ b/fdbrpc/AsyncFileCached.actor.cpp @@ -29,14 +29,7 @@ static std::map, 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; diff --git a/fdbrpc/AsyncFileCached.actor.h b/fdbrpc/AsyncFileCached.actor.h index 8c986db9b5..38236615ea 100644 --- a/fdbrpc/AsyncFileCached.actor.h +++ b/fdbrpc/AsyncFileCached.actor.h @@ -79,14 +79,9 @@ struct EvictablePageCache : ReferenceCounted { 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 { 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 write(void const* data, int length, int offset) { diff --git a/flow/FastAlloc.h b/flow/FastAlloc.h index 935f00e358..31c7f037f0 100644 --- a/flow/FastAlloc.h +++ b/flow/FastAlloc.h @@ -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