1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "partition_alloc/internal_allocator.h"
6 
7 namespace partition_alloc::internal {
PA_COMPONENT_EXPORT(PARTITION_ALLOC)8 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
9 PartitionRoot& InternalAllocatorRoot() {
10   static internal::base::NoDestructor<PartitionRoot> allocator([]() {
11     // Disable features using the internal root to avoid reentrancy issue.
12     PartitionOptions opts;
13     opts.thread_cache = PartitionOptions::kDisabled;
14     opts.scheduler_loop_quarantine = PartitionOptions::kDisabled;
15     return opts;
16   }());
17 
18   return *allocator;
19 }
20 
21 // static
operator new(size_t count)22 void* InternalPartitionAllocated::operator new(size_t count) {
23   return InternalAllocatorRoot().Alloc<AllocFlags::kNoHooks>(count);
24 }
25 // static
operator new(size_t count,std::align_val_t alignment)26 void* InternalPartitionAllocated::operator new(size_t count,
27                                                std::align_val_t alignment) {
28   return InternalAllocatorRoot().AlignedAlloc<AllocFlags::kNoHooks>(
29       static_cast<size_t>(alignment), count);
30 }
31 // static
operator delete(void * ptr)32 void InternalPartitionAllocated::operator delete(void* ptr) {
33   InternalAllocatorRoot().Free<FreeFlags::kNoHooks>(ptr);
34 }
35 // static
operator delete(void * ptr,std::align_val_t)36 void InternalPartitionAllocated::operator delete(void* ptr, std::align_val_t) {
37   InternalAllocatorRoot().Free<FreeFlags::kNoHooks>(ptr);
38 }
39 
40 // A deleter for `std::unique_ptr<T>`.
operator ()(void * ptr) const41 void InternalPartitionDeleter::operator()(void* ptr) const {
42   InternalAllocatorRoot().Free<FreeFlags::kNoHooks>(ptr);
43 }
44 }  // namespace partition_alloc::internal
45