isc_refcount_increment0 is wrong, the code using it needs refactoring
The isc_refcount_increment0()
does two things and that's wrong.
-
The first purpose is to bump the value from
0
->1
making the object referenced. -
The second purpose is to increment the reference counter.
This has several problems:
- You can't check whether the previous value really was
0
. - When object becomes dereferenced with
isc_refcount_decrement() == 1
, theisc_refcount_increment0()
can make it referenced again while destroying the object.
There are two things that we could do about it:
- Don't use isc_refcount API when it's not reference counting, prepare similar API for object counting (isc_objcount?)
- Use
isc_refcount_init()
when initializing object for the first time (note thatisc_refcount_init()
is not atomic) - Always initialize the value to
1
and adjust the code that destroys the object - Split
isc_refcount_increment0()
toisc_refcount_incfirst()
and existingisc_refcount_increment()
Nevertheless, the overloading of the API for <1, MAX>
and <0, MAX>
reference counting is wrong.