IRC_SERVER
By @hyunjunk (hyunjun2372@gmail.com)
Loading...
Searching...
No Matches
FlexibleMemoryPoolingBase.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace IRCCore
6{
7
8/** Base class for memory pooling with new/delete operator overloading
9 *
10 * How to use:
11 * @code
12 * class MyClass : public MemoryPoolingBase<MyClass>
13 * {
14 * public:
15 * int a;
16 * int b;
17 * int c;
18 * };
19 *
20 * // You can also set MinNumDataPerChunk template parameter
21 * template <typename T>
22 * struct ControlBlock : public FlexibleMemoryPoolingBase<ControlBlock<T>, 128>;
23 *
24 * int main()
25 * {
26 * // Allocate MyClass object from the pool
27 * MyClass* p = new MyClass;
28 *
29 * // Deallocate MyClass object to the pool
30 * delete p;
31 * }
32 * @endcode
33 *
34 * @tparam DerivedType Type of the derived class
35 * @tparam MinNumDataPerChunk Minimum number of data to allocate per chunk
36 */
37template <typename DerivedType, size_t MinNumDataPerChunk = 64>
39{
40public:
41 FORCEINLINE void* operator new(size_t size)
42 {
43 Assert(size == sizeof(DerivedType));
44 return mPool.Allocate();
45 }
46
47 FORCEINLINE void operator delete(void* ptr)
48 {
49 mPool.Deallocate(static_cast<DerivedType*>(ptr));
50 }
51
52 FORCEINLINE void* operator new(size_t size, void* ptr)
53 {
54 Assert(size == sizeof(DerivedType));
55 return ptr;
56 }
57
58 FORCEINLINE void operator delete(void* ptr, size_t size)
59 {
60 (void)ptr;
61 (void)size;
62 }
63
64private:
65 /** Unavailable new/delete for array */
66 void* operator new[](size_t size);
67
68 /** Unavailable new/delete for array */
69 void operator delete[](void* ptr);
70
71private:
72 /**
73 * Use reference for prevent the error of sizeof to incomplete-type T
74 * Reference : https://stackoverflow.com/questions/47462707/static-class-template-member-invalid-application-of-sizeof-to-incomplete-type
75 */
77};
78
79template <typename DerivedType, size_t MinNumDataPerChunk>
81
82} // namespace IRCCore
83
#define FORCEINLINE
Definition AttributeDefines.hpp:55
#define Assert(exp)
Implemented as direct interrupt to avoid dirtying the call stack with assert function when debugging.
Definition MacroDefines.hpp:30
A memory pool that can allocate flexible number of data.
Definition FlexibleFixedMemoryPool.hpp:25
Base class for memory pooling with new/delete operator overloading.
Definition FlexibleMemoryPoolingBase.hpp:39
static FlexibleFixedMemoryPool< DerivedType, MinNumDataPerChunk > & mPool
Use reference for prevent the error of sizeof to incomplete-type T Reference : https://stackoverflow....
Definition FlexibleMemoryPoolingBase.hpp:76
Definition ControlBlock.hpp:7