OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
index_allocator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Juniper Networks, Inc. All rights reserved.
3  */
4 #include "base/index_allocator.h"
5 #include "base/util.h"
6 
8  size_t index = BitSet::npos;
9  if (last_index_ == BitSet::npos) {
10  index = bitset_.find_first_clear();
11  } else {
13  if (index > max_index_) {
14  index = bitset_.find_first_clear();
15  }
16  }
17 
18  if (index > max_index_) index = BitSet::npos;
19  if (index != BitSet::npos) {
20  bitset_.set(index);
21  }
22  last_index_ = index;
23  return index;
24 }
25 
26 void IndexAllocator::FreeIndex(size_t index) {
27  assert(index <= max_index_);
28  bitset_.reset(index);
29 }
30 
32  return bitset_.none();
33 }
34 
36  return bitset_.any();
37 }
BitSet & reset(size_t pos)
Definition: bitset.cc:136
size_t find_first_clear() const
Definition: bitset.cc:307
size_t AllocIndex()
static const size_t npos
Definition: bitset.h:19
void FreeIndex(size_t index)
size_t find_next_clear(size_t pos) const
Definition: bitset.cc:324
BitSet & set(size_t pos)
Definition: bitset.cc:125
bool any() const
Definition: bitset.cc:179
bool none() const
Definition: bitset.cc:172