summaryrefslogtreecommitdiffstats
path: root/src/msg/async/dpdk/circular_buffer.h
blob: 2c92c12044408ad5cc2e4cb15a37108ec5789f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
/*
 * This file is open source software, licensed to you under the terms
 * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
 * distributed with this work for additional information regarding copyright
 * ownership.  You may not use this file except in compliance with the License.
 *
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
/*
 * Copyright (C) 2014 Cloudius Systems, Ltd.
 */

#ifndef CEPH_CIRCULAR_BUFFER_HH_
#define CEPH_CIRCULAR_BUFFER_HH_

// A growable double-ended queue container that can be efficiently
// extended (and shrunk) from both ends.  Implementation is a single
// storage vector.
//
// Similar to libstdc++'s std::deque, except that it uses a single level
// store, and so is more efficient for simple stored items.
// Similar to boost::circular_buffer_space_optimized, except it uses
// uninitialized storage for unoccupied elements (and thus move/copy
// constructors instead of move/copy assignments, which are less efficient).

#include <memory>
#include <algorithm>

#include "transfer.h"

template <typename T, typename Alloc = std::allocator<T>>
class circular_buffer {
  struct impl : Alloc {
    T* storage = nullptr;
    // begin, end interpreted (mod capacity)
    size_t begin = 0;
    size_t end = 0;
    size_t capacity = 0;
  };
  impl _impl;
 public:
  using value_type = T;
  using size_type = size_t;
  using reference = T&;
  using pointer = T*;
  using const_reference = const T&;
  using const_pointer = const T*;
 public:
  circular_buffer() = default;
  circular_buffer(circular_buffer&& X);
  circular_buffer(const circular_buffer& X) = delete;
  ~circular_buffer();
  circular_buffer& operator=(const circular_buffer&) = delete;
  circular_buffer& operator=(circular_buffer&&) = delete;
  void push_front(const T& data);
  void push_front(T&& data);
  template <typename... A>
  void emplace_front(A&&... args);
  void push_back(const T& data);
  void push_back(T&& data);
  template <typename... A>
  void emplace_back(A&&... args);
  T& front();
  T& back();
  void pop_front();
  void pop_back();
  bool empty() const;
  size_t size() const;
  size_t capacity() const;
  T& operator[](size_t idx);
  template <typename Func>
  void for_each(Func func);
  // access an element, may return wrong or destroyed element
  // only useful if you do not rely on data accuracy (e.g. prefetch)
  T& access_element_unsafe(size_t idx);
 private:
  void expand();
  void maybe_expand(size_t nr = 1);
  size_t mask(size_t idx) const;

  template<typename CB, typename ValueType>
  struct cbiterator : std::iterator<std::random_access_iterator_tag, ValueType> {
    typedef std::iterator<std::random_access_iterator_tag, ValueType> super_t;

    ValueType& operator*() const { return cb->_impl.storage[cb->mask(idx)]; }
    ValueType* operator->() const { return &cb->_impl.storage[cb->mask(idx)]; }
    // prefix
    cbiterator<CB, ValueType>& operator++() {
      idx++;
      return *this;
    }
    // postfix
    cbiterator<CB, ValueType> operator++(int unused) {
      auto v = *this;
      idx++;
      return v;
    }
    // prefix
    cbiterator<CB, ValueType>& operator--() {
      idx--;
      return *this;
    }
    // postfix
    cbiterator<CB, ValueType> operator--(int unused) {
      auto v = *this;
      idx--;
      return v;
    }
    cbiterator<CB, ValueType> operator+(typename super_t::difference_type n) const {
      return cbiterator<CB, ValueType>(cb, idx + n);
    }
    cbiterator<CB, ValueType> operator-(typename super_t::difference_type n) const {
      return cbiterator<CB, ValueType>(cb, idx - n);
    }
    cbiterator<CB, ValueType>& operator+=(typename super_t::difference_type n) {
      idx += n;
      return *this;
    }
    cbiterator<CB, ValueType>& operator-=(typename super_t::difference_type n) {
      idx -= n;
      return *this;
    }
    bool operator==(const cbiterator<CB, ValueType>& rhs) const {
      return idx == rhs.idx;
    }
    bool operator!=(const cbiterator<CB, ValueType>& rhs) const {
      return idx != rhs.idx;
    }
    bool operator<(const cbiterator<CB, ValueType>& rhs) const {
      return idx < rhs.idx;
    }
    bool operator>(const cbiterator<CB, ValueType>& rhs) const {
      return idx > rhs.idx;
    }
    bool operator>=(const cbiterator<CB, ValueType>& rhs) const {
      return idx >= rhs.idx;
    }
    bool operator<=(const cbiterator<CB, ValueType>& rhs) const {
      return idx <= rhs.idx;
    }
    typename super_t::difference_type operator-(const cbiterator<CB, ValueType>& rhs) const {
      return idx - rhs.idx;
    }
   private:
    CB* cb;
    size_t idx;
    cbiterator<CB, ValueType>(CB* b, size_t i) : cb(b), idx(i) {}
    friend class circular_buffer;
  };
  friend class iterator;

 public:
  typedef cbiterator<circular_buffer, T> iterator;
  typedef cbiterator<const circular_buffer, const T> const_iterator;

