summaryrefslogtreecommitdiff
path: root/src/til_setup.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2022-04-24 17:46:18 -0700
committerVito Caputo <vcaputo@pengaru.com>2022-04-24 17:46:18 -0700
commit1435249cd1cac95d31403a9592018eaad9c7cb00 (patch)
tree82c72ee95d706aa138574ae28bfcc7b240af4a61 /src/til_setup.c
parent23f54e51f5bb62ffa98383df996441c5852b5d2d (diff)
til_setup: add til_setup_new() helper
For use when setup functions allocate their private setup to return in *res_setup. They specify the size of their private setup, and supply the free function to use. This may be libc's free() when it's a simple setup struct, or a bespoke free function when deep/complex freeing is required for cleanup. It's expected that callers will be embedding til_setup_t at the start of their private setup struct, and returning a pointer to this in *res_setup which would be the same value as a pointer to to their private setup struct.
Diffstat (limited to 'src/til_setup.c')
-rw-r--r--src/til_setup.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/til_setup.c b/src/til_setup.c
new file mode 100644
index 0000000..c5fd451
--- /dev/null
+++ b/src/til_setup.c
@@ -0,0 +1,26 @@
+#include <assert.h>
+#include <errno.h>
+#include <stdlib.h>
+
+#include "til_setup.h"
+
+
+/* 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.
+ */
+void * til_setup_new(size_t size, void (*free_func)(til_setup_t *setup))
+{
+ til_setup_t *setup;
+
+ assert(size >= sizeof(til_setup_t));
+ assert(free_func);
+
+ setup = calloc(1, size);
+ if (!setup)
+ return NULL;
+
+ setup->free = free_func;
+
+ return setup;
+}
© All Rights Reserved