Pigweed
Loading...
Searching...
No Matches
as_pmr_allocator.h
1// Copyright 2024 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#if __has_include(<memory_resource>)
17#include <memory_resource>
18namespace pw {
19namespace pmr = ::std::pmr;
20} // namespace pw
21
22#elif __has_include(<experimental/memory_resource>)
23#include <experimental/memory_resource>
24namespace pw {
25namespace pmr = ::std::experimental::pmr;
26} // namespace pw
27
28#else
29#error "<memory_resource> is required to use this header!"
30#endif // __has_include(<memory_resource>)
31
32#include "pw_status/status_with_size.h"
33
34namespace pw {
35
36// Forward declaration
37class Allocator;
38
39namespace allocator {
40
41// Forward declaration
42class AsPmrAllocator;
43
44namespace internal {
45
52class MemoryResource final : public pw::pmr::memory_resource {
53 public:
54 constexpr MemoryResource() = default;
55
56 Allocator& allocator() { return *allocator_; }
57
58 private:
59 friend class ::pw::allocator::AsPmrAllocator;
60 void set_allocator(Allocator& allocator) { allocator_ = &allocator; }
61
62 void* do_allocate(size_t bytes, size_t alignment) override;
63 void do_deallocate(void* p, size_t bytes, size_t alignment) override;
64 bool do_is_equal(
65 const pw::pmr::memory_resource& other) const noexcept override;
66
67 Allocator* allocator_ = nullptr;
68};
69
70} // namespace internal
71
79class AsPmrAllocator final : public pw::pmr::polymorphic_allocator<std::byte> {
80 public:
81 using Base = pw::pmr::polymorphic_allocator<std::byte>;
82
83 AsPmrAllocator(Allocator& allocator) : Base(&memory_resource_) {
84 memory_resource_.set_allocator(allocator);
85 }
86
87 Allocator& allocator() { return memory_resource_.allocator(); }
88
89 private:
90 internal::MemoryResource memory_resource_;
91};
92
93} // namespace allocator
94} // namespace pw
Definition: allocator.h:32
Definition: as_pmr_allocator.h:79
Definition: as_pmr_allocator.h:52
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27