From 4d9c14ca6766204ba998e3f89588b723abcd1db3 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 6 Apr 2023 07:29:05 -0400 Subject: [PATCH] AK: Allow specifying a minimum value for IDs returned by IDAllocator --- AK/IDAllocator.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/AK/IDAllocator.h b/AK/IDAllocator.h index 91f2fda7a4..c8ba848256 100644 --- a/AK/IDAllocator.h +++ b/AK/IDAllocator.h @@ -12,11 +12,17 @@ namespace AK { -// This class manages a pool of random ID's in the range 1..INT32_MAX +// This class manages a pool of random ID's in the range N (default of 1) to INT32_MAX class IDAllocator { public: IDAllocator() = default; + + explicit IDAllocator(int minimum_value) + : m_minimum_value(minimum_value) + { + } + ~IDAllocator() = default; int allocate() @@ -25,7 +31,7 @@ public: int id = 0; for (;;) { id = static_cast(get_random_uniform(NumericLimits::max())); - if (id == 0) + if (id < m_minimum_value) continue; if (m_allocated_ids.set(id) == AK::HashSetResult::InsertedNewEntry) break; @@ -40,6 +46,7 @@ public: private: HashTable m_allocated_ids; + int m_minimum_value { 1 }; }; }