Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Variables
refcount.h File Reference

Reference counter mechanism. More...

#include <glib.h>
#include "mutex.h"
Include dependency graph for refcount.h:
This graph shows which files directly or indirectly include this file:

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
 

Detailed Description

Reference counter mechanism.

Author
Lorenzo Miniero loren.nosp@m.zo@m.nosp@m.eetec.nosp@m.ho.c.nosp@m.om

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);
}

Macro Definition Documentation

◆ imquic_refcount_containerof

#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.

◆ imquic_refcount_decrease

#define imquic_refcount_decrease ( refp)
Value:
{ \
imquic_refcount_decrease_nodebug(refp); \
} else { \
imquic_refcount_decrease_debug(refp); \
} \
}
int imquic_refcount_debug
Definition imquic.c:30

Decrease the imquic reference counter (debug according to settings)

Note
Will invoke the free function if the counter reaches 0
Parameters
refpPointer to the imquic reference counter instance

◆ imquic_refcount_decrease_debug

#define imquic_refcount_decrease_debug ( refp)
Value:
{ \
IMQUIC_PRINT("[%s:%s:%d:decrease] %p (%d)\n", __FILE__, __FUNCTION__, __LINE__, refp, (refp)->count-1); \
if(g_atomic_int_dec_and_test((gint *)&(refp)->count)) { \
(refp)->free(refp); \
} \
}

Decrease the imquic reference counter (debug)

Note
Will invoke the free function if the counter reaches 0
Parameters
refpPointer to the imquic reference counter instance

◆ imquic_refcount_decrease_nodebug

#define imquic_refcount_decrease_nodebug ( refp)
Value:
{ \
if(g_atomic_int_dec_and_test((gint *)&(refp)->count)) { \
(refp)->free(refp); \
} \
}

Decrease the imquic reference counter (no debug)

Note
Will invoke the free function if the counter reaches 0
Parameters
refpPointer to the imquic reference counter instance

◆ imquic_refcount_increase

#define imquic_refcount_increase ( refp)
Value:
{ \
imquic_refcount_increase_nodebug(refp); \
} else { \
imquic_refcount_increase_debug(refp); \
} \
}

Increase the imquic reference counter (debug according to settings)

Parameters
refpPointer to the imquic reference counter instance

◆ imquic_refcount_increase_debug

#define imquic_refcount_increase_debug ( refp)
Value:
{ \
IMQUIC_PRINT("[%s:%s:%d:increase] %p (%d)\n", __FILE__, __FUNCTION__, __LINE__, refp, (refp)->count+1); \
g_atomic_int_inc((gint *)&(refp)->count); \
}

Increase the imquic reference counter (debug)

Parameters
refpPointer to the imquic reference counter instance

◆ imquic_refcount_increase_nodebug

#define imquic_refcount_increase_nodebug ( refp)
Value:
{ \
g_atomic_int_inc((gint *)&(refp)->count); \
}

Increase the imquic reference counter (no debug)

Parameters
refpPointer to the imquic reference counter instance

◆ imquic_refcount_init

#define imquic_refcount_init ( refp,
free_fn )
Value:
{ \
imquic_refcount_init_nodebug(refp, free_fn); \
} else { \
imquic_refcount_init_debug(refp, free_fn); \
} \
}

imquic reference counter initialization (debug according to settings)

Note
Also sets the counter to 1 automatically, so no need to increase it again manually via imquic_refcount_increase() after the initialization
Parameters
refpPointer to the imquic reference counter instance
free_fnPointer to the function to invoke when the object the counter refers to needs to be destroyed

◆ imquic_refcount_init_debug

#define imquic_refcount_init_debug ( refp,
free_fn )
Value:
{ \
(refp)->count = 1; \
IMQUIC_PRINT("[%s:%s:%d:init] %p (%d)\n", __FILE__, __FUNCTION__, __LINE__, refp, (refp)->count); \
(refp)->free = free_fn; \
}

imquic reference counter initialization (debug)

Note
Also sets the counter to 1 automatically, so no need to increase it again manually via imquic_refcount_increase() after the initialization
Parameters
refpPointer to the imquic reference counter instance
free_fnPointer to the function to invoke when the object the counter refers to needs to be destroyed

◆ imquic_refcount_init_nodebug

#define imquic_refcount_init_nodebug ( refp,
free_fn )
Value:
{ \
(refp)->count = 1; \
(refp)->free = free_fn; \
}

imquic reference counter initialization (no debug)

Note
Also sets the counter to 1 automatically, so no need to increase it again manually via imquic_refcount_increase() after the initialization
Parameters
refpPointer to the imquic reference counter instance
free_fnPointer to the function to invoke when the object the counter refers to needs to be destroyed

Typedef Documentation

◆ imquic_refcount

typedef struct imquic_refcount imquic_refcount

imquic reference counter structure

Variable Documentation

◆ imquic_refcount_debug

int imquic_refcount_debug
extern