summaryrefslogtreecommitdiff
path: root/src/til_module_context.c
diff options
context:
space:
mode:
authorVito Caputo <vcaputo@pengaru.com>2023-06-12 15:38:11 -0700
committerVito Caputo <vcaputo@pengaru.com>2023-06-12 17:02:58 -0700
commit8fb3b27efa707f53bf0b0d74aec0e055f0d10e96 (patch)
treeb6dd8014312e1c417f5fa7b819c599959e44fef1 /src/til_module_context.c
parenta3ad7e5bfdbb54f59555bfe408c52a5237db14d3 (diff)
til_module_context: refcount module contexts
This becomes necessary in a world with externall-discoverable-on-stream-contexts that can be arbitrarily referenced by other contexts. There will probably be more complexity necessary for invalidating references, but this is a start.
Diffstat (limited to 'src/til_module_context.c')
-rw-r--r--src/til_module_context.c45
1 files changed, 42 insertions, 3 deletions
diff --git a/src/til_module_context.c b/src/til_module_context.c
index 98a32cd..092e3e1 100644
--- a/src/til_module_context.c
+++ b/src/til_module_context.c
@@ -50,6 +50,7 @@ void * til_module_context_new(const til_module_t *module, size_t size, til_strea
module_context->ticks = ticks;
module_context->n_cpus = n_cpus;
module_context->setup = til_setup_ref(setup);
+ module_context->refcount = 1;
return module_context;
}
@@ -60,13 +61,13 @@ void * til_module_context_new(const til_module_t *module, size_t size, til_strea
*
* Note this replaces til_module_destroy_context(), which has been removed.
*/
-void * til_module_context_free(til_module_context_t *module_context)
+static inline void * module_context_free(til_module_context_t *module_context)
{
til_stream_t *stream;
til_setup_t *setup;
- if (!module_context)
- return NULL;
+ assert(module_context);
+ assert(!module_context->refcount);
stream = module_context->stream;
setup = module_context->setup;
@@ -87,3 +88,41 @@ void * til_module_context_free(til_module_context_t *module_context)
return NULL;
}
+
+
+/* bump refcount on context */
+void * til_module_context_ref(til_module_context_t *module_context)
+{
+ assert(module_context);
+
+ module_context->refcount++;
+
+ return module_context;
+}
+
+
+/* unref the module_context, returns non-NULL when module_context persists */
+static void * til_module_context_unref(til_module_context_t *module_context)
+{
+/* XXX this is kept private until there's a real use case in need of the distinct return values vs. free.
+ * til_setup is the same way, which this is basically mirroring
+ */
+ if (!module_context)
+ return NULL;
+
+ assert(module_context->refcount > 0);
+
+ module_context->refcount--;
+ if (!module_context->refcount)
+ return module_context_free(module_context);
+
+ return module_context;
+}
+
+
+void * til_module_context_free(til_module_context_t *module_context)
+{
+ (void) til_module_context_unref(module_context);
+
+ return NULL;
+}
© All Rights Reserved