From 6d1d7f95529d826ea916ac80d236f5e8616daf64 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 29 May 2022 09:59:39 -0700 Subject: *: pivot to til_module_context_t - modules now allocate their contexts using til_module_context_new() instead of [cm]alloc(). - modules simply embed til_module_context_t at the start of their respective private context structs, if they do anything with contexts - modules that do nothing with contexts (lack a create_context() method), will now *always* get a til_module_context_t supplied to their other methods regardless of their create_context() presence. So even if you don't have a create_context(), your prepare_frame() and/or render_fragment() methods can still access seed and n_cpus from within the til_module_context_t passed in as context, *always*. - modules that *do* have a create_context() method, implying they have their own private context type, will have to cast the til_module_context_t supplied to the other methods to their private context type. By embedding the til_module_context_t at the *start* of their private context struct, a simple cast is all that's needed. If it's placed somewhere else, more annoying container_of() style macros are needed - this is strongly discouraged, just put it at the start of struct. - til_module_create_context() now takes n_cpus, which may be set to 0 for automatically assigning the number of threads in its place. Any non-zero value is treated as an explicit n_cpus, primarily intended for setting it to 1 for single-threaded contexts necessary when embedded within an already-threaded composite module. - modules like montage which open-coded a single-threaded render are now using the same til_module_render_fragment() as everything else, since til_module_create_context() is accepting n_cpus. - til_module_create_context() now produces a real type, not void *, that is til_module_context_t *. All the other module context functions now operate on this type, and since til_module_context_t.module tracks the module this context relates to, those functions no longer require both the module and context be passed in. This is especially helpful for compositing modules which do a lot of module context creation and destruction; the module handle is now only needed to create the contexts. Everything else operating on that context only needs the single context pointer, not module+context pairs, which was unnecessarily annoying. - if your module's context can be destroyed with a simple free(), without any deeper knowledge or freeing of nested pointers, you can now simply omit destroy_context() altogether. When destroy_context() is missing, til_module_context_free() will automatically use libc's free() on the pointer returned from your create_context() (or on the pointer that was automatically created if you omitted create_context() too, for the bare til_module_context_t that got created on your behalf anyways). For the most part, these changes don't affect module creation. In some ways this eases module creation by making it more convenient access seed and n_cpus if you had no further requirement for a context struct. In other ways it's slightly annoying to have to do type-casts when you're working with your own context type, since before it was all void* and didn't require casts when assigning to your typed context variables. The elimination for requiring a destroy_context() method in simple free() of private context scenarios removes some boilerplate in simple cases. I think it's a wash for module writers, or maybe a slight win for the simple cases. --- src/modules/flui2d/flui2d.c | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) (limited to 'src/modules/flui2d') diff --git a/src/modules/flui2d/flui2d.c b/src/modules/flui2d/flui2d.c index 4f87b54..df980ba 100644 --- a/src/modules/flui2d/flui2d.c +++ b/src/modules/flui2d/flui2d.c @@ -6,6 +6,7 @@ #include "til.h" #include "til_fb.h" +#include "til_module_context.h" #include "til_settings.h" @@ -186,6 +187,7 @@ typedef struct flui2d_setup_t { } flui2d_setup_t; typedef struct flui2d_context_t { + til_module_context_t til_module_context; flui2d_t fluid; flui2d_emitters_t emitters; float clockstep; @@ -243,7 +245,7 @@ static void gamma_init(float gamma) } -static void * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup) +static til_module_context_t * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cpus, til_setup_t *setup) { static int initialized; flui2d_context_t *ctxt; @@ -251,7 +253,7 @@ static void * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cp if (!setup) setup = &flui2d_default_setup.til_setup; - ctxt = calloc(1, sizeof(flui2d_context_t)); + ctxt = til_module_context_new(sizeof(flui2d_context_t), seed, n_cpus); if (!ctxt) return NULL; @@ -266,20 +268,14 @@ static void * flui2d_create_context(unsigned seed, unsigned ticks, unsigned n_cp ctxt->emitters = ((flui2d_setup_t *)setup)->emitters; ctxt->clockstep = ((flui2d_setup_t *)setup)->clockstep; - return ctxt; -} - - -static void flui2d_destroy_context(void *context) -{ - free(context); + return &ctxt->til_module_context; } /* Prepare a frame for concurrent drawing of fragment using multiple fragments */ -static void flui2d_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter) +static void flui2d_prepare_frame(til_module_context_t *context, unsigned ticks, til_fb_fragment_t *fragment, til_fragmenter_t *res_fragmenter) { - flui2d_context_t *ctxt = context; + flui2d_context_t *ctxt = (flui2d_context_t *)context; float r = (ticks % (unsigned)(2 * M_PI * 1000)) * .001f; *res_fragmenter = til_fragmenter_tile64; @@ -336,9 +332,9 @@ static void flui2d_prepare_frame(void *context, unsigned ticks, unsigned n_cpus, /* Draw a the flui2d densities */ -static void flui2d_render_fragment(void *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment) +static void flui2d_render_fragment(til_module_context_t *context, unsigned ticks, unsigned cpu, til_fb_fragment_t *fragment) { - flui2d_context_t *ctxt = context; + flui2d_context_t *ctxt = (flui2d_context_t *)context; for (int y = fragment->y; y < fragment->y + fragment->height; y++) { int y0, y1; @@ -542,11 +538,10 @@ static int flui2d_setup(const til_settings_t *settings, til_setting_t **res_sett til_module_t flui2d_module = { .create_context = flui2d_create_context, - .destroy_context = flui2d_destroy_context, .prepare_frame = flui2d_prepare_frame, .render_fragment = flui2d_render_fragment, + .setup = flui2d_setup, .name = "flui2d", .description = "Fluid dynamics simulation in 2D (threaded (poorly))", .author = "Vito Caputo ", - .setup = flui2d_setup, }; -- cgit v1.2.3