C/C++ API Reference
Loading...
Searching...
No Matches
header_chunk_region_tracker.h
1// Copyright 2023 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 <memory>
18#include <optional>
19
20#include "pw_allocator/allocator.h"
21#include "pw_bytes/span.h"
22#include "pw_multibuf/chunk.h"
23#include "pw_multibuf/config.h"
24
25namespace pw::multibuf {
26
28
34class PW_MULTIBUF_DEPRECATED HeaderChunkRegionTracker final
35 : public ChunkRegionTracker {
36 public:
44 static std::optional<OwnedChunk> AllocateRegionAsChunk(
45 allocator::Allocator& alloc, size_t size) {
46 HeaderChunkRegionTracker* tracker = AllocateRegion(alloc, size);
47 if (tracker == nullptr) {
48 return std::nullopt;
49 }
50 std::optional<OwnedChunk> chunk = tracker->CreateFirstChunk();
51 if (!chunk.has_value()) {
52 tracker->Destroy();
53 return std::nullopt;
54 }
55 return chunk;
56 }
57
67 size_t size) {
68 auto layout =
69 allocator::Layout::Of<HeaderChunkRegionTracker>().Extend(size);
70 void* ptr = alloc.Allocate(layout);
71 if (ptr == nullptr) {
72 return nullptr;
73 }
74 std::byte* data =
75 reinterpret_cast<std::byte*>(ptr) + sizeof(HeaderChunkRegionTracker);
76 return new (ptr) HeaderChunkRegionTracker(ByteSpan(data, size), alloc);
77 }
78
79 ByteSpan Region() const final { return region_; }
81
82 protected:
83 void Destroy() final {
84 std::byte* ptr = reinterpret_cast<std::byte*>(this);
85 auto alloc = alloc_;
86 std::destroy_at(this);
87 alloc->Deallocate(ptr);
88 }
89
90 void* AllocateChunkClass() final {
91 return alloc_->Allocate(allocator::Layout::Of<Chunk>());
92 }
93
94 void DeallocateChunkClass(void* ptr) final { alloc_->Deallocate(ptr); }
95
96 private:
97 ByteSpan region_;
99
100 // NOTE: `region` must directly follow this `FakeChunkRegionTracker`
101 // in memory allocated by allocated by `alloc`.
103 : region_(region), alloc_(&alloc) {}
104};
105
107
108} // namespace pw::multibuf
Definition: allocator.h:36
void * Allocate(Layout layout)
Definition: allocator.h:44
Definition: chunk.h:274
std::optional< OwnedChunk > CreateFirstChunk()
Definition: header_chunk_region_tracker.h:35
static std::optional< OwnedChunk > AllocateRegionAsChunk(allocator::Allocator &alloc, size_t size)
Definition: header_chunk_region_tracker.h:44
void * AllocateChunkClass() final
Definition: header_chunk_region_tracker.h:90
ByteSpan Region() const final
Definition: header_chunk_region_tracker.h:79
void DeallocateChunkClass(void *ptr) final
Deallocates a pointer returned by AllocateChunkClass.
Definition: header_chunk_region_tracker.h:94
void Destroy() final
Definition: header_chunk_region_tracker.h:83
static HeaderChunkRegionTracker * AllocateRegion(allocator::Allocator &alloc, size_t size)
Definition: header_chunk_region_tracker.h:66