C/C++ API Reference
Loading...
Searching...
No Matches
chunk.h
1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17#include <utility>
18
19#include "pw_allocator/allocator.h"
20#include "pw_allocator/shared_ptr.h"
21#include "pw_assert/assert.h"
22#include "pw_bytes/span.h"
23
24namespace pw::multibuf::v1_adapter {
25
26// Forward declarations
27class OwnedChunk;
28
29namespace internal {
30class BasicSimpleAllocator;
31class ChunkAllocator;
32} // namespace internal
33
35
50class Chunk {
51 public:
52 Chunk() = delete;
53
54 Chunk(const Chunk& other) { *this = other; }
55
56 Chunk& operator=(const Chunk& other) {
57 metadata_allocator_ = other.metadata_allocator_;
58 region_ = other.region_;
59 span_ = other.span_;
60 return *this;
61 }
62
63 ~Chunk() = default;
64
65 std::byte* data() { return span_.data(); }
66 const std::byte* data() const { return span_.data(); }
67
68 size_t size() const { return span_.size(); }
69
70 [[nodiscard]] bool empty() const { return span_.empty(); }
71
72 std::byte& operator[](size_t index) { return span_[index]; }
73 const std::byte& operator[](size_t index) const { return span_[index]; }
74
79 constexpr Allocator* metadata_allocator() const {
80 return metadata_allocator_;
81 }
82
87 constexpr const SharedPtr<std::byte[]>& region() const { return region_; }
88
89 // Container declarations
90 using element_type = std::byte;
91 using value_type = std::byte;
92 using size_type = size_t;
93 using difference_type = ptrdiff_t;
94 using pointer = std::byte*;
95 using const_pointer = const std::byte*;
96 using reference = std::byte&;
97 using const_reference = const std::byte&;
98 using iterator = std::byte*;
99 using const_iterator = const std::byte*;
100 using reverse_iterator = std::byte*;
101 using const_reverse_iterator = const std::byte*;
102
103 std::byte* begin() { return span_.data(); }
104 const std::byte* begin() const { return cbegin(); }
105 const std::byte* cbegin() const { return span_.data(); }
106 std::byte* end() { return span_.data() + span_.size(); }
107 const std::byte* end() const { return cend(); }
108 const std::byte* cend() const { return span_.data() + span_.size(); }
109
111 [[nodiscard]] bool CanMerge(const Chunk& next_chunk) const;
112
114 bool Merge(OwnedChunk& next_chunk);
115
118 [[nodiscard]] bool ClaimPrefix(size_t bytes_to_claim);
119
122 [[nodiscard]] bool ClaimSuffix(size_t bytes_to_claim);
123
126 void DiscardPrefix(size_t bytes_to_discard);
127
130 void Slice(size_t begin, size_t end);
131
134 void Truncate(size_t len);
135
138 std::optional<OwnedChunk> TakePrefix(size_t bytes_to_take);
139
142 std::optional<OwnedChunk> TakeSuffix(size_t bytes_to_take);
143
144 private:
145 friend class OwnedChunk;
146 friend class MultiBufChunks;
147
149 const SharedPtr<std::byte[]>& region)
150 : metadata_allocator_(&metadata_allocator),
151 region_(region),
152 span_(region.get(), region.size()) {}
153
154 Allocator* metadata_allocator_;
156 ByteSpan span_;
157};
158
164 public:
165 using element_type = std::byte;
166 using value_type = std::byte;
167 using size_type = size_t;
168 using difference_type = ptrdiff_t;
169 using pointer = std::byte*;
170 using const_pointer = const std::byte*;
171 using reference = std::byte&;
172 using const_reference = const std::byte&;
173 using iterator = std::byte*;
174 using const_iterator = const std::byte*;
175 using reverse_iterator = std::byte*;
176 using const_reverse_iterator = const std::byte*;
177
178 constexpr OwnedChunk() = default;
179
180 OwnedChunk(Allocator& metadata_allocator, const SharedPtr<std::byte[]>& data)
181 : inner_(Chunk(metadata_allocator, data)) {}
182
183 OwnedChunk(const OwnedChunk&) = delete;
184 OwnedChunk& operator=(const OwnedChunk&) = delete;
185
186 OwnedChunk(OwnedChunk&& other) = default;
187 OwnedChunk& operator=(OwnedChunk&& other) = default;
188
189 ~OwnedChunk() = default;
190
191 // Accessors
192
193 std::byte* data() { return inner_.has_value() ? inner_->data() : nullptr; }
194 const std::byte* data() const {
195 return inner_.has_value() ? inner_->data() : nullptr;
196 }
197
198 size_t size() const { return inner_.has_value() ? inner_->size() : 0; }
199
200 std::byte& operator[](size_t index) { return data()[index]; }
201 std::byte operator[](size_t index) const { return data()[index]; }
202
203 Chunk& operator*() { return *inner_; }
204 const Chunk& operator*() const { return *inner_; }
205 Chunk* operator->() { return &(*inner_); }
206 const Chunk* operator->() const { return &(*inner_); }
207
208 // Iterators
209
210 std::byte* begin() { return data(); }
211 const std::byte* begin() const { return cbegin(); }
212 const std::byte* cbegin() const { return data(); }
213 std::byte* end() { return data() + size(); }
214 const std::byte* end() const { return cend(); }
215 const std::byte* cend() const { return data() + size(); }
216
217 // Mutators
218
220 void Release() { inner_.reset(); }
221
227 std::optional<Chunk> Take() && {
228 std::optional<Chunk> result = std::move(inner_);
229 inner_.reset();
230 return result;
231 }
232
233 private:
234 friend class Chunk;
235
236 std::optional<Chunk> inner_;
237};
238
240
241} // namespace pw::multibuf::v1_adapter
Definition: allocator.h:45
Definition: shared_ptr.h:68
Definition: chunk.h:50
bool ClaimPrefix(size_t bytes_to_claim)
bool Merge(OwnedChunk &next_chunk)
bool CanMerge(const Chunk &next_chunk) const
void Slice(size_t begin, size_t end)
constexpr Allocator * metadata_allocator() const
Definition: chunk.h:79
std::optional< OwnedChunk > TakePrefix(size_t bytes_to_take)
std::optional< OwnedChunk > TakeSuffix(size_t bytes_to_take)
void DiscardPrefix(size_t bytes_to_discard)
bool ClaimSuffix(size_t bytes_to_claim)
constexpr const SharedPtr< std::byte[]> & region() const
Definition: chunk.h:87
std::optional< Chunk > Take() &&
Definition: chunk.h:227
void Release()
Definition: chunk.h:220