1b1994897Sopenharmony_ci/**
2b1994897Sopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3b1994897Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b1994897Sopenharmony_ci * you may not use this file except in compliance with the License.
5b1994897Sopenharmony_ci * You may obtain a copy of the License at
6b1994897Sopenharmony_ci *
7b1994897Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8b1994897Sopenharmony_ci *
9b1994897Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b1994897Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b1994897Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b1994897Sopenharmony_ci * See the License for the specific language governing permissions and
13b1994897Sopenharmony_ci * limitations under the License.
14b1994897Sopenharmony_ci */
15b1994897Sopenharmony_ci
16b1994897Sopenharmony_ci#ifndef LIBPANDABASE_MEM_MEM_CONFIG_H
17b1994897Sopenharmony_ci#define LIBPANDABASE_MEM_MEM_CONFIG_H
18b1994897Sopenharmony_ci
19b1994897Sopenharmony_ci#include <cstddef>
20b1994897Sopenharmony_ci
21b1994897Sopenharmony_ci#include "macros.h"
22b1994897Sopenharmony_ci
23b1994897Sopenharmony_cinamespace panda::mem {
24b1994897Sopenharmony_ci
25b1994897Sopenharmony_ci/**
26b1994897Sopenharmony_ci * class for global memory parameters
27b1994897Sopenharmony_ci */
28b1994897Sopenharmony_ciclass MemConfig {
29b1994897Sopenharmony_cipublic:
30b1994897Sopenharmony_ci    static void Initialize(size_t object_pool_size, size_t internal_size, size_t compiler_size, size_t code_size,
31b1994897Sopenharmony_ci                           size_t initial_object_pool_size)
32b1994897Sopenharmony_ci    {
33b1994897Sopenharmony_ci        ASSERT(!is_initialized);
34b1994897Sopenharmony_ci        initial_heap_size_limit = initial_object_pool_size;
35b1994897Sopenharmony_ci        heap_size_limit = object_pool_size;
36b1994897Sopenharmony_ci        internal_memory_size_limit = internal_size;
37b1994897Sopenharmony_ci        compiler_memory_size_limit = compiler_size;
38b1994897Sopenharmony_ci        code_cache_size_limit = code_size;
39b1994897Sopenharmony_ci        is_initialized = true;
40b1994897Sopenharmony_ci    }
41b1994897Sopenharmony_ci
42b1994897Sopenharmony_ci    static void Initialize(size_t object_pool_size, size_t internal_size, size_t compiler_size, size_t code_size)
43b1994897Sopenharmony_ci    {
44b1994897Sopenharmony_ci        Initialize(object_pool_size, internal_size, compiler_size, code_size, object_pool_size);
45b1994897Sopenharmony_ci    }
46b1994897Sopenharmony_ci
47b1994897Sopenharmony_ci    static void Finalize()
48b1994897Sopenharmony_ci    {
49b1994897Sopenharmony_ci        is_initialized = false;
50b1994897Sopenharmony_ci        heap_size_limit = 0;
51b1994897Sopenharmony_ci        internal_memory_size_limit = 0;
52b1994897Sopenharmony_ci        code_cache_size_limit = 0;
53b1994897Sopenharmony_ci    }
54b1994897Sopenharmony_ci
55b1994897Sopenharmony_ci    static size_t GetInitialHeapSizeLimit()
56b1994897Sopenharmony_ci    {
57b1994897Sopenharmony_ci        ASSERT(is_initialized);
58b1994897Sopenharmony_ci        return initial_heap_size_limit;
59b1994897Sopenharmony_ci    }
60b1994897Sopenharmony_ci
61b1994897Sopenharmony_ci    static size_t GetHeapSizeLimit()
62b1994897Sopenharmony_ci    {
63b1994897Sopenharmony_ci        ASSERT(is_initialized);
64b1994897Sopenharmony_ci        return heap_size_limit;
65b1994897Sopenharmony_ci    }
66b1994897Sopenharmony_ci
67b1994897Sopenharmony_ci    static size_t GetInternalMemorySizeLimit()
68b1994897Sopenharmony_ci    {
69b1994897Sopenharmony_ci        ASSERT(is_initialized);
70b1994897Sopenharmony_ci        return internal_memory_size_limit;
71b1994897Sopenharmony_ci    }
72b1994897Sopenharmony_ci
73b1994897Sopenharmony_ci    static size_t GetCodeCacheSizeLimit()
74b1994897Sopenharmony_ci    {
75b1994897Sopenharmony_ci        ASSERT(is_initialized);
76b1994897Sopenharmony_ci        return code_cache_size_limit;
77b1994897Sopenharmony_ci    }
78b1994897Sopenharmony_ci
79b1994897Sopenharmony_ci    static size_t GetCompilerMemorySizeLimit()
80b1994897Sopenharmony_ci    {
81b1994897Sopenharmony_ci        ASSERT(is_initialized);
82b1994897Sopenharmony_ci        return compiler_memory_size_limit;
83b1994897Sopenharmony_ci    }
84b1994897Sopenharmony_ci
85b1994897Sopenharmony_ci    MemConfig() = delete;
86b1994897Sopenharmony_ci
87b1994897Sopenharmony_ci    ~MemConfig() = delete;
88b1994897Sopenharmony_ci
89b1994897Sopenharmony_ci    NO_COPY_SEMANTIC(MemConfig);
90b1994897Sopenharmony_ci    NO_MOVE_SEMANTIC(MemConfig);
91b1994897Sopenharmony_ci
92b1994897Sopenharmony_ciprivate:
93b1994897Sopenharmony_ci    static bool is_initialized;
94b1994897Sopenharmony_ci    static size_t initial_heap_size_limit;     // Initial heap size
95b1994897Sopenharmony_ci    static size_t heap_size_limit;             // Max heap size
96b1994897Sopenharmony_ci    static size_t internal_memory_size_limit;  // Max internal memory used by the VM
97b1994897Sopenharmony_ci    static size_t code_cache_size_limit;       // The limit for compiled code size.
98b1994897Sopenharmony_ci    static size_t compiler_memory_size_limit;  // Max memory used by compiler
99b1994897Sopenharmony_ci};
100b1994897Sopenharmony_ci
101b1994897Sopenharmony_ci}  // namespace panda::mem
102b1994897Sopenharmony_ci
103b1994897Sopenharmony_ci#endif  // LIBPANDABASE_MEM_MEM_CONFIG_H
104