1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_LIBARTBASE_BASE_GLOBALS_H_
18 #define ART_LIBARTBASE_BASE_GLOBALS_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 
23 #include "base/macros.h"
24 
25 namespace art {
26 
27 static constexpr size_t KB = 1024;
28 static constexpr size_t MB = KB * KB;
29 static constexpr size_t GB = KB * KB * KB;
30 
31 // Runtime sizes.
32 static constexpr size_t kBitsPerByte = 8;
33 static constexpr size_t kBitsPerByteLog2 = 3;
34 static constexpr int kBitsPerIntPtrT = sizeof(intptr_t) * kBitsPerByte;
35 
36 // Required stack alignment
37 static constexpr size_t kStackAlignment = 16;
38 
39 // Minimum supported page size.
40 static constexpr size_t kMinPageSize = 4096;
41 
42 #if defined(ART_PAGE_SIZE_AGNOSTIC)
43 static constexpr bool kPageSizeAgnostic = true;
44 // Maximum supported page size.
45 static constexpr size_t kMaxPageSize = 16384;
46 #else
47 static constexpr bool kPageSizeAgnostic = false;
48 // Maximum supported page size.
49 static constexpr size_t kMaxPageSize = kMinPageSize;
50 #endif
51 
52 // Targets can have different page size (eg. 4kB or 16kB). Because Art can crosscompile, it needs
53 // to be able to generate OAT (ELF) and other image files with alignment other than the host page
54 // size. kElfSegmentAlignment needs to be equal to the largest page size supported. Effectively,
55 // this is the value to be used in images files for aligning contents to page size.
56 static constexpr size_t kElfSegmentAlignment = kMaxPageSize;
57 
58 // Clion, clang analyzer, etc can falsely believe that "if (kIsDebugBuild)" always
59 // returns the same value. By wrapping into a call to another constexpr function, we force it
60 // to realize that is not actually always evaluating to the same value.
GlobalsReturnSelf(bool self)61 static constexpr bool GlobalsReturnSelf(bool self) { return self; }
62 
63 // Whether or not this is a debug build. Useful in conditionals where NDEBUG isn't.
64 // TODO: Use only __clang_analyzer__ here. b/64455231
65 #if defined(NDEBUG) && !defined(__CLION_IDE__)
66 static constexpr bool kIsDebugBuild = GlobalsReturnSelf(false);
67 #else
68 static constexpr bool kIsDebugBuild = GlobalsReturnSelf(true);
69 #endif
70 
71 #if defined(ART_PGO_INSTRUMENTATION)
72 static constexpr bool kIsPGOInstrumentation = true;
73 #else
74 static constexpr bool kIsPGOInstrumentation = false;
75 #endif
76 
77 // ART_TARGET - Defined for target builds of ART.
78 // ART_TARGET_LINUX - Defined for target Linux builds of ART.
79 // ART_TARGET_ANDROID - Defined for target Android builds of ART.
80 // ART_TARGET_FUCHSIA - Defined for Fuchsia builds of ART.
81 // Note: Either ART_TARGET_LINUX, ART_TARGET_ANDROID or ART_TARGET_FUCHSIA
82 //       need to be set when ART_TARGET is set.
83 // Note: When ART_TARGET_LINUX is defined mem_map.h will not be using Ashmem for memory mappings
84 // (usually only available on Android kernels).
85 #if defined(ART_TARGET)
86 // Useful in conditionals where ART_TARGET isn't.
87 static constexpr bool kIsTargetBuild = true;
88 # if defined(ART_TARGET_LINUX)
89 static constexpr bool kIsTargetLinux = true;
90 static constexpr bool kIsTargetFuchsia = false;
91 static constexpr bool kIsTargetAndroid = false;
92 # elif defined(ART_TARGET_ANDROID)
93 static constexpr bool kIsTargetLinux = false;
94 static constexpr bool kIsTargetFuchsia = false;
95 static constexpr bool kIsTargetAndroid = true;
96 # elif defined(ART_TARGET_FUCHSIA)
97 static constexpr bool kIsTargetLinux = false;
98 static constexpr bool kIsTargetFuchsia = true;
99 static constexpr bool kIsTargetAndroid = false;
100 # else
101 # error "Either ART_TARGET_LINUX, ART_TARGET_ANDROID or ART_TARGET_FUCHSIA " \
102         "needs to be defined for target builds."
103 # endif
104 #else
105 static constexpr bool kIsTargetBuild = false;
106 # if defined(ART_TARGET_LINUX)
107 # error "ART_TARGET_LINUX defined for host build."
108 # elif defined(ART_TARGET_ANDROID)
109 # error "ART_TARGET_ANDROID defined for host build."
110 # elif defined(ART_TARGET_FUCHSIA)
111 # error "ART_TARGET_FUCHSIA defined for host build."
112 # else
113 static constexpr bool kIsTargetLinux = false;
114 static constexpr bool kIsTargetFuchsia = false;
115 static constexpr bool kIsTargetAndroid = false;
116 # endif
117 #endif
118 
119 // Additional statically-linked ART binaries (dex2oats, oatdumps, etc.) are
120 // always available on the host
121 #if !defined(ART_TARGET)
122 static constexpr bool kHostStaticBuildEnabled = true;
123 #else
124 static constexpr bool kHostStaticBuildEnabled = false;
125 #endif
126 
127 // Within libart, gPageSize should be used to get the page size value once Runtime is initialized.
128 // For most other cases MemMap::GetPageSize() should be used instead. However, where MemMap is
129 // unavailable e.g. during static initialization or another stage when MemMap isn't yet initialized,
130 // or in a component which might operate without MemMap being initialized, the GetPageSizeSlow()
131 // would be generally suitable. For performance-sensitive code, GetPageSizeSlow() shouldn't be used
132 // without caching the value to remove repeated calls of the function.
133 #ifdef ART_PAGE_SIZE_AGNOSTIC
GetPageSizeSlow()134 inline ALWAYS_INLINE size_t GetPageSizeSlow() {
135   static_assert(kPageSizeAgnostic, "The dynamic version is only for page size agnostic build");
136 #ifdef __linux__
137   static const size_t page_size = sysconf(_SC_PAGE_SIZE);
138 #else
139   static const size_t page_size = 4096;
140 #endif
141   return page_size;
142 }
143 #else
GetPageSizeSlow()144 constexpr size_t GetPageSizeSlow() {
145   static_assert(!kPageSizeAgnostic, "The constexpr version is only for page size agnostic build");
146   return kMinPageSize;
147 }
148 #endif
149 
150 }  // namespace art
151 
152 #endif  // ART_LIBARTBASE_BASE_GLOBALS_H_
153