00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <QtCore/QMutex>
00020 #include <QtCore/QHash>
00021 #include <QtCore/QAtomicInt>
00022
00023 #include "objectstore_p.h"
00024
00025 namespace {
00026 class GlobalStore
00027 {
00028 public:
00029 QMutex mutex;
00030 QHash<const void *, QAtomicInt> refCount;
00031 };
00032 }
00033
00034 Q_GLOBAL_STATIC(GlobalStore, globalStore)
00035
00036 namespace QGst {
00037 namespace Private {
00038
00039 bool ObjectStore::put(const void * ptr)
00040 {
00041 bool mustAddStrongRef = false;
00042 GlobalStore *const gs = globalStore();
00043 if (!gs) return mustAddStrongRef;
00044
00045 QMutexLocker lock(&gs->mutex);
00046 if (!gs->refCount.contains(ptr)) {
00047 gs->refCount.insert(ptr, QAtomicInt(0));
00048 mustAddStrongRef = true;
00049 }
00050 (gs->refCount[ptr]).ref();
00051
00052 return mustAddStrongRef;
00053 }
00054
00055 bool ObjectStore::take(const void * ptr)
00056 {
00057 bool mustSubtractStrongRef = false;
00058 GlobalStore *const gs = globalStore();
00059 if (!gs) return mustSubtractStrongRef;
00060
00061 QMutexLocker lock(&gs->mutex);
00062
00063
00064 Q_ASSERT(gs->refCount.contains(ptr));
00065
00066 if (!gs->refCount.contains(ptr)) {
00067 return false;
00068 }
00069
00070
00071 (gs->refCount[ptr]).deref();
00072
00073 if (!gs->refCount[ptr]) {
00074
00075 gs->refCount.remove(ptr);
00076 mustSubtractStrongRef = true;
00077 }
00078 return mustSubtractStrongRef;
00079 }
00080
00081 bool ObjectStore::isEmpty()
00082 {
00083 GlobalStore *const gs = globalStore();
00084 if (!gs) return true;
00085
00086 QMutexLocker lock(&gs->mutex);
00087
00088 if (gs->refCount.count()>0) {
00089 return false;
00090 }
00091
00092 return true;
00093 }
00094
00095 }
00096 }