From 5e647dee95763d8f628bdc771a32c5d33c51d78a Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Thu, 19 Jan 2023 21:00:40 -0800 Subject: til: pass module to .context_create()/til_module_context_new() Let's make it so til_module_context_t as returned from til_module_context_new() can immediately be freed via til_module_context_free(). Previously it was only after the context propagated out to til_module_context_create() that it could be freed that way, as that was where the module member was being assigned. With this change, and wiring up the module pointer into til_module_t.create_context() as well for convenient providing to til_module_context_new(), til_module_t.create_context() error paths can easily cleanup via `return til_module_context_free()` But this does require the til_module_t.destroy_context() be able to safely handle partially constructed contexts, since the mid-create failure freeing won't necessarily have all the members initialized. There will probably be some NULL derefs to fix up, but at least the contexts are zero-initialized @ new. --- src/til_module_context.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/til_module_context.c') diff --git a/src/til_module_context.c b/src/til_module_context.c index 58de3d5..e065f0d 100644 --- a/src/til_module_context.c +++ b/src/til_module_context.c @@ -26,20 +26,25 @@ * 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_module_context_t. * - * path must not be NULL, and the context takes ownership of the path; it's freed @ context_free(). + * path must not be NULL, and the context always takes ownership of the path; it's freed @ context_free(). */ -void * til_module_context_new(til_stream_t *stream, size_t size, unsigned seed, unsigned ticks, unsigned n_cpus, char *path) +void * til_module_context_new(const til_module_t *module, size_t size, til_stream_t *stream, unsigned seed, unsigned ticks, unsigned n_cpus, char *path) { til_module_context_t *module_context; + assert(module); assert(size >= sizeof(til_module_context_t)); assert(n_cpus > 0); assert(path); /* modules must be able to key things like taps off their context's path */ module_context = calloc(1, size); - if (!module_context) + if (!module_context) { + free(path); + return NULL; + } + module_context->module = module; module_context->stream = stream; module_context->seed = seed; module_context->ticks = ticks; -- cgit v1.2.1