Skip to content

Commit

Permalink
Fix leaks when using FLECS_USE_OS_ALLOC
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 11, 2024
1 parent f86e608 commit 3f9c4bf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 12 deletions.
17 changes: 12 additions & 5 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -20662,8 +20662,13 @@ ecs_entities_t ecs_get_entities(
ecs_flags32_t ecs_world_get_flags(
const ecs_world_t *world)
{
flecs_poly_assert(world, ecs_world_t);
return world->flags;
if (flecs_poly_is(world, ecs_world_t)) {
return world->flags;
} else {
flecs_poly_assert(world, ecs_stage_t);
const ecs_stage_t *stage = (const ecs_stage_t*)world;
return stage->world->flags;
}
}

/**
Expand Down Expand Up @@ -29984,7 +29989,7 @@ void flecs_hashmap_fini(
ecs_hm_bucket_t *bucket = ecs_map_ptr(&it);
ecs_vec_fini(a, &bucket->keys, map->key_size);
ecs_vec_fini(a, &bucket->values, map->value_size);
#ifdef FLECS_SANITIZE
#if defined(FLECS_SANITIZE) || defined(FLECS_USE_OS_ALLOC)
flecs_bfree(&map->bucket_allocator, bucket);
#endif
}
Expand Down Expand Up @@ -30439,7 +30444,7 @@ void ecs_map_fini(
}

bool sanitize = false;
#ifdef FLECS_SANITIZE
#if defined(FLECS_SANITIZE) || defined(FLECS_USE_OS_ALLOC)
sanitize = true;
#endif

Expand Down Expand Up @@ -71024,7 +71029,9 @@ ecs_iter_t ecs_query_iter(
if (cache) {
/* If monitors changed, do query rematching */
ecs_flags32_t flags = q->flags;
if (!(world->flags & EcsWorldReadonly) && flags & EcsQueryHasRefs) {
if (!(ecs_world_get_flags(world) & EcsWorldReadonly) &&
(flags & EcsQueryHasRefs))
{
flecs_eval_component_monitors(q->world);
}
}
Expand Down
2 changes: 1 addition & 1 deletion distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@
* When enabled, Flecs will use the OS allocator provided in the OS API directly
* instead of the builtin block allocator. This can decrease memory utilization
* as memory will be freed more often, at the cost of decreased performance. */
#define FLECS_USE_OS_ALLOC
// #define FLECS_USE_OS_ALLOC

/** @def FLECS_ID_DESC_MAX
* Maximum number of ids to add ecs_entity_desc_t / ecs_bulk_desc_t */
Expand Down
2 changes: 1 addition & 1 deletion include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@
* When enabled, Flecs will use the OS allocator provided in the OS API directly
* instead of the builtin block allocator. This can decrease memory utilization
* as memory will be freed more often, at the cost of decreased performance. */
#define FLECS_USE_OS_ALLOC
// #define FLECS_USE_OS_ALLOC

/** @def FLECS_ID_DESC_MAX
* Maximum number of ids to add ecs_entity_desc_t / ecs_bulk_desc_t */
Expand Down
2 changes: 1 addition & 1 deletion src/datastructures/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void flecs_hashmap_fini(
ecs_hm_bucket_t *bucket = ecs_map_ptr(&it);
ecs_vec_fini(a, &bucket->keys, map->key_size);
ecs_vec_fini(a, &bucket->values, map->value_size);
#ifdef FLECS_SANITIZE
#if defined(FLECS_SANITIZE) || defined(FLECS_USE_OS_ALLOC)
flecs_bfree(&map->bucket_allocator, bucket);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/datastructures/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void ecs_map_fini(
}

bool sanitize = false;
#ifdef FLECS_SANITIZE
#if defined(FLECS_SANITIZE) || defined(FLECS_USE_OS_ALLOC)
sanitize = true;
#endif

Expand Down
4 changes: 3 additions & 1 deletion src/query/engine/eval_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ ecs_iter_t ecs_query_iter(
if (cache) {
/* If monitors changed, do query rematching */
ecs_flags32_t flags = q->flags;
if (!(world->flags & EcsWorldReadonly) && flags & EcsQueryHasRefs) {
if (!(ecs_world_get_flags(world) & EcsWorldReadonly) &&
(flags & EcsQueryHasRefs))
{
flecs_eval_component_monitors(q->world);
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -2400,6 +2400,11 @@ ecs_entities_t ecs_get_entities(
ecs_flags32_t ecs_world_get_flags(
const ecs_world_t *world)
{
flecs_poly_assert(world, ecs_world_t);
return world->flags;
if (flecs_poly_is(world, ecs_world_t)) {
return world->flags;
} else {
flecs_poly_assert(world, ecs_stage_t);
const ecs_stage_t *stage = (const ecs_stage_t*)world;
return stage->world->flags;
}
}

0 comments on commit 3f9c4bf

Please sign in to comment.