diff --git a/include/refcount.h b/include/refcount.h index 21f6935..397845c 100644 --- a/include/refcount.h +++ b/include/refcount.h @@ -57,6 +57,12 @@ typedef struct libab_ref_count_s libab_ref_count; * @return the result of the construction of the reference. */ libab_result libab_ref_new(libab_ref* ref, void* data, void (*free_func)(void* data)); +/** + * Creates a reference to NULL. This does + * not require a memory allocation. + * @param ref the reference to initialize with null. + */ +void libab_ref_null(libab_ref* ref); /** * Turns the given reference into a weak reference, * making it not keep the data allocated. diff --git a/src/refcount.c b/src/refcount.c index 5d32cee..5e7c643 100644 --- a/src/refcount.c +++ b/src/refcount.c @@ -15,6 +15,19 @@ libab_result libab_ref_new(libab_ref* ref, void* data, void (*free_func)(void* d return result; } +static libab_ref_count null_count = { + NULL, + 0, + 1 +}; + +void libab_ref_null(libab_ref* ref) { + ref->strong = 0; + ref->data = NULL; + ref->count = &null_count; + null_count.weak++; +} + void _libab_ref_changed(libab_ref* ref) { if(ref->count->strong == 0) { ref->count->strong--;