Reference counter mechanism. More...
Go to the source code of this file.
Data Structures | |
struct | imquic_refcount |
Macros | |
#define | imquic_refcount_containerof(refptr, type, member) ((type *)((char *)(refptr) - offsetof(type, member))) |
Macro to programmatically address the object itself from its counter. | |
#define | imquic_refcount_init(refp, free_fn) |
imquic reference counter initialization (debug according to settings) | |
#define | imquic_refcount_init_nodebug(refp, free_fn) |
imquic reference counter initialization (no debug) | |
#define | imquic_refcount_init_debug(refp, free_fn) |
imquic reference counter initialization (debug) | |
#define | imquic_refcount_increase(refp) |
Increase the imquic reference counter (debug according to settings) | |
#define | imquic_refcount_increase_nodebug(refp) |
Increase the imquic reference counter (no debug) | |
#define | imquic_refcount_increase_debug(refp) |
Increase the imquic reference counter (debug) | |
#define | imquic_refcount_decrease(refp) |
Decrease the imquic reference counter (debug according to settings) | |
#define | imquic_refcount_decrease_debug(refp) |
Decrease the imquic reference counter (debug) | |
#define | imquic_refcount_decrease_nodebug(refp) |
Decrease the imquic reference counter (no debug) | |
Typedefs | |
typedef struct imquic_refcount | imquic_refcount |
imquic reference counter structure | |
Variables | |
int | imquic_refcount_debug |
Reference counter mechanism.
Implementation of a simple reference counter that can be used to keep track of memory management in imquic, 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 imquic_refcount instance as one of the members of the object itself, and then call imquic_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 imquic_refcount_increase() and imquic_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(imquic_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 imquic_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; imquic_refcount myref; } void my_free_function(imquic_refcount *counter) { struct my_struct *my_object = imquic_refcount_containerof(counter, struct my_struct, myref); if(my_object->string) free(my_object->string); free(my_object); }
#define imquic_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 imquic_refcount instance, type
is the type of the object itself (e.g., struct mystruct
), while member
is how the imquic_refcount instance is called in the object that contains it.
#define imquic_refcount_decrease | ( | refp | ) |
Decrease the imquic reference counter (debug according to settings)
free
function if the counter reaches 0 refp | Pointer to the imquic reference counter instance |
#define imquic_refcount_decrease_debug | ( | refp | ) |
Decrease the imquic reference counter (debug)
free
function if the counter reaches 0 refp | Pointer to the imquic reference counter instance |
#define imquic_refcount_decrease_nodebug | ( | refp | ) |
Decrease the imquic reference counter (no debug)
free
function if the counter reaches 0 refp | Pointer to the imquic reference counter instance |
#define imquic_refcount_increase | ( | refp | ) |
Increase the imquic reference counter (debug according to settings)
refp | Pointer to the imquic reference counter instance |
#define imquic_refcount_increase_debug | ( | refp | ) |
Increase the imquic reference counter (debug)
refp | Pointer to the imquic reference counter instance |
#define imquic_refcount_increase_nodebug | ( | refp | ) |
Increase the imquic reference counter (no debug)
refp | Pointer to the imquic reference counter instance |
#define imquic_refcount_init | ( | refp, | |
free_fn ) |
imquic reference counter initialization (debug according to settings)
refp | Pointer to the imquic reference counter instance |
free_fn | Pointer to the function to invoke when the object the counter refers to needs to be destroyed |
#define imquic_refcount_init_debug | ( | refp, | |
free_fn ) |
imquic reference counter initialization (debug)
refp | Pointer to the imquic reference counter instance |
free_fn | Pointer to the function to invoke when the object the counter refers to needs to be destroyed |
#define imquic_refcount_init_nodebug | ( | refp, | |
free_fn ) |
imquic reference counter initialization (no debug)
refp | Pointer to the imquic reference counter instance |
free_fn | Pointer to the function to invoke when the object the counter refers to needs to be destroyed |
typedef struct imquic_refcount imquic_refcount |
imquic reference counter structure
|
extern |