OpenSDN source code
index_allocator.cc
Go to the documentation of this file.
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 
9  size_t index = BitSet::npos;
10  if (last_index_ == BitSet::npos) {
11  index = bitset_.find_first_clear();
12  } else {
14  if (index > max_index_) {
15  index = bitset_.find_first_clear();
16  }
17  }
18 
19  if (index > max_index_) index = BitSet::npos;
20  if (index != BitSet::npos) {
21  bitset_.set(index);
22  }
23  last_index_ = index;
24  return index;
25 }
26 
27 void IndexAllocator::FreeIndex(size_t index) {
28  assert(index <= max_index_);
29  bitset_.reset(index);
30 }
31 
33  return bitset_.none();
34 }
35 
37  return bitset_.any();
38 }
size_t find_first_clear() const
Definition: bitset.cc:308
bool none() const
Definition: bitset.cc:173
BitSet & reset(size_t pos)
Definition: bitset.cc:137
static const size_t npos
Definition: bitset.h:20
size_t find_next_clear(size_t pos) const
Definition: bitset.cc:325
bool any() const
Definition: bitset.cc:180
BitSet & set(size_t pos)
Definition: bitset.cc:126
void FreeIndex(size_t index)
size_t AllocIndex()