Skip to content

Commit

Permalink
Add templated reallocate & fix the templated allocate definition
Browse files Browse the repository at this point in the history
  • Loading branch information
LessComplexity committed Mar 30, 2021
1 parent 192aa2b commit 598f850
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
22 changes: 15 additions & 7 deletions MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ namespace CPPShift::Memory {
*/
void* reallocate(void* unit_pointer_start, size_t new_size);

// Templated re-allocation
template<typename T>
T* reallocate(T* unit_pointer_start, size_t new_size);

/**
* Frees memory in a pool
*
Expand Down Expand Up @@ -106,14 +110,18 @@ namespace CPPShift::Memory {
*/
void endScope();
};

template<typename T>
inline T* MemoryPool::allocate(size_t instances) {
return reinterpret_cast<T*>(this->allocate(instances * sizeof(T)));
}

template<typename T>
inline T* MemoryPool::reallocate(T* unit_pointer_start, size_t instances) {
return reinterpret_cast<T*>(this->reallocate(reinterpret_cast<void*>(unit_pointer_start), instances * sizeof(T)));
}
}

// Override new operators to create with memory pool
extern void* operator new(size_t size, CPPShift::Memory::MemoryPool* mp);
extern void* operator new[](size_t size, CPPShift::Memory::MemoryPool* mp);


template<typename T>
inline T* allocate(size_t instances) {
return reinterpret_cast<T*>(this->allocate(instances * sizeof(T)));
}
extern void* operator new[](size_t size, CPPShift::Memory::MemoryPool* mp);
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ I hope this simple feature will help you increase your software's performance -
To use the memory pool features you just need to copy the [MemoryPool.cpp](MemoryPool.cpp), [MemoryPool.h](MemoryPool.h) & [MemoryPoolData.h](MemoryPoolData.h) files to your project. The memory pool structure is `CPPShift::Memory::MemoryPool`. ***The Memory Pool Is Not Thread Safe - In case of threads it is better to create a memory pool for each thread***

* _Create a memory pool_: `CPPShift::Memory::MemoryPool * mp = new CPPShift::Memory::MemoryPool(size);` Create a new memory pool structure and a first memory block. If you don't specify a size then by default it will be the `MEMORYPOOL_DEFAULT_BLOCK_SIZE` macro.
* _Allocate space_: `Type* allocated = new (mp) Type[size];` or `(Type*) mp->allocate(size * sizeof(Type));` or `mp->allocate<Type>(size);` Where `Type` is the object\primitive type to create, `mp` is the memory pool object address, and `size` is a represention of the amount of types to allocate.
* _Allocate space_: `Type* allocated = new (mp) Type[size];` or `Type* allocated = (Type*) mp->allocate(size * sizeof(Type));` or `Type* allocated = mp->allocate<Type>(size);` Where `Type` is the object\primitive type to create, `mp` is the memory pool object address, and `size` is a represention of the amount of types to allocate.
* _Deallocate space_: `mp->free(allocated)` Remove an allocated space
* _Reallocate space_: `Type* allocated = (Type*) mp->reallocate(allocated, size);` Rellocate a pre-allocated space, will copy the previous values to the new memory allocated.
* _Reallocate space_: `Type* allocated = mp->reallocate<Type>(allocated, size);` or `Type* allocated = (Type*) mp->reallocate(allocated, size);` Rellocate a pre-allocated space, will copy the previous values to the new memory allocated.
* _Dump data of a memory pool_: `mp->dumpPoolData()` This function prints outs the data about the blocks and units in the pool.

## Memory scoping
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")

add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp")
add_executable(MemoryPool "main.cpp" "../MemoryPool.cpp" "String.cpp" "STDString.h" "STDString.cpp")

0 comments on commit 598f850

Please sign in to comment.