From 01e2a1dc5aaf6da4fcd1c75003510f786dbc3cd3 Mon Sep 17 00:00:00 2001 From: Holger Schemel Date: Mon, 20 Jan 2025 19:16:32 +0100 Subject: [PATCH] fixed bad use of function pointers that was breaking Emscripten port --- src/game_bd/bd_bdcff.c | 4 ++-- src/game_bd/bd_cave.c | 4 ++-- src/game_bd/bd_caveobject.c | 2 +- src/game_bd/bd_caveset.c | 2 +- src/libgame/list.c | 39 +++++++++++++++++++++++++++++++------ src/libgame/list.h | 6 ++++-- 6 files changed, 43 insertions(+), 14 deletions(-) diff --git a/src/game_bd/bd_bdcff.c b/src/game_bd/bd_bdcff.c index 56ab2790..b473b5d0 100644 --- a/src/game_bd/bd_bdcff.c +++ b/src/game_bd/bd_bdcff.c @@ -1557,7 +1557,7 @@ static void cave_properties_remove(GdPtrArray *out, const char *prefix) } // output properties of a structure to a file. -// list_foreach func, so "out" is the last parameter! +// list_foreach_fn_2 func, so "out" is the last parameter! static void caveset_save_cave_func(GdCave *cave, GdPtrArray *out) { GdCave *default_cave; @@ -1790,7 +1790,7 @@ GdPtrArray *gd_caveset_save_to_bdcff(void) gd_caveset_data_free(default_caveset); gd_ptr_array_add(out, getStringCopy("Levels=5")); - list_foreach(gd_caveset, (list_fn)caveset_save_cave_func, out); + list_foreach_fn_2(gd_caveset, (list_fn_2) caveset_save_cave_func, out); gd_ptr_array_add(out, getStringCopy("[/game]")); gd_ptr_array_add(out, getStringCopy("[/BDCFF]")); diff --git a/src/game_bd/bd_cave.c b/src/game_bd/bd_cave.c index 5061a8b6..a8a2c7f5 100644 --- a/src/game_bd/bd_cave.c +++ b/src/game_bd/bd_cave.c @@ -548,11 +548,11 @@ void gd_cave_free(GdCave *cave) gd_cave_map_free(cave->hammered_reappear); // free objects - list_foreach(cave->objects, (list_fn) free, NULL); + list_foreach_fn_1(cave->objects, (list_fn_1) free); list_free(cave->objects); // free replays - list_foreach(cave->replays, (list_fn) gd_replay_free, NULL); + list_foreach_fn_1(cave->replays, (list_fn_1) gd_replay_free); list_free(cave->replays); // freeing main pointer diff --git a/src/game_bd/bd_caveobject.c b/src/game_bd/bd_caveobject.c index 19f3b91c..41fe4f0a 100644 --- a/src/game_bd/bd_caveobject.c +++ b/src/game_bd/bd_caveobject.c @@ -1668,6 +1668,6 @@ void gd_flatten_cave(GdCave *cave, const int level) gd_cave_free(rendered); // forget objects - list_foreach(cave->objects, (list_fn) free, NULL); + list_foreach_fn_1(cave->objects, (list_fn_1) free); cave->objects = NULL; } diff --git a/src/game_bd/bd_caveset.c b/src/game_bd/bd_caveset.c index 8ebbe269..75325bc9 100644 --- a/src/game_bd/bd_caveset.c +++ b/src/game_bd/bd_caveset.c @@ -170,7 +170,7 @@ void gd_caveset_clear(void) { if (gd_caveset) { - list_foreach(gd_caveset, (list_fn) gd_cave_free, NULL); + list_foreach_fn_1(gd_caveset, (list_fn_1) gd_cave_free); list_free(gd_caveset); gd_caveset = NULL; } diff --git a/src/libgame/list.c b/src/libgame/list.c index 86e7c29a..781a6150 100644 --- a/src/libgame/list.c +++ b/src/libgame/list.c @@ -744,7 +744,35 @@ list_length (List *list) } /** - * list_foreach: + * list_foreach_fn_1: + * @list: a #List, this must point to the top of the list + * @func: (scope call): the function to call with each element's data + * + * Calls a function for each element of a #List. + * + * It is safe for @func to remove the element from @list, but it must + * not modify any part of the list after that element. + */ +/** + * list_fn_1: + * @data: the element's data + * + * Specifies the type of functions passed to list_foreach_fn_1(). + */ +void +list_foreach_fn_1 (List *list, list_fn_1 func) +{ + while (list) + { + List *next = list->next; + + (*func) (list->data); + list = next; + } +} + +/** + * list_foreach_fn_2: * @list: a #List, this must point to the top of the list * @func: (scope call): the function to call with each element's data * @user_data: user data to pass to the function @@ -755,15 +783,14 @@ list_length (List *list) * not modify any part of the list after that element. */ /** - * list_fn: + * list_fn_2: * @data: the element's data - * @user_data: user data passed to list_foreach() or slist_foreach() + * @user_data: user data passed to list_foreach_fn_2() * - * Specifies the type of functions passed to list_foreach() and - * slist_foreach(). + * Specifies the type of functions passed to list_foreach_fn_2(). */ void -list_foreach (List *list, list_fn func, void *user_data) +list_foreach_fn_2 (List *list, list_fn_2 func, void *user_data) { while (list) { diff --git a/src/libgame/list.h b/src/libgame/list.h index 2ad694bd..f5af6702 100644 --- a/src/libgame/list.h +++ b/src/libgame/list.h @@ -44,7 +44,8 @@ struct _List }; -typedef void (*list_fn) (void *data, void *userdata); +typedef void (*list_fn_1) (void *data); +typedef void (*list_fn_2) (void *data, void *userdata); typedef void *(*list_copy_fn) (const void *data, void *userdata); /* Doubly linked lists */ @@ -69,7 +70,8 @@ int list_index(List *list, const void *data); List *list_last(List *list); List *list_first(List *list); unsigned int list_length(List *list); -void list_foreach(List *list, list_fn func, void *user_data); +void list_foreach_fn_1(List *list, list_fn_1 func); +void list_foreach_fn_2(List *list, list_fn_2 func, void *user_data); void *list_nth_data(List *list, unsigned int n); #define list_previous(list) ((list) ? (((List *)(list))->prev) : NULL) -- 2.34.1