-
Notifications
You must be signed in to change notification settings - Fork 1
/
IntrusiveBlockAllocator.h
129 lines (100 loc) · 2.64 KB
/
IntrusiveBlockAllocator.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef BlockAllocator_H
#define BlockAllocator_H
#include<vector>
#include<map>
#include<iostream>
/** to be used with a base class....
* allows fast dealloc
*/
class IntrusiveBlockAllocator {
public:
typedef unsigned short int Index;
IntrusiveBlockAllocator( std::size_t typeSize,
std::size_t blockSize):
m_typeSize(typeSize), m_blockSize(blockSize),nBlocks(0){
init();
}
std::pair<void *, Block*> alloc() {
std::pair<void *, Block*> ret(m_next, m_current);
m_next+=m_typeSize;
Block & block = *m_current;
++block.m_allocated;
if(m_next==(&block.m_data.back())+1)
init();
return ret;
}
void dealloc(void * p, Block * block) {
if (block==m_current) {
--(*m_current).m_allocated;
pointer cp = static_cast<pointer>(p);
// check if was last allocated
if (cp==m_next-m_typeSize) m_next=cp;
return;
}
//remove block if empty
if (0==(--(*block).m_allocated) {\
delete block;
nBlocks++;
}
public:
struct Stat {
size_t blockSize;
size_t currentOccupancy;
size_t currentAvailable;
size_t nBlocks;
};
Stat stat() const {
Stat s = { m_blockSize, (*m_current).m_allocated,
(&*(*m_current).m_data.end()-m_next)/m_typeSize,
nBlocks};
return s;
}
private:
void init() {
m_current = new Block();
m_current->parent = this;
m_current->m_data.resize(m_blockSize*m_typeSize);
m_current->m_allocated=0;
m_next = &(m_current->m_data.front());
nBlocks++;
}
struct Block {
IntrusiveBlockAllocator * parent;
std::size_t m_allocated;
std::vector<unsigned char> m_data;
};
typedef unsigned char * pointer;
std::size_t m_typeSize;
std::size_t m_blockSize;
pointer m_next;
Block * m_current;
size_t nBlocks;
};
class BlockAllocated {
public:
virtual ~BlockAllocated(){}
static void * operator new(size_t s) {
std::pair<void *, Block*> ret = allocator(s)->alloc();
myblock=ret.second;
return ret.first;
}
static void operator delete(void *) {
myblock->parent->dealloc(myblock);
}
static IntrusiveBlockAllocator * allocator(size_t s) {
typedef map<size_t, IntrusiveBlockAllocator *> Allocators;
typedef Allocators::iterator AI;
static Allocators local;
AI p =local.find(s);
if (p!=local.end()) return (*p).second;
return
(*local.insert(std:make_pair(s, new IntrusiveBlockAllocator(s, 1024)).second).second;
}
static BlockAllocator::Stat stat() {
return allocator()->stat();
}
private:
IntrusiveBlockAllocator::Block * myblock;
// static BlockAllocator * s_allocator;
};
#endif // BlockAllocator_H