summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2011-11-25 11:09:09 +0100
committerQt by Nokia <qt-info@nokia.com>2012-01-05 13:59:14 +0100
commit9a890a519e9e314b99ca765a7127aa8bdc5b8870 (patch)
tree0d48e04b269d2a22359d0eb68fa3f3540646cf17 /src/corelib/tools
parent3fad9a846f6a89bd342b25c319d0263794bda575 (diff)
Add support for setSharable in RefCount
A reference count of 0 (zero) would never change. RefCount::deref to zero would return false (resource should be freed), subsequent calls on the same state would return true and not change state. While safe from RefCount's side, calling deref on a reference count of zero potentially indicated a dangling reference. With this change, a reference count of 0 is now abused to imply a non-sharable instance (cf. QVector::setSharable). This instance is to be deleted upon deref(), as the data is not shared and has a single owner. In practice, this means an (intentional) change in behaviour in that deref'ing zero still won't change state, but will return false, turning previous access to dangling references into double free errors. Users of RefCount wanting to support non-sharable instances are required to check the return of RefCount::ref() and use RefCount::isShared() to determine whether to detach (instead of directly checking count == 1). New functions are introduced to determine whether RefCount indicates a "Static" (permanent, typically read-only) or "Sharable" instance and whether the instance is currently "Shared" and requires detaching prior to accepting modifications.. This change formalizes -1 as the value used to flag persistent, read-only instances, no longer reserving the full negative domain. The concrete value is part of the ABI, but not of the API. (isStatic and Q_REFCOUNT_INITIALIZE_STATIC are part of the API, instead) Change-Id: I9a63c844155319bef0411e02b47f9d92476afefe Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com> Reviewed-by: Robin Burchell <robin+qt@viroteck.net>
Diffstat (limited to 'src/corelib/tools')
-rw-r--r--src/corelib/tools/qrefcount.h40
1 files changed, 37 insertions, 3 deletions
diff --git a/src/corelib/tools/qrefcount.h b/src/corelib/tools/qrefcount.h
index 17be893744..9478ff1269 100644
--- a/src/corelib/tools/qrefcount.h
+++ b/src/corelib/tools/qrefcount.h
@@ -56,17 +56,51 @@ namespace QtPrivate
class RefCount
{
public:
- inline void ref() {
- if (atomic.load() > 0)
+ inline bool ref() {
+ int count = atomic.load();
+ if (count == 0) // !isSharable
+ return false;
+ if (count != -1) // !isStatic
atomic.ref();
+ return true;
}
inline bool deref() {
- if (atomic.load() <= 0)
+ int count = atomic.load();
+ if (count == 0) // !isSharable
+ return false;
+ if (count == -1) // isStatic
return true;
return atomic.deref();
}
+ bool setSharable(bool sharable)
+ {
+ Q_ASSERT(!isShared());
+ if (sharable)
+ return atomic.testAndSetRelaxed(0, 1);
+ else
+ return atomic.testAndSetRelaxed(1, 0);
+ }
+
+ bool isStatic() const
+ {
+ // Persistent object, never deleted
+ return atomic.load() == -1;
+ }
+
+ bool isSharable() const
+ {
+ // Sharable === Shared ownership.
+ return atomic.load() != 0;
+ }
+
+ bool isShared() const
+ {
+ int count = atomic.load();
+ return (count != 1) && (count != 0);
+ }
+
inline bool operator==(int value) const
{ return atomic.load() == value; }
inline bool operator!=(int value) const