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
18#include "pw_allocator/allocator.h"
19#include "pw_sync/borrow.h"
20
21namespace pw::allocator {
22
32template <typename LockType>
34 public:
35 constexpr SynchronizedAllocator(Allocator& allocator) noexcept
36 : Allocator(allocator.capabilities()), borrowable_(allocator, lock_) {}
37
38 private:
40
42 void* DoAllocate(Layout layout) override {
43 Pointer allocator = borrowable_.acquire();
44 return allocator->Allocate(layout);
45 }
46
48 void DoDeallocate(void* ptr) override {
49 Pointer allocator = borrowable_.acquire();
50 return allocator->Deallocate(ptr);
51 }
52
54 void DoDeallocate(void* ptr, Layout) override { DoDeallocate(ptr); }
55
57 bool DoResize(void* ptr, size_t new_size) override {
58 Pointer allocator = borrowable_.acquire();
59 return allocator->Resize(ptr, new_size);
60 }
61
62 void* DoReallocate(void* ptr, Layout new_layout) override {
63 Pointer allocator = borrowable_.acquire();
64 return allocator->Reallocate(ptr, new_layout);
65 }
66
68 Result<Layout> DoGetInfo(InfoType info_type, const void* ptr) const override {
69 Pointer allocator = borrowable_.acquire();
70 return GetInfo(*allocator, info_type, ptr);
71 }
72
73 LockType lock_;
75};
76
81struct NoSync {};
82
83} // namespace pw::allocator
Definition: allocator.h:32
constexpr Allocator()=default
TODO(b/326509341): Remove when downstream consumers migrate.
Definition: layout.h:39
Definition: synchronized_allocator.h:33
Definition: borrow.h:164
Definition: borrow.h:31
Definition: synchronized_allocator.h:81