forked from endurodave/stl_allocator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Allocator.h
118 lines (95 loc) · 3.67 KB
/
Allocator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#ifndef __ALLOCATOR_H
#define __ALLOCATOR_H
#include "DataTypes.h"
#include <stddef.h>
/// See http://www.codeproject.com/Articles/1083210/An-efficient-Cplusplus-fixed-block-memory-allocato
class Allocator
{
public:
/// Constructor
/// @param[in] size - size of the fixed blocks
/// @param[in] objects - maximum number of object. If 0, new blocks are
/// created off the heap as necessary.
/// @param[in] memory - pointer to a block of static memory for allocator or NULL
/// to obtain memory from global heap. If not NULL, the objects argument
/// defines the size of the memory block (size x objects = memory size in bytes).
/// @param[in] name - optional allocator name string.
Allocator(size_t size, UINT objects=0, CHAR* memory = NULL, const CHAR* name=NULL);
/// Destructor
~Allocator();
/// Get a pointer to a memory block.
/// @param[in] size - size of the block to allocate
/// @return Returns pointer to the block. Otherwise NULL if unsuccessful.
void* Allocate(size_t size);
/// Return a pointer to the memory pool.
/// @param[in] pBlock - block of memory deallocate (i.e push onto free-list)
void Deallocate(void* pBlock);
/// Get the allocator name string.
/// @return A pointer to the allocator name or NULL if none was assigned.
const CHAR* GetName() { return m_name; }
/// Gets the fixed block memory size, in bytes, handled by the allocator.
/// @return The fixed block size in bytes.
size_t GetBlockSize() { return m_blockSize; }
/// Gets the maximum number of blocks created by the allocator.
/// @return The number of fixed memory blocks created.
UINT GetBlockCount() { return m_blockCnt; }
/// Gets the number of blocks in use.
/// @return The number of blocks in use by the application.
UINT GetBlocksInUse() { return m_blocksInUse; }
/// Gets the total number of allocations for this allocator instance.
/// @return The total number of allocations.
UINT GetAllocations() { return m_allocations; }
/// Gets the total number of deallocations for this allocator instance.
/// @return The total number of deallocations.
UINT GetDeallocations() { return m_deallocations; }
private:
/// Push a memory block onto head of free-list.
/// @param[in] pMemory - block of memory to push onto free-list
void Push(void* pMemory);
/// Pop a memory block from head of free-list.
/// @return Returns pointer to the block. Otherwise NULL if unsuccessful.
void* Pop();
struct Block
{
Block* pNext;
};
enum AllocatorMode { HEAP_BLOCKS, HEAP_POOL, STATIC_POOL };
const size_t m_blockSize;
const size_t m_objectSize;
const UINT m_maxObjects;
AllocatorMode m_allocatorMode;
Block* m_pHead;
CHAR* m_pPool;
UINT m_poolIndex;
UINT m_blockCnt;
UINT m_blocksInUse;
UINT m_allocations;
UINT m_deallocations;
const CHAR* m_name;
};
// Template class to create external memory pool
template <class T, UINT Objects>
class AllocatorPool : public Allocator
{
public:
AllocatorPool() : Allocator(sizeof(T), Objects, m_memory)
{
}
private:
CHAR m_memory[sizeof(T) * Objects];
};
// macro to provide header file interface
#define DECLARE_ALLOCATOR \
public: \
void* operator new(size_t size) { \
return _allocator.Allocate(size); \
} \
void operator delete(void* pObject) { \
_allocator.Deallocate(pObject); \
} \
private: \
static Allocator _allocator;
// macro to provide source file interface
#define IMPLEMENT_ALLOCATOR(class, objects, memory) \
Allocator class::_allocator(sizeof(class), objects, memory, #class);
#endif