diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-04-24 20:19:33 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-04-24 20:39:20 -0700 |
commit | 2bcc2ca1ed6b7e1fa075770bcb306a3fdff917af (patch) | |
tree | 35537bd08d9f37b16c83f2180fd3cb4f3c11093e | |
parent | 9f9f9eaa096e6be8c1613014868e919d6991b188 (diff) |
til_setup: introduce til_setup_free()
simple wrapper around til_setup_t.free()
-rw-r--r-- | src/til_setup.c | 19 | ||||
-rw-r--r-- | src/til_setup.h | 1 |
2 files changed, 20 insertions, 0 deletions
diff --git a/src/til_setup.c b/src/til_setup.c index c5fd451..aab196b 100644 --- a/src/til_setup.c +++ b/src/til_setup.c @@ -8,6 +8,10 @@ /* Allocate and initialize a new til_setup_t of size bytes. * free_func is assigned to til_setup_t.free, and will be used for freeing the * instance returned when destroyed. + * + * Note this returns void * despite creating a til_setup_t, this is for convenience + * as the callers are generally using it in place of calloc(), and assign it to a + * container struct of some other type but having an embedded til_setup_t. */ void * til_setup_new(size_t size, void (*free_func)(til_setup_t *setup)) { @@ -24,3 +28,18 @@ void * til_setup_new(size_t size, void (*free_func)(til_setup_t *setup)) return setup; } + + +/* Free the setup when non-NULL, using setup->free if non-NULL. + * Always returns NULL for uses like foo = til_setup_free(foo); + */ +void * til_setup_free(til_setup_t *setup) +{ + if (!setup) + return NULL; + + if (setup->free) + setup->free(setup); + + return NULL; +} diff --git a/src/til_setup.h b/src/til_setup.h index 7f61213..16380d5 100644 --- a/src/til_setup.h +++ b/src/til_setup.h @@ -8,5 +8,6 @@ struct til_setup_t { }; void * til_setup_new(size_t size, void (*free_func)(til_setup_t *setup)); +void * til_setup_free(til_setup_t *setup); #endif |