Commit 6fcbb9ec authored by wooway777's avatar wooway777
Browse files

Removed a few redundant operations

parent afab9951
...@@ -43,7 +43,6 @@ private: ...@@ -43,7 +43,6 @@ private:
void *allocateNewRegion(size_t size); void *allocateNewRegion(size_t size);
void tryCoalesce(const Block &block); void tryCoalesce(const Block &block);
void *alignPointer(void *ptr) const;
size_t _alignment; size_t _alignment;
std::vector<void *> _base_regions; std::vector<void *> _base_regions;
......
...@@ -19,11 +19,6 @@ MemoryPool::~MemoryPool() { ...@@ -19,11 +19,6 @@ MemoryPool::~MemoryPool() {
} }
} }
void *MemoryPool::alignPointer(void *ptr) const {
return reinterpret_cast<void *>(
(reinterpret_cast<uintptr_t>(ptr) + _alignment - 1) & ~(_alignment - 1));
}
void *MemoryPool::alloc(size_t size) { void *MemoryPool::alloc(size_t size) {
if (size == 0) { if (size == 0) {
return nullptr; return nullptr;
...@@ -48,29 +43,25 @@ void *MemoryPool::alloc(size_t size) { ...@@ -48,29 +43,25 @@ void *MemoryPool::alloc(size_t size) {
_all_blocks.erase(block_it); _all_blocks.erase(block_it);
// Align the pointer within the block // Align the pointer within the block
void *aligned_ptr = alignPointer(block.ptr); size_t alignment_padding = reinterpret_cast<char *>(block.ptr) - reinterpret_cast<char *>(block.ptr);
size_t alignment_padding = reinterpret_cast<char *>(aligned_ptr) - reinterpret_cast<char *>(block.ptr);
// Calculate remaining space after allocation // Calculate remaining space after allocation
const size_t remaining = block.size - aligned_size - alignment_padding; const size_t remaining = block.size - aligned_size - alignment_padding;
// Create allocated block // Create allocated block
Block alloc_block(block.base, aligned_ptr, aligned_size, false); Block alloc_block(block.base, block.ptr, aligned_size, false);
auto alloc_it = _all_blocks.insert(alloc_block).first; auto alloc_it = _all_blocks.insert(alloc_block).first;
_ptr_to_block[aligned_ptr] = alloc_it; _ptr_to_block[block.ptr] = alloc_it;
// Split remaining space if it's large enough // Split remaining space if it's large enough
if (remaining >= _alignment) { if (remaining >= _alignment) {
void *rem_ptr = static_cast<char *>(aligned_ptr) + aligned_size; void *rem_ptr = static_cast<char *>(block.ptr) + aligned_size;
Block rem_block(block.base, rem_ptr, remaining, true); Block rem_block(block.base, rem_ptr, remaining, true);
auto rem_it = _all_blocks.insert(rem_block).first; auto rem_it = _all_blocks.insert(rem_block).first;
_free_blocks.emplace(remaining, rem_it); _free_blocks.emplace(remaining, rem_it);
} else {
// If remaining space is too small, include it in the allocated block
alloc_block.size += remaining;
} }
return aligned_ptr; return block.ptr;
} }
void MemoryPool::release(void *ptr) { void MemoryPool::release(void *ptr) {
...@@ -99,15 +90,14 @@ void *MemoryPool::allocateNewRegion(size_t size) { ...@@ -99,15 +90,14 @@ void *MemoryPool::allocateNewRegion(size_t size) {
_base_regions.push_back(ptr); _base_regions.push_back(ptr);
// Align the pointer within the allocated region // Align the pointer within the allocated region
void *aligned_ptr = alignPointer(ptr); size_t alignment_padding = reinterpret_cast<char *>(ptr) - reinterpret_cast<char *>(ptr);
size_t alignment_padding = reinterpret_cast<char *>(aligned_ptr) - reinterpret_cast<char *>(ptr);
size_t usable_size = size - alignment_padding; size_t usable_size = size - alignment_padding;
Block new_block(ptr, aligned_ptr, usable_size, true); Block new_block(ptr, ptr, usable_size, true);
auto it = _all_blocks.insert(new_block).first; auto it = _all_blocks.insert(new_block).first;
_free_blocks.emplace(usable_size, it); _free_blocks.emplace(usable_size, it);
return aligned_ptr; return ptr;
} }
void MemoryPool::tryCoalesce(const Block &block) { void MemoryPool::tryCoalesce(const Block &block) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment