From f6ddc195395a695984a4a291773ddcedebbe50a7 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Thu, 13 Oct 2022 17:53:47 -0700 Subject: [PATCH] Don't free chunks on flecs_sparse_clear --- flecs.c | 25 +++++++++++++++++++------ src/datastructures/sparse.c | 25 +++++++++++++++++++------ 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/flecs.c b/flecs.c index fcf12bf258..842325d14a 100644 --- a/flecs.c +++ b/flecs.c @@ -11404,14 +11404,17 @@ void flecs_sparse_clear( { ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_vector_each(sparse->chunks, chunk_t, chunk, { - flecs_sparse_chunk_free(sparse, chunk); - }); + int32_t i, count = ecs_vector_count(sparse->chunks); + chunk_t *chunks = ecs_vector_first(sparse->chunks, chunk_t); + for (i = 0; i < count; i ++) { + int32_t *indices = chunks[i].sparse; + if (indices) { + ecs_os_memset_n(indices, 0, int32_t, FLECS_SPARSE_CHUNK_SIZE); + } + } - ecs_vector_free(sparse->chunks); ecs_vector_set_count(&sparse->dense, uint64_t, 1); - sparse->chunks = NULL; sparse->count = 1; sparse->max_id_local = 0; } @@ -11420,8 +11423,18 @@ void _flecs_sparse_fini( ecs_sparse_t *sparse) { ecs_assert(sparse != NULL, ECS_INTERNAL_ERROR, NULL); - flecs_sparse_clear(sparse); + + int32_t i, count = ecs_vector_count(sparse->chunks); + chunk_t *chunks = ecs_vector_first(sparse->chunks, chunk_t); + for (i = 0; i < count; i ++) { + flecs_sparse_chunk_free(sparse, &chunks[i]); + } + + ecs_vector_free(sparse->chunks); ecs_vector_free(sparse->dense); + + sparse->chunks = NULL; + sparse->dense = NULL; } void flecs_sparse_free( diff --git a/src/datastructures/sparse.c b/src/datastructures/sparse.c index 9b631a2902..77dbc42f6c 100644 --- a/src/datastructures/sparse.c +++ b/src/datastructures/sparse.c @@ -363,14 +363,17 @@ void flecs_sparse_clear( { ecs_assert(sparse != NULL, ECS_INVALID_PARAMETER, NULL); - ecs_vector_each(sparse->chunks, chunk_t, chunk, { - flecs_sparse_chunk_free(sparse, chunk); - }); + int32_t i, count = ecs_vector_count(sparse->chunks); + chunk_t *chunks = ecs_vector_first(sparse->chunks, chunk_t); + for (i = 0; i < count; i ++) { + int32_t *indices = chunks[i].sparse; + if (indices) { + ecs_os_memset_n(indices, 0, int32_t, FLECS_SPARSE_CHUNK_SIZE); + } + } - ecs_vector_free(sparse->chunks); ecs_vector_set_count(&sparse->dense, uint64_t, 1); - sparse->chunks = NULL; sparse->count = 1; sparse->max_id_local = 0; } @@ -379,8 +382,18 @@ void _flecs_sparse_fini( ecs_sparse_t *sparse) { ecs_assert(sparse != NULL, ECS_INTERNAL_ERROR, NULL); - flecs_sparse_clear(sparse); + + int32_t i, count = ecs_vector_count(sparse->chunks); + chunk_t *chunks = ecs_vector_first(sparse->chunks, chunk_t); + for (i = 0; i < count; i ++) { + flecs_sparse_chunk_free(sparse, &chunks[i]); + } + + ecs_vector_free(sparse->chunks); ecs_vector_free(sparse->dense); + + sparse->chunks = NULL; + sparse->dense = NULL; } void flecs_sparse_free(