Skip to content

Commit 598f850

Browse files
Add templated reallocate & fix the templated allocate definition
1 parent 192aa2b commit 598f850

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

MemoryPool.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ namespace CPPShift::Memory {
7777
*/
7878
void* reallocate(void* unit_pointer_start, size_t new_size);
7979

80+
// Templated re-allocation
81+
template<typename T>
82+
T* reallocate(T* unit_pointer_start, size_t new_size);
83+
8084
/**
8185
* Frees memory in a pool
8286
*
@@ -106,14 +110,18 @@ namespace CPPShift::Memory {
106110
*/
107111
void endScope();
108112
};
113+
114+
template<typename T>
115+
inline T* MemoryPool::allocate(size_t instances) {
116+
return reinterpret_cast<T*>(this->allocate(instances * sizeof(T)));
117+
}
118+
119+
template<typename T>
120+
inline T* MemoryPool::reallocate(T* unit_pointer_start, size_t instances) {
121+
return reinterpret_cast<T*>(this->reallocate(reinterpret_cast<void*>(unit_pointer_start), instances * sizeof(T)));
122+
}
109123
}
110124

111125
// Override new operators to create with memory pool
112126
extern void* operator new(size_t size, CPPShift::Memory::MemoryPool* mp);
113-
extern void* operator new[](size_t size, CPPShift::Memory::MemoryPool* mp);
114-
115-
116-
template<typename T>
117-
inline T* allocate(size_t instances) {
118-
return reinterpret_cast<T*>(this->allocate(instances * sizeof(T)));
119-
}
127+
extern void* operator new[](size_t size, CPPShift::Memory::MemoryPool* mp);

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ I hope this simple feature will help you increase your software's performance -
2626
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***
2727

2828
* _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.
29-
* _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.
29+
* _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.
3030
* _Deallocate space_: `mp->free(allocated)` Remove an allocated space
31-
* _Reallocate space_: `Type* allocated = (Type*) mp->reallocate(allocated, size);` Rellocate a pre-allocated space, will copy the previous values to the new memory allocated.
31+
* _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.
3232
* _Dump data of a memory pool_: `mp->dumpPoolData()` This function prints outs the data about the blocks and units in the pool.
3333

3434
## Memory scoping

test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_BUILD_TYPE Release)
77
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2")
88

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

0 commit comments

Comments
 (0)