Skip to content

Commit 5dfcc01

Browse files
committed
[Store]: Support zero based buffer in Cachelib Allocator
Signed-off-by: Jinlong Chen <[email protected]>
1 parent 57c3d9d commit 5dfcc01

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

mooncake-store/src/allocator.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ CachelibBufferAllocator::CachelibBufferAllocator(std::string segment_name,
5656

5757
LOG_ASSERT(header_region_start_);
5858

59+
// Add a padding to base to support zero-based buffers.
60+
auto padded_base = base + facebook::cachelib::Slab::kSize;
61+
5962
// Initialize the CacheLib MemoryAllocator.
6063
memory_allocator_ = std::make_unique<facebook::cachelib::MemoryAllocator>(
6164
facebook::cachelib::MemoryAllocator::Config(
6265
facebook::cachelib::MemoryAllocator::generateAllocSizes()),
6366
reinterpret_cast<void*>(header_region_start_.get()),
64-
header_region_size_, reinterpret_cast<void*>(base), size);
67+
header_region_size_, reinterpret_cast<void*>(padded_base), size);
6568

6669
if (!memory_allocator_) {
6770
LOG(ERROR) << "status=failed_to_init_facebook_memory_allocator";
@@ -88,6 +91,10 @@ std::unique_ptr<AllocatedBuffer> CachelibBufferAllocator::allocate(
8891
<< " current_size=" << cur_size_;
8992
return nullptr;
9093
}
94+
95+
// Un-padding the buffer.
96+
buffer = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(buffer) -
97+
facebook::cachelib::Slab::kSize);
9198
} catch (const std::exception& e) {
9299
LOG(ERROR) << "allocation_exception error=" << e.what();
93100
return nullptr;
@@ -106,7 +113,10 @@ std::unique_ptr<AllocatedBuffer> CachelibBufferAllocator::allocate(
106113
void CachelibBufferAllocator::deallocate(AllocatedBuffer* handle) {
107114
try {
108115
// Deallocate memory using CacheLib.
109-
memory_allocator_->free(handle->buffer_ptr_);
116+
auto buffer = reinterpret_cast<void*>(
117+
reinterpret_cast<uintptr_t>(handle->buffer_ptr_) +
118+
facebook::cachelib::Slab::kSize);
119+
memory_allocator_->free(buffer);
110120
handle->status = BufStatus::UNREGISTERED;
111121
size_t freed_size =
112122
handle->size_; // Store size before handle might become invalid

0 commit comments

Comments
 (0)