C/C++ API Reference
Loading...
Searching...
No Matches
simple_allocator.h
1// Copyright 2025 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 <optional>
18
19#include "pw_bytes/span.h"
20#include "pw_multibuf/v1_adapter/allocator.h"
21#include "pw_multibuf/v1_adapter/internal/chunk_allocator.h"
22#include "pw_multibuf/v1_adapter/multibuf.h"
23#include "pw_sync/lock_annotations.h"
24#include "pw_sync/mutex.h"
25
26namespace pw::multibuf::v1_adapter {
27
36 public:
38 Allocator& metadata_allocator,
39 size_t alignment = 1)
40 : chunk_allocator_(region, metadata_allocator, alignment) {}
41
50 [[nodiscard]] bool TryReserveRegions(size_t num_regions)
51 PW_LOCKS_EXCLUDED(mutex_);
52
53 protected:
54 void Reset() PW_LOCKS_EXCLUDED(mutex_);
55
56 private:
58 class SimpleChunkAllocator : public internal::ChunkAllocator {
59 public:
60 SimpleChunkAllocator(ByteSpan region,
61 Allocator& metadata_allocator,
62 size_t alignment);
63
64 ~SimpleChunkAllocator() override;
65
70 [[nodiscard]] bool Init();
71
74 size_t CountChunks(size_t desired_size);
75
77 [[nodiscard]] bool TryReserveRegions(size_t num_regions);
78
80 void Reset();
81
82 private:
83 using iterator = DynamicVector<Region>::iterator;
84
86 Region* AllocateRegion(size_t min_size, size_t desired_size) override;
87
89 size_t TryDeallocateRegion(void* ptr) override;
90
93 std::tuple<iterator, iterator> Split(iterator iter, size_t offset);
94
97 iterator Merge(iterator left, iterator right);
98
99 size_t available_ = 0;
100
105 DynamicVector<Region> subregions_;
106 };
107
109 std::optional<MultiBuf> DoAllocate(size_t min_size,
110 size_t desired_size,
111 bool contiguous) override
112 PW_LOCKS_EXCLUDED(mutex_);
113
115 std::optional<size_t> DoGetBackingCapacity() override
116 PW_LOCKS_EXCLUDED(mutex_);
117
118 pw::sync::Mutex mutex_;
119
120 SimpleChunkAllocator chunk_allocator_ PW_GUARDED_BY(mutex_);
121};
122
124
125} // namespace pw::multibuf::v1_adapter
Definition: allocator.h:42
Definition: dynamic_vector.h:63
Definition: ptr_iterator.h:142
Definition: simple_allocator.h:35
std::optional< MultiBuf > DoAllocate(size_t min_size, size_t desired_size, bool contiguous) override
std::optional< size_t > DoGetBackingCapacity() override
bool TryReserveRegions(size_t num_regions)
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60
#define PW_LOCKS_EXCLUDED(...)
Definition: lock_annotations.h:178
The Pigweed namespace.
Definition: alignment.h:27