From c207b329654cc3a5efa58127f43477cd915a941d Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Sat, 2 Sep 2023 09:51:39 -0700 Subject: modules/flow: minor cleanup of ff_new()/ff_free() Simplify ff_new() failure path by using ff_free(), also make ff_free() more ergonomic by returning NULL. --- src/modules/flow/ff.c | 31 +++++++++++++++---------------- src/modules/flow/ff.h | 2 +- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/modules/flow/ff.c b/src/modules/flow/ff.c index 18b55d2..a0ab56c 100644 --- a/src/modules/flow/ff.c +++ b/src/modules/flow/ff.c @@ -26,6 +26,19 @@ void ff_populate(ff_t *ff, unsigned idx) } +ff_t * ff_free(ff_t *ff) +{ + if (ff) { + for (int i = 0; i < 2; i++) + free(ff->fields[i]); + + free(ff); + } + + return NULL; +} + + ff_t * ff_new(unsigned size, void (*populator)(void *context, unsigned size, const v3f_t *other, v3f_t *field), void *context) { ff_t *ff; @@ -36,13 +49,8 @@ ff_t * ff_new(unsigned size, void (*populator)(void *context, unsigned size, con for (int i = 0; i < 2; i++) { ff->fields[i] = calloc(size * size * size, sizeof(v3f_t)); - if (!ff->fields[i]) { - for (int j = 0; j < i; j++) - free(ff->fields[j]); - - free(ff); - return NULL; - } + if (!ff->fields[i]) + return ff_free(ff); } ff->size = size; @@ -56,15 +64,6 @@ ff_t * ff_new(unsigned size, void (*populator)(void *context, unsigned size, con } -void ff_free(ff_t *ff) -{ - for (int i = 0; i < 2; i++) - free(ff->fields[i]); - - free(ff); -} - - static inline v3f_t ff_sample(v3f_t *field, size_t size, v3f_t *min, v3f_t *max, v3f_t *t) { v3f_t *a, *b, *c, *d, *e, *f, *g, *h; diff --git a/src/modules/flow/ff.h b/src/modules/flow/ff.h index 4d30143..0324ea0 100644 --- a/src/modules/flow/ff.h +++ b/src/modules/flow/ff.h @@ -6,7 +6,7 @@ typedef struct ff_t ff_t; ff_t * ff_new(unsigned size, void (*populator)(void *context, unsigned size, const v3f_t *other, v3f_t *field), void *context); -void ff_free(ff_t *ff); +ff_t * ff_free(ff_t *ff); v3f_t ff_get(ff_t *ff, v3f_t *coordinate, float w); void ff_populate(ff_t *ff, unsigned idx); -- cgit v1.2.3