1 /* 2 * Copyright (c) 2023 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 #ifndef ECMASCRIPT_COMPILER_AOT_FILE_EXECUTED_MEMORY_ALLOCATOR_H 16 #define ECMASCRIPT_COMPILER_AOT_FILE_EXECUTED_MEMORY_ALLOCATOR_H 17 18 #include "ecmascript/mem/mem.h" 19 #include "ecmascript/platform/map.h" 20 21 namespace panda::ecmascript { 22 class ExecutedMemoryAllocator { 23 public: 24 struct ExeMem { 25 void *addr_ {nullptr}; 26 size_t size_ {0}; 27 }; 28 AllocateBuf(uint32_t size, ExeMem &exeMem, int prot)29 static void AllocateBuf(uint32_t size, ExeMem &exeMem, int prot) 30 { 31 // For safety reason, Disallow prot have both write & exec capability except in JIT. 32 ASSERT(prot != PAGE_PROT_EXEC_READWRITE); 33 MemMap buf = MachineCodePageMap(AlignUp(size, PageSize()), prot); 34 exeMem.addr_ = buf.GetMem(); 35 exeMem.size_ = buf.GetSize(); 36 } 37 DestroyBuf(ExeMem &exeMem)38 static void DestroyBuf(ExeMem &exeMem) 39 { 40 if (exeMem.addr_ != nullptr) { 41 MachineCodePageUnmap(MemMap(exeMem.addr_, exeMem.size_)); 42 exeMem.addr_ = nullptr; 43 exeMem.size_ = 0; 44 } 45 } 46 }; 47 } // namespace panda::ecmascript 48 #endif // ECMASCRIPT_COMPILER_AOT_FILE_EXECUTED_MEMORY_ALLOCATOR_H 49