This repository has been archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemblock.h
436 lines (356 loc) · 10.9 KB
/
memblock.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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// -*- C++ -*-
/***************************************************************************
* blitz/memblock.h MemoryBlock<T> and MemoryBlockReference<T>
*
* $Id: memblock.h,v 1.18 2005/10/11 21:54:57 julianc Exp $
*
* Copyright (C) 1997-1999 Todd Veldhuizen <[email protected]>
*
* 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: [email protected]
* Bugs: [email protected]
*
* For more information, please see the Blitz++ Home Page:
* http://oonumerics.org/blitz/
*
***************************************************************************/
/***************************************************************************
* Changes made, in accordance with the Artistic License, by Cory Beutler
*
* - Removed the restrict keyword on some return types
* Returning a pointer returns an r-value pointer. GCC ignores these
* anyway as the r-value is already optimized.
*
* - Removed trailing whitespace
*
* - Added a memoryPolicy flag to MemoryBlock to allow the user to choose
* where the memory should be allocated, the cpu or in a managed state
*
* - Added a new type of null block for the managed memory references
* to point to.
*
* - Allocate and Deallocate now use the managed flag to determine whether
* to use 'new' and 'free' or 'cudaMallocManaged' and 'cudaFree'
*
* - Allocate and Deallocate will not use managed memory unless Tegra flag
* is set at compile time. This allows backwards compatability.
*
***************************************************************************/
#ifndef BZ_MEMBLOCK_H
#define BZ_MEMBLOCK_H
#include <blitz/blitz.h>
#include <stddef.h> // ptrdiff_t
#ifdef BZ_THREADSAFE
#include <pthread.h>
#endif
BZ_NAMESPACE(blitz)
enum preexistingMemoryPolicy {
duplicateData,
deleteDataWhenDone,
neverDeleteData
};
enum managedMemoryPolicy {
managedMemory,
notManagedMemory
};
// 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(managedMemoryPolicy memoryPolicy)
: memoryPolicy_(memoryPolicy)
{
length_ = 0;
data_ = 0;
dataBlockAddress_ = 0;
references_ = 0;
BZ_MUTEX_INIT(mutex)
}
explicit MemoryBlock(size_t items, managedMemoryPolicy memoryPolicy)
: memoryPolicy_(memoryPolicy)
{
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)
}
MemoryBlock(size_t length, T_type* data, managedMemoryPolicy memoryPolicy)
: memoryPolicy_(memoryPolicy)
{
length_ = length;
data_ = data;
dataBlockAddress_ = data;
references_ = 0;
BZ_MUTEX_INIT(mutex)
}
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)
}
void addReference()
{
BZ_MUTEX_LOCK(mutex)
++references_;
#ifdef BZ_DEBUG_LOG_REFERENCES
cout << "MemoryBlock: reffed " << setw(8) << length_
<< " at " << ((void *)dataBlockAddress_) << " (r="
<< (int)references_ << ")" << endl;
#endif
BZ_MUTEX_UNLOCK(mutex)
}
T_type* data()
{
return data_;
}
const T_type* data() const
{
return data_;
}
T_type*& dataBlockAddress()
{
return dataBlockAddress_;
}
managedMemoryPolicy memoryPolicy() const
{
return memoryPolicy_;
}
size_t length() const
{
return length_;
}
int removeReference()
{
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
BZ_MUTEX_UNLOCK(mutex)
return refcount;
}
int references() const
{
BZ_MUTEX_LOCK(mutex)
int refcount = references_;
BZ_MUTEX_UNLOCK(mutex)
return refcount;
}
protected:
inline void allocate(size_t length);
void deallocate();
private: // Disabled member functions
MemoryBlock(const MemoryBlock<T_type>&)
{ }
void operator=(const MemoryBlock<T_type>&)
{ }
private: // Data members
managedMemoryPolicy memoryPolicy_;
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)
size_t length_;
};
template<typename P_type>
class UnownedMemoryBlock : public MemoryBlock<P_type> {
public:
UnownedMemoryBlock(size_t length, P_type* data, managedMemoryPolicy memoryPolicy)
: MemoryBlock<P_type>(length,data,memoryPolicy)
{
// This ensures that MemoryBlock destructor will not
// attempt to delete data
MemoryBlock<P_type>::dataBlockAddress() = 0;
}
virtual ~UnownedMemoryBlock()
{
}
};
template<typename P_type>
class NullMemoryBlock : public MemoryBlock<P_type> {
public:
NullMemoryBlock()
: MemoryBlock<P_type>(notManagedMemory)
{
// This ensures that the delete operator will not be invoked
// on an instance of NullMemoryBlock in removeReference().
MemoryBlock<P_type>::addReference();
}
virtual ~NullMemoryBlock()
{ }
};
template<typename P_type>
class ManagedNullMemoryBlock : public MemoryBlock<P_type> {
public:
ManagedNullMemoryBlock()
: MemoryBlock<P_type>(managedMemory)
{
// This ensures that the delete operator will not be invoked
// on an instance of ManagedNullMemoryBlock in removeReference().
MemoryBlock<P_type>::addReference();
}
virtual ~ManagedNullMemoryBlock()
{ }
};
template<typename P_type>
class MemoryBlockReference {
public:
typedef P_type T_type;
protected:
T_type * restrict data_;
private:
MemoryBlock<T_type>* block_;
static NullMemoryBlock<T_type> nullBlock_;
static ManagedNullMemoryBlock<T_type> managedNullBlock_;
public:
MemoryBlockReference(managedMemoryPolicy memoryPolicy = notManagedMemory)
{
if (memoryPolicy == managedMemory)
block_ = &managedNullBlock_;
else
block_ = &nullBlock_;
block_->addReference();
data_ = 0;
}
MemoryBlockReference(MemoryBlockReference<T_type>& ref, size_t offset=0)
{
block_ = ref.block_;
block_->addReference();
data_ = ref.data_ + offset;
}
MemoryBlockReference(size_t length, T_type* data,
preexistingMemoryPolicy deletionPolicy, managedMemoryPolicy memoryPolicy)
{
// 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))
block_ = new UnownedMemoryBlock<T_type>(length, data, memoryPolicy);
else if (deletionPolicy == deleteDataWhenDone)
block_ = new MemoryBlock<T_type>(length, data, memoryPolicy);
block_->addReference();
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlockReference: created MemoryBlock at "
<< ((void*)block_) << endl;
#endif
data_ = data;
}
explicit MemoryBlockReference(size_t items, managedMemoryPolicy memoryPolicy)
{
block_ = new MemoryBlock<T_type>(items, memoryPolicy);
block_->addReference();
data_ = block_->data();
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlockReference: created MemoryBlock at "
<< ((void*)block_) << endl;
#endif
}
managedMemoryPolicy memoryPolicy() const
{
return block_->memoryPolicy();
}
void blockRemoveReference()
{
int refcount = block_->removeReference();
if ((refcount == 0) && (block_ != &nullBlock_) && (block_ != &managedNullBlock_))
{
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlock: no more refs, delete MemoryBlock object at "
<< ((void*)block_) << endl;
#endif
delete block_;
}
}
~MemoryBlockReference()
{
blockRemoveReference();
}
int numReferences() const
{
return block_->references();
}
protected:
void changeToNullBlock()
{
managedMemoryPolicy isBlockManaged = block_->memoryPolicy();
blockRemoveReference();
if (isBlockManaged == managedMemory)
block_ = &managedNullBlock_;
else
block_ = &nullBlock_;
block_->addReference();
data_ = 0;
}
void changeToNullBlock(managedMemoryPolicy memoryPolicy)
{
blockRemoveReference();
if (memoryPolicy == managedMemory)
block_ = &managedNullBlock_;
else
block_ = &nullBlock_;
block_->addReference();
data_ = 0;
}
void changeBlock(MemoryBlockReference<T_type>& ref, size_t offset=0)
{
blockRemoveReference();
block_ = ref.block_;
block_->addReference();
data_ = ref.data_ + offset;
}
void newBlock(size_t items, managedMemoryPolicy memoryPolicy)
{
blockRemoveReference();
block_ = new MemoryBlock<T_type>(items, memoryPolicy);
block_->addReference();
data_ = block_->data();
#ifdef BZ_DEBUG_LOG_ALLOCATIONS
cout << "MemoryBlockReference: created MemoryBlock at "
<< ((void*)block_) << endl;
#endif
}
private:
void operator=(const MemoryBlockReference<T_type>&)
{ }
};
BZ_NAMESPACE_END
#include <blitz/memblock.cc>
#endif // BZ_MEMBLOCK_H