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 private:
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 void DoDeallocate(void* ptr, Layout) override { DoDeallocate(ptr); }
81
83 bool DoResize(void* ptr, size_t new_size) override {
84 std::lock_guard lock(lock_);
85 return allocator_.Resize(ptr, new_size);
86 }
87
88 void* DoReallocate(void* ptr, Layout new_layout) override {
89 std::lock_guard lock(lock_);
90 return allocator_.Reallocate(ptr, new_layout);
91 }
92
94 size_t DoGetAllocated() const override {
95 std::lock_guard lock(lock_);
96 return allocator_.GetAllocated();
97 }
98
100 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
101 std::lock_guard lock(lock_);
102 return GetInfo(allocator_, info_type, ptr);
103 }
104
105 Allocator& allocator_ PW_GUARDED_BY(lock_);
106 mutable LockType lock_;
108};
109
115
117
118} // namespace pw::allocator
Definition: allocator.h:36
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:287
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:321
void * Allocate(Layout layout)
Definition: allocator.h:44
size_t GetAllocated() const
Definition: allocator.h:346
Definition: poll.h:25
Definition: layout.h:58
Definition: synchronized_allocator.h:38
void DoDeallocate(void *ptr, Layout) override
Definition: synchronized_allocator.h:80
size_t DoGetAllocated() const override
Definition: synchronized_allocator.h:94
Result< Layout > DoGetInfo(InfoType info_type, const void *ptr) const override
Definition: synchronized_allocator.h:100
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:83
void * DoAllocate(Layout layout) override
Definition: synchronized_allocator.h:68
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: synchronized_allocator.h:88
Definition: borrow.h:164
Definition: borrow.h:33
Definition: no_lock.h:27
void Deallocate(void *ptr)
Definition: deallocator.h:60
#define PW_GUARDED_BY(x)
Definition: lock_annotations.h:60