WPILibC++  2019.3.2-1-g99e4f7d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
Buffer.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef WPIUTIL_WPI_UV_BUFFER_H_
9 #define WPIUTIL_WPI_UV_BUFFER_H_
10 
11 #include <uv.h>
12 
13 #include <cstring>
14 #include <initializer_list>
15 #include <utility>
16 
17 #include "wpi/ArrayRef.h"
18 #include "wpi/SmallVector.h"
19 #include "wpi/StringRef.h"
20 
21 namespace wpi {
22 namespace uv {
23 
27 class Buffer : public uv_buf_t {
28  public:
29  Buffer() {
30  base = nullptr;
31  len = 0;
32  }
33  /*implicit*/ Buffer(const uv_buf_t& oth) { // NOLINT(runtime/explicit)
34  base = oth.base;
35  len = oth.len;
36  }
37  /*implicit*/ Buffer(StringRef str) // NOLINT(runtime/explicit)
38  : Buffer{str.data(), str.size()} {}
39  /*implicit*/ Buffer(ArrayRef<uint8_t> arr) // NOLINT(runtime/explicit)
40  : Buffer{reinterpret_cast<const char*>(arr.data()), arr.size()} {}
41  Buffer(char* base_, size_t len_) {
42  base = base_;
43  len = len_;
44  }
45  Buffer(const char* base_, size_t len_) {
46  base = const_cast<char*>(base_);
47  len = len_;
48  }
49 
50  ArrayRef<char> data() const { return ArrayRef<char>{base, len}; }
51  MutableArrayRef<char> data() { return MutableArrayRef<char>{base, len}; }
52 
53  operator ArrayRef<char>() const { return data(); }
54  operator MutableArrayRef<char>() { return data(); }
55 
56  static Buffer Allocate(size_t size) { return Buffer{new char[size], size}; }
57 
58  static Buffer Dup(StringRef in) {
59  Buffer buf = Allocate(in.size());
60  std::memcpy(buf.base, in.data(), in.size());
61  return buf;
62  }
63 
64  static Buffer Dup(ArrayRef<uint8_t> in) {
65  Buffer buf = Allocate(in.size());
66  std::memcpy(buf.base, in.begin(), in.size());
67  return buf;
68  }
69 
70  Buffer Dup() const {
71  Buffer buf = Allocate(len);
72  std::memcpy(buf.base, base, len);
73  return buf;
74  }
75 
76  void Deallocate() {
77  delete[] base;
78  base = nullptr;
79  len = 0;
80  }
81 
82  Buffer Move() {
83  Buffer buf = *this;
84  base = nullptr;
85  len = 0;
86  return buf;
87  }
88 
89  friend void swap(Buffer& a, Buffer& b) {
90  using std::swap;
91  swap(a.base, b.base);
92  swap(a.len, b.len);
93  }
94 };
95 
102 template <size_t DEPTH = 4>
104  public:
109  explicit SimpleBufferPool(size_t size = 4096) : m_size{size} {}
110 
115  if (m_pool.empty()) return Buffer::Allocate(m_size);
116  auto buf = m_pool.back();
117  m_pool.pop_back();
118  buf.len = m_size;
119  return buf;
120  }
121 
125  Buffer operator()() { return Allocate(); }
126 
133  for (auto& buf : bufs) m_pool.emplace_back(buf.Move());
134  }
135 
139  void Clear() {
140  for (auto& buf : m_pool) buf.Deallocate();
141  m_pool.clear();
142  }
143 
148  size_t Remaining() const { return m_pool.size(); }
149 
150  private:
152  size_t m_size;
153 };
154 
155 } // namespace uv
156 } // namespace wpi
157 
158 #endif // WPIUTIL_WPI_UV_BUFFER_H_
SimpleBufferPool(size_t size=4096)
Constructor.
Definition: Buffer.h:109
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:868
Buffer Allocate()
Allocate a buffer.
Definition: Buffer.h:114
Buffer operator()()
Allocate a buffer.
Definition: Buffer.h:125
void Clear()
Clear the pool, releasing all buffers.
Definition: Buffer.h:139
size_t Remaining() const
Get number of buffers left in the pool before a new buffer will be allocated from the heap...
Definition: Buffer.h:148
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const noexcept
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
WPILib C++ utilities (wpiutil) namespace.
Definition: SmallString.h:21
It should be possible to cast uv_buf_t[] to WSABUF[] see http://msdn.microsoft.com/en-us/library/ms74...
Definition: win.h:213
void Release(MutableArrayRef< Buffer > bufs)
Release allocated buffers back into the pool.
Definition: Buffer.h:132
A simple pool allocator for Buffers.
Definition: Buffer.h:103
auto size(R &&Range, typename std::enable_if< std::is_same< typename std::iterator_traits< decltype(Range.begin())>::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr) -> decltype(std::distance(Range.begin(), Range.end()))
Get the size of a range.
Definition: STLExtras.h:999
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const noexcept
size - Get the string size.
Definition: StringRef.h:138
Data buffer.
Definition: Buffer.h:27
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:291