WPILibC++  2019.1.1-beta-2-1-g9bc998f
 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  void Deallocate() {
71  delete[] base;
72  base = nullptr;
73  len = 0;
74  }
75 
76  Buffer Move() {
77  Buffer buf = *this;
78  base = nullptr;
79  len = 0;
80  return buf;
81  }
82 
83  friend void swap(Buffer& a, Buffer& b) {
84  using std::swap;
85  swap(a.base, b.base);
86  swap(a.len, b.len);
87  }
88 };
89 
96 template <size_t DEPTH = 4>
98  public:
103  explicit SimpleBufferPool(size_t size = 4096) : m_size{size} {}
104 
109  if (m_pool.empty()) return Buffer::Allocate(m_size);
110  auto buf = m_pool.back();
111  m_pool.pop_back();
112  buf.len = m_size;
113  return buf;
114  }
115 
119  Buffer operator()() { return Allocate(); }
120 
127  for (auto& buf : bufs) m_pool.emplace_back(buf.Move());
128  }
129 
133  void Clear() {
134  for (auto& buf : m_pool) buf.Deallocate();
135  m_pool.clear();
136  }
137 
142  size_t Remaining() const { return m_pool.size(); }
143 
144  private:
146  size_t m_size;
147 };
148 
149 } // namespace uv
150 } // namespace wpi
151 
152 #endif // WPIUTIL_WPI_UV_BUFFER_H_
SimpleBufferPool(size_t size=4096)
Constructor.
Definition: Buffer.h:103
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:108
Buffer operator()()
Allocate a buffer.
Definition: Buffer.h:119
void Clear()
Clear the pool, releasing all buffers.
Definition: Buffer.h:133
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:142
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:126
A simple pool allocator for Buffers.
Definition: Buffer.h:97
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