[2b666a]: blitz / memblock.h Maximize Restore History

Download this file

memblock.h    394 lines (322 with data), 9.4 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// -*- C++ -*-
/***************************************************************************
* blitz/memblock.h MemoryBlock<T> and MemoryBlockReference<T>
*
* $Id$
*
* Copyright (C) 1997-1999 Todd Veldhuizen <tveldhui@oonumerics.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Suggestions: blitz-dev@oonumerics.org
* Bugs: blitz-bugs@oonumerics.org
*
* For more information, please see the Blitz++ Home Page:
* http://oonumerics.org/blitz/
*
***************************************************************************/
#ifndef BZ_MEMBLOCK_H
#define BZ_MEMBLOCK_H
#include <blitz/blitz.h>
#include <stddef.h> // diffType
BZ_NAMESPACE(blitz)
enum preexistingMemoryPolicy {
duplicateData,
deleteDataWhenDone,
neverDeleteData
};
// Forward declaration of MemoryBlockReference
template<typename T_type> class MemoryBlockReference;
// Class MemoryBlock provides a reference-counted block of memory. This block
// may be referred to by multiple vector, matrix and array objects. The memory
// is automatically deallocated when the last referring object is destructed.
// MemoryBlock may be subclassed to provide special allocators.
template<typename P_type>
class MemoryBlock {
friend class MemoryBlockReference<P_type>;
public:
typedef P_type T_type;
protected:
MemoryBlock()
{
length_ = 0;
data_ = 0;
dataBlockAddress_ = 0;
references_ = 0;
BZ_MUTEX_INIT(mutex)
mutexLocking_ = true;
}
explicit MemoryBlock(sizeType items)
{
length_ = items;
allocate(length_);
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlock: allocated " << setw(8) << length_
<< " at " << ((void *)dataBlockAddress_) << endl;
#endif
BZASSERT(dataBlockAddress_ != 0);
references_ = 0;
BZ_MUTEX_INIT(mutex)
mutexLocking_ = true;
}
MemoryBlock(sizeType length, T_type* data)
{
length_ = length;
data_ = data;
dataBlockAddress_ = data;
references_ = 0;
BZ_MUTEX_INIT(mutex)
mutexLocking_ = true;
}
virtual ~MemoryBlock()
{
if (dataBlockAddress_)
{
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlock: freed " << setw(8) << length_
<< " at " << ((void *)dataBlockAddress_) << endl;
#endif
deallocate();
}
BZ_MUTEX_DESTROY(mutex)
}
// set mutex locking policy and return true if successful
bool doLock(bool lockingPolicy)
{
if (mutexLocking_ == lockingPolicy) { // already set
return true;
}
else if (references_ <= 1) { // no multiple references, safe to change
mutexLocking_ = lockingPolicy;
return true;
}
return false; // unsafe to change
}
void addReference()
{
if (mutexLocking_) {
BZ_MUTEX_LOCK(mutex)
}
++references_;
#ifdef BZ_DEBUG_LOG_REFERENCES
cout << "MemoryBlock: reffed " << setw(8) << length_
<< " at " << ((void *)dataBlockAddress_) << " (r="
<< (int)references_ << ")" << endl;
#endif
if (mutexLocking_) {
BZ_MUTEX_UNLOCK(mutex)
}
}
T_type* restrict data()
{
return data_;
}
const T_type* restrict data() const
{
return data_;
}
T_type*& dataBlockAddress()
{
return dataBlockAddress_;
}
sizeType length() const
{
return length_;
}
int removeReference()
{
if (mutexLocking_) {
BZ_MUTEX_LOCK(mutex)
}
int refcount = --references_;
#ifdef BZ_DEBUG_LOG_REFERENCES
cout << "MemoryBlock: dereffed " << setw(8) << length_
<< " at " << ((void *)dataBlockAddress_) << " (r=" << (int)references_
<< ")" << endl;
#endif
if (mutexLocking_) {
BZ_MUTEX_UNLOCK(mutex)
}
return refcount;
}
int references() const
{
if (mutexLocking_) {
BZ_MUTEX_LOCK(mutex)
}
int refcount = references_;
if (mutexLocking_) {
BZ_MUTEX_UNLOCK(mutex)
}
return refcount;
}
protected:
inline void allocate(sizeType length);
void deallocate();
private: // Disabled member functions
MemoryBlock(const MemoryBlock<T_type>&)
{ }
void operator=(const MemoryBlock<T_type>&)
{ }
private: // Data members
T_type * restrict data_;
T_type * dataBlockAddress_;
#ifdef BZ_DEBUG_REFERENCE_ROLLOVER
volatile unsigned char references_;
#else
volatile int references_;
#endif
BZ_MUTEX_DECLARE(mutex)
bool mutexLocking_;
sizeType length_;
};
template<typename P_type>
class MemoryBlockReference {
public:
typedef P_type T_type;
protected:
T_type * restrict data_;
private:
MemoryBlock<T_type>* block_;
public:
MemoryBlockReference()
{
block_ = 0;
addReference();
data_ = 0;
}
MemoryBlockReference(MemoryBlockReference<T_type>& ref, sizeType offset=0)
{
block_ = ref.block_;
addReference();
data_ = ref.data_ + offset;
}
MemoryBlockReference(sizeType length, T_type* data,
preexistingMemoryPolicy deletionPolicy)
{
// Create a memory block using already allocated memory.
// Note: if the deletionPolicy is duplicateData, this must
// be handled by the leaf class. In MemoryBlockReference,
// this is treated as neverDeleteData; the leaf class (e.g. Array)
// must duplicate the data.
if ((deletionPolicy == neverDeleteData)
|| (deletionPolicy == duplicateData)) {
// in this case, we do not need a MemoryBlock to ref-count the data
block_ = 0;
}
else if (deletionPolicy == deleteDataWhenDone) {
block_ = new MemoryBlock<T_type>(length, data);
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlockReference: created MemoryBlock at "
<< ((void*)block_) << endl;
#endif
}
addReference();
data_ = data;
}
explicit MemoryBlockReference(sizeType items)
{
block_ = new MemoryBlock<T_type>(items);
addReference();
data_ = block_->data();
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlockReference: created MemoryBlock at "
<< ((void*)block_) << endl;
#endif
}
~MemoryBlockReference()
{
blockRemoveReference();
}
protected:
int numReferences() const
{
if (block_)
return block_->references();
#ifdef BZ_DEBUG_LOG_REFERENCES
cout << "Invalid reference count for data at "<< data_ << endl;
#endif
return -1;
}
bool lockReferenceCount(bool lockingPolicy) const
{
if (block_)
return block_->doLock(lockingPolicy);
// if we have no block, consider request successful
#ifdef BZ_DEBUG_LOG_REFERENCES
cout << "No reference count locking for data at "<< data_ << endl;
#endif
return true;
}
void changeToNullBlock()
{
blockRemoveReference();
block_ = 0;
addReference();
data_ = 0;
}
void changeBlock(MemoryBlockReference<T_type>& ref, sizeType offset=0)
{
blockRemoveReference();
block_ = ref.block_;
addReference();
data_ = ref.data_ + offset;
}
void newBlock(sizeType items)
{
blockRemoveReference();
block_ = new MemoryBlock<T_type>(items);
addReference();
data_ = block_->data();
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlockReference: created MemoryBlock at "
<< ((void*)block_) << endl;
#endif
}
private:
void blockRemoveReference()
{
int refcount = removeReference();
if (refcount == 0)
{
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlock: no more refs, delete MemoryBlock object at "
<< ((void*)block_) << endl;
#endif
delete block_;
}
}
void addReference() const
{
if (block_) {
block_->addReference();
}
else {
#ifdef BZ_DEBUG_LOG_REFERENCES
cout << "Skipping reference count for data at "<< data_ << endl;
#endif
}
};
int removeReference() const
{
if (block_)
return block_->removeReference();
#ifdef BZ_DEBUG_LOG_REFERENCES
cout << "Skipping reference count for data at "<< data_ << endl;
#endif
return -1;
};
void operator=(const MemoryBlockReference<T_type>&)
{ }
};
BZ_NAMESPACE_END
#include <blitz/memblock.cc>
#endif // BZ_MEMBLOCK_H