1 // Copyright 2024 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
15 #include "pw_multibuf/from_span.h"
16
17 namespace pw::multibuf {
18 namespace {
19
20 /// A simple ``ChunkRegionTracker`` that calls ``deleter`` for destruction.
21 class SpanTracker final : public ChunkRegionTracker {
22 private:
23 class PrivateConstructorType {};
24 static constexpr PrivateConstructorType kPrivateConstructor{};
25
26 public:
New(Allocator & alloc,ByteSpan region,pw::Function<void (ByteSpan)> && deleter)27 static std::optional<OwnedChunk> New(Allocator& alloc,
28 ByteSpan region,
29 pw::Function<void(ByteSpan)>&& deleter) {
30 SpanTracker* tracker = alloc.New<SpanTracker>(
31 kPrivateConstructor, alloc, region, std::move(deleter));
32 if (tracker == nullptr) {
33 return std::nullopt;
34 }
35 std::optional<OwnedChunk> chunk = tracker->CreateFirstChunk();
36 if (!chunk.has_value()) {
37 // Delete without destroying the associated memory.
38 alloc.Delete(tracker);
39 return std::nullopt;
40 }
41 return chunk;
42 }
43
SpanTracker(PrivateConstructorType,pw::Allocator & alloc,ByteSpan region,pw::Function<void (ByteSpan)> && deleter)44 SpanTracker(PrivateConstructorType,
45 pw::Allocator& alloc,
46 ByteSpan region,
47 pw::Function<void(ByteSpan)>&& deleter)
48 : alloc_(alloc), region_(region), deleter_(std::move(deleter)) {}
49
50 ~SpanTracker() final = default;
51
52 // SpanTracker is not copyable nor movable.
53 SpanTracker(const SpanTracker&) = delete;
54 SpanTracker& operator=(const SpanTracker&) = delete;
55 SpanTracker(SpanTracker&&) = delete;
56 SpanTracker& operator=(SpanTracker&&) = delete;
57
58 private:
Destroy()59 void Destroy() final {
60 deleter_(region_);
61 alloc_.Delete(this);
62 }
63
Region() const64 ByteSpan Region() const final { return region_; }
65
AllocateChunkClass()66 void* AllocateChunkClass() final {
67 return alloc_.Allocate(allocator::Layout::Of<Chunk>());
68 }
69
DeallocateChunkClass(void * ptr)70 void DeallocateChunkClass(void* ptr) final { return alloc_.Deallocate(ptr); }
71
72 pw::Allocator& alloc_;
73 const ByteSpan region_;
74 pw::Function<void(ByteSpan)> deleter_;
75 };
76
77 } // namespace
78
FromSpan(pw::Allocator & metadata_allocator,ByteSpan region,pw::Function<void (ByteSpan)> && deleter)79 std::optional<MultiBuf> FromSpan(pw::Allocator& metadata_allocator,
80 ByteSpan region,
81 pw::Function<void(ByteSpan)>&& deleter) {
82 std::optional<OwnedChunk> chunk =
83 SpanTracker::New(metadata_allocator, region, std::move(deleter));
84 if (chunk == std::nullopt) {
85 return std::nullopt;
86 }
87 return MultiBuf::FromChunk(std::move(*chunk));
88 }
89
90 } // namespace pw::multibuf
91