• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_LINEAR_SPACE_H
17 #define ECMASCRIPT_MEM_LINEAR_SPACE_H
18 
19 #include "ecmascript/mem/space-inl.h"
20 
21 namespace panda::ecmascript {
22 class LinearSpace : public Space {
23 public:
24     LinearSpace(Heap *heap, MemSpaceType type, size_t initialCapacity, size_t maximumCapacity);
25     NO_COPY_SEMANTIC(LinearSpace);
26     NO_MOVE_SEMANTIC(LinearSpace);
27     uintptr_t Allocate(size_t size, bool isPromoted = false);
28     bool Expand(bool isPromoted);
29     void Stop();
30     void ResetAllocator();
31     void IterateOverObjects(const std::function<void(TaggedObject *object)> &objectVisitor) const;
DecreaseSurvivalObjectSize(size_t objSize)32     void DecreaseSurvivalObjectSize(size_t objSize)
33     {
34         survivalObjectSize_ -= objSize;
35     }
GetAllocationTopAddress()36     const uintptr_t *GetAllocationTopAddress()
37     {
38         return allocator_.GetTopAddress();
39     }
GetAllocationEndAddress()40     const uintptr_t *GetAllocationEndAddress()
41     {
42         return allocator_.GetEndAddress();
43     }
ResetNativeBindingSize()44     void ResetNativeBindingSize()
45     {
46         newSpaceNativeBindingSize_ = 0;
47     }
IncreaseNativeBindingSize(size_t size)48     void IncreaseNativeBindingSize(size_t size)
49     {
50         newSpaceNativeBindingSize_ += size;
51     }
GetNativeBindingSize()52     size_t GetNativeBindingSize()
53     {
54         return newSpaceNativeBindingSize_;
55     }
56 
NativeBindingSizeLargerThanLimit()57     bool NativeBindingSizeLargerThanLimit()
58     {
59         return newSpaceNativeBindingSize_ > newSpaceNativeLimit_;
60     }
61 
62     void InvokeAllocationInspector(Address object, size_t size, size_t alignedSize);
63 protected:
64     Heap *heap_ {nullptr};
65 
66     BumpPointerAllocator allocator_;
67     size_t overShootSize_ {0};
68     size_t allocateAfterLastGC_ {0};
69     size_t survivalObjectSize_ {0};
70     uintptr_t waterLine_ {0};
71     size_t newSpaceNativeLimit_;
72     size_t newSpaceNativeBindingSize_ {0};
73 };
74 
75 class SemiSpace : public LinearSpace {
76 public:
77     SemiSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
78     ~SemiSpace() override = default;
79     NO_COPY_SEMANTIC(SemiSpace);
80     NO_MOVE_SEMANTIC(SemiSpace);
81 
82     void Initialize() override;
83     void Restart();
84 
85     uintptr_t AllocateSync(size_t size);
86 
87     void SetOverShootSize(size_t size);
88     bool AdjustCapacity(size_t allocatedSizeSinceGC);
89     void AdjustNativeLimit(size_t previousNativeSize);
90     void SetWaterLine();
91 
GetWaterLine()92     uintptr_t GetWaterLine() const
93     {
94         return waterLine_;
95     }
GetTop()96     uintptr_t GetTop() const
97     {
98         return allocator_.GetTop();
99     }
100     size_t GetHeapObjectSize() const;
101     size_t GetSurvivalObjectSize() const;
102     size_t GetAllocatedSizeSinceGC(uintptr_t top = 0) const;
103 
104     bool SwapRegion(Region *region, SemiSpace *fromSpace);
105 
106 private:
107     static constexpr int GROWING_FACTOR = 2;
108     os::memory::Mutex lock_;
109     size_t minimumCapacity_;
110 };
111 
112 class SnapshotSpace : public LinearSpace {
113 public:
114     SnapshotSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
115     ~SnapshotSpace() override = default;
116     NO_COPY_SEMANTIC(SnapshotSpace);
117     NO_MOVE_SEMANTIC(SnapshotSpace);
118 
GetHeapObjectSize()119     size_t GetHeapObjectSize() const
120     {
121         return liveObjectSize_;
122     }
123 
IncreaseLiveObjectSize(size_t size)124     void IncreaseLiveObjectSize(size_t size)
125     {
126         liveObjectSize_ += size;
127     }
128 
129 private:
130     size_t liveObjectSize_ {0};
131 };
132 
133 class ReadOnlySpace : public LinearSpace {
134 public:
135     ReadOnlySpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
136     ~ReadOnlySpace() override = default;
SetReadOnly()137     void SetReadOnly()
138     {
139         auto cb = [](Region *region) {
140             region->SetReadOnlyAndMarked();
141         };
142         EnumerateRegions(cb);
143     }
144 
ClearReadOnly()145     void ClearReadOnly()
146     {
147         auto cb = [](Region *region) {
148             region->ClearReadOnly();
149         };
150         EnumerateRegions(cb);
151     }
152 
153     NO_COPY_SEMANTIC(ReadOnlySpace);
154     NO_MOVE_SEMANTIC(ReadOnlySpace);
155 };
156 }  // namespace panda::ecmascript
157 #endif  // ECMASCRIPT_MEM_LINEAR_SPACE_H
158