bmv2
Designing your own switch target with bmv2
handle_mgr.h
1 /* Copyright 2013-present Barefoot Networks, Inc.
2  * Copyright 2021 VMware, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Antonin Bas
19  *
20  */
21 
22 #ifndef BM_BM_SIM_HANDLE_MGR_H_
23 #define BM_BM_SIM_HANDLE_MGR_H_
24 
25 #include <bm/config.h>
26 
27 #include <algorithm> // for swap
28 #include <type_traits>
29 #include <utility>
30 
31 #include "dynamic_bitset.h"
32 
33 namespace bm {
34 
35 using handle_t = uintptr_t;
36 
37 class HandleMgr {
38  public:
39  using bitset = DynamicBitset;
40  using size_type = bitset::size_type;
41  static constexpr size_type npos = bitset::npos;
42 
43  // iterators: since they are very simple, I did not mind duplicating the
44  // code. Maybe it would be a good idea to condition the const'ness of the
45  // iterator with a boolean template, to unify the 2 iterators.
46 
47  class iterator {
48  public:
49  iterator(HandleMgr *handle_mgr, handle_t index)
50  : handle_mgr(handle_mgr), index(index) {}
51 
52  // NOLINTNEXTLINE(runtime/explicit)
53  iterator(const iterator &other) = default;
54 
55  handle_t &operator*() {return index;}
56  handle_t *operator->() {return &index;}
57 
58  bool operator==(const iterator &other) const {
59  return (handle_mgr == other.handle_mgr) && (index == other.index);
60  }
61 
62  bool operator!=(const iterator &other) const {
63  return !(*this == other);
64  }
65 
66  iterator& operator++() {
67  auto pos = static_cast<size_type>(index);
68  pos = handle_mgr->handles.find_next(pos);
69  if (pos == npos)
70  index = -1;
71  else
72  index = static_cast<handle_t>(pos);
73  return *this;
74  }
75 
76  iterator operator++(int) {
77  // Use operator++()
78  const iterator old(*this);
79  ++(*this);
80  return old;
81  }
82 
83  private:
84  HandleMgr *handle_mgr;
85  handle_t index;
86  };
87 
88  class const_iterator {
89  public:
90  const_iterator(const HandleMgr *handle_mgr, handle_t index)
91  : handle_mgr(handle_mgr), index(index) {}
92 
93  // NOLINTNEXTLINE(runtime/explicit)
94  const_iterator(const const_iterator &other) = default;
95 
96  const handle_t &operator*() const {return index;}
97  const handle_t *operator->() const {return &index;}
98 
99  const_iterator& operator=(const const_iterator &other) {
100  handle_mgr = other.handle_mgr;
101  index = other.index;
102  return *this;
103  }
104 
105  bool operator==(const const_iterator &other) const {
106  return (handle_mgr == other.handle_mgr) && (index == other.index);
107  }
108 
109  bool operator!=(const const_iterator &other) const {
110  return !(*this == other);
111  }
112 
113  const const_iterator& operator++() {
114  auto pos = static_cast<size_type>(index);
115  pos = handle_mgr->handles.find_next(pos);
116  if (pos == npos)
117  index = -1;
118  else
119  index = static_cast<handle_t>(pos);
120  return *this;
121  }
122 
123  const const_iterator operator++(int) {
124  // Use operator++()
125  const const_iterator old(*this);
126  ++(*this);
127  return old;
128  }
129 
130  private:
131  const HandleMgr *handle_mgr;
132  handle_t index;
133  };
134 
135 
136  public:
137  bool operator==(const HandleMgr &other) const {
138  return (handles == other.handles);
139  }
140 
141  bool operator!=(const HandleMgr &other) const {
142  return !(*this == other);
143  }
144 
145  /* Return 0 on success, -1 on failure */
146 
147  int get_handle(handle_t *handle) {
148  auto pos = first_unset;
149  *handle = static_cast<handle_t>(pos);
150  auto s = handles.size();
151  if (pos < s) {
152  handles.set(pos);
153  first_unset = handles.find_unset_next(first_unset);
154  first_unset = (first_unset == DynamicBitset::npos) ? s : first_unset;
155  } else {
156  assert(pos == s);
157  first_unset++;
158  handles.push_back(true);
159  }
160  return 0;
161  }
162 
163  int release_handle(handle_t handle) {
164  auto pos = static_cast<size_type>(handle);
165  auto s = handles.size();
166  if (pos >= s || !handles.reset(pos)) return -1;
167  if (first_unset > pos) first_unset = pos;
168  return 0;
169  }
170 
171  int set_handle(handle_t handle) {
172  auto pos = static_cast<size_type>(handle);
173  auto s = handles.size();
174  if (pos >= s)
175  handles.resize(pos + 1);
176  if (!handles.set(pos))
177  return -1;
178  if (first_unset == pos) {
179  first_unset = handles.find_unset_next(pos);
180  first_unset = (first_unset == DynamicBitset::npos) ? s : first_unset;
181  }
182  return 0;
183  }
184 
185  size_t size() const {
186  return static_cast<size_t>(handles.count());
187  }
188 
189  bool valid_handle(handle_t handle) const {
190  auto pos = static_cast<size_type>(handle);
191  auto s = handles.size();
192  if (pos >= s) return false;
193  return handles.test(pos);
194  }
195 
196  void clear() {
197  handles.clear();
198  first_unset = 0;
199  }
200 
201  // iterators
202 
203  iterator begin() {
204  auto pos = handles.find_first();
205  if (pos == npos)
206  return iterator(this, -1);
207  else
208  return iterator(this, static_cast<handle_t>(pos));
209  }
210 
211  const_iterator begin() const {
212  auto pos = handles.find_first();
213  if (pos == npos)
214  return const_iterator(this, -1);
215  else
216  return const_iterator(this, static_cast<handle_t>(pos));
217  }
218 
219  iterator end() {
220  return iterator(this, -1);
221  }
222 
223  const_iterator end() const {
224  return const_iterator(this, -1);
225  }
226 
227  private:
228  bitset handles;
229  size_type first_unset{0};
230 };
231 
232 } // namespace bm
233 
234 #endif // BM_BM_SIM_HANDLE_MGR_H_