1 // Copyright 2020 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 #ifndef PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_DEFAULT_DISPATCH_TO_PARTITION_ALLOC_H_
6 #define PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_DEFAULT_DISPATCH_TO_PARTITION_ALLOC_H_
7
8 #include "partition_alloc/partition_alloc_buildflags.h"
9
10 #if BUILDFLAG(USE_ALLOCATOR_SHIM)
11 #include "partition_alloc/partition_alloc.h"
12 #include "partition_alloc/partition_alloc_base/component_export.h"
13 #include "partition_alloc/shim/allocator_shim.h"
14
15 namespace allocator_shim {
16
17 struct AllocatorDispatch;
18
19 namespace internal {
20
PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)21 class PA_COMPONENT_EXPORT(ALLOCATOR_SHIM) PartitionAllocMalloc {
22 public:
23 // Returns true if ConfigurePartitions() has completed, meaning that the
24 // allocators are effectively set in stone.
25 static bool AllocatorConfigurationFinalized();
26
27 static partition_alloc::PartitionRoot* Allocator();
28 // May return |nullptr|, will never return the same pointer as |Allocator()|.
29 static partition_alloc::PartitionRoot* OriginalAllocator();
30 };
31
32 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
33 void* PartitionMalloc(const AllocatorDispatch*, size_t size, void* context);
34
35 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
36 void* PartitionMallocUnchecked(const AllocatorDispatch*,
37 size_t size,
38 void* context);
39
40 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
41 void* PartitionCalloc(const AllocatorDispatch*,
42 size_t n,
43 size_t size,
44 void* context);
45
46 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
47 void* PartitionMemalign(const AllocatorDispatch*,
48 size_t alignment,
49 size_t size,
50 void* context);
51
52 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
53 void* PartitionAlignedAlloc(const AllocatorDispatch* dispatch,
54 size_t size,
55 size_t alignment,
56 void* context);
57
58 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
59 void* PartitionAlignedRealloc(const AllocatorDispatch* dispatch,
60 void* address,
61 size_t size,
62 size_t alignment,
63 void* context);
64
65 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
66 void* PartitionRealloc(const AllocatorDispatch*,
67 void* address,
68 size_t size,
69 void* context);
70
71 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
72 void PartitionFree(const AllocatorDispatch*, void* object, void* context);
73
74 PA_COMPONENT_EXPORT(ALLOCATOR_SHIM)
75 size_t PartitionGetSizeEstimate(const AllocatorDispatch*,
76 void* address,
77 void* context);
78
79 } // namespace internal
80
81 #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
82 // Provide a ConfigurePartitions() helper, to mimic what Chromium uses. This way
83 // we're making it more resilient to ConfigurePartitions() interface changes, so
84 // that we don't have to modify multiple callers. This is particularly important
85 // when callers are in a different repo, like PDFium or Dawn.
ConfigurePartitionsForTesting()86 PA_ALWAYS_INLINE void ConfigurePartitionsForTesting() {
87 auto enable_brp = allocator_shim::EnableBrp(true);
88 auto enable_memory_tagging = allocator_shim::EnableMemoryTagging(false);
89 // Since the only user of this function is a test function, we use
90 // synchronous reporting mode, if MTE is enabled.
91 auto memory_tagging_reporting_mode =
92 enable_memory_tagging
93 ? partition_alloc::TagViolationReportingMode::kSynchronous
94 : partition_alloc::TagViolationReportingMode::kDisabled;
95 auto distribution = BucketDistribution::kNeutral;
96 auto scheduler_loop_quarantine = SchedulerLoopQuarantine(false);
97 size_t scheduler_loop_quarantine_capacity_in_bytes = 0;
98 auto zapping_by_free_flags = ZappingByFreeFlags(false);
99 auto use_pool_offset_freelists = UsePoolOffsetFreelists(true);
100
101 ConfigurePartitions(enable_brp, enable_memory_tagging,
102 memory_tagging_reporting_mode, distribution,
103 scheduler_loop_quarantine,
104 scheduler_loop_quarantine_capacity_in_bytes,
105 zapping_by_free_flags, use_pool_offset_freelists);
106 }
107 #endif // BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
108
109 } // namespace allocator_shim
110
111 #endif // BUILDFLAG(USE_ALLOCATOR_SHIM)
112
113 #endif // PARTITION_ALLOC_SHIM_ALLOCATOR_SHIM_DEFAULT_DISPATCH_TO_PARTITION_ALLOC_H_
114