From b4846569bb8ab1cfaee0a39fd78dada8357e7b4a Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Tue, 30 Jan 2024 22:31:31 +0100 Subject: [PATCH] Added entity creation and a test for it; fixed memory leaks. --- action.bash | 4 ++-- build_tests.bash | 10 +++++++-- core/build-config/tests.txt | 3 ++- core/exports/maintree/maintree.h | 1 + core/inc-c/entity.h | 5 ++++- core/inc-c/state.h | 3 ++- core/src-c/entity_lookup.c | 12 ++++++----- core/src-c/entity_registry.c | 21 ++++++++++++------- core/src-c/state.c | 19 ++++++++++++++++- core/tests/interface/entity-creation/main.c | 9 ++++++++ .../include_paths.txt | 3 --- 11 files changed, 67 insertions(+), 23 deletions(-) create mode 100644 core/tests/interface/entity-creation/main.c delete mode 100644 core/tests/internal/basic-tag-registry-usage/include_paths.txt diff --git a/action.bash b/action.bash index 3aa7458..73071fa 100755 --- a/action.bash +++ b/action.bash @@ -5,10 +5,10 @@ REPOSITORY_FOLDER=$(pwd) PROJECT_NAME="maintree" -DEBUG_CC_OPTIONS="-g2 -Wall -Wextra -Wpedantic" +DEBUG_CC_OPTIONS="-g3 -Wall -Wextra -Wpedantic" RELEASE_CC_OPTIONS="-O3 -Wall" -MAIN_OBJECTS_FOLDER="$REPOSITORY_FOLDER/.build/objects/" +MAIN_OBJECTS_FOLDER="$REPOSITORY_FOLDER/.build/objects" CONFIG_FILE_INCLUDE_PATHS="build-config/include_paths.txt" function clone_dependencies { diff --git a/build_tests.bash b/build_tests.bash index adce3a6..4cebf35 100755 --- a/build_tests.bash +++ b/build_tests.bash @@ -4,6 +4,12 @@ cd $(dirname "$(pwd)/$0") REPOSITORY_FOLDER=$(pwd) MAIN_OBJECTS_FOLDER="$REPOSITORY_FOLDER/.build/objects/" +DEFAULT_INCLUDE_PATHS=" +-I .build/depends/libRR/Core/core/exports/ +-I .build/depends/libRR/Core/platform/exports/ +-I core/exports/ +-I core/inc-c/ +" DEFAULT_LINKAGE_PATHS=" $REPOSITORY_FOLDER/.build/maintree-core.a $REPOSITORY_FOLDER/.build/librr-core.a @@ -14,7 +20,7 @@ function get_include_path_configuration() { TEST_PATH=$1 INCLUDE_CONFIG_PATH="$TEST_PATH/include_paths.txt" - INCLUDE_STATEMENTS="-I $TEST_PATH/inc-c/" + INCLUDE_STATEMENTS="$DEFAULT_INCLUDE_PATHS -I $TEST_PATH/inc-c/" if [[ ! -f $INCLUDE_CONFIG_PATH ]] then return @@ -49,7 +55,7 @@ function compile_single_test() { get_include_path_configuration $TEST_PATH get_linkage_path_configuration $TEST_PATH - gcc -o $TEST_PATH/$TEST_NAME.elf $TEST_PATH/*.c $LINKAGE_PATHS $INCLUDE_STATEMENTS + gcc -g3 -o $TEST_PATH/$TEST_NAME.elf $TEST_PATH/*.c $LINKAGE_PATHS $INCLUDE_STATEMENTS } function compile_all_tests_of_module() { diff --git a/core/build-config/tests.txt b/core/build-config/tests.txt index f0a7585..b8284b4 100644 --- a/core/build-config/tests.txt +++ b/core/build-config/tests.txt @@ -1 +1,2 @@ -tests/internal/basic-tag-registry-usage \ No newline at end of file +tests/internal/basic-tag-registry-usage +tests/interface/entity-creation \ No newline at end of file diff --git a/core/exports/maintree/maintree.h b/core/exports/maintree/maintree.h index cdf6684..f136ccf 100644 --- a/core/exports/maintree/maintree.h +++ b/core/exports/maintree/maintree.h @@ -32,6 +32,7 @@ void mt_cleanup(MtState state); void mt_start(MtState state); MtEntity mt_summon(MtState state); +void mt_drop(MtEntity entity); void mt_add_create_effector(MtState state, char *effector_name, MtEffectorFn function, void *userdata); void mt_add_delete_effector(MtState state, char *effector_name, MtEffectorFn function, void *userdata); diff --git a/core/inc-c/entity.h b/core/inc-c/entity.h index 618568d..dca572d 100644 --- a/core/inc-c/entity.h +++ b/core/inc-c/entity.h @@ -46,6 +46,7 @@ struct MtEntityRegistry { MtEntityPool pool; MtEntityLookupTree lookup; + u32_t entity_id_counter; }; struct MtEntity @@ -100,10 +101,12 @@ rr_vec4f_s mt_get_vec4_tag(MtEntity entity, char *name); +void mt_delete_entity_data(MtEntity entity); + MtEntityPool mt_create_entity_pool(usz_t capacity); void mt_delete_entity_pool(MtEntityPool *pool); MtEntity mt_get_entity_pool_slot(MtEntityPool *pool); - +void mt_give_back_entity_pool_slot(MtEntityPool *pool, MtEntity entity); #endif // MT_ENTITY_POOL_H diff --git a/core/inc-c/state.h b/core/inc-c/state.h index 13296e1..5864149 100644 --- a/core/inc-c/state.h +++ b/core/inc-c/state.h @@ -15,7 +15,7 @@ struct MtState char *app_name; MtTagRegistry tag_registry; - MtEntityRegistry entity_list; + MtEntityRegistry entity_registry; }; MtState mt_initialize(char *app_name); @@ -23,5 +23,6 @@ void mt_cleanup(MtState state); void mt_start(MtState state); MtEntity mt_summon(MtState state); +void mt_delete(MtEntity entity); #endif // MT_STATE_H diff --git a/core/src-c/entity_lookup.c b/core/src-c/entity_lookup.c index b5119b7..d93350f 100644 --- a/core/src-c/entity_lookup.c +++ b/core/src-c/entity_lookup.c @@ -5,11 +5,11 @@ MtEntity mt_create_entity(void *state_voidptr, u32_t identifier) { MtState state = state_voidptr; - MtEntityListL1 *level1 = state->entity_list.lookup.sublists[identifier >> 24]; + MtEntityListL1 *level1 = state->entity_registry.lookup.sublists[identifier >> 24]; if(level1 == NULL) { - state->entity_list.lookup.sublists[identifier >> 24] = calloc(sizeof(MtEntityListL1), 1); - level1 = state->entity_list.lookup.sublists[identifier >> 24]; + state->entity_registry.lookup.sublists[identifier >> 24] = calloc(sizeof(MtEntityListL1), 1); + level1 = state->entity_registry.lookup.sublists[identifier >> 24]; } MtEntityListL2 *level2 = level1->sublists[(identifier >> 16) & 0xff]; @@ -29,7 +29,9 @@ MtEntity mt_create_entity(void *state_voidptr, u32_t identifier) MtEntity entity = level3->entities[identifier & 0xff]; if(entity == NULL) { - entity = mt_get_entity_pool_slot(&state->entity_list.pool); + entity = mt_get_entity_pool_slot(&state->entity_registry.pool); + entity->parent_state = state; + level3->entities[identifier & 0xff] = entity; } return entity; } @@ -37,7 +39,7 @@ MtEntity mt_create_entity(void *state_voidptr, u32_t identifier) MtEntity mt_get_entity(void *state_voidptr, u32_t identifier) { MtState state = state_voidptr; - MtEntityListL1 *level1 = state->entity_list.lookup.sublists[identifier >> 24]; + MtEntityListL1 *level1 = state->entity_registry.lookup.sublists[identifier >> 24]; if(level1 == NULL) return NULL; diff --git a/core/src-c/entity_registry.c b/core/src-c/entity_registry.c index 9518f78..54d7570 100644 --- a/core/src-c/entity_registry.c +++ b/core/src-c/entity_registry.c @@ -2,8 +2,6 @@ #include #include -void mt_delete_entity(MtEntity entity); - MtEntityLookupTree mt_create_entity_lookup_tree(); void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree); @@ -13,11 +11,14 @@ MtEntityRegistry mt_create_entity_registry() { MtEntityRegistry entity_registry; entity_registry.lookup = mt_create_entity_lookup_tree(); + entity_registry.pool = mt_create_entity_pool(16384); + entity_registry.entity_id_counter = 1; return entity_registry; } void mt_delete_entity_registry(MtEntityRegistry *entity_registry) { + mt_delete_entity_lookup_tree(entity_registry->lookup); mt_delete_entity_pool(&entity_registry->pool); } @@ -26,7 +27,7 @@ void mt_delete_entity_registry(MtEntityRegistry *entity_registry) MtEntityLookupTree mt_create_entity_lookup_tree() { MtEntityLookupTree lookup_tree; - rr_memset(&lookup_tree.sublists[0], sizeof(MtEntityListL1 *) * 256, 0x00); + rr_memset(&lookup_tree, sizeof(MtEntityLookupTree), 0x00); return lookup_tree; } @@ -51,7 +52,7 @@ void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree) continue; } usz_t level3_index = 0; - while(level3_index) + while(level3_index < 256) { MtEntityListL3 *level3_list = level2_list->sublists[level3_index]; if(level3_list == NULL) @@ -60,7 +61,7 @@ void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree) continue; } usz_t entity_index = 0; - while(entity_index) + while(entity_index < 256) { MtEntity entity = level3_list->entities[entity_index]; if(entity == NULL) @@ -68,7 +69,7 @@ void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree) ++entity_index; continue; } - mt_delete_entity(entity); + mt_delete_entity_data(entity); ++entity_index; } free(level3_list); @@ -84,7 +85,7 @@ void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree) -void mt_delete_entity(MtEntity entity) +void mt_delete_entity_data(MtEntity entity) { if(entity->tags != NULL) free(entity->tags); @@ -141,3 +142,9 @@ MtEntity mt_get_entity_pool_slot(MtEntityPool *pool) pool->first_free = allocated->pool_next_free; return allocated; } + +void mt_give_back_entity_pool_slot(MtEntityPool *pool, MtEntity entity) +{ + entity->pool_next_free = pool->first_free; + pool->first_free = entity; +} diff --git a/core/src-c/state.c b/core/src-c/state.c index c368253..9e714af 100644 --- a/core/src-c/state.c +++ b/core/src-c/state.c @@ -11,13 +11,17 @@ MtState mt_initialize(char *app_name) state->app_name = malloc(len_app_name + 1); rr_memcopy(state->app_name, app_name, len_app_name + 1); state->tag_registry = mt_create_tag_registry(state); + state->entity_registry = mt_create_entity_registry(); return state; } void mt_cleanup(MtState state) { - mt_delete_entity_registry(&state->entity_list); + mt_delete_entity_registry(&state->entity_registry); + mt_delete_tag_registry(state->tag_registry); + if(state->app_name != NULL) + free(state->app_name); free(state); } @@ -27,3 +31,16 @@ void mt_start(MtState state) } */ + +MtEntity mt_summon(MtState state) +{ + return mt_create_entity(state, state->entity_registry.entity_id_counter++); +} + +void mt_drop(MtEntity entity) +{ + MtState state = entity->parent_state; + + mt_delete_entity_data(entity); + mt_give_back_entity_pool_slot(&state->entity_registry.pool, state->entity_registry.pool.first_free); +} diff --git a/core/tests/interface/entity-creation/main.c b/core/tests/interface/entity-creation/main.c new file mode 100644 index 0000000..4ad1c9a --- /dev/null +++ b/core/tests/interface/entity-creation/main.c @@ -0,0 +1,9 @@ +#include + +int main(int argc, char **argv) +{ + MtState state = mt_initialize("Interface Test");; + MtEntity entity = mt_summon(state); + mt_drop(entity); + mt_cleanup(state); +} diff --git a/core/tests/internal/basic-tag-registry-usage/include_paths.txt b/core/tests/internal/basic-tag-registry-usage/include_paths.txt deleted file mode 100644 index 615bb49..0000000 --- a/core/tests/internal/basic-tag-registry-usage/include_paths.txt +++ /dev/null @@ -1,3 +0,0 @@ -.build/depends/libRR/Core/core/exports/ -.build/depends/libRR/Core/platform/exports/ -core/inc-c/ \ No newline at end of file