37 #ifdef BZ_HAVE_BOOST_SERIALIZATION
38 #include <boost/serialization/serialization.hpp>
39 #include <boost/serialization/base_object.hpp>
40 #include <boost/serialization/split_member.hpp>
41 #include <boost/serialization/array.hpp>
42 #include <boost/serialization/collection_size_type.hpp>
43 #include <boost/serialization/nvp.hpp>
73 template<
typename P_type>
86 const int mod = items%w;
116 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
117 cout <<
"MemoryBlock: freed " << setw(8) <<
length_
130 #if defined(BZ_THREADSAFE) && !defined(BZ_THREADSAFE_USE_ATOMIC)
131 if (mutexLocking_ == lockingPolicy) {
134 else if (references_ <= 1) {
135 mutexLocking_ = lockingPolicy;
139 #elif defined(BZ_THREADSAFE_USE_ATOMIC)
144 return !lockingPolicy;
154 #ifdef BZ_DEBUG_LOG_REFERENCES
156 const int refcount = ++references_;
159 cout <<
"MemoryBlock: reffed " << setw(8) <<
length_
161 << refcount <<
")" << endl;
193 const int refcount = --references_;
196 #ifdef BZ_DEBUG_LOG_REFERENCES
197 cout <<
"MemoryBlock: dereffed " << setw(8) <<
length_
207 const int refcount = references_;
224 #ifdef BZ_HAVE_BOOST_SERIALIZATION
225 friend class boost::serialization::access;
231 template<
class T_arch>
232 void save(T_arch& ar,
const unsigned int version)
const {
233 #if defined(BZ_THREADSAFE) && !defined(BZ_THREADSAFE_USE_ATOMIC)
236 const boost::serialization::collection_size_type count(
length_);
237 ar << BOOST_SERIALIZATION_NVP(count);
238 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
239 cout <<
"MemoryBlock: serializing " <<
length_ <<
" data for MemoryBlock at "
240 << ((
void*)
this) << endl;
246 template<
class T_arch>
247 void load(T_arch& ar,
const unsigned int version) {
248 #if defined(BZ_THREADSAFE) && !defined(BZ_THREADSAFE_USE_ATOMIC)
251 boost::serialization::collection_size_type count(
length_);
252 ar >> BOOST_SERIALIZATION_NVP(count);
256 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
257 cout <<
"MemoryBlock: unserializing " <<
length_ <<
" data for MemoryBlock at "
258 << ((
void*)
this) << endl;
273 BOOST_SERIALIZATION_SPLIT_MEMBER()
278 #if defined(BZ_THREADSAFE) && !defined(BZ_THREADSAFE_USE_ATOMIC)
306 template<typename P_type>
318 #ifdef BZ_HAVE_BOOST_SERIALIZATION
319 friend class boost::serialization::access;
328 template<
class T_arch>
329 void save(T_arch& ar,
const unsigned int version)
const {
330 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
331 cout <<
"MemoryBlockReference: serializing object at " << ((
void*)
this) <<
", MemoryBlock at "
332 << ((
void*)block_) <<endl;
335 ptrdiff_t ptroffset=0;
337 ptroffset = data_ - block_->
data();
338 boost::serialization::collection_size_type
339 offset(*reinterpret_cast<size_t*>(&ptroffset));
340 ar << BOOST_SERIALIZATION_NVP(offset);
346 template<
class T_arch>
347 void load(T_arch& ar,
const unsigned int version) {
348 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
349 cout <<
"MemoryBlockReference: unserializing at " << ((
void*)
this) << endl;
353 boost::serialization::collection_size_type offset;
354 ar >> BOOST_SERIALIZATION_NVP(offset);
355 ptrdiff_t ptroffset = *
reinterpret_cast<ptrdiff_t*
>(&offset);
357 data_ = block_->data() + ptroffset;
362 BOOST_SERIALIZATION_SPLIT_MEMBER()
378 data_ = ref.
data_ + offset;
399 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
400 cout <<
"MemoryBlockReference: created MemoryBlock at "
401 << ((
void*)block_) << endl;
417 data_ = block_->
data();
419 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
420 cout <<
"MemoryBlockReference: created MemoryBlock at "
421 << ((
void*)block_) << endl;
427 blockRemoveReference();
444 return block_->references();
445 #ifdef BZ_DEBUG_LOG_REFERENCES
446 cout <<
"Invalid reference count for data at "<< data_ << endl;
454 return block_->doLock(lockingPolicy);
456 #ifdef BZ_DEBUG_LOG_REFERENCES
457 cout <<
"No reference count locking for data at "<< data_ << endl;
464 blockRemoveReference();
472 blockRemoveReference();
475 data_ = ref.
data_ + offset;
480 blockRemoveReference();
483 data_ = block_->
data();
485 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
486 cout <<
"MemoryBlockReference: created MemoryBlock at "
487 << ((
void*)block_) << endl;
497 #ifdef BZ_DEBUG_LOG_ALLOCATIONS
498 cout <<
"MemoryBlockReference: no more refs, delete MemoryBlock object at "
499 << ((
void*)block_) << endl;
509 #ifdef BZ_DEBUG_LOG_REFERENCES
510 cout <<
"MemoryBlockReference: reffing block at " << ((
void*)block_)
513 block_->addReference();
516 #ifdef BZ_DEBUG_LOG_REFERENCES
517 cout <<
"MemoryBlockReference:: Skipping reference count for data at "<< data_ << endl;
525 #ifdef BZ_DEBUG_LOG_REFERENCES
526 cout <<
"MemoryBlockReference: dereffing block at " << ((
void*)block_)
529 return block_->removeReference();
531 #ifdef BZ_DEBUG_LOG_REFERENCES
532 cout <<
"Skipping reference count for data at "<< data_ << endl;
544 #include <blitz/memblock.cc>
546 #endif // BZ_MEMBLOCK_H
void operator=(const MemoryBlock< T_type > &)
Definition: memblock.h:220
#define BZ_MUTEX_INIT(name)
Definition: blitz.h:190
MemoryBlock(sizeType items)
Definition: memblock.h:82
virtual ~MemoryBlock()
Definition: memblock.h:112
void changeToNullBlock()
Definition: memblock.h:462
T_type * dataBlockAddress_
Definition: memblock.h:293
Helper class that defines the width of the simd instructions for a given type.
Definition: simdtypes.h:31
char * dBA_char_
Definition: memblock.h:295
MemoryBlockReference(sizeType length, T_type *data, preexistingMemoryPolicy deletionPolicy)
Definition: memblock.h:381
bool doLock(bool lockingPolicy)
Definition: memblock.h:128
void operator=(const MemoryBlockReference< T_type > &)
Definition: memblock.h:537
P_type T_type
Definition: memblock.h:310
MemoryBlockReference()
Definition: memblock.h:367
simdTypes< T_type >::vecType *restrict dBA_tv_
Definition: memblock.h:294
sizeType blockLength() const
Returns the allocated length of the memory block.
Definition: memblock.h:435
~MemoryBlockReference()
Definition: memblock.h:425
Definition: memblock.h:74
static bool isVectorAligned(const T *restrict pointer)
Test if a pointer to T is simd aligned.
Definition: simdtypes.h:46
void addReference() const
Definition: memblock.h:506
MemoryBlock()
The default constructor is needed for serialization.
Definition: memblock.h:223
Definition: memblock.h:50
void allocate(sizeType length)
const T_type *restrict data() const
Definition: memblock.h:174
#define BZ_MUTEX_UNLOCK(name)
Definition: blitz.h:192
void changeBlock(MemoryBlockReference< T_type > &ref, sizeType offset=0)
Definition: memblock.h:470
void newBlock(sizeType items)
Definition: memblock.h:478
int removeReference()
Definition: memblock.h:189
bool allocatedByUs_
Keeps track of whether the block was preallocated or not.
Definition: memblock.h:223
T_type *& dataBlockAddress()
Definition: memblock.h:179
T_type *restrict data_
Definition: memblock.h:313
sizeType length() const
Definition: memblock.h:184
Definition: array-impl.h:66
P_type T_type
Definition: memblock.h:79
MemoryBlock< T_type > * block_
Definition: memblock.h:316
#define BZ_REFCOUNT_DECLARE(name)
Definition: blitz.h:156
bool isVectorAligned(size_t offset) const
Returns true if the offset from data_ is vector aligned.
Definition: memblock.h:431
size_t sizeType
Definition: blitz.h:110
Definition: memblock.h:67
T_type *restrict data()
Definition: memblock.h:169
void addReference()
Definition: memblock.h:152
void blockRemoveReference()
Definition: memblock.h:492
#define BZ_MUTEX_DESTROY(name)
Definition: blitz.h:193
MemoryBlock(const MemoryBlock< T_type > &)
Definition: memblock.h:218
Definition: memblock.h:51
MemoryBlockReference(sizeType items)
Definition: memblock.h:411
#define BZ_MUTEX_DECLARE(name)
Definition: blitz.h:189
bool lockReferenceCount(bool lockingPolicy) const
Definition: memblock.h:451
int references() const
Definition: memblock.h:204
simdTypes< T_type >::vecType *restrict data_tv_
Definition: memblock.h:289
int removeReference() const
Definition: memblock.h:522
preexistingMemoryPolicy
Definition: memblock.h:49
Definition: memblock.h:52
sizeType length_
Definition: memblock.h:297
MemoryBlock(sizeType length, T_type *data)
Constructor for a preallocated block that should be deleted when we are done?
Definition: memblock.h:102
char *restrict data_char_
Definition: memblock.h:290
int numReferences() const
Definition: memblock.h:441
#define restrict
Definition: compiler.h:95
bool isThreadsafe()
Definition: memblock.h:57
T_type *restrict data_
Definition: memblock.h:288
MemoryBlockReference(MemoryBlockReference< T_type > &ref, sizeType offset=0)
Definition: memblock.h:374
#define BZ_MUTEX_LOCK(name)
Definition: blitz.h:191