-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryPool.cc
More file actions
133 lines (118 loc) · 3.33 KB
/
Copy pathMemoryPool.cc
File metadata and controls
133 lines (118 loc) · 3.33 KB
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
130
131
132
133
#include "MemoryPool.h"
template <typename T, size_t BlockSize>
inline size_t MemoryPool<T, BlockSize>::padPointer(data_pointer_ p, size_t align) const throw()
{
size_t result = reinterpret_cast<size_t>(p);
return ((align - result) % align);
}
template <typename T, size_t BlockSize>
MemoryPool<T, BlockSize>::MemoryPool() throw()
{
currentBlock_ = 0;
currentSlot_ = 0;
lastSlot_ = 0;
freeSlots_ = 0;
}
template <typename T, size_t BlockSize>
MemoryPool<T, BlockSize>::MemoryPool(const MemoryPool& memoryPool) throw()
{
MemoryPool();
}
template <typename T, size_t BlockSize>
template <class U>
MemoryPool<T, BlockSize>::MemoryPool(const MemoryPool<U>& memoryPool) throw()
{
MemoryPool();
}
template <typename T, size_t BlockSize>
MemoryPool<T, BlockSize>::~MemoryPool() throw()
{
slot_pointer_ curr = currentBlock_;
while (curr != 0)
{
slot_pointer_ prev = curr->next;
operator delete(reinterpret_cast<void*>(curr));
curr = prev;
}
}
template <typename T, size_t BlockSize>
inline typename MemoryPool<T, BlockSize>::pointer
MemoryPool<T, BlockSize>::address(reference x) const throw()
{
return &x;
}
template <typename T, size_t BlockSize>
inline typename MemoryPool<T, BlockSize>::const_pointer
MemoryPool<T, BlockSize>::address(const_reference x) const throw()
{
return &x;
}
template <typename T, size_t BlockSize>
void MemoryPool<T, BlockSize>::allocateBlock()
{
data_pointer_ newBlock = reinterpret_cast<data_pointer_>(operator new(BlockSize));
reinterpret_cast<slot_pointer_>(newBlock)->next = currentBlock_;
currentBlock_ =reinterpret_cast<slot_pointer_>(newBlock);
data_pointer_ body = newBlock + sizeof(slot_pointer_);
size_t bodyPadding = padPointer(body, sizeof(slot_type_));
currentSlot_ = reinterpret_cast<slot_pointer_>(body + bodyPadding);
lastSlot_ = reinterpret_cast<slot_pointer_>(newBlock + BlockSize - sizeof(slot_type_) + 1);
}
template <typename T, size_t BlockSize>
inline typename MemoryPool<T, BlockSize>::pointer
MemoryPool<T, BlockSize>::allocate(size_t, const_pointer)
{
if (freeSlots_ != 0)
{
pointer result = reinterpret_cast<pointer>(freeSlots_);
freeSlots_ = freeSlots_->next;
return result;
}
else
{
if (currentSlot_>= lastSlot_) allocateBlock();
return reinterpret_cast<pointer>(currentSlot_++);
}
}
template <typename T, size_t BlockSize>
inline void MemoryPool<T, BlockSize>::deallocate(pointer p, size_t)
{
if (p != 0)
{
reinterpret_cast<slot_pointer_>(p)->next = freeSlots_;
freeSlots_ = reinterpret_cast<slot_pointer_>(p);
}
}
template <typename T, size_t BlockSize>
inline size_t MemoryPool<T, BlockSize>::max_size() const throw()
{
size_t maxBlocks = -1 / BlockSize;
return (BlockSize - sizeof(data_pointer_)) / sizeof(slot_type_) * maxBlocks;
}
template <typename T, size_t BlockSize>
inline void MemoryPool<T, BlockSize>::construct(pointer p, const_reference val)
{
new (p) value_type(val);
}
template <typename T, size_t BlockSize>
inline void MemoryPool<T, BlockSize>::destroy(pointer p)
{
p->~value_type();
}
template <typename T, size_t BlockSize>
inline typename MemoryPool<T, BlockSize>::pointer
MemoryPool<T, BlockSize>::newElement(const_reference val)
{
pointer result = allocate();
construct(result, val);
return result;
}
template <typename T, size_t BlockSize>
inline void MemoryPool<T, BlockSize>::deleteElement(pointer p)
{
if (p != 0)
{
p->~value_type();
deallocate(p);
}
}