From fb0807d7696142d403bb6b18e14acabbdf2b9308 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 12 Mar 2019 01:08:27 +0200 Subject: [PATCH] putil: add is_allocated convenience method to UniqueIdAllocator Closes #580 --- panda/src/putil/uniqueIdAllocator.cxx | 14 ++++++++++++++ panda/src/putil/uniqueIdAllocator.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/panda/src/putil/uniqueIdAllocator.cxx b/panda/src/putil/uniqueIdAllocator.cxx index 6c4edd9795..19e3a8ee5a 100644 --- a/panda/src/putil/uniqueIdAllocator.cxx +++ b/panda/src/putil/uniqueIdAllocator.cxx @@ -173,6 +173,20 @@ initial_reserve_id(uint32_t id) { --_free; } +/** + * Checks the allocated state of an index. Returns true for + * indices that are currently allocated and in use. + */ +bool UniqueIdAllocator:: +is_allocated(uint32_t id) { + if (id < _min || id > _max) { + // This id is out of range, not allocated. + return false; + } + + uint32_t index = id - _min; // Convert to _table index. + return _table[index] == IndexAllocated; +} /** * Free an allocated index (index must be between _min and _max that were diff --git a/panda/src/putil/uniqueIdAllocator.h b/panda/src/putil/uniqueIdAllocator.h index 50db80b0b0..2f3e3b9e8f 100644 --- a/panda/src/putil/uniqueIdAllocator.h +++ b/panda/src/putil/uniqueIdAllocator.h @@ -43,6 +43,8 @@ PUBLISHED: uint32_t allocate(); void initial_reserve_id(uint32_t id); + bool is_allocated(uint32_t index); + void free(uint32_t index); PN_stdfloat fraction_used() const;