Added entity creation and a test for it; fixed memory leaks.

This commit is contained in:
Eric-Paul Ickhorn 2024-01-30 22:31:31 +01:00
parent 6979728a96
commit b4846569bb
Signed by: epickh
GPG Key ID: F5EBBE013924D95F
11 changed files with 67 additions and 23 deletions

View File

@ -5,10 +5,10 @@ REPOSITORY_FOLDER=$(pwd)
PROJECT_NAME="maintree" PROJECT_NAME="maintree"
DEBUG_CC_OPTIONS="-g2 -Wall -Wextra -Wpedantic" DEBUG_CC_OPTIONS="-g3 -Wall -Wextra -Wpedantic"
RELEASE_CC_OPTIONS="-O3 -Wall" 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" CONFIG_FILE_INCLUDE_PATHS="build-config/include_paths.txt"
function clone_dependencies { function clone_dependencies {

View File

@ -4,6 +4,12 @@ cd $(dirname "$(pwd)/$0")
REPOSITORY_FOLDER=$(pwd) REPOSITORY_FOLDER=$(pwd)
MAIN_OBJECTS_FOLDER="$REPOSITORY_FOLDER/.build/objects/" 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=" DEFAULT_LINKAGE_PATHS="
$REPOSITORY_FOLDER/.build/maintree-core.a $REPOSITORY_FOLDER/.build/maintree-core.a
$REPOSITORY_FOLDER/.build/librr-core.a $REPOSITORY_FOLDER/.build/librr-core.a
@ -14,7 +20,7 @@ function get_include_path_configuration() {
TEST_PATH=$1 TEST_PATH=$1
INCLUDE_CONFIG_PATH="$TEST_PATH/include_paths.txt" 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 ]] if [[ ! -f $INCLUDE_CONFIG_PATH ]]
then then
return return
@ -49,7 +55,7 @@ function compile_single_test() {
get_include_path_configuration $TEST_PATH get_include_path_configuration $TEST_PATH
get_linkage_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() { function compile_all_tests_of_module() {

View File

@ -1 +1,2 @@
tests/internal/basic-tag-registry-usage tests/internal/basic-tag-registry-usage
tests/interface/entity-creation

View File

@ -32,6 +32,7 @@ void mt_cleanup(MtState state);
void mt_start(MtState state); void mt_start(MtState state);
MtEntity mt_summon(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_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); void mt_add_delete_effector(MtState state, char *effector_name, MtEffectorFn function, void *userdata);

View File

@ -46,6 +46,7 @@ struct MtEntityRegistry
{ {
MtEntityPool pool; MtEntityPool pool;
MtEntityLookupTree lookup; MtEntityLookupTree lookup;
u32_t entity_id_counter;
}; };
struct MtEntity 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); MtEntityPool mt_create_entity_pool(usz_t capacity);
void mt_delete_entity_pool(MtEntityPool *pool); void mt_delete_entity_pool(MtEntityPool *pool);
MtEntity mt_get_entity_pool_slot(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 #endif // MT_ENTITY_POOL_H

View File

@ -15,7 +15,7 @@ struct MtState
char *app_name; char *app_name;
MtTagRegistry tag_registry; MtTagRegistry tag_registry;
MtEntityRegistry entity_list; MtEntityRegistry entity_registry;
}; };
MtState mt_initialize(char *app_name); MtState mt_initialize(char *app_name);
@ -23,5 +23,6 @@ void mt_cleanup(MtState state);
void mt_start(MtState state); void mt_start(MtState state);
MtEntity mt_summon(MtState state); MtEntity mt_summon(MtState state);
void mt_delete(MtEntity entity);
#endif // MT_STATE_H #endif // MT_STATE_H

View File

@ -5,11 +5,11 @@
MtEntity mt_create_entity(void *state_voidptr, u32_t identifier) MtEntity mt_create_entity(void *state_voidptr, u32_t identifier)
{ {
MtState state = state_voidptr; 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) if(level1 == NULL)
{ {
state->entity_list.lookup.sublists[identifier >> 24] = calloc(sizeof(MtEntityListL1), 1); state->entity_registry.lookup.sublists[identifier >> 24] = calloc(sizeof(MtEntityListL1), 1);
level1 = state->entity_list.lookup.sublists[identifier >> 24]; level1 = state->entity_registry.lookup.sublists[identifier >> 24];
} }
MtEntityListL2 *level2 = level1->sublists[(identifier >> 16) & 0xff]; 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]; MtEntity entity = level3->entities[identifier & 0xff];
if(entity == NULL) 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; 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) MtEntity mt_get_entity(void *state_voidptr, u32_t identifier)
{ {
MtState state = state_voidptr; 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) if(level1 == NULL)
return NULL; return NULL;

View File

@ -2,8 +2,6 @@
#include <librr/memory.h> #include <librr/memory.h>
#include <stdlib.h> #include <stdlib.h>
void mt_delete_entity(MtEntity entity);
MtEntityLookupTree mt_create_entity_lookup_tree(); MtEntityLookupTree mt_create_entity_lookup_tree();
void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree); void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree);
@ -13,11 +11,14 @@ MtEntityRegistry mt_create_entity_registry()
{ {
MtEntityRegistry entity_registry; MtEntityRegistry entity_registry;
entity_registry.lookup = mt_create_entity_lookup_tree(); 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; return entity_registry;
} }
void mt_delete_entity_registry(MtEntityRegistry *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); 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 mt_create_entity_lookup_tree()
{ {
MtEntityLookupTree 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; return lookup_tree;
} }
@ -51,7 +52,7 @@ void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree)
continue; continue;
} }
usz_t level3_index = 0; usz_t level3_index = 0;
while(level3_index) while(level3_index < 256)
{ {
MtEntityListL3 *level3_list = level2_list->sublists[level3_index]; MtEntityListL3 *level3_list = level2_list->sublists[level3_index];
if(level3_list == NULL) if(level3_list == NULL)
@ -60,7 +61,7 @@ void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree)
continue; continue;
} }
usz_t entity_index = 0; usz_t entity_index = 0;
while(entity_index) while(entity_index < 256)
{ {
MtEntity entity = level3_list->entities[entity_index]; MtEntity entity = level3_list->entities[entity_index];
if(entity == NULL) if(entity == NULL)
@ -68,7 +69,7 @@ void mt_delete_entity_lookup_tree(MtEntityLookupTree lookup_tree)
++entity_index; ++entity_index;
continue; continue;
} }
mt_delete_entity(entity); mt_delete_entity_data(entity);
++entity_index; ++entity_index;
} }
free(level3_list); 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) if(entity->tags != NULL)
free(entity->tags); free(entity->tags);
@ -141,3 +142,9 @@ MtEntity mt_get_entity_pool_slot(MtEntityPool *pool)
pool->first_free = allocated->pool_next_free; pool->first_free = allocated->pool_next_free;
return allocated; return allocated;
} }
void mt_give_back_entity_pool_slot(MtEntityPool *pool, MtEntity entity)
{
entity->pool_next_free = pool->first_free;
pool->first_free = entity;
}

View File

@ -11,13 +11,17 @@ MtState mt_initialize(char *app_name)
state->app_name = malloc(len_app_name + 1); state->app_name = malloc(len_app_name + 1);
rr_memcopy(state->app_name, app_name, len_app_name + 1); rr_memcopy(state->app_name, app_name, len_app_name + 1);
state->tag_registry = mt_create_tag_registry(state); state->tag_registry = mt_create_tag_registry(state);
state->entity_registry = mt_create_entity_registry();
return state; return state;
} }
void mt_cleanup(MtState 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); 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);
}

View File

@ -0,0 +1,9 @@
#include <maintree/maintree.h>
int main(int argc, char **argv)
{
MtState state = mt_initialize("Interface Test");;
MtEntity entity = mt_summon(state);
mt_drop(entity);
mt_cleanup(state);
}

View File

@ -1,3 +0,0 @@
.build/depends/libRR/Core/core/exports/
.build/depends/libRR/Core/platform/exports/
core/inc-c/