Pigweed
C/C++ API Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
32class PW_MULTIBUF_DEPRECATED HeaderChunkRegionTracker final
33 : public ChunkRegionTracker {
34 public:
42 static std::optional<OwnedChunk> AllocateRegionAsChunk(
43 allocator::Allocator& alloc, size_t size) {
44 HeaderChunkRegionTracker* tracker = AllocateRegion(alloc, size);
45 if (tracker == nullptr) {
46 return std::nullopt;
47 }
48 std::optional<OwnedChunk> chunk = tracker->CreateFirstChunk();
49 if (!chunk.has_value()) {
50 tracker->Destroy();
51 return std::nullopt;
52 }
53 return chunk;
54 }
55
65 size_t size) {
66 auto layout =
67 allocator::Layout::Of<HeaderChunkRegionTracker>().Extend(size);
68 void* ptr = alloc.Allocate(layout);
69 if (ptr == nullptr) {
70 return nullptr;
71 }
72 std::byte* data =
73 reinterpret_cast<std::byte*>(ptr) + sizeof(HeaderChunkRegionTracker);
74 return new (ptr) HeaderChunkRegionTracker(ByteSpan(data, size), alloc);
75 }
76
77 ByteSpan Region() const final { return region_; }
79
80 protected:
81 void Destroy() final {
82 std::byte* ptr = reinterpret_cast<std::byte*>(this);
83 auto alloc = alloc_;
84 std::destroy_at(this);
85 alloc->Deallocate(ptr);
86 }
87
88 void* AllocateChunkClass() final {
89 return alloc_->Allocate(allocator::Layout::Of<Chunk>());
90 }
91
92 void DeallocateChunkClass(void* ptr) final { alloc_->Deallocate(ptr); }
93
94 private:
95 ByteSpan region_;
97
98 // NOTE: `region` must directly follow this `FakeChunkRegionTracker`
99 // in memory allocated by allocated by `alloc`.
100 HeaderChunkRegionTracker(ByteSpan region, allocator::Allocator& alloc)
101 : region_(region), alloc_(&alloc) {}
102};
103
104} // namespace pw::multibuf
Definition: allocator.h:34
void * Allocate(Layout layout)
Definition: allocator.h:42
Definition: chunk.h:272
std::optional< OwnedChunk > CreateFirstChunk()
Definition: header_chunk_region_tracker.h:33
static std::optional< OwnedChunk > AllocateRegionAsChunk(allocator::Allocator &alloc, size_t size)
Definition: header_chunk_region_tracker.h:42
void * AllocateChunkClass() final
Definition: header_chunk_region_tracker.h:88
ByteSpan Region() const final
Definition: header_chunk_region_tracker.h:77
void DeallocateChunkClass(void *ptr) final
Deallocates a pointer returned by AllocateChunkClass.
Definition: header_chunk_region_tracker.h:92
void Destroy() final
Definition: header_chunk_region_tracker.h:81
static HeaderChunkRegionTracker * AllocateRegion(allocator::Allocator &alloc, size_t size)
Definition: header_chunk_region_tracker.h:64