31 #if defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER)
32 # define squash_atomic_inc(var) __sync_fetch_and_add(var, 1)
33 # define squash_atomic_dec(var) __sync_fetch_and_sub(var, 1)
34 # define squash_atomic_cas(var, orig, val) __sync_val_compare_and_swap(var, orig, val)
36 # define squash_atomic_cas(var, orig, val) InterlockedCompareExchange(var, orig, val)
38 SQUASH_MTX_DEFINE(atomic_ref)
41 squash_atomic_cas (volatile
unsigned int* var,
46 SQUASH_MTX_LOCK(atomic_ref);
50 SQUASH_MTX_UNLOCK(atomic_ref);
56 #if !defined(squash_atomic_inc)
58 squash_atomic_inc (
volatile unsigned int* var) {
60 unsigned int tmp = *var;
61 if (squash_atomic_cas (var, tmp, tmp + 1) == tmp) {
68 #if !defined(squash_atomic_dec)
70 squash_atomic_dec (
volatile unsigned int* var) {
72 unsigned int tmp = *var;
74 if (squash_atomic_cas (var, tmp, tmp - 1) == tmp) {
207 SquashObject*
object = (SquashObject*) obj;
212 if (object->is_floating) {
213 if (squash_atomic_cas (&(object->is_floating), 1, 0) == 0) {
214 squash_atomic_inc (&(object->ref_count));
217 squash_atomic_inc (&(object->ref_count));
238 SquashObject*
object = (SquashObject*) obj;
243 return (squash_atomic_cas (&(object->is_floating), 1, 0) == 1) ? obj : NULL;
256 SquashObject*
object = (SquashObject*) obj;
261 unsigned int ref_count = squash_atomic_dec (&(object->ref_count));
263 if (ref_count == 1) {
264 if (object->destroy_notify != NULL) {
265 object->destroy_notify (obj);
284 return ((SquashObject*) obj)->ref_count;
303 SquashObject*
object = (SquashObject*) obj;
305 assert (
object != NULL);
307 object->ref_count = 1;
308 object->is_floating = is_floating;
309 object->destroy_notify = destroy_notify;
326 assert (obj != NULL);
void(* SquashDestroyNotify)(void *data)
Callback to be invoked when information data is no longer needed.
void * squash_object_ref_sink(void *obj)
Sink a floating reference if one exists.
void * squash_object_unref(void *obj)
Decrement the reference count on an object.
unsigned int squash_object_get_ref_count(void *obj)
Get the current reference count of an object.
void * squash_object_ref(void *obj)
Increment the reference count on an object.
void squash_object_destroy(void *obj)
Destroy an object.
void squash_object_init(void *obj, bool is_floating, SquashDestroyNotify destroy_notify)
Initialize a new object.