-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManagedMemoryBlock.h
More file actions
305 lines (283 loc) · 12 KB
/
Copy pathManagedMemoryBlock.h
File metadata and controls
305 lines (283 loc) · 12 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#pragma once
// A block is arranged in memory as follows:
// [previous block ptr][current ptr][last block byte ptr][count of non-POD ptrs][memory ...............][managed ptr]
// [managed ptr]
template<typename POLYTYPE>
class tManagedMemoryBlockT
{
//=====================================================================================================================
// PROPERTIES
//=====================================================================================================================
public:
enum
{
eOverheadForManagedObject=sizeof(POLYTYPE*), // The overhead required for managing an
// object lifespan
};
int32_t NumManagedObjects(void) const; // The number of objects allocated where
// destruction is managed by the the memory
// block
int32_t NumBytesLeft(void) const; // The number of bytes left in the memory
// block that can be used for allocation
int32_t NumBytesUsed(void) const; // The number of bytes used including the
// managed objects
//=====================================================================================================================
// FUNCTIONS/MODIFIERS
//=====================================================================================================================
tManagedMemoryBlockT(
tManagedMemoryBlockT* const previousblock,
const int32_t blocksize,
const bool zeroinitialise) throw(); // Zero initialising memory to begin with
// means the performance is improved the
// next time the memory is accessed.
// 10-15% speed increase on a quad-core
// Windows Vista machine 8GB RAM.
~tManagedMemoryBlockT(void);
void Invariant(void) const;
bool EnoughSpace(
const int32_t size,
const unsigned short alignmentpadrequired,
const bool ismanaged) const; // Is there enough space for an object of
// this size/alignment?
void ManageObjectDestruction(POLYTYPE& managedobject); // Manage the destruction of this object
unsigned short AlignmentPadRequired(const unsigned short alignment)
const; // Padding required to allocate an object
// with this alignment
void* Use(
const int32_t size,
const unsigned short alignment,
const bool ismanaged); // Use this amount of memory with this
// alignment requirement. Returns
// a pointer to the memory
void ChainAttachBlock(tManagedMemoryBlockT& block) throw(); // Add this block to the end of the previous
// block chain
tManagedMemoryBlockT* PreviousBlock(void); // Return the previous block (if any)
private:
//=====================================================================================================================
// PRIVATE
//=====================================================================================================================
tManagedMemoryBlockT* m_PreviousBlock;
char* m_Ptr;
char* const m_EndBytePtr;
int32_t m_NumManagedObjects;
//~V
char* BeginBytePtr(void); // The beginning of the memory
const char* BeginBytePtr(void) const;
char* EndAllocateableBytePtr(void); // The last byte in the block of memory + 1
// that is available for allocation. This
// will be different to 'EndBytePtr' where
// there are objects allocated which are
// managed by the allocator
const char* EndAllocateableBytePtr(void) const;
const POLYTYPE** PFirstManagedObject(void) const; // Pointer to the first managed object
POLYTYPE** PFirstManagedObject(void);
void DestroyManagedObjects(void);
//~F
};
//=====================================================================================================================
// IMPLEMENTATION
//=====================================================================================================================
template<typename POLYTYPE>
tManagedMemoryBlockT<POLYTYPE>::tManagedMemoryBlockT(tManagedMemoryBlockT* const previousblock,const int32_t blocksize,
const bool zeroinitialise) throw():m_PreviousBlock(previousblock),m_Ptr(BeginBytePtr()),
//warning C4355: 'this' : used in base member initializer list
#pragma warning(suppress:4355)
m_EndBytePtr(reinterpret_cast<char*>(this)+blocksize),m_NumManagedObjects(0)
{
_ASSERTE(blocksize>0);
if(zeroinitialise)
{
// Zero initialise the memory (not 'this'!)
memset(BeginBytePtr(),0,blocksize-sizeof(*this));
}
Invariant();
}
template<typename POLYTYPE>
tManagedMemoryBlockT<POLYTYPE>::~tManagedMemoryBlockT(void)
{
Invariant();
DestroyManagedObjects();
Invariant();
}
template<typename POLYTYPE>
void tManagedMemoryBlockT<POLYTYPE>::ChainAttachBlock(tManagedMemoryBlockT& block) throw()
{
Invariant();
for(tManagedMemoryBlockT* iterblock=this;;/* Increment done inside the loop */)
{
if(!iterblock->m_PreviousBlock)
{
// Found the end - attach this one.
iterblock->m_PreviousBlock=█
break;
}
iterblock=iterblock->m_PreviousBlock;
}
Invariant();
}
template<typename POLYTYPE>
int32_t tManagedMemoryBlockT<POLYTYPE>::NumManagedObjects(void) const
{
return m_NumManagedObjects;
}
template<typename POLYTYPE>
const POLYTYPE** tManagedMemoryBlockT<POLYTYPE>::PFirstManagedObject(void) const
{
const POLYTYPE** const pfirst=reinterpret_cast<const POLYTYPE**>(m_EndBytePtr-(1*sizeof(POLYTYPE*)));
// Sanity check
_ASSERTE(reinterpret_cast<const char*>(pfirst)<m_EndBytePtr);
return pfirst;
}
template<typename POLYTYPE>
POLYTYPE** tManagedMemoryBlockT<POLYTYPE>::PFirstManagedObject(void)
{
return const_cast<POLYTYPE**>(static_cast<const tManagedMemoryBlockT&>(*this).PFirstManagedObject());
}
template<typename POLYTYPE>
const char* tManagedMemoryBlockT<POLYTYPE>::BeginBytePtr(void) const
{
// The first byte of memory is 'this' + the sizeof this struct.
return reinterpret_cast<const char*>(this)+sizeof(*this);
}
template<typename POLYTYPE>
char* tManagedMemoryBlockT<POLYTYPE>::BeginBytePtr(void)
{
return const_cast<char*>(static_cast<const tManagedMemoryBlockT&>(*this).BeginBytePtr());
}
template<typename POLYTYPE>
char* tManagedMemoryBlockT<POLYTYPE>::EndAllocateableBytePtr(void)
{
return const_cast<char*>(static_cast<const tManagedMemoryBlockT&>(*this).EndAllocateableBytePtr());
}
template<typename POLYTYPE>
const char* tManagedMemoryBlockT<POLYTYPE>::EndAllocateableBytePtr(void) const
{
char* rv=m_EndBytePtr-(NumManagedObjects()*sizeof(POLYTYPE*));
// Sanity check
_ASSERTE(rv>=m_Ptr && rv<=m_EndBytePtr);
return rv;
}
template<typename POLYTYPE>
int32_t tManagedMemoryBlockT<POLYTYPE>::NumBytesLeft(void) const
{
const int32_t rv=static_cast<int32_t>(EndAllocateableBytePtr()-m_Ptr);
// At no point can there be a negative number of bytes left
_ASSERTE(rv>=0);
return rv;
}
template<typename POLYTYPE>
int32_t tManagedMemoryBlockT<POLYTYPE>::NumBytesUsed(void) const
{
const int32_t rv=static_cast<int32_t>((m_Ptr-BeginBytePtr())+(m_NumManagedObjects*sizeof(POLYTYPE*)));
// Never should the number of bytes used be less than 0
_ASSERTE(rv>=0);
return rv;
}
template<typename POLYTYPE>
void tManagedMemoryBlockT<POLYTYPE>::ManageObjectDestruction(POLYTYPE& managedobject)
{
Invariant();
// Get the next managed object to use
POLYTYPE*& pnewmanaged=*(PFirstManagedObject()-m_NumManagedObjects);
pnewmanaged=&managedobject;
++m_NumManagedObjects;
Invariant();
}
template<typename POLYTYPE>
unsigned short tManagedMemoryBlockT<POLYTYPE>::AlignmentPadRequired(const unsigned short alignment) const
{
return static_cast<unsigned short>(reinterpret_cast<uintptr_t>(m_Ptr)%alignment);
}
template<typename POLYTYPE>
void tManagedMemoryBlockT<POLYTYPE>::DestroyManagedObjects(void)
{
Invariant();
POLYTYPE** pmanagedobject=PFirstManagedObject();
for(int32_t i=0;i<m_NumManagedObjects;++i)
{
// Call the virtual destructor
(*pmanagedobject)->~POLYTYPE();
// Work backwards
--pmanagedobject;
}
m_NumManagedObjects=0;
Invariant();
}
template<typename POLYTYPE>
void tManagedMemoryBlockT<POLYTYPE>::Invariant(void) const
{
#ifdef _DEBUG
if(m_PreviousBlock)
{
m_PreviousBlock->Invariant();
}
const char* const beginbyteptr=BeginBytePtr();
// ==== m_Ptr ======================================================================================================
_ASSERTE(m_Ptr<=m_EndBytePtr); // Would mean the pointer has gone past the memory
_ASSERTE(m_Ptr<=EndAllocateableBytePtr()); // Would mean the pointer has gone past the allocateable area
_ASSERTE(!m_Ptr || m_Ptr>=beginbyteptr); // Ptr could be 0 if it wrapped (unlikely)
_ASSERTE(!m_Ptr || m_Ptr>reinterpret_cast<const char*>(this));
// ==== m_EndBytePtr ===============================================================================================
_ASSERTE(!m_EndBytePtr || m_EndBytePtr>reinterpret_cast<const char*>(this)); // Could be 0 it wrapped (unlikely).
_ASSERTE(!m_EndBytePtr || m_EndBytePtr>beginbyteptr);
_ASSERTE(!m_EndBytePtr || m_EndBytePtr>=EndAllocateableBytePtr());
// ==== EndAllocateableBytePtr =====================================================================================
_ASSERTE(!EndAllocateableBytePtr() ||
EndAllocateableBytePtr()>reinterpret_cast<const char*>(this)); // Could be 0 if wrapped (unlikely)
_ASSERTE(!EndAllocateableBytePtr() || EndAllocateableBytePtr()>beginbyteptr);
_ASSERTE(!EndAllocateableBytePtr() || EndAllocateableBytePtr()<=m_EndBytePtr);
// ==== BeginBytePtr() ============================================================================================
_ASSERTE(BeginBytePtr()==reinterpret_cast<const char*>(this)+sizeof(*this));
// ==== m_NumManagedObjects =======================================================================================
_ASSERTE(m_NumManagedObjects<=NumBytesUsed()); // Can't have more managed objects than bytes used
// ==== PFirstManagedObject =======================================================================================
const char* const pfirstmanagedobject=reinterpret_cast<const char*>(PFirstManagedObject());
_ASSERTE(pfirstmanagedobject>reinterpret_cast<const char*>(this));
_ASSERTE(pfirstmanagedobject>beginbyteptr);
// ==== Misc =======================================================================================================
_ASSERTE(m_EndBytePtr-BeginBytePtr()==NumBytesLeft()+NumBytesUsed()); // Test the counts
_ASSERTE(NumBytesLeft()>=0);
_ASSERTE(NumBytesUsed()>=0);
#endif
}
template<typename POLYTYPE>
bool tManagedMemoryBlockT<POLYTYPE>::EnoughSpace(const int32_t size,const unsigned short alignmentpadrequired,
const bool ismanaged) const
{
// Why would you allocate <=0 bytes?
_ASSERTE(size>0);
const int32_t nbytesleft=NumBytesLeft();
const int32_t sizeneeded=static_cast<int32_t>(alignmentpadrequired+size+((ismanaged)?sizeof(POLYTYPE*):0));
return (sizeneeded<=nbytesleft);
}
template<typename POLYTYPE>
void* tManagedMemoryBlockT<POLYTYPE>::Use(const int32_t size,const unsigned short alignment,const bool ismanaged)
{
Invariant();
void* rv;
const unsigned short pad=AlignmentPadRequired(alignment);
if(EnoughSpace(size,pad,ismanaged))
{
if(pad>0)
{
// Add the padding to align on correct boundary
m_Ptr+=pad;
}
// Ptr should now be 'alignment' aligned
_ASSERTE(!(reinterpret_cast<uintptr_t>(m_Ptr)%alignment));
rv=m_Ptr;
// Increment the ptr ready for another object
m_Ptr+=size;
}
else
{
rv=NULL;
}
Invariant();
return rv;
}
template<typename POLYTYPE>
tManagedMemoryBlockT<POLYTYPE>* tManagedMemoryBlockT<POLYTYPE>::PreviousBlock(void)
{
return m_PreviousBlock;
}