19 #include <QtCore/QMutex>
20 #include <QtCore/QHash>
21 #include <QtCore/QAtomicInt>
23 #include "objectstore_p.h"
30 QHash<const void *, QAtomicInt> refCount;
34 Q_GLOBAL_STATIC(GlobalStore, globalStore)
39 bool ObjectStore::put(
const void * ptr)
41 bool mustAddStrongRef =
false;
42 GlobalStore *
const gs = globalStore();
43 if (!gs)
return mustAddStrongRef;
45 QMutexLocker lock(&gs->mutex);
46 if (!gs->refCount.contains(ptr)) {
47 gs->refCount.insert(ptr, QAtomicInt(0));
48 mustAddStrongRef =
true;
50 (gs->refCount[ptr]).ref();
52 return mustAddStrongRef;
55 bool ObjectStore::take(
const void * ptr)
57 bool mustSubtractStrongRef =
false;
58 GlobalStore *
const gs = globalStore();
59 if (!gs)
return mustSubtractStrongRef;
61 QMutexLocker lock(&gs->mutex);
64 Q_ASSERT(gs->refCount.contains(ptr));
66 if (!gs->refCount.contains(ptr)) {
71 (gs->refCount[ptr]).deref();
73 if (!gs->refCount[ptr]) {
75 gs->refCount.remove(ptr);
76 mustSubtractStrongRef =
true;
78 return mustSubtractStrongRef;
81 bool ObjectStore::isEmpty()
83 GlobalStore *
const gs = globalStore();
86 QMutexLocker lock(&gs->mutex);
88 if (gs->refCount.count()>0) {