Minor guf_id_pool changes

This commit is contained in:
jun 2025-04-09 06:27:35 +02:00
parent f3e184da73
commit 0910ee4bd8

View File

@ -140,20 +140,20 @@ static bool GUF_CAT(GUF_ID_POOL_NAME, _is_valid)(const GUF_ID_POOL_NAME *pool)
GUF_ID_POOL_NAME *GUF_CAT(GUF_ID_POOL_NAME, _try_init)(GUF_ID_POOL_NAME *pool, ptrdiff_t initial_cap, guf_allocator *allocator, guf_err *err)
{
if (!pool) {
guf_err_set_or_panic(err, GUF_ERR_NULL_PTR, "in guf_idpool_try_init: pool is NULL");
guf_err_set_or_panic(err, GUF_ERR_NULL_PTR, GUF_ERR_MSG("in guf_idpool_try_init: pool is NULL"));
return NULL;
} else if (!allocator || !allocator->alloc || !allocator->realloc || !allocator->free) {
guf_err_set_or_panic(err, GUF_ERR_NULL_PTR, "in guf_idpool_try_init: allocator is NULL");
guf_err_set_or_panic(err, GUF_ERR_NULL_PTR, GUF_ERR_MSG("in guf_idpool_try_init: allocator is NULL"));
return NULL;
} else if (initial_cap < 0) {
guf_err_set_or_panic(err, GUF_ERR_INVALID_ARG, "in guf_idpool_try_init: initial_cap is < 0");
guf_err_set_or_panic(err, GUF_ERR_INVALID_ARG, GUF_ERR_MSG("in guf_idpool_try_init: initial_cap is < 0"));
return NULL;
}
pool->free_id_dbuf = {0};
GUF_CAT(GUF_ID_POOL_DBUF, _try_init)(&pool->free_id_dbuf, initial_cap, allocator, err);
if (err && *err != GUF_ERR_NONE) {
guf_err_set_or_panic(err, *err, "in guf_idpool_try_init: failed to allocate dbuf");
guf_err_set_or_panic(err, *err, GUF_ERR_MSG("in guf_idpool_try_init: failed to allocate dbuf"));
return NULL;
}
@ -201,7 +201,7 @@ GUF_ID_POOL_T GUF_CAT(GUF_ID_POOL_NAME, _try_alloc_id)(GUF_ID_POOL_NAME *pool, g
pool->next_id += 1;
}
if (id == GUF_ID_POOL_NULL) {
guf_err_set_or_panic(err, GUF_ERR_INT_OVERFLOW, "in guf_idpool_try_alloc_id: Out of ids.");
guf_err_set_or_panic(err, GUF_ERR_INT_OVERFLOW, GUF_ERR_MSG("in guf_idpool_try_alloc_id: Out of ids."));
} else {
guf_err_set_if_not_null(err, GUF_ERR_NONE);
}
@ -219,27 +219,29 @@ void GUF_CAT(GUF_ID_POOL_NAME, _try_free_id)(GUF_ID_POOL_NAME *pool, GUF_ID_POOL
GUF_ASSERT(GUF_CAT(GUF_ID_POOL_NAME, _is_valid)(pool));
if (id == GUF_ID_POOL_NULL) { // ID is NULL.
guf_err_set_or_panic(err, GUF_ERR_IDX_RANGE, "in guf_idpool_try_free_id: id is NULL");
guf_err_set_or_panic(err, GUF_ERR_IDX_RANGE, GUF_ERR_MSG("in guf_idpool_try_free_id: id is NULL"));
return;
} else if (id < 0 || id >= pool->next_id) { // ID is beyond the allocated range.
guf_err_set_or_panic(err, GUF_ERR_IDX_RANGE, "in guf_idpool_try_free_id: id < 0 or id >= pool->next_id");
guf_err_set_or_panic(err, GUF_ERR_IDX_RANGE, GUF_ERR_MSG("in guf_idpool_try_free_id: id < 0 or id >= pool->next_id"));
return;
} else if (pool->next_id == 0) { // There haven't been any IDs allocated yet.
GUF_ASSERT(pool->free_id_dbuf.size == 0);
guf_err_set_or_panic(err, GUF_ERR_IDX_RANGE, "in guf_idpool_try_free_id: pool->next_id == 0");
guf_err_set_or_panic(err, GUF_ERR_IDX_RANGE, GUF_ERR_MSG("in guf_idpool_try_free_id: pool->next_id == 0"));
return;
}
// NOTE: https://github.com/erincatto/box2d/issues/893 (last-retrieved 2025-03-17)
/*
// NOTE: https://github.com/erincatto/box2d/issues/893 (last-retrieved 2025-04-09)
if (id == pool->next_id - 1) { // Optimisation in case we remove the largest allocated id so far
pool->next_id -= 1;
guf_err_set_if_not_null(err, GUF_ERR_NONE);
return;
}
*/
GUF_CAT(GUF_ID_POOL_DBUF, _try_push_val)(&pool->free_id_dbuf, id, err);
if (err && *err != GUF_ERR_NONE) {
guf_err_set_or_panic(err, *err, "in guf_idpool_try_free_id: failed to push id");
guf_err_set_or_panic(err, *err, GUF_ERR_MSG("in guf_idpool_try_free_id: failed to push id"));
} else {
guf_err_set_if_not_null(err, GUF_ERR_NONE);
}