WPILibC++ 2023.4.3
Buffer.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#ifndef WPINET_UV_BUFFER_H_
6#define WPINET_UV_BUFFER_H_
7
8#include <uv.h>
9
10#include <cstring>
11#include <initializer_list>
12#include <span>
13#include <string_view>
14#include <utility>
15
16#include <wpi/SmallVector.h>
17
18namespace wpi::uv {
19
20/**
21 * Data buffer. Convenience wrapper around uv_buf_t.
22 */
23class Buffer : public uv_buf_t {
24 public:
26 base = nullptr;
27 len = 0;
28 }
29 /*implicit*/ Buffer(const uv_buf_t& oth) { // NOLINT
30 base = oth.base;
31 len = oth.len;
32 }
33 /*implicit*/ Buffer(std::string_view str) // NOLINT
34 : Buffer{str.data(), str.size()} {}
35 /*implicit*/ Buffer(std::span<const uint8_t> arr) // NOLINT
36 : Buffer{reinterpret_cast<const char*>(arr.data()), arr.size()} {}
37 Buffer(char* base_, size_t len_) {
38 base = base_;
39 len = static_cast<decltype(len)>(len_);
40 }
41 Buffer(const char* base_, size_t len_) {
42 base = const_cast<char*>(base_);
43 len = static_cast<decltype(len)>(len_);
44 }
45
46 std::span<const char> data() const { return {base, len}; }
47 std::span<char> data() { return {base, len}; }
48
49 operator std::span<const char>() const { return data(); } // NOLINT
50 operator std::span<char>() { return data(); } // NOLINT
51
52 static Buffer Allocate(size_t size) { return Buffer{new char[size], size}; }
53
55 Buffer buf = Allocate(in.size());
56 std::memcpy(buf.base, in.data(), in.size());
57 return buf;
58 }
59
60 static Buffer Dup(std::span<const uint8_t> in) {
61 Buffer buf = Allocate(in.size());
62 std::memcpy(buf.base, in.data(), in.size());
63 return buf;
64 }
65
66 Buffer Dup() const {
67 Buffer buf = Allocate(len);
68 std::memcpy(buf.base, base, len);
69 return buf;
70 }
71
72 void Deallocate() {
73 delete[] base;
74 base = nullptr;
75 len = 0;
76 }
77
79 Buffer buf = *this;
80 base = nullptr;
81 len = 0;
82 return buf;
83 }
84
85 friend void swap(Buffer& a, Buffer& b) {
86 using std::swap;
87 swap(a.base, b.base);
88 swap(a.len, b.len);
89 }
90};
91
92/**
93 * A simple pool allocator for Buffers.
94 * Buffers are allocated individually but are reused rather than returned
95 * to the heap.
96 * @tparam DEPTH depth of pool
97 */
98template <size_t DEPTH = 4>
100 public:
101 /**
102 * Constructor.
103 * @param size Size of each buffer to allocate.
104 */
105 explicit SimpleBufferPool(size_t size = 4096) : m_size{size} {}
107
108 SimpleBufferPool(const SimpleBufferPool& other) = delete;
110
111 /**
112 * Allocate a buffer.
113 */
115 if (m_pool.empty()) {
116 return Buffer::Allocate(m_size);
117 }
118 auto buf = m_pool.back();
119 m_pool.pop_back();
120 buf.len = m_size;
121 return buf;
122 }
123
124 /**
125 * Allocate a buffer.
126 */
127 Buffer operator()() { return Allocate(); }
128
129 /**
130 * Release allocated buffers back into the pool.
131 * This is NOT safe to use with arbitrary buffers unless they were
132 * allocated with the same size as the buffer pool allocation size.
133 */
134 void Release(std::span<Buffer> bufs) {
135 for (auto& buf : bufs) {
136 m_pool.emplace_back(buf.Move());
137 }
138 }
139
140 /**
141 * Clear the pool, releasing all buffers.
142 */
143 void Clear() {
144 for (auto& buf : m_pool) {
145 buf.Deallocate();
146 }
147 m_pool.clear();
148 }
149
150 /**
151 * Get number of buffers left in the pool before a new buffer will be
152 * allocated from the heap.
153 */
154 size_t Remaining() const { return m_pool.size(); }
155
156 private:
158 size_t m_size; // NOLINT
159};
160
161} // namespace wpi::uv
162
163#endif // WPINET_UV_BUFFER_H_
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1186
Data buffer.
Definition: Buffer.h:23
std::span< char > data()
Definition: Buffer.h:47
std::span< const char > data() const
Definition: Buffer.h:46
Buffer()
Definition: Buffer.h:25
Buffer(std::string_view str)
Definition: Buffer.h:33
void Deallocate()
Definition: Buffer.h:72
Buffer(const uv_buf_t &oth)
Definition: Buffer.h:29
Buffer Dup() const
Definition: Buffer.h:66
Buffer(const char *base_, size_t len_)
Definition: Buffer.h:41
static Buffer Allocate(size_t size)
Definition: Buffer.h:52
Buffer(std::span< const uint8_t > arr)
Definition: Buffer.h:35
static Buffer Dup(std::string_view in)
Definition: Buffer.h:54
static Buffer Dup(std::span< const uint8_t > in)
Definition: Buffer.h:60
Buffer Move()
Definition: Buffer.h:78
Buffer(char *base_, size_t len_)
Definition: Buffer.h:37
friend void swap(Buffer &a, Buffer &b)
Definition: Buffer.h:85
A simple pool allocator for Buffers.
Definition: Buffer.h:99
Buffer operator()()
Allocate a buffer.
Definition: Buffer.h:127
SimpleBufferPool(size_t size=4096)
Constructor.
Definition: Buffer.h:105
SimpleBufferPool & operator=(const SimpleBufferPool &other)=delete
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:154
void Release(std::span< Buffer > bufs)
Release allocated buffers back into the pool.
Definition: Buffer.h:134
void Clear()
Clear the pool, releasing all buffers.
Definition: Buffer.h:143
SimpleBufferPool(const SimpleBufferPool &other)=delete
Buffer Allocate()
Allocate a buffer.
Definition: Buffer.h:114
~SimpleBufferPool()
Definition: Buffer.h:106
basic_string_view< char > string_view
Definition: core.h:520
EIGEN_CONSTEXPR Index size(const T &x)
Definition: Meta.h:479
void swap(wpi::SmallVectorImpl< T > &LHS, wpi::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1299
b
Definition: data.h:44
Definition: ParallelTcpConnector.h:22
Definition: format.h:1544
It should be possible to cast uv_buf_t[] to WSABUF[] see http://msdn.microsoft.com/en-us/library/ms74...
Definition: win.h:234
char * base
Definition: win.h:236
ULONG len
Definition: win.h:235