1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #ifndef ECMASCRIPT_MEM_MEM_H
17 #define ECMASCRIPT_MEM_MEM_H
18
19 #include <cstdint>
20
21 #include "ecmascript/base/math_helper.h"
22 #include "ecmascript/ecma_param_configuration.h"
23 #include "ecmascript/mem/mem_common.h"
24 #include "ecmascript/mem/tagged_object.h"
25
26 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage, bugprone-lambda-function-name)
27 #define LOG_ECMA_MEM(level) LOG_GC(level) << __func__ << ":" << __LINE__ << " "
28
29 namespace panda::ecmascript {
30 enum class MemAlignment : uint8_t {
31 MEM_ALIGN_OBJECT = 8,
32 MEM_ALIGN_REGION = 16,
33 };
34
35 enum class MemAlignmentLog2 : uint8_t {
36 MEM_ALIGN_OBJECT_LOG2 = 3,
37 MEM_ALIGN_REGION_LOG2 = 4,
38 };
39
40 static constexpr size_t MAX_HUGE_OBJECT_CAPACITY = 512_MB;
41 static constexpr size_t LARGE_POOL_SIZE = 480_MB;
42 static constexpr size_t MEDIUM_POOL_SIZE = 256_MB;
43 static constexpr size_t LOW_POOL_SIZE = 64_MB;
44 static constexpr size_t MIN_MEM_POOL_CAPACITY = 64_MB;
45 static constexpr size_t PHY_SIZE_MULTIPLE = 4;
46 static constexpr size_t WORKER_NUM = 7;
47 static constexpr size_t STANDARD_POOL_SIZE = WORKER_NUM * DEFAULT_WORKER_HEAP_SIZE + DEFAULT_HEAP_SIZE;
48
49 static constexpr size_t MIN_OLD_SPACE_LIMIT = 2_MB;
50
51 static constexpr size_t REGION_SIZE_LOG2 = 18U;
52
53 static constexpr size_t MIN_HEAP_SIZE = 5_MB;
54
55 static constexpr size_t DEFAULT_REGION_SIZE = 1U << REGION_SIZE_LOG2;
56 static constexpr size_t DEFAULT_REGION_MASK = DEFAULT_REGION_SIZE - 1;
57
58 static constexpr size_t DEFAULT_MARK_STACK_SIZE = 4_KB;
59
60 static constexpr double MIN_OBJECT_SURVIVAL_RATE = 0.75;
61 static constexpr double GROW_OBJECT_SURVIVAL_RATE = 0.8;
62 static constexpr double SHRINK_OBJECT_SURVIVAL_RATE = 0.2;
63 static constexpr double LOW_ALLOCATION_SPEED_PER_MS = 1000;
64 // Objects which are larger than half of the region size are huge objects.
65 // Regular objects will be allocated on regular regions and migrated on spaces.
66 // They will never be moved to huge object space. So we take half of a regular
67 // region as the border of regular objects.
68 static constexpr size_t MAX_32BIT_OBJECT_SPACE_SIZE = 1_GB;
69 static constexpr size_t MAX_REGULAR_HEAP_OBJECT_SIZE = DEFAULT_REGION_SIZE * 2 / 3;
70 // internal allocator
71 static constexpr size_t CHUNK_ALIGN_SIZE = 4_KB;
72 static constexpr size_t MIN_CHUNK_AREA_SIZE = 4_KB;
73 static constexpr size_t MAX_CACHED_CHUNK_AREA_SIZE = 16_KB;
74 static constexpr size_t MAX_CHUNK_AREA_SIZE = 1_MB;
75
76 // idle gc
77 static constexpr size_t IDLE_GC_YOUNG_SPACE = 3_MB;
78
79 using TaggedType = uint64_t;
80 static constexpr uint32_t TAGGED_TYPE_SIZE = sizeof(TaggedType);
81 static constexpr uint32_t TAGGED_TYPE_SIZE_LOG = base::MathHelper::GetIntLog2(TAGGED_TYPE_SIZE);
82
83 template<typename T>
IsAligned(T value,size_t alignment)84 constexpr inline bool IsAligned(T value, size_t alignment)
85 {
86 return (value & (alignment - 1U)) == 0;
87 }
88
89 template<typename T>
AlignDown(T x,size_t alignment)90 inline T AlignDown(T x, size_t alignment)
91 {
92 ASSERT(std::is_integral<T>::value);
93 // alignment must be a power of two.
94 ASSERT(alignment != 0 && ((alignment & (alignment - 1U)) == 0));
95 return x & ~(alignment - 1U);
96 }
97
98 template<typename T>
AlignUp(T x,size_t alignment)99 inline T AlignUp(T x, size_t alignment)
100 {
101 ASSERT(std::is_integral<T>::value);
102 return AlignDown<T>(static_cast<T>(x + alignment - 1U), alignment);
103 }
104 } // namespace panda::ecmascript
105
106 #endif // ECMASCRIPT_MEM_MEM_H
107