• 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_CHUNK_H
17 #define ECMASCRIPT_MEM_CHUNK_H
18 
19 #include "ecmascript/common.h"
20 #include "ecmascript/log_wrapper.h"
21 #include "ecmascript/mem/ecma_list.h"
22 #include "ecmascript/mem/area.h"
23 
24 namespace panda::ecmascript {
25 class NativeAreaAllocator;
26 
27 class PUBLIC_API Chunk {
28 public:
29     static constexpr size_t MEM_ALIGN = 8U;
30 
31     explicit Chunk(NativeAreaAllocator *allocator);
~Chunk()32     ~Chunk()
33     {
34         ReleaseMemory();
35     }
36 
37     NO_COPY_SEMANTIC(Chunk);
38     NO_MOVE_SEMANTIC(Chunk);
39 
Allocate(size_t size)40     [[nodiscard]] void *Allocate(size_t size)
41     {
42         if (size == 0) {
43             LOG_ECMA_MEM(FATAL) << "size must have a size bigger than 0";
44             UNREACHABLE();
45         }
46         uintptr_t result = ptr_;
47         size = AlignUp(size, MEM_ALIGN);
48         if (UNLIKELY(size > end_ - ptr_)) {
49             result = Expand(size);
50         } else {
51             ptr_ += size;
52         }
53 
54         return reinterpret_cast<void *>(result);
55     }
56 
57     template<class T>
NewArray(size_t size)58     [[nodiscard]] T *NewArray(size_t size)
59     {
60         return static_cast<T *>(Allocate(size * sizeof(T)));
61     }
62 
63     template<typename T, typename... Args>
New(Args &&...args)64     [[nodiscard]] T *New(Args &&... args)
65     {
66         auto p = reinterpret_cast<void *>(Allocate(sizeof(T)));
67         new (p) T(std::forward<Args>(args)...);
68         return reinterpret_cast<T *>(p);
69     }
70 
71     template<class T>
Delete(T * ptr)72     void Delete(T *ptr)
73     {
74         ASSERT(ptr != nullptr);
75         // NOLINTNEXTLINE(readability-braces-around-statements,bugprone-suspicious-semicolon)
76         if constexpr (std::is_class_v<T>) {
77             ptr->~T();
78         }
79         Free(ptr);
80     }
81 
Free(void * mem)82     void Free([[maybe_unused]] void *mem)
83     {
84         // do nothing
85     }
86 
87 private:
88     uintptr_t Expand(size_t size);
89     Area *NewArea(size_t size);
90     void ReleaseMemory();
91 
92     uintptr_t ptr_ {0};
93     uintptr_t end_ {0};
94 
95     Area *currentArea_ {nullptr};
96     EcmaList<Area> areaList_ {};
97     NativeAreaAllocator *allocator_ {nullptr};
98 };
99 
100 class PUBLIC_API ChunkObject {
101 public:
new(size_t size,Chunk * chunk)102     void *operator new(size_t size, Chunk* chunk)
103     {
104         return chunk->Allocate(size);
105     }
106 
delete(void * ptr)107     void operator delete([[maybe_unused]] void* ptr)
108     {
109         UNREACHABLE();
110     }
111 };
112 }  // namespace panda::ecmascript
113 
114 #endif  // ECMASCRIPT_MEM_CHUNK_H
115