Add support for setSharable in RefCount
authorJoão Abecasis <joao.abecasis@nokia.com>
Fri, 25 Nov 2011 10:09:09 +0000 (11:09 +0100)
committerQt by Nokia <qt-info@nokia.com>
Thu, 5 Jan 2012 12:59:14 +0000 (13:59 +0100)
commit9a890a519e9e314b99ca765a7127aa8bdc5b8870
tree0d48e04b269d2a22359d0eb68fa3f3540646cf17
parent3fad9a846f6a89bd342b25c319d0263794bda575
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>
src/corelib/tools/qrefcount.h
tests/auto/corelib/tools/qarraydata/simplevector.h
tests/auto/corelib/tools/qarraydata/tst_qarraydata.cpp