• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
15 
16 #include <atomic>
17 #include <cstring>
18 #include <optional>
19 
20 #include "pw_assert/assert.h"
21 #include "pw_bytes/span.h"
22 #include "pw_multibuf/chunk.h"
23 
24 namespace pw::multibuf {
25 
26 /// A `ChunkRegionTracker` that uses inline memory to create a single `Chunk`
27 /// with the only caveat that the provided `Chunk` cannot be split. All attempts
28 /// will result in `std::nullopt`.
29 class SingleChunkRegionTracker : public ChunkRegionTracker {
30  public:
31   /// Constructs a region tracker with a single `Chunk` that maps to `region`,
32   /// which must outlive this tracker and any `OwnedChunk` it creates.
SingleChunkRegionTracker(ByteSpan region)33   explicit SingleChunkRegionTracker(ByteSpan region) : region_(region) {}
~SingleChunkRegionTracker()34   ~SingleChunkRegionTracker() override { Destroy(); }
35 
36   /// Gets a `Chunk` of a given size, which must be less than or equal to the
37   /// provided region.
38   ///
39   /// Returns:
40   ///   An `OwnedChunk` if the `Chunk` is free, otherwise `std::nullopt`, in
41   /// which case `GetChunk()` can be called again.
GetChunk(size_t size)42   std::optional<OwnedChunk> GetChunk(size_t size) {
43     PW_DASSERT(size <= region_.size());
44     // Since this is a single `Chunk` region, re-create the first `Chunk` is
45     // allowed if freed.
46     std::optional<OwnedChunk> chunk = CreateFirstChunk();
47     if (chunk.has_value() && size < region_.size()) {
48       (*chunk)->Truncate(size);
49     }
50     return chunk;
51   }
52 
Destroy()53   void Destroy() final {
54     // Nothing to release here.
55     PW_ASSERT(!chunk_in_use_);
56   }
57 
Region()58   ByteSpan Region() const final { return region_; }
59 
AllocateChunkClass()60   void* AllocateChunkClass() final {
61     bool in_use = false;
62     if (!chunk_in_use_.compare_exchange_strong(in_use, true)) {
63       return nullptr;
64     }
65     return &chunk_storage_;
66   }
67 
DeallocateChunkClass(void * chunk)68   void DeallocateChunkClass(void* chunk) final {
69     PW_ASSERT(chunk == chunk_storage_.data());
70     // Mark the `Chunk` as not in use and zero-out the region and chunk storage.
71     std::memset(chunk_storage_.data(), 0, chunk_storage_.size());
72     std::memset(region_.data(), 0, region_.size());
73     chunk_in_use_ = false;
74   }
75 
76  private:
77   ByteSpan region_;
78   std::atomic<bool> chunk_in_use_ = false;
79   alignas(Chunk) std::array<std::byte, sizeof(Chunk)> chunk_storage_;
80 };
81 
82 }  // namespace pw::multibuf
83