• 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     }
44 
45     void InvokeAllocationInspector(Address object, size_t size, size_t alignedSize);
46 
47 protected:
48     BumpPointerAllocator allocator_;
49     size_t overShootSize_ {0};
50     size_t allocateAfterLastGC_ {0};
51     size_t survivalObjectSize_ {0};
52     uintptr_t waterLine_ {0};
53 };
54 
55 class SemiSpace : public LinearSpace {
56 public:
57     SemiSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
58     ~SemiSpace() override = default;
59     NO_COPY_SEMANTIC(SemiSpace);
60     NO_MOVE_SEMANTIC(SemiSpace);
61 
62     void Initialize() override;
63     void Restart();
64 
65     uintptr_t AllocateSync(size_t size);
66 
67     void SetOverShootSize(size_t size);
68     bool AdjustCapacity(size_t allocatedSizeSinceGC);
69     void SetWaterLine();
70     void SetWaterLineWithoutGC();
71 
GetWaterLine()72     uintptr_t GetWaterLine() const
73     {
74         return waterLine_;
75     }
GetTop()76     uintptr_t GetTop() const
77     {
78         return allocator_.GetTop();
79     }
80     size_t GetHeapObjectSize() const;
81     size_t GetSurvivalObjectSize() const;
82     size_t GetAllocatedSizeSinceGC(uintptr_t top = 0) const;
83 
84     bool SwapRegion(Region *region, SemiSpace *fromSpace);
85 
86 private:
87     static constexpr int GROWING_FACTOR = 2;
88     Mutex lock_;
89     size_t minimumCapacity_;
90 };
91 
92 class SnapshotSpace : public LinearSpace {
93 public:
94     SnapshotSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
95     ~SnapshotSpace() override = default;
96     NO_COPY_SEMANTIC(SnapshotSpace);
97     NO_MOVE_SEMANTIC(SnapshotSpace);
98 
GetHeapObjectSize()99     size_t GetHeapObjectSize() const
100     {
101         return liveObjectSize_;
102     }
103 
IncreaseLiveObjectSize(size_t size)104     void IncreaseLiveObjectSize(size_t size)
105     {
106         liveObjectSize_ += size;
107     }
108 
109 private:
110     size_t liveObjectSize_ {0};
111 };
112 
113 class ReadOnlySpace : public LinearSpace {
114 public:
115     ReadOnlySpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
116     ~ReadOnlySpace() override = default;
SetReadOnly()117     void SetReadOnly()
118     {
119         auto cb = [](Region *region) {
120             region->SetReadOnlyAndMarked();
121         };
122         EnumerateRegions(cb);
123     }
124 
ClearReadOnly()125     void ClearReadOnly()
126     {
127         auto cb = [](Region *region) {
128             region->ClearReadOnly();
129         };
130         EnumerateRegions(cb);
131     }
132 
133     NO_COPY_SEMANTIC(ReadOnlySpace);
134     NO_MOVE_SEMANTIC(ReadOnlySpace);
135 };
136 }  // namespace panda::ecmascript
137 #endif  // ECMASCRIPT_MEM_LINEAR_SPACE_H
138