Create type registry

It is useful  to have all types  registered before they  are allocated
because the memory needed for them can then be allocated before any of
it is  required, speeding up allocations because  a pool may have been
created beforehand.
This commit is contained in:
Eric-Paul Ickhorn 2024-09-14 16:37:45 +02:00
parent a8c941f041
commit 86479af2fb
2 changed files with 74 additions and 2 deletions

View File

@ -9,6 +9,8 @@
#include <voxula/internals/ecs/tag.h>
typedef struct vecs vecs_s;
typedef struct vecs_type_resolver vecs_type_resolver_s;
typedef struct vecs_type vecs_type_s;
typedef struct vecs_shadow vecs_shadow_s;
struct vecs_shadow
@ -18,16 +20,35 @@ struct vecs_shadow
void *first_tag;
};
struct vecs_type
{
uint32_t size;
uint16_t len_name;
char *name;
};
struct vecs_type_resolver
{
uint16_t types_capacity;
uint16_t num_types;
vecs_type_s *types;
};
struct vecs
{
vx_pool_s *tag_pool;
vx_pool_s *shadow_pool;
vecs_tag_name_resolver_s resolver;
vecs_tag_name_resolver_s resolver; // todo: Rename this
vecs_type_resolver_s type_resolver;
vecs_entity_map_s lookup_map;
vx_uuid_table_s *uuid_table;
vx_arena_s *static_arena;
};
vecs_s * vecs_new(vx_uuid_table_s *uuid_table);
void vecs_free(vecs_s *ecs);
vecs_type_s * vecs_resolve_type(vecs_s *ecs, const char *name);
void vecs_remark(vecs_s *ecs, char *type_name, uint32_t size);
#endif // VECS_H

View File

@ -1,7 +1,9 @@
#include <voxula/internals/ecs/lookup.h>
#include <voxula/internals/ecs/tag.h>
#include <voxula/internals/ecs/head.h>
#include <stdlib.h>
#include <string.h>
vecs_s * vecs_new(vx_uuid_table_s *uuid_table)
{
@ -12,9 +14,14 @@ vecs_s * vecs_new(vx_uuid_table_s *uuid_table)
vecs_s *ecs = malloc(sizeof(vecs_s));
ecs->resolver = vecs_create_tag_name_resolver(uuid_table);
ecs->lookup_map = vecs_create_lookup_map(uuid_table);
ecs->uuid_table = uuid_table;
ecs->shadow_pool = vx_new_pool(sizeof(vecs_shadow_s), 8192);
ecs->tag_pool = vx_new_pool(sizeof(vecs_tag_s), 32768);
ecs->uuid_table = uuid_table;
ecs->type_resolver.num_types = 0;
ecs->type_resolver.types_capacity = 256;
ecs->type_resolver.types = malloc(ecs->type_resolver.types_capacity * sizeof(vecs_type_s));
return ecs;
}
@ -24,3 +31,47 @@ void vecs_free(vecs_s *ecs)
vx_free_pool(ecs->shadow_pool);
vx_free_pool(ecs->tag_pool);
}
vecs_type_s * vecs_resolve_type(
vecs_s *ecs,
const char *name
) {
uint16_t len_name = strlen(name);
uint32_t type_index = 0;
while(type_index < ecs->type_resolver.num_types)
{
vecs_type_s *type = &ecs->type_resolver.types[type_index];
if(type->len_name != len_name)
{
++type_index;
continue;
}
if( ! memcmp(type->name, name, type->len_name))
{
return type;
}
++type_index;
}
return NULL;
}
void vecs_remark(
vecs_s *ecs,
char *type_name,
uint32_t size
) {
if(vecs_resolve_type(ecs, type_name))
{
return;
}
if(ecs->type_resolver.num_types >= ecs->type_resolver.types_capacity)
{
ecs->type_resolver.types_capacity *= 2;
ecs->type_resolver.types = realloc(ecs->type_resolver.types, ecs->type_resolver.types_capacity * sizeof(vecs_type_s));
}
vecs_type_s *type = &ecs->type_resolver.types[ecs->type_resolver.num_types++];
type->size = size;
type->len_name = strlen(type_name);
type->name = vx_arena_dupe_string(ecs->static_arena, type_name);
}