  iterator begin() {
    return iterator(this, _impl.begin);
  }
  const_iterator begin() const {
    return const_iterator(this, _impl.begin);
  }
  iterator end() {
    return iterator(this, _impl.end);
  }
  const_iterator end() const {
    return const_iterator(this, _impl.end);
  }
  const_iterator cbegin() const {
    return const_iterator(this, _impl.begin);
  }
  const_iterator cend() const {
    return const_iterator(this, _impl.end);
  }
};

template <typename T, typename Alloc>
inline size_t circular_buffer<T, Alloc>::mask(size_t idx) const {
  return idx & (_impl.capacity - 1);
}

template <typename T, typename Alloc>
inline bool circular_buffer<T, Alloc>::empty() const {
  return _impl.begin == _impl.end;
}

template <typename T, typename Alloc>
inline size_t circular_buffer<T, Alloc>::size() const {
  return _impl.end - _impl.begin;
}

template <typename T, typename Alloc>
inline size_t circular_buffer<T, Alloc>::capacity() const {
  return _impl.capacity;
}

template <typename T, typename Alloc>
inline circular_buffer<T, Alloc>::circular_buffer(circular_buffer&& x)
    : _impl(std::move(x._impl)) {
  x._impl = {};
}

template <typename T, typename Alloc>
template <typename Func>
inline void circular_buffer<T, Alloc>::for_each(Func func) {
  auto s = _impl.storage;
  auto m = _impl.capacity - 1;
  for (auto i = _impl.begin; i != _impl.end; ++i) {
    func(s[i & m]);
  }
}

template <typename T, typename Alloc>
inline circular_buffer<T, Alloc>::~circular_buffer() {
  for_each([this] (T& obj) {
    _impl.destroy(&obj);
  });
  _impl.deallocate(_impl.storage, _impl.capacity);
}

template <typename T, typename Alloc>
void circular_buffer<T, Alloc>::expand() {
  auto new_cap = std::max<size_t>(_impl.capacity * 2, 1);
  auto new_storage = _impl.allocate(new_cap);
  auto p = new_storage;
  try {
    for_each([this, &p] (T& obj) {
      transfer_pass1(_impl, &obj, p);
      p++;
    });
  } catch (...) {
    while (p != new_storage) {
      _impl.destroy(--p);
    }
    _impl.deallocate(new_storage, new_cap);
    throw;
  }
  p = new_storage;
  for_each([this, &p] (T& obj) {
    transfer_pass2(_impl, &obj, p++);
  });
  std::swap(_impl.storage, new_storage);
  std::swap(_impl.capacity, new_cap);
  _impl.begin = 0;
  _impl.end = p - _impl.storage;
  _impl.deallocate(new_storage, new_cap);
}

template <typename T, typename Alloc>
inline void circular_buffer<T, Alloc>::maybe_expand(size_t nr) {
  if (_impl.end - _impl.begin + nr > _impl.capacity) {
    expand();
  }
}

template <typename T, typename Alloc>
inline void circular_buffer<T, Alloc>::push_front(const T& data) {
  maybe_expand();
  auto p = &_impl.storage[mask(_impl.begin - 1)];
  _impl.construct(p, data);
  --_impl.begin;
}

template <typename T, typename Alloc>
inline void circular_buffer<T, Alloc>::push_front(T&& data) {
  maybe_expand();
  auto p = &_impl.storage[mask(_impl.begin - 1)];
  _impl.construct(p, std::move(data));
  --_impl.begin;
}

template <typename T, typename Alloc>
template <typename... Args>
inline void circular_buffer<T, Alloc>::emplace_front(Args&&... args) {
  maybe_expand();
  auto p = &_impl.storage[mask(_impl.begin - 1)];
  _impl.construct(p, std::forward<Args>(args)...);
  --_impl.begin;
}

template <typename T, typename Alloc>
inline void circular_buffer<T, Alloc>::push_back(const T& data) {
  maybe_expand();
  auto p = &_impl.storage[mask(_impl.end)];
  _impl.construct(p, data);
  ++_impl.end;
}

template <typename T, typename Alloc>
inline void circular_buffer<T, Alloc>::push_back(T&& data) {
  maybe_expand();
  auto p = &_impl.storage[mask(_impl.end)];
  _impl.construct(p, std::move(data));
  ++_impl.end;
}

template <typename T, typename Alloc>
template <typename... Args>
inline void circular_buffer<T, Alloc>::emplace_back(Args&&... args) {
  maybe_expand();
  auto p = &_impl.storage[mask(_impl.end)];
  _impl.construct(p, std::forward<Args>(args)...);
  ++_impl.end;
}

template <typename T, typename Alloc>
inline T& circular_buffer<T, Alloc>::front() {
  return _impl.storage[mask(_impl.begin)];
}

template <typename T, typename Alloc>
inline T& circular_buffer<T, Alloc>::back() {
  return _impl.storage[mask(_impl.end - 1)];
}

template <typename T, typename Alloc>
inline void circular_buffer<T, Alloc>::pop_front() {
  _impl.destroy(&front());
  ++_impl.begin;
}

template <typename T, typename Alloc>
inline void circular_buffer<T, Alloc>::pop_back() {
  _impl.destroy(&back());
  --_impl.end;
}

template <typename T, typename Alloc>
inline T& circular_buffer<T, Alloc>::operator[](size_t idx) {
  return _impl.storage[mask(_impl.begin + idx)];
}

template <typename T, typename Alloc>
inline T& circular_buffer<T, Alloc>::access_element_unsafe(size_t idx) {
  return _impl.storage[mask(_impl.begin + idx)];
}

#endif /* CEPH_CIRCULAR_BUFFER_HH_ */