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 namespace art {
24
25 static constexpr size_t KB = 1024;
26 static constexpr size_t MB = KB * KB;
27 static constexpr size_t GB = KB * KB * KB;
28
29 // Runtime sizes.
30 static constexpr size_t kBitsPerByte = 8;
31 static constexpr size_t kBitsPerByteLog2 = 3;
32 static constexpr int kBitsPerIntPtrT = sizeof(intptr_t) * kBitsPerByte;
33
34 // Required stack alignment
35 static constexpr size_t kStackAlignment = 16;
36
37 // System page size. We check this against sysconf(_SC_PAGE_SIZE) at runtime, but use a simple
38 // compile-time constant so the compiler can generate better code.
39 static constexpr int kPageSize = 4096;
40
41 // Returns whether the given memory offset can be used for generating
42 // an implicit null check.
CanDoImplicitNullCheckOn(uintptr_t offset)43 static inline bool CanDoImplicitNullCheckOn(uintptr_t offset) {
44 return offset < kPageSize;
45 }
46
47 // Required object alignment
48 static constexpr size_t kObjectAlignmentShift = 3;
49 static constexpr size_t kObjectAlignment = 1u << kObjectAlignmentShift;
50 static constexpr size_t kLargeObjectAlignment = kPageSize;
51
52 // Clion, clang analyzer, etc can falsely believe that "if (kIsDebugBuild)" always
53 // returns the same value. By wrapping into a call to another constexpr function, we force it
54 // to realize that is not actually always evaluating to the same value.
GlobalsReturnSelf(bool self)55 static constexpr bool GlobalsReturnSelf(bool self) { return self; }
56
57 // Whether or not this is a debug build. Useful in conditionals where NDEBUG isn't.
58 // TODO: Use only __clang_analyzer__ here. b/64455231
59 #if defined(NDEBUG) && !defined(__CLION_IDE__)
60 static constexpr bool kIsDebugBuild = GlobalsReturnSelf(false);
61 #else
62 static constexpr bool kIsDebugBuild = GlobalsReturnSelf(true);
63 #endif
64
65 #if defined(ART_PGO_INSTRUMENTATION)
66 static constexpr bool kIsPGOInstrumentation = true;
67 #else
68 static constexpr bool kIsPGOInstrumentation = false;
69 #endif
70
71 // ART_TARGET - Defined for target builds of ART.
72 // ART_TARGET_LINUX - Defined for target Linux builds of ART.
73 // ART_TARGET_ANDROID - Defined for target Android builds of ART.
74 // Note: Either ART_TARGET_LINUX or ART_TARGET_ANDROID need to be set when ART_TARGET is set.
75 // Note: When ART_TARGET_LINUX is defined mem_map.h will not be using Ashmem for memory mappings
76 // (usually only available on Android kernels).
77 #if defined(ART_TARGET)
78 // Useful in conditionals where ART_TARGET isn't.
79 static constexpr bool kIsTargetBuild = true;
80 # if defined(ART_TARGET_LINUX)
81 static constexpr bool kIsTargetLinux = true;
82 # elif defined(ART_TARGET_ANDROID)
83 static constexpr bool kIsTargetLinux = false;
84 # else
85 # error "Either ART_TARGET_LINUX or ART_TARGET_ANDROID needs to be defined for target builds."
86 # endif
87 #else
88 static constexpr bool kIsTargetBuild = false;
89 # if defined(ART_TARGET_LINUX)
90 # error "ART_TARGET_LINUX defined for host build."
91 # elif defined(ART_TARGET_ANDROID)
92 # error "ART_TARGET_ANDROID defined for host build."
93 # else
94 static constexpr bool kIsTargetLinux = false;
95 # endif
96 #endif
97
98 // Additional statically-linked ART binaries (dex2oats, oatdumps, etc.) are
99 // always available on the host
100 #if !defined(ART_TARGET)
101 static constexpr bool kHostStaticBuildEnabled = true;
102 #else
103 static constexpr bool kHostStaticBuildEnabled = false;
104 #endif
105
106 // Garbage collector constants.
107 static constexpr bool kMovingCollector = true;
108 static constexpr bool kMarkCompactSupport = false && kMovingCollector;
109 // True if we allow moving classes.
110 static constexpr bool kMovingClasses = !kMarkCompactSupport;
111
112 // If true, enable the tlab allocator by default.
113 #ifdef ART_USE_TLAB
114 static constexpr bool kUseTlab = true;
115 #else
116 static constexpr bool kUseTlab = false;
117 #endif
118
119 // Kinds of tracing clocks.
120 enum class TraceClockSource {
121 kThreadCpu,
122 kWall,
123 kDual, // Both wall and thread CPU clocks.
124 };
125
126 #if defined(__linux__)
127 static constexpr TraceClockSource kDefaultTraceClockSource = TraceClockSource::kDual;
128 #else
129 static constexpr TraceClockSource kDefaultTraceClockSource = TraceClockSource::kWall;
130 #endif
131
132 static constexpr bool kDefaultMustRelocate = true;
133
134 // Size of a heap reference.
135 static constexpr size_t kHeapReferenceSize = sizeof(uint32_t);
136
137 } // namespace art
138
139 #endif // ART_LIBARTBASE_BASE_GLOBALS_H_
140