From 3b340a2f9e83ae2749cdd82af8ac40b64e7daa90 Mon Sep 17 00:00:00 2001 From: Pekka Paalanen Date: Mon, 3 Mar 2025 13:33:01 +0200 Subject: [PATCH] libweston: refactor update_lowest_free_bucket() No functional change here other than asserting that next_num didn't wrap around. This reorganization helps adding the code needed to clear the memory added by xrealloc() in the next patch. Signed-off-by: Pekka Paalanen (cherry picked from commit 2733a9596227336c7ca52d7e46022f9316777cb5) --- libweston/id-number-allocator.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/libweston/id-number-allocator.c b/libweston/id-number-allocator.c index aeae7e739..af41f0a3f 100644 --- a/libweston/id-number-allocator.c +++ b/libweston/id-number-allocator.c @@ -87,6 +87,7 @@ update_lowest_free_bucket(struct weston_idalloc *idalloc) { uint32_t old_lowest_free_bucket = idalloc->lowest_free_bucket; uint32_t *bucket; + uint32_t next_num; unsigned int i; for (i = old_lowest_free_bucket; i < idalloc->num_buckets; i++) { @@ -100,12 +101,16 @@ update_lowest_free_bucket(struct weston_idalloc *idalloc) return; } - /* We didn't find any free bucket, so we need to add more buckets. The - * first one (from the new added) will be the lowest free. */ + /* We didn't find any free bucket, so we need to add more buckets. */ + next_num = idalloc->num_buckets * 2; + weston_assert_uint32_gt(idalloc->compositor, next_num, idalloc->num_buckets); + + idalloc->buckets = xrealloc(idalloc->buckets, next_num * sizeof(*idalloc->buckets)); + + /* The first one (from the new added) is the lowest free. */ idalloc->lowest_free_bucket = idalloc->num_buckets; - idalloc->num_buckets *= 2; - idalloc->buckets = xrealloc(idalloc->buckets, - idalloc->num_buckets * sizeof(*idalloc->buckets)); + + idalloc->num_buckets = next_num; } /**