• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_ALLOCATOR_H
17 #define ECMASCRIPT_MEM_ALLOCATOR_H
18 
19 #include <memory>
20 
21 #include "ecmascript/mem/free_object_list.h"
22 #include "ecmascript/mem/mem.h"
23 
24 namespace panda::ecmascript {
25 class Region;
26 class Heap;
27 
28 class Allocator {
29 public:
30     Allocator() = default;
31     virtual ~Allocator() = default;
32     NO_COPY_SEMANTIC(Allocator);
33     NO_MOVE_SEMANTIC(Allocator);
34 };
35 
36 class BumpPointerAllocator : public Allocator {
37 public:
38     BumpPointerAllocator() = default;
39     ~BumpPointerAllocator() override = default;
40     NO_COPY_SEMANTIC(BumpPointerAllocator);
41     NO_MOVE_SEMANTIC(BumpPointerAllocator);
42 
43     inline BumpPointerAllocator(uintptr_t begin, uintptr_t end);
44 
45     inline void Reset();
46     inline void Reset(uintptr_t begin, uintptr_t end);
47     inline void Reset(uintptr_t begin, uintptr_t end, uintptr_t top);
48     inline void ResetTopPointer(uintptr_t top);
49     inline uintptr_t Allocate(size_t size);
50 
GetTop()51     uintptr_t GetTop() const
52     {
53         return top_;
54     }
55 
GetEnd()56     uintptr_t GetEnd() const
57     {
58         return end_;
59     }
60 
GetTopAddress()61     const uintptr_t *GetTopAddress()
62     {
63         return &top_;
64     }
65 
GetEndAddress()66     const uintptr_t *GetEndAddress()
67     {
68         return &end_;
69     }
70 
Available()71     size_t Available() const
72     {
73         return (end_ - top_);
74     }
75 
76 private:
77     uintptr_t begin_ {0};
78     uintptr_t top_ {0};
79     uintptr_t end_ {0};
80 };
81 
82 class FreeListAllocator : public Allocator {
83 public:
84     FreeListAllocator() = delete;
85     ~FreeListAllocator() override = default;
86 
87     NO_COPY_SEMANTIC(FreeListAllocator);
88     NO_MOVE_SEMANTIC(FreeListAllocator);
89 
90     inline explicit FreeListAllocator(Heap *heap);
91     inline void Initialize(Region *region);
92 
93     inline void Reset(Heap *heap);
94 
95     inline uintptr_t Allocate(size_t size);
96     inline void AddFree(Region *region);
97     inline uintptr_t LookupSuitableFreeObject(size_t size);
98 
99     inline void RebuildFreeList();
100 
101     inline bool MatchFreeObjectSet(Region *region, size_t size);
102     inline void CollectFreeObjectSet(Region *region);
103     inline void DetachFreeObjectSet(Region *region);
104 
105     inline void FreeBumpPoint();
106     // Only fill free object
107     inline void FillBumpPointer();
108 
109     inline void ResetBumpPointer(uintptr_t begin, uintptr_t end, uintptr_t top);
110     inline void ResetTopPointer(uintptr_t top);
111 
112     inline void Free(uintptr_t begin, size_t size, bool isAdd = true);
113 
114     inline size_t GetAvailableSize() const;
115     inline size_t GetWastedSize() const;
116 
GetTop()117     uintptr_t GetTop() const
118     {
119         return bpAllocator_.GetTop();
120     }
121 
GetAllocatedSize()122     size_t GetAllocatedSize() const
123     {
124         return allocationSizeAccumulator_;
125     }
126 
IncreaseAllocatedSize(size_t allocatedSize)127     void IncreaseAllocatedSize(size_t allocatedSize)
128     {
129         allocationSizeAccumulator_ += allocatedSize;
130     }
131 
132 private:
133     inline uintptr_t Allocate(FreeObject *object, size_t size);
134 
135     BumpPointerAllocator bpAllocator_;
136     std::unique_ptr<FreeObjectList> freeList_ {nullptr};
137     Heap *heap_{nullptr};
138     size_t allocationSizeAccumulator_ {0};
139 };
140 }  // namespace panda::ecmascript
141 
142 #endif  // ECMASCRIPT_MEM_ALLOCATOR_H
143