Line data Source code
1 : /* 2 : * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : #include "base/index_allocator.h" 5 : #include "base/util.h" 6 : 7 68 : size_t IndexAllocator::AllocIndex() { 8 68 : size_t index = BitSet::npos; 9 68 : if (last_index_ == BitSet::npos) { 10 18 : index = bitset_.find_first_clear(); 11 : } else { 12 50 : index = bitset_.find_next_clear(last_index_); 13 50 : if (index > max_index_) { 14 2 : index = bitset_.find_first_clear(); 15 : } 16 : } 17 : 18 68 : if (index > max_index_) index = BitSet::npos; 19 68 : if (index != BitSet::npos) { 20 66 : bitset_.set(index); 21 : } 22 68 : last_index_ = index; 23 68 : return index; 24 : } 25 : 26 44 : void IndexAllocator::FreeIndex(size_t index) { 27 44 : assert(index <= max_index_); 28 44 : bitset_.reset(index); 29 44 : } 30 : 31 0 : bool IndexAllocator::NoneIndexSet() { 32 0 : return bitset_.none(); 33 : } 34 : 35 0 : bool IndexAllocator::AnyIndexSet() { 36 0 : return bitset_.any(); 37 : }