[ecs] Handle indices beyond the subpools

It turns out that the bsearch bug was hiding incorrect handling of
indices in the subpool beyond the last tracked subpool. In which case, a
correctly working bsearch correctly fails to find the range, but the
search can be skipped entirely.
This commit is contained in:
Bill Currie 2023-03-05 16:10:09 +09:00
parent 76ac446156
commit 28226ac75c

View file

@ -89,6 +89,7 @@ range_cmp (const void *_key, const void *_range, void *_subpool)
const uint32_t *range = _range; const uint32_t *range = _range;
ecs_subpool_t *subpool = _subpool; ecs_subpool_t *subpool = _subpool;
// range is the first index for the next subpool range
if (*key >= *range) { if (*key >= *range) {
return 1; return 1;
} }
@ -117,8 +118,9 @@ Ent_RemoveComponent (uint32_t ent, uint32_t comp, ecs_registry_t *registry)
if (ind < pool->count && pool->dense[ind] == ent) { if (ind < pool->count && pool->dense[ind] == ent) {
uint32_t last = pool->count - 1; uint32_t last = pool->count - 1;
Component_DestroyElements (c, pool->data, ind, 1); Component_DestroyElements (c, pool->data, ind, 1);
if (subpool->num_ranges - subpool->available) { uint32_t range_count = subpool->num_ranges - subpool->available;
uint32_t range_count = subpool->num_ranges - subpool->available; // if ind >= the last range, then it is outside the subpools
if (range_count && ind < subpool->ranges[range_count - 1]) {
uint32_t *range = find_range (subpool, ind); uint32_t *range = find_range (subpool, ind);
while (range - subpool->ranges < range_count) { while (range - subpool->ranges < range_count) {
uint32_t end = --*range; uint32_t end = --*range;