Mercurial > molko
changeset 392:e6d7ebad33cc
core: don't use fixed arrays in action_stack
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 15 Feb 2022 20:47:47 +0100 |
parents | 9334b420c975 |
children | b172b10bbe1c |
files | src/libmlk-core/core/action-stack.c src/libmlk-core/core/action-stack.h tests/test-action.c |
diffstat | 3 files changed, 25 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/src/libmlk-core/core/action-stack.c Tue Feb 15 20:35:23 2022 +0100 +++ b/src/libmlk-core/core/action-stack.c Tue Feb 15 20:47:47 2022 +0100 @@ -23,14 +23,18 @@ #include "action-stack.h" #define ACTION_FOREACH(st, iter) \ - for (size_t i = 0; i < ACTION_STACK_MAX && ((iter) = (st)->actions[i], 1); ++i) + for (size_t i = 0; i < (st)->actionsz && ((iter) = (st)->actions[i], 1); ++i) void -action_stack_init(struct action_stack *st) +action_stack_init(struct action_stack *st, struct action **actions, size_t actionsz) { assert(st); - memset(st, 0, sizeof (*st)); + st->actions = actions; + st->actionsz = actionsz; + + for (size_t i = 0; i < st->actionsz; ++i) + st->actions[i] = NULL; } int @@ -39,7 +43,7 @@ assert(st); assert(act); - for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { + for (size_t i = 0; i < st->actionsz; ++i) { if (!st->actions[i]) { st->actions[i] = act; return 0; @@ -69,7 +73,7 @@ struct action *act; - for (size_t i = 0; i < ACTION_STACK_MAX; ++i) { + for (size_t i = 0; i < st->actionsz; ++i) { act = st->actions[i]; if (act && action_update(act, ticks)) {
--- a/src/libmlk-core/core/action-stack.h Tue Feb 15 20:35:23 2022 +0100 +++ b/src/libmlk-core/core/action-stack.h Tue Feb 15 20:47:47 2022 +0100 @@ -19,20 +19,21 @@ #ifndef MLK_CORE_ACTION_STACK_H #define MLK_CORE_ACTION_STACK_H -#include "core.h" +#include <stddef.h> -#define ACTION_STACK_MAX (128) +#include "core.h" union event; struct action_stack { - struct action *actions[ACTION_STACK_MAX]; + struct action **actions; + size_t actionsz; }; CORE_BEGIN_DECLS void -action_stack_init(struct action_stack *); +action_stack_init(struct action_stack *, struct action **, size_t); int action_stack_add(struct action_stack *, struct action *);
--- a/tests/test-action.c Tue Feb 15 20:35:23 2022 +0100 +++ b/tests/test-action.c Tue Feb 15 20:47:47 2022 +0100 @@ -172,13 +172,16 @@ RX_TEST_CASE(stack, add) { + struct action *actions[10]; struct action_stack st = {0}; struct action act = {0}; + action_stack_init(&st, actions, 10); + RX_INT_REQUIRE_EQUAL(action_stack_add(&st, &act), 0); /* Now fill up. */ - for (int i = 0; i < ACTION_STACK_MAX - 1; ++i) + for (int i = 0; i < 9; ++i) RX_INT_REQUIRE_EQUAL(action_stack_add(&st, &act), 0); /* This one should not fit in. */ @@ -196,8 +199,10 @@ { 0, { .data = &table[2].called, .handle = my_handle } }, }; + struct action *actions[10]; struct action_stack st = {0}; + action_stack_init(&st, actions, 10); action_stack_add(&st, &table[0].act); action_stack_add(&st, &table[1].act); action_stack_add(&st, &table[2].act); @@ -223,8 +228,10 @@ { .act = INIT(&table[6], my_update_false) }, }; + struct action *actions[10]; struct action_stack st = {0}; + action_stack_init(&st, actions, 10); action_stack_add(&st, &table[0].act); action_stack_add(&st, &table[1].act); action_stack_add(&st, &table[2].act); @@ -313,8 +320,11 @@ { .act = INIT(&table[0], my_update_true) }, { .act = INIT(&table[0], my_update_false) }, }; + + struct action *actions[10]; struct action_stack st = {0}; + action_stack_init(&st, actions, 10); action_stack_add(&st, &table[0].act); action_stack_add(&st, &table[1].act); action_stack_finish(&st); @@ -330,10 +340,6 @@ RX_REQUIRE(!table[0].inv.draw); RX_REQUIRE(table[0].inv.end); RX_REQUIRE(table[0].inv.finish); - - /* They should also be NULL'ed. */ - RX_PTR_REQUIRE_EQUAL(st.actions[0], NULL); - RX_PTR_REQUIRE_EQUAL(st.actions[1], NULL); } int