14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_MEM_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_MEM_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <cstdint>
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci#include "ecmascript/base/math_helper.h"
224514f5e3Sopenharmony_ci#include "ecmascript/ecma_param_configuration.h"
234514f5e3Sopenharmony_ci#include "ecmascript/mem/mem_common.h"
244514f5e3Sopenharmony_ci#include "ecmascript/mem/tagged_object.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_ci// NOLINTNEXTLINE(cppcoreguidelines-macro-usage, bugprone-lambda-function-name)
274514f5e3Sopenharmony_ci#define LOG_ECMA_MEM(level) LOG_GC(level) << __func__ << ":" << __LINE__ << " "
284514f5e3Sopenharmony_ci
294514f5e3Sopenharmony_cinamespace panda::ecmascript {
304514f5e3Sopenharmony_cienum class MemAlignment : uint8_t {
314514f5e3Sopenharmony_ci    MEM_ALIGN_OBJECT = 8,
324514f5e3Sopenharmony_ci    MEM_ALIGN_REGION = 16,
334514f5e3Sopenharmony_ci};
344514f5e3Sopenharmony_ci
354514f5e3Sopenharmony_cienum class MemAlignmentLog2 : uint8_t {
364514f5e3Sopenharmony_ci    MEM_ALIGN_OBJECT_LOG2 = 3,
374514f5e3Sopenharmony_ci    MEM_ALIGN_REGION_LOG2 = 4,
384514f5e3Sopenharmony_ci};
394514f5e3Sopenharmony_ci
404514f5e3Sopenharmony_cistatic constexpr size_t INITIAL_REGULAR_OBJECT_CAPACITY = 1024_MB;
414514f5e3Sopenharmony_cistatic constexpr size_t INITIAL_HUGE_OBJECT_CAPACITY = 1024_MB;
424514f5e3Sopenharmony_cistatic constexpr size_t INCREMENT_HUGE_OBJECT_CAPACITY = 128_MB;
434514f5e3Sopenharmony_cistatic constexpr size_t LARGE_POOL_SIZE = 480_MB;
444514f5e3Sopenharmony_cistatic constexpr size_t MEDIUM_POOL_SIZE = 256_MB;
454514f5e3Sopenharmony_cistatic constexpr size_t LOW_POOL_SIZE = 64_MB;
464514f5e3Sopenharmony_cistatic constexpr size_t MIN_MEM_POOL_CAPACITY = 64_MB;
474514f5e3Sopenharmony_cistatic constexpr size_t MAX_MEM_POOL_CAPACITY = 1536_MB;
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci#if defined(PANDA_TARGET_32)
504514f5e3Sopenharmony_ci    static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 512_MB;
514514f5e3Sopenharmony_ci#else
524514f5e3Sopenharmony_ci    static constexpr size_t MAX_GLOBAL_NATIVE_LIMIT = 2048_MB;
534514f5e3Sopenharmony_ci#endif
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_cistatic constexpr size_t MIN_OLD_SPACE_LIMIT = 2_MB;
564514f5e3Sopenharmony_cistatic constexpr size_t MIN_BACKGROUNG_GC_LIMIT = 30_MB;
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_cistatic constexpr size_t MAX_NONMOVABLE_LIVE_OBJ_SIZE = 60_MB;
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_cistatic constexpr size_t REGION_SIZE_LOG2 = 18U;
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_cistatic constexpr size_t MIN_HEAP_SIZE = 5_MB;
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_cistatic constexpr size_t DEFAULT_REGION_SIZE = 1U << REGION_SIZE_LOG2;
654514f5e3Sopenharmony_cistatic constexpr size_t DEFAULT_REGION_MASK = DEFAULT_REGION_SIZE - 1;
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_cistatic constexpr size_t DEFAULT_MARK_STACK_SIZE = 4_KB;
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_cistatic constexpr double MIN_OBJECT_SURVIVAL_RATE = 0.75;
704514f5e3Sopenharmony_cistatic constexpr double GROW_OBJECT_SURVIVAL_RATE = 0.8;
714514f5e3Sopenharmony_cistatic constexpr double SHRINK_OBJECT_SURVIVAL_RATE = 0.2;
724514f5e3Sopenharmony_cistatic constexpr double LOW_ALLOCATION_SPEED_PER_MS = 1000;
734514f5e3Sopenharmony_cistatic constexpr double DEFAULT_CAPACITY_RATE = 0.6;
744514f5e3Sopenharmony_ci// Objects which are larger than half of the region size are huge objects.
754514f5e3Sopenharmony_ci// Regular objects will be allocated on regular regions and migrated on spaces.
764514f5e3Sopenharmony_ci// They will never be moved to huge object space. So we take half of a regular
774514f5e3Sopenharmony_ci// region as the border of regular objects.
784514f5e3Sopenharmony_cistatic constexpr size_t MAX_32BIT_OBJECT_SPACE_SIZE = 1_GB;
794514f5e3Sopenharmony_cistatic constexpr size_t MAX_REGULAR_HEAP_OBJECT_SIZE = DEFAULT_REGION_SIZE * 2 / 3;
804514f5e3Sopenharmony_ci// internal allocator
814514f5e3Sopenharmony_cistatic constexpr size_t CHUNK_ALIGN_SIZE = 4_KB;
824514f5e3Sopenharmony_cistatic constexpr size_t MIN_CHUNK_AREA_SIZE = 4_KB;
834514f5e3Sopenharmony_cistatic constexpr size_t MAX_CACHED_CHUNK_AREA_SIZE = 16_KB;
844514f5e3Sopenharmony_cistatic constexpr uint32_t WORKNODE_SPACE_SIZE = 8_KB;
854514f5e3Sopenharmony_cistatic constexpr size_t MAX_CHUNK_AREA_SIZE = 1_MB;
864514f5e3Sopenharmony_cistatic constexpr size_t MAX_REGEXP_CACHE_SIZE = 8_KB;
874514f5e3Sopenharmony_ci// taskpool
884514f5e3Sopenharmony_cistatic constexpr double TRIGGER_OLDGC_OBJECT_LIMIT_RATE = 0.1;
894514f5e3Sopenharmony_cistatic constexpr double TRIGGER_OLDGC_OBJECT_SIZE_LIMIT = 20_MB;
904514f5e3Sopenharmony_cistatic constexpr double TRIGGER_OLDGC_NATIVE_LIMIT_RATE = 0.1;
914514f5e3Sopenharmony_cistatic constexpr double TRIGGER_OLDGC_NATIVE_SIZE_LIMIT = 20_MB;
924514f5e3Sopenharmony_ci// idle gc
934514f5e3Sopenharmony_cistatic constexpr size_t IDLE_GC_YOUNG_SPACE = 3_MB;
944514f5e3Sopenharmony_ci
954514f5e3Sopenharmony_cistatic constexpr double LOW_ALLOCATION_RATE_PER_MS = 10;
964514f5e3Sopenharmony_cistatic constexpr double IDLE_SPACE_SIZE_LIMIT_RATE = 0.8f;
974514f5e3Sopenharmony_cistatic constexpr double IDLE_SPACE_SIZE_MIN_INC_RATIO = 1.1f;
984514f5e3Sopenharmony_cistatic constexpr double IDLE_FRAGMENT_SIZE_RATIO = 0.1f;
994514f5e3Sopenharmony_cistatic constexpr size_t IDLE_SPACE_SIZE_MIN_INC_STEP = 5_MB;
1004514f5e3Sopenharmony_cistatic constexpr size_t IDLE_SPACE_SIZE_MIN_INC_STEP_FULL = 1_MB;
1014514f5e3Sopenharmony_cistatic constexpr size_t IDLE_MIN_EXPECT_RECLAIM_SIZE = 1_MB;
1024514f5e3Sopenharmony_ci
1034514f5e3Sopenharmony_ciusing TaggedType = uint64_t;
1044514f5e3Sopenharmony_cistatic constexpr uint32_t TAGGED_TYPE_SIZE = sizeof(TaggedType);
1054514f5e3Sopenharmony_cistatic constexpr uint32_t TAGGED_TYPE_SIZE_LOG = base::MathHelper::GetIntLog2(TAGGED_TYPE_SIZE);
1064514f5e3Sopenharmony_ciconstexpr size_t HEAD_SIZE = TaggedObject::TaggedObjectSize();
1074514f5e3Sopenharmony_ci
1084514f5e3Sopenharmony_citemplate<typename T>
1094514f5e3Sopenharmony_ciconstexpr inline bool IsAligned(T value, size_t alignment)
1104514f5e3Sopenharmony_ci{
1114514f5e3Sopenharmony_ci    return (value & (alignment - 1U)) == 0;
1124514f5e3Sopenharmony_ci}
1134514f5e3Sopenharmony_ci
1144514f5e3Sopenharmony_citemplate<typename T>
1154514f5e3Sopenharmony_ciinline T AlignDown(T x, size_t alignment)
1164514f5e3Sopenharmony_ci{
1174514f5e3Sopenharmony_ci    ASSERT(std::is_integral<T>::value);
1184514f5e3Sopenharmony_ci    // alignment must be a power of two.
1194514f5e3Sopenharmony_ci    ASSERT(alignment != 0 && ((alignment & (alignment - 1U)) == 0));
1204514f5e3Sopenharmony_ci    return x & ~(alignment - 1U);
1214514f5e3Sopenharmony_ci}
1224514f5e3Sopenharmony_ci
1234514f5e3Sopenharmony_citemplate<typename T>
1244514f5e3Sopenharmony_ciinline T AlignUp(T x, size_t alignment)
1254514f5e3Sopenharmony_ci{
1264514f5e3Sopenharmony_ci    ASSERT(std::is_integral<T>::value && (x + alignment) > 0);
1274514f5e3Sopenharmony_ci    return AlignDown<T>(static_cast<T>(x + alignment - 1U), alignment);
1284514f5e3Sopenharmony_ci}
1294514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1304514f5e3Sopenharmony_ci
1314514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_MEM_H
132