• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2024 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 LIBPANDABASE_MEM_SPACE_H
17 #define LIBPANDABASE_MEM_SPACE_H
18 
19 #include "utils/type_helpers.h"
20 
21 namespace ark {
22 
23 /// SpaceType and GCCollectMode provide info when we should collect from some allocator or not
24 enum class SpaceType : uint16_t {
25     SPACE_TYPE_UNDEFINED = 0,
26     SPACE_TYPE_OBJECT,              // Space for objects (all non-humongous sizes)
27     SPACE_TYPE_HUMONGOUS_OBJECT,    // Space for humongous objects
28     SPACE_TYPE_NON_MOVABLE_OBJECT,  // Space for non-movable objects
29     SPACE_TYPE_INTERNAL,            // Space for runtime internal needs
30     SPACE_TYPE_FRAMES,              // Space for runtime frames allocations
31     SPACE_TYPE_NATIVE_STACKS,       // Space for native stacks allocations (e.g. for fibers/threads)
32     SPACE_TYPE_CODE,                // Space for compiled code
33     SPACE_TYPE_COMPILER,            // Space for memory allocation in compiler
34 
35     SPACE_TYPE_LAST  // Count of different types
36 };
37 
ToSpaceType(size_t index)38 constexpr SpaceType ToSpaceType(size_t index)
39 {
40     return static_cast<SpaceType>(index);
41 }
42 
SpaceTypeToIndex(SpaceType spaceType)43 constexpr size_t SpaceTypeToIndex(SpaceType spaceType)
44 {
45     return helpers::ToUnderlying(spaceType);
46 }
47 
48 constexpr size_t SPACE_TYPE_SIZE = helpers::ToUnderlying(SpaceType::SPACE_TYPE_LAST);
49 
IsHeapSpace(SpaceType spaceType)50 constexpr bool IsHeapSpace(SpaceType spaceType)
51 {
52     return (spaceType == SpaceType::SPACE_TYPE_OBJECT) || (spaceType == SpaceType::SPACE_TYPE_HUMONGOUS_OBJECT) ||
53            (spaceType == SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT);
54 }
55 
56 // CC-OFFNXT(G.FUD.06) switch-case, ODR
SpaceTypeToString(SpaceType type)57 inline const char *SpaceTypeToString(SpaceType type)
58 {
59     switch (type) {
60         case SpaceType::SPACE_TYPE_UNDEFINED:
61             return "ark-Undefined Space";
62         case SpaceType::SPACE_TYPE_OBJECT:
63             return "ark-Object Space";
64         case SpaceType::SPACE_TYPE_HUMONGOUS_OBJECT:
65             return "ark-Humongous Object Space";
66         case SpaceType::SPACE_TYPE_NON_MOVABLE_OBJECT:
67             return "ark-Non Movable Space";
68         case SpaceType::SPACE_TYPE_INTERNAL:
69             return "ark-Internal Space";
70         case SpaceType::SPACE_TYPE_CODE:
71             return "ark-Code Space";
72         case SpaceType::SPACE_TYPE_COMPILER:
73             return "ark-Compiler Space";
74         case SpaceType::SPACE_TYPE_FRAMES:
75             return "ark-Frames Space";
76         case SpaceType::SPACE_TYPE_NATIVE_STACKS:
77             return "ark-Native Stacks Space";
78         default:
79             return "ark-Unknown Space";
80     }
81 }
82 
83 }  // namespace ark
84 
85 #endif  // LIBPANDABASE_MEM_SPACE_H
86