C/C++ API Reference
Loading...
Searching...
No Matches
chunk_allocator.h
1// Copyright 2026 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 <cstdint>
18#include <tuple>
19
20#include "pw_allocator/allocator.h"
21#include "pw_allocator/capability.h"
22#include "pw_allocator/internal/control_block.h"
23#include "pw_allocator/layout.h"
24#include "pw_assert/assert.h"
25#include "pw_bytes/span.h"
26#include "pw_containers/dynamic_vector.h"
27#include "pw_containers/storage.h"
28#include "pw_multibuf/v1_adapter/chunk.h"
29#include "pw_preprocessor/compiler.h"
30#include "pw_result/result.h"
31
32namespace pw::multibuf::v1_adapter::internal {
33
35
46class ChunkAllocator : public Allocator {
47 public:
48 ~ChunkAllocator() override = default;
49
50 constexpr Allocator& metadata_allocator() const {
51 return metadata_allocator_;
52 }
53
54 constexpr size_t alignment() const { return 1u << alignment_log2_; }
55
56 constexpr size_t available() const { return available_; }
57
58 constexpr ByteSpan buffer() const { return buffer_; }
59
62 std::optional<OwnedChunk> AllocateChunk(size_t min_size, size_t desired_size);
63
66 std::optional<OwnedChunk> AllocateChunk(size_t size) {
67 return AllocateChunk(size, size);
68 }
69
70 protected:
71 using ControlBlock = allocator::internal::ControlBlock;
72
73 // A compact representation of a span that may be allocated or free.
74 PW_PACKED(struct) Region {
75 std::byte* data;
76 uint32_t size : 31;
77 uint32_t free : 1;
78
79 constexpr Region() : data(nullptr), size(0), free(true) {}
80 };
81
82 ChunkAllocator(Allocator& metadata_allocator, size_t alignment);
83
84 constexpr size_t num_allocations() const {
85 return static_cast<size_t>(num_allocations_);
86 }
87
91 void SetRegion(ByteSpan region);
92
93 protected:
95 void* DoAllocate(allocator::Layout layout) override;
96
98 void DoDeallocate(void* ptr) override;
99
101 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override;
102
105 virtual Region* AllocateRegion(size_t min_size, size_t desired_size) = 0;
106
110 virtual size_t TryDeallocateRegion(void* ptr) = 0;
111
112 private:
115 Allocator& metadata_allocator_;
116
118 ByteSpan buffer_;
119
121 uint32_t available_;
122
124 uint16_t num_allocations_ = 0;
125
128 uint8_t alignment_log2_;
129};
130
136 public:
137 explicit SingleChunkAllocator(Allocator& metadata_allocator);
138
140
141 protected:
142 constexpr bool control_block_free() const { return control_block_free_; }
143
145 void DeallocateControlBlock(void* ptr);
146
148 void* DoAllocate(Layout layout) override;
149
151 void DoDeallocate(void* ptr) override;
152
154 Region* AllocateRegion(size_t min_size, size_t desired_size) override;
155
157 size_t TryDeallocateRegion(void* ptr) override;
158
160 virtual void Destroy() {}
161
162 private:
164 Region region_;
165
167 bool control_block_free_ = true;
168
171};
172
174
175} // namespace pw::multibuf::v1_adapter::internal
Definition: allocator.h:42
InfoType
Definition: deallocator.h:173
Definition: result.h:145
Definition: layout.h:64
Definition: storage.h:39
void * DoAllocate(allocator::Layout layout) override
std::optional< OwnedChunk > AllocateChunk(size_t size)
Definition: chunk_allocator.h:66
std::optional< OwnedChunk > AllocateChunk(size_t min_size, size_t desired_size)
virtual Region * AllocateRegion(size_t min_size, size_t desired_size)=0
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
virtual void Destroy()
Definition: chunk_allocator.h:160
void DeallocateControlBlock(void *ptr)
Release the chunk shared pointer metadata.
Region * AllocateRegion(size_t min_size, size_t desired_size) override
#define PW_PACKED(declaration)
Definition: compiler.h:54