-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleBlockPool.h
42 lines (34 loc) · 1.09 KB
/
SingleBlockPool.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
#pragma once
#include <list>
#include <mutex>
typedef unsigned char BYTE;
class SingleBlockPool
{
private:
// internal/private struct for tracking memory blocks
struct MEM_PTR {
MEM_PTR(BYTE* mem, size_t blockSize) { block = mem; size = blockSize; }
size_t size;
BYTE *block;
bool operator==(const MEM_PTR& rhs) { return block == rhs.block; }
};
size_t max_size;
size_t size_used;
// marking the boundaries of the allocated memory block
BYTE *mem_block;
BYTE *end_block;
std::list<MEM_PTR> lstMemNodes;
std::mutex mtxPool;
// private function to add nodes at a particular memory location
void AddNodeAt(BYTE *pos, size_t sizeBlock);
public:
// constructor allocates block of maxMemory size to use
SingleBlockPool(size_t maxMemory);
virtual ~SingleBlockPool();
// finds the MEM_PTR holding this location and frees up the
// memory area specified by the MEM_PTR
virtual void returnToPool(BYTE *node);
// finds a contiguous block of memory for size
// returns nullptr if there is no space for it
virtual BYTE *getFromPool(size_t size);
};