Pigweed
 
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
23namespace pw::allocator {
24
34template <typename LockType>
36 private:
38
39 public:
40 SynchronizedAllocator(Allocator& allocator) noexcept
41 : Allocator(allocator.capabilities()),
42 allocator_(allocator),
43 borrowable_(allocator_, lock_) {}
44
61 Pointer Borrow() const { return borrowable_.acquire(); }
62
63 private:
65 void* DoAllocate(Layout layout) override {
66 std::lock_guard lock(lock_);
67 return allocator_.Allocate(layout);
68 }
69
71 void DoDeallocate(void* ptr) override {
72 std::lock_guard lock(lock_);
73 return allocator_.Deallocate(ptr);
74 }
75
77 void DoDeallocate(void* ptr, Layout) override { DoDeallocate(ptr); }
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 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
98 std::lock_guard lock(lock_);
99 return GetInfo(allocator_, info_type, ptr);
100 }
101
102 Allocator& allocator_ PW_GUARDED_BY(lock_);
103 mutable LockType lock_;
105};
106
111struct NoSync {
112 constexpr void lock() {}
113 constexpr void unlock() {}
114};
115
116} // namespace pw::allocator
Definition: allocator.h:34
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
bool Resize(void *ptr, size_t new_size)
Definition: allocator.h:261
void * Reallocate(void *ptr, Layout new_layout)
Definition: allocator.h:295
void * Allocate(Layout layout)
Definition: allocator.h:45
size_t GetAllocated() const
Definition: allocator.h:320
void Deallocate(void *ptr)
Definition: deallocator.h:57
Definition: layout.h:56
Definition: synchronized_allocator.h:35
void DoDeallocate(void *ptr, Layout) override
Definition: synchronized_allocator.h:77
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:97
Pointer Borrow() const
Definition: synchronized_allocator.h:61
void DoDeallocate(void *ptr) override
Definition: synchronized_allocator.h:71
bool DoResize(void *ptr, size_t new_size) override
Definition: synchronized_allocator.h:80
void * DoAllocate(Layout layout) override
Definition: synchronized_allocator.h:65
void * DoReallocate(void *ptr, Layout new_layout) override
Definition: synchronized_allocator.h:85
Definition: borrow.h:164
Definition: borrow.h:31
Definition: synchronized_allocator.h:111