AK: Allow specifying a minimum value for IDs returned by IDAllocator

This commit is contained in:
Timothy Flynn 2023-04-06 07:29:05 -04:00 committed by Linus Groh
parent c978beb18b
commit 4d9c14ca67
Notes: sideshowbarker 2024-07-17 06:20:50 +09:00

View File

@ -12,11 +12,17 @@
namespace AK { 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 { class IDAllocator {
public: public:
IDAllocator() = default; IDAllocator() = default;
explicit IDAllocator(int minimum_value)
: m_minimum_value(minimum_value)
{
}
~IDAllocator() = default; ~IDAllocator() = default;
int allocate() int allocate()
@ -25,7 +31,7 @@ public:
int id = 0; int id = 0;
for (;;) { for (;;) {
id = static_cast<int>(get_random_uniform(NumericLimits<int>::max())); id = static_cast<int>(get_random_uniform(NumericLimits<int>::max()));
if (id == 0) if (id < m_minimum_value)
continue; continue;
if (m_allocated_ids.set(id) == AK::HashSetResult::InsertedNewEntry) if (m_allocated_ids.set(id) == AK::HashSetResult::InsertedNewEntry)
break; break;
@ -40,6 +46,7 @@ public:
private: private:
HashTable<int> m_allocated_ids; HashTable<int> m_allocated_ids;
int m_minimum_value { 1 };
}; };
} }