• 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     explicit LinearSpace(Heap *heap, MemSpaceType type, size_t initialCapacity, size_t maximumCapacity);
25     uintptr_t Allocate(size_t size, bool isPromoted = false);
26     bool Expand(bool isPromoted);
27     void Stop();
DecrementSurvivalObjectSize(size_t objSize)28     void DecrementSurvivalObjectSize(size_t objSize)
29     {
30         survivalObjectSize_ -= objSize;
31     }
32 protected:
33     BumpPointerAllocator *allocator_;
34     size_t overShootSize_ {0};
35     size_t allocateAfterLastGC_ {0};
36     size_t survivalObjectSize_ {0};
37     uintptr_t waterLine_;
38 };
39 
40 class SemiSpace : public LinearSpace {
41 public:
42     explicit SemiSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
43     ~SemiSpace() override = default;
44     NO_COPY_SEMANTIC(SemiSpace);
45     NO_MOVE_SEMANTIC(SemiSpace);
46 
47     void Initialize() override;
48     void Restart();
49 
50     uintptr_t AllocateSync(size_t size);
51 
52     void SetOverShootSize(size_t size);
53     bool AdjustCapacity(size_t allocatedSizeSinceGC);
54 
55     void SetWaterLine();
56 
GetWaterLine()57     uintptr_t GetWaterLine() const
58     {
59         return waterLine_;
60     }
GetTop()61     uintptr_t GetTop() const
62     {
63         return allocator_->GetTop();
64     }
65     size_t GetHeapObjectSize() const;
66     size_t GetSurvivalObjectSize() const;
67     size_t GetAllocatedSizeSinceGC() const;
68 
69     bool SwapRegion(Region *region, SemiSpace *fromSpace);
70 
71     void IterateOverObjects(const std::function<void(TaggedObject *object)> &objectVisitor) const;
72 
73 private:
74     os::memory::Mutex lock_;
75 };
76 
77 class SnapShotSpace : public LinearSpace {
78 public:
79     explicit SnapShotSpace(Heap *heap, size_t initialCapacity, size_t maximumCapacity);
80     ~SnapShotSpace() override = default;
81     NO_COPY_SEMANTIC(SnapShotSpace);
82     NO_MOVE_SEMANTIC(SnapShotSpace);
83 };
84 }  // namespace panda::ecmascript
85 #endif  // ECMASCRIPT_MEM_LINEAR_SPACE_H
86