atomics: Add a ref_get() variant returning non-zero on overflows
This is useful for users using ref_get() for unique identifier allocation, but the zero value has special meaning.
This commit is contained in:
@@ -124,6 +124,33 @@ bool cas_ptr(void **ptr, void *oldval, void *newval);
|
||||
|
||||
#endif /* HAVE_GCC_ATOMIC_OPERATIONS */
|
||||
|
||||
/**
|
||||
* Get a new reference, but skip zero on overflow.
|
||||
*
|
||||
* If a reference counter is used to allocate unique identifiers, the
|
||||
* refcount value may overflow if it is never decremented. The 0 identifier
|
||||
* may have special semantics, hence returning can be problematic for some
|
||||
* users.
|
||||
*
|
||||
* This call does an additional ref_get() if ref_get() overflows and returns
|
||||
* zero. This ensures that zero is never returned, in the assumption that it
|
||||
* has special meaning.
|
||||
*
|
||||
* @param ref pointer to ref counter
|
||||
* @return new value of ref
|
||||
*/
|
||||
static inline refcount_t ref_get_nonzero(refcount_t *ref)
|
||||
{
|
||||
refcount_t v;
|
||||
|
||||
v = ref_get(ref);
|
||||
if (v == 0)
|
||||
{
|
||||
v = ref_get(ref);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize atomics utility functions
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user