From 6aa77bc0efc27d976f2d478ca54fa59a7c47e934 Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sun, 28 May 2023 18:38:52 -0700 Subject: til_setup,*: note settings path in til_setup_t This commit adds passing the settings instance to til_setup_new() which is used for deriving a path for the setup via til_settings_print_path() on the supplied settings. That path gets an allocated copy left in the returned til_setup_t at til_setup_t.path This path will exist for the lifetime of the til_setup_t, to be freed along with the rest of the baked setup instance when the refcount reaches 0. The incoming til_settings_t is only read @ til_setup_new() in constructing the path, no reference is kept. Basically the til_settings_t* is just passed in for convenience reasons, since constructing the path needs memory and may fail, this approach lets the existing til_setup_new() call error handling also capture the path allocation failures as-is turning til_setup_new() into a bit more of a convenience helper. Note that now all code may assume a til_setup_t has a set and valid til_setup_t.path, which should be useful for context creates when a setup is available. --- src/drm_fb.c | 2 +- src/mem_fb.c | 2 +- src/modules/blinds/blinds.c | 2 +- src/modules/checkers/checkers.c | 2 +- src/modules/compose/compose.c | 2 +- src/modules/drizzle/drizzle.c | 2 +- src/modules/flui2d/flui2d.c | 2 +- src/modules/moire/moire.c | 2 +- src/modules/pixbounce/pixbounce.c | 2 +- src/modules/plato/plato.c | 2 +- src/modules/rkt/rkt.c | 2 +- src/modules/rtv/rtv.c | 2 +- src/modules/shapes/shapes.c | 2 +- src/modules/sparkler/sparkler.c | 2 +- src/modules/stars/stars.c | 2 +- src/modules/strobe/strobe.c | 2 +- src/modules/submit/submit.c | 2 +- src/modules/swarm/swarm.c | 2 +- src/modules/voronoi/voronoi.c | 2 +- src/sdl_fb.c | 2 +- src/til_setup.c | 35 +++++++++++++++++++++++++++++++++-- src/til_setup.h | 4 +++- 22 files changed, 56 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/drm_fb.c b/src/drm_fb.c index d23a2f7..fe2fefd 100644 --- a/src/drm_fb.c +++ b/src/drm_fb.c @@ -301,7 +301,7 @@ static void drm_fb_setup_free(til_setup_t *setup) /* positive value indicates another setting is needed, described in next_setting */ static int drm_fb_setup(const til_settings_t *settings, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup) { - drm_fb_setup_t *setup = til_setup_new(sizeof(*setup), drm_fb_setup_free); + drm_fb_setup_t *setup = til_setup_new(settings, sizeof(*setup), drm_fb_setup_free); til_setting_desc_generator_t generators[] = { { .key = "dev", diff --git a/src/mem_fb.c b/src/mem_fb.c index a152d6a..2d8f936 100644 --- a/src/mem_fb.c +++ b/src/mem_fb.c @@ -48,7 +48,7 @@ static int mem_fb_setup(const til_settings_t *settings, til_setting_t **res_sett if (res_setup) { mem_fb_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/blinds/blinds.c b/src/modules/blinds/blinds.c index ecfa2d5..39b439f 100644 --- a/src/modules/blinds/blinds.c +++ b/src/modules/blinds/blinds.c @@ -175,7 +175,7 @@ static int blinds_setup(const til_settings_t *settings, til_setting_t **res_sett if (res_setup) { blinds_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/checkers/checkers.c b/src/modules/checkers/checkers.c index f13727e..3bd4dda 100644 --- a/src/modules/checkers/checkers.c +++ b/src/modules/checkers/checkers.c @@ -670,7 +670,7 @@ static int checkers_setup(const til_settings_t *settings, til_setting_t **res_se if (res_setup) { checkers_setup_t *setup; - setup = til_setup_new(sizeof(*setup), checkers_setup_free); + setup = til_setup_new(settings, sizeof(*setup), checkers_setup_free); if (!setup) return -ENOMEM; diff --git a/src/modules/compose/compose.c b/src/modules/compose/compose.c index 600193c..4f0a8c8 100644 --- a/src/modules/compose/compose.c +++ b/src/modules/compose/compose.c @@ -454,7 +454,7 @@ static int compose_setup(const til_settings_t *settings, til_setting_t **res_set til_setting_t *layer_setting; compose_setup_t *setup; - setup = til_setup_new(sizeof(*setup) + n_layers * sizeof(*setup->layers), compose_setup_free); + setup = til_setup_new(settings, sizeof(*setup) + n_layers * sizeof(*setup->layers), compose_setup_free); if (!setup) return -ENOMEM; diff --git a/src/modules/drizzle/drizzle.c b/src/modules/drizzle/drizzle.c index 3485cc1..844a19a 100644 --- a/src/modules/drizzle/drizzle.c +++ b/src/modules/drizzle/drizzle.c @@ -402,7 +402,7 @@ static int drizzle_setup(const til_settings_t *settings, til_setting_t **res_set drizzle_setup_t *setup; int i; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/flui2d/flui2d.c b/src/modules/flui2d/flui2d.c index ba95023..41baaac 100644 --- a/src/modules/flui2d/flui2d.c +++ b/src/modules/flui2d/flui2d.c @@ -523,7 +523,7 @@ static int flui2d_setup(const til_settings_t *settings, til_setting_t **res_sett if (res_setup) { flui2d_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/moire/moire.c b/src/modules/moire/moire.c index 8616f3b..e75a5df 100644 --- a/src/modules/moire/moire.c +++ b/src/modules/moire/moire.c @@ -142,7 +142,7 @@ static int moire_setup(const til_settings_t *settings, til_setting_t **res_setti if (res_setup) { moire_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/pixbounce/pixbounce.c b/src/modules/pixbounce/pixbounce.c index 80857f7..4589b60 100644 --- a/src/modules/pixbounce/pixbounce.c +++ b/src/modules/pixbounce/pixbounce.c @@ -374,7 +374,7 @@ int pixbounce_setup(const til_settings_t *settings, til_setting_t **res_setting, if (res_setup) { pixbounce_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/plato/plato.c b/src/modules/plato/plato.c index f8a3483..52e653a 100644 --- a/src/modules/plato/plato.c +++ b/src/modules/plato/plato.c @@ -744,7 +744,7 @@ static int plato_setup(const til_settings_t *settings, til_setting_t **res_setti plato_setup_t *setup; int i; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/rkt/rkt.c b/src/modules/rkt/rkt.c index d274391..6bd4ddd 100644 --- a/src/modules/rkt/rkt.c +++ b/src/modules/rkt/rkt.c @@ -391,7 +391,7 @@ static int rkt_setup(const til_settings_t *settings, til_setting_t **res_setting return -ENOENT; /* TODO: we're going to need a custom setup_free to cleanup host+base etc. */ - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/rtv/rtv.c b/src/modules/rtv/rtv.c index 29d1e71..389a0f5 100644 --- a/src/modules/rtv/rtv.c +++ b/src/modules/rtv/rtv.c @@ -492,7 +492,7 @@ static int rtv_setup(const til_settings_t *settings, til_setting_t **res_setting rtv_setup_t *setup; /* FIXME: rtv_setup_t.snow_module needs freeing, so we need a bespoke free_func */ - setup = til_setup_new(sizeof(*setup) + sizeof(setup->channels[0]), NULL); + setup = til_setup_new(settings, sizeof(*setup) + sizeof(setup->channels[0]), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/shapes/shapes.c b/src/modules/shapes/shapes.c index c3b0ef4..ab836a0 100644 --- a/src/modules/shapes/shapes.c +++ b/src/modules/shapes/shapes.c @@ -455,7 +455,7 @@ static int shapes_setup(const til_settings_t *settings, til_setting_t **res_sett shapes_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index 6105112..b6ffd01 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -184,7 +184,7 @@ static int sparkler_setup(const til_settings_t *settings, til_setting_t **res_se if (res_setup) { sparkler_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/stars/stars.c b/src/modules/stars/stars.c index 68e0335..6d5f3c0 100644 --- a/src/modules/stars/stars.c +++ b/src/modules/stars/stars.c @@ -276,7 +276,7 @@ int stars_setup(const til_settings_t *settings, til_setting_t **res_setting, con if (res_setup) { stars_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/strobe/strobe.c b/src/modules/strobe/strobe.c index cd3ba94..9f6b04a 100644 --- a/src/modules/strobe/strobe.c +++ b/src/modules/strobe/strobe.c @@ -123,7 +123,7 @@ static int strobe_setup(const til_settings_t *settings, til_setting_t **res_sett if (res_setup) { strobe_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/submit/submit.c b/src/modules/submit/submit.c index 24e7315..c50b67c 100644 --- a/src/modules/submit/submit.c +++ b/src/modules/submit/submit.c @@ -352,7 +352,7 @@ static int submit_setup(const til_settings_t *settings, til_setting_t **res_sett if (res_setup) { submit_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/swarm/swarm.c b/src/modules/swarm/swarm.c index 96b7e1c..4a21260 100644 --- a/src/modules/swarm/swarm.c +++ b/src/modules/swarm/swarm.c @@ -439,7 +439,7 @@ static int swarm_setup(const til_settings_t *settings, til_setting_t **res_setti if (res_setup) { swarm_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/modules/voronoi/voronoi.c b/src/modules/voronoi/voronoi.c index 23ea00a..45333b0 100644 --- a/src/modules/voronoi/voronoi.c +++ b/src/modules/voronoi/voronoi.c @@ -403,7 +403,7 @@ static int voronoi_setup(const til_settings_t *settings, til_setting_t **res_set if (res_setup) { voronoi_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/sdl_fb.c b/src/sdl_fb.c index 6d30064..2b0a605 100644 --- a/src/sdl_fb.c +++ b/src/sdl_fb.c @@ -102,7 +102,7 @@ static int sdl_fb_setup(const til_settings_t *settings, til_setting_t **res_sett if (res_setup) { sdl_fb_setup_t *setup; - setup = til_setup_new(sizeof(*setup), NULL); + setup = til_setup_new(settings, sizeof(*setup), NULL); if (!setup) return -ENOMEM; diff --git a/src/til_setup.c b/src/til_setup.c index d819173..c1198fd 100644 --- a/src/til_setup.c +++ b/src/til_setup.c @@ -1,7 +1,9 @@ #include #include +#include #include +#include "til_settings.h" #include "til_setup.h" @@ -10,20 +12,46 @@ * instance returned when destroyed. If free_func is NULL, free() will be * used by default. * + * A copy of the provided settings' path is stored at til_setup_t.path, and will + * always be freed automatically when the setup instance is freed, independent of + * free_func. + * * 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)) +void * til_setup_new(const til_settings_t *settings, size_t size, void (*free_func)(til_setup_t *setup)) { + char *path_buf = NULL; til_setup_t *setup; + assert(settings); assert(size >= sizeof(til_setup_t)); + { /* TODO FIXME: more unportable memstream use! */ + FILE *path_fp; + size_t path_sz; + int r; + + path_fp = open_memstream(&path_buf, &path_sz); + if (!path_fp) + return NULL; + + r = til_settings_print_path(settings, path_fp); + fclose(path_fp); + if (r < 0) { + free(path_buf); + return NULL; + } + } + setup = calloc(1, size); - if (!setup) + if (!setup) { + free(path_buf); return NULL; + } + setup->path = path_buf; setup->refcount = 1; setup->free = free_func; @@ -57,6 +85,9 @@ static void * til_setup_unref(til_setup_t *setup) setup->refcount--; if (!setup->refcount) { + /* don't make setup->free() free the path when provided */ + free((void *)setup->path); + if (setup->free) setup->free(setup); else diff --git a/src/til_setup.h b/src/til_setup.h index 8cc7a88..2d0facb 100644 --- a/src/til_setup.h +++ b/src/til_setup.h @@ -1,14 +1,16 @@ #ifndef _TIL_SETUP_H #define _TIL_SETUP_H +typedef struct til_settings_t til_settings_t; typedef struct til_setup_t til_setup_t; struct til_setup_t { + const char *path; unsigned refcount; void (*free)(til_setup_t *setup); }; -void * til_setup_new(size_t size, void (*free_func)(til_setup_t *setup)); +void * til_setup_new(const til_settings_t *settings, size_t size, void (*free_func)(til_setup_t *setup)); void * til_setup_ref(til_setup_t *setup); void * til_setup_free(til_setup_t *setup); -- cgit v1.2.1