• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 BASE_ALLOCATOR_PARTITION_ALLOCATOR_PAGE_ALLOCATOR_CONSTANTS_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PAGE_ALLOCATOR_CONSTANTS_H_
7 
8 #include <stddef.h>
9 
10 #include "base/allocator/partition_allocator/partition_alloc_base/compiler_specific.h"
11 #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
12 #include "build/build_config.h"
13 
14 #if BUILDFLAG(IS_APPLE) && defined(ARCH_CPU_64_BITS)
15 
16 #include <mach/vm_page_size.h>
17 
18 // Although page allocator constants are not constexpr, they are run-time
19 // constant. Because the underlying variables they access, such as vm_page_size,
20 // are not marked const, the compiler normally has no way to know that they
21 // don’t change and must obtain their values whenever it can't prove that they
22 // haven't been modified, even if they had already been obtained previously.
23 // Attaching __attribute__((const)) to these declarations allows these redundant
24 // accesses to be omitted under optimization such as common subexpression
25 // elimination.
26 #define PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR __attribute__((const))
27 
28 #elif BUILDFLAG(IS_LINUX) && defined(ARCH_CPU_ARM64)
29 // This should work for all POSIX (if needed), but currently all other
30 // supported OS/architecture combinations use either hard-coded values
31 // (such as x86) or have means to determine these values without needing
32 // atomics (such as macOS on arm64).
33 
34 // Page allocator constants are run-time constant
35 #define PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR __attribute__((const))
36 
37 #include <unistd.h>
38 #include <atomic>
39 
40 namespace partition_alloc::internal {
41 
42 // Holds the current page size and shift, where size = 1 << shift
43 // Use PageAllocationGranularity(), PageAllocationGranularityShift()
44 // to initialize and retrieve these values safely.
45 struct PageCharacteristics {
46   std::atomic<size_t> size;
47   std::atomic<size_t> shift;
48 };
49 PA_COMPONENT_EXPORT(PARTITION_ALLOC)
50 extern PageCharacteristics page_characteristics;
51 
52 }  // namespace partition_alloc::internal
53 
54 #else
55 
56 // When defined, page size constants are fixed at compile time. When not
57 // defined, they may vary at run time.
58 #define PAGE_ALLOCATOR_CONSTANTS_ARE_CONSTEXPR 1
59 
60 // Use this macro to declare a function as constexpr or not based on whether
61 // PAGE_ALLOCATOR_CONSTANTS_ARE_CONSTEXPR is defined.
62 #define PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR constexpr
63 
64 #endif
65 
66 namespace partition_alloc::internal {
67 
68 // Forward declaration, implementation below
69 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
70 PageAllocationGranularity();
71 
72 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
PageAllocationGranularityShift()73 PageAllocationGranularityShift() {
74 #if BUILDFLAG(IS_WIN) || defined(ARCH_CPU_PPC64)
75   // Modern ppc64 systems support 4kB (shift = 12) and 64kB (shift = 16) page
76   // sizes.  Since 64kB is the de facto standard on the platform and binaries
77   // compiled for 64kB are likely to work on 4kB systems, 64kB is a good choice
78   // here.
79   return 16;  // 64kB
80 #elif defined(_MIPS_ARCH_LOONGSON) || defined(ARCH_CPU_LOONG64)
81   return 14;  // 16kB
82 #elif BUILDFLAG(IS_APPLE) && defined(ARCH_CPU_64_BITS)
83   return static_cast<size_t>(vm_page_shift);
84 #elif BUILDFLAG(IS_LINUX) && defined(ARCH_CPU_ARM64)
85   // arm64 supports 4kb (shift = 12), 16kb (shift = 14), and 64kb (shift = 16)
86   // page sizes. Retrieve from or initialize cache.
87   size_t shift = page_characteristics.shift.load(std::memory_order_relaxed);
88   if (PA_UNLIKELY(shift == 0)) {
89     shift = static_cast<size_t>(
90         __builtin_ctz((unsigned int)PageAllocationGranularity()));
91     page_characteristics.shift.store(shift, std::memory_order_relaxed);
92   }
93   return shift;
94 #else
95   return 12;  // 4kB
96 #endif
97 }
98 
99 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
PageAllocationGranularity()100 PageAllocationGranularity() {
101 #if BUILDFLAG(IS_APPLE) && defined(ARCH_CPU_64_BITS)
102   // This is literally equivalent to |1 << PageAllocationGranularityShift()|
103   // below, but was separated out for IS_APPLE to avoid << on a non-constexpr.
104   return vm_page_size;
105 #elif BUILDFLAG(IS_LINUX) && defined(ARCH_CPU_ARM64)
106   // arm64 supports 4kb, 16kb, and 64kb page sizes. Retrieve from or
107   // initialize cache.
108   size_t size = page_characteristics.size.load(std::memory_order_relaxed);
109   if (PA_UNLIKELY(size == 0)) {
110     size = static_cast<size_t>(getpagesize());
111     page_characteristics.size.store(size, std::memory_order_relaxed);
112   }
113   return size;
114 #else
115   return 1 << PageAllocationGranularityShift();
116 #endif
117 }
118 
119 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
PageAllocationGranularityOffsetMask()120 PageAllocationGranularityOffsetMask() {
121   return PageAllocationGranularity() - 1;
122 }
123 
124 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
PageAllocationGranularityBaseMask()125 PageAllocationGranularityBaseMask() {
126   return ~PageAllocationGranularityOffsetMask();
127 }
128 
129 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
SystemPageShift()130 SystemPageShift() {
131   // On Windows allocation granularity is higher than the page size. This comes
132   // into play when reserving address space range (allocation granularity),
133   // compared to committing pages into memory (system page granularity).
134 #if BUILDFLAG(IS_WIN)
135   return 12;  // 4096=1<<12
136 #else
137   return PageAllocationGranularityShift();
138 #endif
139 }
140 
141 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
SystemPageSize()142 SystemPageSize() {
143 #if (BUILDFLAG(IS_APPLE) && defined(ARCH_CPU_64_BITS)) || \
144     (BUILDFLAG(IS_LINUX) && defined(ARCH_CPU_ARM64))
145   // This is literally equivalent to |1 << SystemPageShift()| below, but was
146   // separated out for 64-bit IS_APPLE and arm64 on Linux to avoid << on a
147   // non-constexpr.
148   return PageAllocationGranularity();
149 #else
150   return 1 << SystemPageShift();
151 #endif
152 }
153 
154 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
SystemPageOffsetMask()155 SystemPageOffsetMask() {
156   return SystemPageSize() - 1;
157 }
158 
159 PA_ALWAYS_INLINE PAGE_ALLOCATOR_CONSTANTS_DECLARE_CONSTEXPR size_t
SystemPageBaseMask()160 SystemPageBaseMask() {
161   return ~SystemPageOffsetMask();
162 }
163 
164 constexpr size_t kPageMetadataShift = 5;  // 32 bytes per partition page.
165 constexpr size_t kPageMetadataSize = 1 << kPageMetadataShift;
166 
167 }  // namespace partition_alloc::internal
168 
169 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PAGE_ALLOCATOR_CONSTANTS_H_
170