Browse Source

putil: add is_allocated convenience method to UniqueIdAllocator

Closes #580
Daniel 6 years ago
parent
commit
fb0807d769
2 changed files with 16 additions and 0 deletions
  1. 14 0
      panda/src/putil/uniqueIdAllocator.cxx
  2. 2 0
      panda/src/putil/uniqueIdAllocator.h

+ 14 - 0
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

+ 2 - 0
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;