6 #ifndef TPZAUTOPOINTER_H 7 #define TPZAUTOPOINTER_H 19 #define AP_MUTEX_ARRAY_SZ 512 24 #ifdef PROFILE_AP_MUTEXES 25 extern uint64_t ap_mutex_accesses[];
28 #define AP_MUTEX_HASH_1 \ 29 addr = (addr >> 32) ^ addr; \ 30 addr = (addr >> 16) ^ addr; \ 31 addr = (addr >> 8) ^ addr; \ 32 addr = (addr >> 4) ^ addr; \ 33 i = (unsigned) (addr % AP_MUTEX_ARRAY_SZ) 35 #define AP_MUTEX_HASH_2 \ 36 addr = (addr >> 8) ^ addr; \ 37 addr = (addr >> 4) ^ addr; \ 38 i = (unsigned) (addr % AP_MUTEX_ARRAY_SZ) 44 uint64_t addr = (uint64_t) obj;
47 #ifdef PROFILE_AP_MUTEXES 48 ap_mutex_accesses[i]++;
104 if (node_id == -2)
return;
109 fPointer = (T2*) fPointer->Clone();
126 bool should_delete =
false;
131 if((*fCounter) <= 0) should_delete =
true;
150 fRef =
new TPZReference<T>();
164 fRef =
new TPZReference<T>(
obj);
187 if(copy.
fRef == fRef)
return *
this;
197 return *(fRef->fPointer);
203 return *(fRef->fPointer);
209 return *(fRef->fPointer);
215 return fRef->fPointer;
219 return fRef->fPointer;
224 fRef->ReallocForNuma(node);
228 operator bool()
const{
229 return (this->fRef->fPointer !=
nullptr);
232 return fRef->fPointer !=
nullptr;
238 return *(fRef->fCounter);
242 return *(fRef->fCounter);
244 template<
typename R,
typename T2>
248 template<
typename R,
typename T>
252 if ( (p = dynamic_cast<R*> (in.operator->())) ) {
254 delete rv.
fRef->fCounter;
256 rv.
fRef->Increment();
#define PZ_PTHREAD_MUTEX_UNLOCK(mutex, fn)
T2 * fPointer
Pointer to T2 object.
bool Increment()
Increment the counter.
friend TPZAutoPointer< R > TPZAutoPointerDynamicCast(TPZAutoPointer< T2 > in)
bool Decrease()
Decrease the counter. If the counter is zero, delete myself.
int Count() const
Returns the counter value.
TPZAutoPointer(const TPZAutoPointer< T > ©)
Share the pointer of the copy.
#define PZ_PTHREAD_MUTEX_LOCK(mutex, fn)
pthread_mutex_t gAutoPointerMutexArray[]
~TPZAutoPointer()
The destructor will delete the administered pointer if its reference count is zero.
void ReallocForNuma(int node_id)
pthread_mutex_t * get_ap_mutex(void *obj)
TPZReference< T > * fRef
The object which contains the pointer and the reference count.
TPZAutoPointer(T *obj)
This method will create an object which will administer the area pointed to by obj.
TPZAutoPointer & operator=(TPZAutoPointer< T > &©)
Move assignment operator.
T * operator->() const
Returns the pointer for referenced object.
TPZReference(T2 *pointer)
T & operator*() const
Returns the referenced object.
TPZAutoPointer()
Creates an reference counted null pointer.
TPZAutoPointer & operator=(const TPZAutoPointer< T > ©)
Assignment operator.
void ReallocForNuma(int node)
This class implements a reference counter mechanism to administer a dynamically allocated object...