Add a null reference that doesn't call malloc.

This commit is contained in:
Danila Fedorin 2018-04-06 23:53:40 -07:00
parent 60d22c2511
commit dabab1e870
2 changed files with 19 additions and 0 deletions

View File

@ -57,6 +57,12 @@ typedef struct libab_ref_count_s libab_ref_count;
* @return the result of the construction of the reference. * @return the result of the construction of the reference.
*/ */
libab_result libab_ref_new(libab_ref* ref, void* data, void (*free_func)(void* data)); 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, * Turns the given reference into a weak reference,
* making it not keep the data allocated. * making it not keep the data allocated.

View File

@ -15,6 +15,19 @@ libab_result libab_ref_new(libab_ref* ref, void* data, void (*free_func)(void* d
return result; 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) { void _libab_ref_changed(libab_ref* ref) {
if(ref->count->strong == 0) { if(ref->count->strong == 0) {
ref->count->strong--; ref->count->strong--;