• 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 uintptr_t Allocate(size_t size);
49 
GetTop()50     uintptr_t GetTop() const
51     {
52         return top_;
53     }
54 
GetEnd()55     uintptr_t GetEnd() const
56     {
57         return end_;
58     }
59 
GetTopAddress()60     const uintptr_t *GetTopAddress()
61     {
62         return &top_;
63     }
64 
GetEndAddress()65     const uintptr_t *GetEndAddress()
66     {
67         return &end_;
68     }
69 
Available()70     size_t Available() const
71     {
72         return (end_ - top_);
73     }
74 
75 private:
76     uintptr_t begin_ {0};
77     uintptr_t top_ {0};
78     uintptr_t end_ {0};
79 };
80 
81 class FreeListAllocator : public Allocator {
82 public:
83     FreeListAllocator() = delete;
84     ~FreeListAllocator() override = default;
85 
86     NO_COPY_SEMANTIC(FreeListAllocator);
87     NO_MOVE_SEMANTIC(FreeListAllocator);
88 
89     inline explicit FreeListAllocator(Heap *heap);
90     inline void Initialize(Region *region);
91 
92     inline void Reset(Heap *heap);
93 
94     inline uintptr_t Allocate(size_t size);
95     inline void AddFree(Region *region);
96     inline uintptr_t LookupSuitableFreeObject(size_t size);
97 
98     inline void RebuildFreeList();
99 
100     inline bool MatchFreeObjectSet(Region *region, size_t size);
101     inline void CollectFreeObjectSet(Region *region);
102     inline void DetachFreeObjectSet(Region *region);
103 
104     inline void FreeBumpPoint();
105     // Only fill free object
106     inline void FillBumpPointer();
107 
108     inline void ResetBumpPointer(uintptr_t begin, uintptr_t end, uintptr_t top);
109 
110     inline void Free(uintptr_t begin, size_t size, bool isAdd = true);
111 
112     inline size_t GetAvailableSize() const;
113     inline size_t GetWastedSize() const;
114 
GetTop()115     uintptr_t GetTop() const
116     {
117         return bpAllocator_.GetTop();
118     }
119 
GetAllocatedSize()120     size_t GetAllocatedSize() const
121     {
122         return allocationSizeAccumulator_;
123     }
124 
IncreaseAllocatedSize(size_t allocatedSize)125     void IncreaseAllocatedSize(size_t allocatedSize)
126     {
127         allocationSizeAccumulator_ += allocatedSize;
128     }
129 
130 private:
131     inline uintptr_t Allocate(FreeObject *object, size_t size);
132 
133     BumpPointerAllocator bpAllocator_;
134     std::unique_ptr<FreeObjectList> freeList_ {nullptr};
135     Heap *heap_{nullptr};
136     size_t allocationSizeAccumulator_ {0};
137 };
138 }  // namespace panda::ecmascript
139 
140 #endif  // ECMASCRIPT_MEM_ALLOCATOR_H
141