5 An object that knows how and when to delete itself, using a reference counter.
7 Whenever a reference to an object like this is obtained/kept, it needs to be grabbed,
8 whenever a reference is discarded, it needs to be dropped.
10 Made for use with the List/Tree/Map types: refcount_inc, refcount_grb, refcount_drp
11 can be used as add, get, del callbacks.
13 #ifndef _DRAGONSTD_REFCOUNT_H_ // include guard
14 #define _DRAGONSTD_REFCOUNT_H_
16 #include <pthread.h> // for pthread_mutex_t
22 unsigned short cnt; // counter
23 pthread_mutex_t mtx; // lock to protect count
26 void refcount_ini(Refcount *refcount, void *obj, void *del);
28 Initializes the refcount.
30 The refcount should be uninitialized or deleted before passed to this function.
31 This function should be called before any other function is called on the refcount.
34 void refcount_dst(Refcount *refcount);
38 This does NOT mean delete the object that the reference counter is referring to-
39 This means delete the refcount structure itself (and is usually called from the
40 delete callback of the referenced object).
42 The refcount is unusable until reinitialized afterwards.
45 void *refcount_inc(Refcount *refcount);
48 Grab a reference to the refcount.
53 void *refcount_grb(Refcount *rc);
56 Does the same as refcount_inc, except it returns the referenced object instead of the
60 void refcount_drp(Refcount *refcount);
63 Drop a reference to the object.
65 May delete the object using the del function if the counter gets down to zero.
68 void *refcount_obj(Refcount *refcount);
71 Return referenced object.
74 #endif // _DRAGONSTD_REFCOUNT_H_