Reference counter mechanism. More...
Go to the source code of this file.
Data Structures | |
struct | janus_refcount |
Macros | |
#define | janus_refcount_containerof(refptr, type, member) ((type *)((char *)(refptr) - offsetof(type, member))) |
Macro to programmatically address the object itself from its counter. | |
#define | janus_refcount_init(refp, free_fn) |
Janus reference counter initialization (debug according to settings) | |
#define | janus_refcount_init_nodebug(refp, free_fn) |
Janus reference counter initialization (no debug) | |
#define | janus_refcount_init_debug(refp, free_fn) |
Janus reference counter initialization (debug) | |
#define | janus_refcount_increase(refp) |
Increase the Janus reference counter (debug according to settings) | |
#define | janus_refcount_increase_nodebug(refp) |
Increase the Janus reference counter (no debug) | |
#define | janus_refcount_increase_debug(refp) |
Increase the Janus reference counter (debug) | |
#define | janus_refcount_decrease(refp) |
Decrease the Janus reference counter (debug according to settings) | |
#define | janus_refcount_decrease_debug(refp) |
Decrease the Janus reference counter (debug) | |
#define | janus_refcount_decrease_nodebug(refp) |
Decrease the Janus reference counter (no debug) | |
Typedefs | |
typedef struct janus_refcount | janus_refcount |
Janus reference counter structure. | |
Variables | |
int | refcount_debug |
Reference counter mechanism.
Implementation of a simple reference counter that can be used to keep track of memory management in Janus, in order to avoid the need for timed garbage collectord and the like which have proven ineffective in the past (e.g., crashes whenever race conditions occurred). This implementation is heavily based on an excellent blog post written by Chris Wellons.
Objects interested in leveraging this reference counter mechanism must add a janus_refcount instance as one of the members of the object itself, and then call janus_refcall_init() to set it up. Initializing the reference counter just needs a pointer to the function to invoke when the object needs to be destroyed (counter reaches 0), while it will automatically set the counter to 1. To increase and decrease the counter just call janus_refcount_increase() and janus_refcount_decrease(). When the counter reaches 0, the function passed when initializing it will be invoked: this means it's up to you to then free all the resources the object may have allocated. Notice that if this involves other objects that are reference counted, freeing the resource will just mean decreasing the related counter, and not destroying it right away.
The free function must be defined like this:
void my_free_function(janus_refcount *counter);
Since the reference counter cannot know the size of the object to be freed, or where in the list of members the counter has been placed, retrieving the pointer to the object to free is up to you, using the janus_refcount_containerof macro. This is an example of how the free function we have defined above may be implemented:
typedef my_struct { int number; char *string; janus_refcount myref; } void my_free_function(janus_refcount *counter) { struct my_struct *my_object = janus_refcount_containerof(counter, struct my_struct, myref); if(my_object->string) free(my_object->string); free(my_object); }
#define janus_refcount_containerof | ( | refptr, | |
type, | |||
member ) ((type *)((char *)(refptr) - offsetof(type, member))) |
Macro to programmatically address the object itself from its counter.
refptr
is the pointer to the janus_refcount instance, type
is the type of the object itself (e.g., struct mystruct
), while member
is how the janus_refcount instance is called in the object that contains it.
#define janus_refcount_decrease | ( | refp | ) |
Decrease the Janus reference counter (debug according to settings)
free
function if the counter reaches 0 refp | Pointer to the Janus reference counter instance |
#define janus_refcount_decrease_debug | ( | refp | ) |
Decrease the Janus reference counter (debug)
free
function if the counter reaches 0 refp | Pointer to the Janus reference counter instance |
#define janus_refcount_decrease_nodebug | ( | refp | ) |
Decrease the Janus reference counter (no debug)
free
function if the counter reaches 0 refp | Pointer to the Janus reference counter instance |
#define janus_refcount_increase | ( | refp | ) |
Increase the Janus reference counter (debug according to settings)
refp | Pointer to the Janus reference counter instance |
#define janus_refcount_increase_debug | ( | refp | ) |
Increase the Janus reference counter (debug)
refp | Pointer to the Janus reference counter instance |
#define janus_refcount_increase_nodebug | ( | refp | ) |
Increase the Janus reference counter (no debug)
refp | Pointer to the Janus reference counter instance |
#define janus_refcount_init | ( | refp, | |
free_fn ) |
Janus reference counter initialization (debug according to settings)
refp | Pointer to the Janus reference counter instance |
free_fn | Pointer to the function to invoke when the object the counter refers to needs to be destroyed |
#define janus_refcount_init_debug | ( | refp, | |
free_fn ) |
Janus reference counter initialization (debug)
refp | Pointer to the Janus reference counter instance |
free_fn | Pointer to the function to invoke when the object the counter refers to needs to be destroyed |
#define janus_refcount_init_nodebug | ( | refp, | |
free_fn ) |
Janus reference counter initialization (no debug)
refp | Pointer to the Janus reference counter instance |
free_fn | Pointer to the function to invoke when the object the counter refers to needs to be destroyed |
typedef struct janus_refcount janus_refcount |
Janus reference counter structure.
|
extern |