C/C++ API Reference
Loading...
Searching...
No Matches
synchronized_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#include <cstddef>
17#include <mutex>
18
19#include "pw_allocator/allocator.h"
20#include "pw_sync/borrow.h"
21#include "pw_sync/lock_annotations.h"
22#include "pw_sync/no_lock.h"
23
24namespace pw::allocator {
25
27
37template <typename LockType>
39 private:
41
42 public:
43 SynchronizedAllocator(Allocator& allocator) noexcept
44 : Allocator(allocator.capabilities()),
45 allocator_(allocator),
46 borrowable_(allocator_, lock_) {}
47
64 Pointer Borrow() const { return borrowable_.acquire(); }
65
66 protected:
68 void* DoAllocate(Layout layout) override {
69 std::lock_guard lock(lock_);
70 return allocator_.Allocate(layout);
71 }
72
74 void DoDeallocate(void* ptr) override {
75 std::lock_guard lock(lock_);
76 return allocator_.Deallocate(ptr);
77 }
78
80 bool DoResize(void* ptr, size_t new_size) override {
81 std::lock_guard lock(lock_);
82 return allocator_.Resize(ptr, new_size);
83 }
84
85 void* DoReallocate(void* ptr, Layout new_layout) override {
86 std::lock_guard lock(lock_);
87 return allocator_.Reallocate(ptr, new_layout);
88 }
89
91 size_t DoGetAllocated() const override {
92 std::lock_guard lock(lock_);
93 return allocator_.GetAllocated();
94 }
95
97 std::optional<Fragmentation> DoMeasureFragmentation() const override {
98 std::lock_guard lock(lock_);
99 return allocator_.MeasureFragmentation();
100 }
101
103 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
104 std::lock_guard lock(lock_);
105 return GetInfo(allocator_, info_type, ptr);
106 }
107
108 private:
109 Allocator& allocator_ PW_GUARDED_BY(lock_);
110 mutable LockType lock_;
112};
113
119
121
122} // namespace pw::allocator
Definition: allocator.h:42
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
bool Resize(void *ptr, size_t new_size)
void * Reallocate(void *ptr, Layout new_layout)
void * Allocate(Layout layout)
size_t GetAllocated() const
Definition: allocator.h:277
std::optional< Fragmentation > MeasureFragmentation() const
Returns fragmentation information for the allocator's memory region.
Definition: allocator.h:280
Definition: result.h:145
Definition: layout.h:64
Definition: synchronized_allocator.h:38
size_t DoGetAllocated() const override
Definition: synchronized_allocator.h:91
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: synchronized_allocator.h:103
Pointer Borrow() const
Definition: synchronized_allocator.h:64
void DoDeallocate(void *ptr) override
Definition: synchronized_allocator.h:74
bool DoResize(void *ptr, size_t new_size) override
Definition: synchronized_allocator.h:80
void * DoAllocate(Layout layout) override
Definition: synchronized_allocator.h:68
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: synchronized_allocator.h:85
std::optional< Fragmentation > DoMeasureFragmentation() const override
Returns fragmentation information for the allocator's memory region.
Definition: synchronized_allocator.h:97
Definition: borrow.h:164
Definition: borrow.h:33
Definition: no_lock.h:27
void Deallocate(void *ptr)
Definition: deallocator.h:314
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60