C/C++ API Reference
Loading...
Searching...
No Matches
single_chunk_region_tracker.h
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 <array>
17#include <atomic>
18#include <cstring>
19#include <optional>
20
21#include "pw_assert/assert.h"
22#include "pw_bytes/span.h"
23#include "pw_multibuf/config.h"
24#include "pw_multibuf/v1/chunk.h"
25
26namespace pw::multibuf::v1 {
27
29
33class PW_MULTIBUF_DEPRECATED SingleChunkRegionTracker
34 : public ChunkRegionTracker {
35 public:
40
43 explicit SingleChunkRegionTracker(ByteSpan region) : region_(region) {}
44
45 ~SingleChunkRegionTracker() override { Destroy(); }
46
50 void SetRegion(ByteSpan region) {
51 PW_DASSERT(region_.empty());
52 region_ = region;
53 }
54
61 std::optional<OwnedChunk> GetChunk(size_t size) {
62 PW_DASSERT(size <= region_.size());
63 // Since this is a single `Chunk` region, re-create the first `Chunk` is
64 // allowed if freed.
65 std::optional<OwnedChunk> chunk = CreateFirstChunk();
66 if (chunk.has_value() && size < region_.size()) {
67 (*chunk)->Truncate(size);
68 }
69 return chunk;
70 }
71
72 void Destroy() override {
73 // Nothing to release here.
74 PW_ASSERT(!chunk_in_use_);
75 }
76
77 ByteSpan Region() const override { return region_; }
78
79 void* AllocateChunkClass() override {
80 bool in_use = false;
81 if (!chunk_in_use_.compare_exchange_strong(in_use, true)) {
82 return nullptr;
83 }
84 return &chunk_storage_;
85 }
86
87 void DeallocateChunkClass(void* chunk) override {
88 PW_ASSERT(chunk == chunk_storage_.data());
89 chunk_in_use_ = false;
90 }
91
92 private:
93 ByteSpan region_;
94 std::atomic<bool> chunk_in_use_ = false;
95 alignas(Chunk) std::array<std::byte, sizeof(Chunk)> chunk_storage_;
96};
97
99
100} // namespace pw::multibuf::v1
Definition: chunk.h:50
Definition: single_chunk_region_tracker.h:34
ByteSpan Region() const override
Definition: single_chunk_region_tracker.h:77
std::optional< OwnedChunk > GetChunk(size_t size)
Definition: single_chunk_region_tracker.h:61
void Destroy() override
Definition: single_chunk_region_tracker.h:72
void * AllocateChunkClass() override
Definition: single_chunk_region_tracker.h:79
void SetRegion(ByteSpan region)
Definition: single_chunk_region_tracker.h:50
SingleChunkRegionTracker(ByteSpan region)
Definition: single_chunk_region_tracker.h:43
void DeallocateChunkClass(void *chunk) override
Deallocates a pointer returned by AllocateChunkClass.
Definition: single_chunk_region_tracker.h:87