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