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