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
29
38 public:
40 Allocator& metadata_allocator,
41 size_t alignment = 1)
42 : chunk_allocator_(region, metadata_allocator, alignment) {}
43
52 [[nodiscard]] bool TryReserveRegions(size_t num_regions)
53 PW_LOCKS_EXCLUDED(mutex_);
54
55 protected:
56 void Reset() PW_LOCKS_EXCLUDED(mutex_);
57
58 private:
60 class SimpleChunkAllocator : public internal::ChunkAllocator {
61 public:
62 SimpleChunkAllocator(ByteSpan region,
63 Allocator& metadata_allocator,
64 size_t alignment);
65
66 ~SimpleChunkAllocator() override;
67
72 [[nodiscard]] bool Init();
73
76 size_t CountChunks(size_t desired_size);
77
79 [[nodiscard]] bool TryReserveRegions(size_t num_regions);
80
82 void Reset();
83
84 private:
85 using iterator = DynamicVector<Region>::iterator;
86
88 Region* AllocateRegion(size_t min_size, size_t desired_size) override;
89
91 size_t TryDeallocateRegion(void* ptr) override;
92
95 std::tuple<iterator, iterator> Split(iterator iter, size_t offset);
96
99 iterator Merge(iterator left, iterator right);
100
101 size_t available_ = 0;
102
107 DynamicVector<Region> subregions_;
108 };
109
111 std::optional<MultiBuf> DoAllocate(size_t min_size,
112 size_t desired_size,
113 bool contiguous) override
114 PW_LOCKS_EXCLUDED(mutex_);
115
117 std::optional<size_t> DoGetBackingCapacity() override
118 PW_LOCKS_EXCLUDED(mutex_);
119
120 pw::sync::Mutex mutex_;
121
122 SimpleChunkAllocator chunk_allocator_ PW_GUARDED_BY(mutex_);
123};
124
126
127} // namespace pw::multibuf::v1_adapter
Definition: allocator.h:42
Definition: dynamic_vector.h:63
Definition: ptr_iterator.h:142
Definition: simple_allocator.h:37
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