MemPool: Sync greatmazinger/MemoryPool

This commit is contained in:
CalciteM Team 2019-08-01 23:33:30 +08:00
parent 514af95f6c
commit 5835ae30fb
1 changed files with 11 additions and 13 deletions

View File

@ -39,11 +39,11 @@ const noexcept
template <typename T, size_t BlockSize> template <typename T, size_t BlockSize>
MemoryPool<T, BlockSize>::MemoryPool() MemoryPool<T, BlockSize>::MemoryPool()
noexcept noexcept
: currentBlock_(nullptr)
, currentSlot_(nullptr)
, lastSlot_(nullptr)
, freeSlots_(nullptr)
{ {
currentBlock_ = nullptr;
currentSlot_ = nullptr;
lastSlot_ = nullptr;
freeSlots_ = nullptr;
} }
@ -59,12 +59,12 @@ MemoryPool()
template <typename T, size_t BlockSize> template <typename T, size_t BlockSize>
MemoryPool<T, BlockSize>::MemoryPool(MemoryPool&& memoryPool) MemoryPool<T, BlockSize>::MemoryPool(MemoryPool&& memoryPool)
noexcept noexcept
: currentBlock_(memoryPool.currentBlock_)
, currentSlot_(memoryPool.currentSlot_)
, lastSlot_(memoryPool.lastSlot_)
, freeSlots_(memoryPool.freeSlots)
{ {
currentBlock_ = memoryPool.currentBlock_;
memoryPool.currentBlock_ = nullptr; memoryPool.currentBlock_ = nullptr;
currentSlot_ = memoryPool.currentSlot_;
lastSlot_ = memoryPool.lastSlot_;
freeSlots_ = memoryPool.freeSlots_;
} }
@ -133,16 +133,14 @@ void
MemoryPool<T, BlockSize>::allocateBlock() MemoryPool<T, BlockSize>::allocateBlock()
{ {
// Allocate space for the new block and store a pointer to the previous one // Allocate space for the new block and store a pointer to the previous one
data_pointer_ newBlock = reinterpret_cast<data_pointer_> data_pointer_ newBlock = reinterpret_cast<data_pointer_>(operator new(BlockSize));
(operator new(BlockSize));
reinterpret_cast<slot_pointer_>(newBlock)->next = currentBlock_; reinterpret_cast<slot_pointer_>(newBlock)->next = currentBlock_;
currentBlock_ = reinterpret_cast<slot_pointer_>(newBlock); currentBlock_ = reinterpret_cast<slot_pointer_>(newBlock);
// Pad block body to staisfy the alignment requirements for elements // Pad block body to satisfy the alignment requirements for elements
data_pointer_ body = newBlock + sizeof(slot_pointer_); data_pointer_ body = newBlock + sizeof(slot_pointer_);
size_type bodyPadding = padPointer(body, alignof(slot_type_)); size_type bodyPadding = padPointer(body, alignof(slot_type_));
currentSlot_ = reinterpret_cast<slot_pointer_>(body + bodyPadding); currentSlot_ = reinterpret_cast<slot_pointer_>(body + bodyPadding);
lastSlot_ = reinterpret_cast<slot_pointer_> lastSlot_ = reinterpret_cast<slot_pointer_>(newBlock + BlockSize - sizeof(slot_type_) + 1);
(newBlock + BlockSize - sizeof(slot_type_) + 1);
} }