1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "berberis/runtime_primitives/code_pool.h" 18 19 #include <cstring> 20 #include <mutex> 21 22 #include "berberis/base/forever_alloc.h" 23 #include "berberis/runtime_primitives/exec_region_anonymous.h" 24 25 #if defined(__BIONIC__) 26 #include "berberis/runtime_primitives/exec_region_elf_backed.h" 27 #endif 28 29 namespace berberis { 30 AddRaw(const void * ptr,uint32_t size)31void* DataPool::AddRaw(const void* ptr, uint32_t size) { 32 void* result; 33 // Take the lock to allocate in arena. 34 { 35 std::lock_guard<std::mutex> lock(mutex_); 36 result = arena_.Alloc(size, /* align = */ 16); 37 } 38 memcpy(result, ptr, size); 39 return result; 40 } 41 ResetAllExecRegions()42void ResetAllExecRegions() { 43 GetDefaultCodePoolInstance()->ResetExecRegion(); 44 GetColdCodePoolInstance()->ResetExecRegion(); 45 GetFunctionWrapperCodePoolInstance()->ResetExecRegion(); 46 } 47 GetDefaultCodePoolInstance()48CodePool<ExecRegionAnonymousFactory>* GetDefaultCodePoolInstance() { 49 static auto* g_code_pool = NewForever<CodePool<ExecRegionAnonymousFactory>>(); 50 return g_code_pool; 51 } 52 GetColdCodePoolInstance()53CodePool<ExecRegionAnonymousFactory>* GetColdCodePoolInstance() { 54 static auto* g_cold_code_pool = NewForever<CodePool<ExecRegionAnonymousFactory>>(); 55 return g_cold_code_pool; 56 } 57 58 #if defined(__BIONIC__) GetFunctionWrapperCodePoolInstance()59CodePool<ExecRegionElfBackedFactory>* GetFunctionWrapperCodePoolInstance() { 60 static auto* g_code_pool = NewForever<CodePool<ExecRegionElfBackedFactory>>(); 61 return g_code_pool; 62 } 63 #else GetFunctionWrapperCodePoolInstance()64CodePool<ExecRegionAnonymousFactory>* GetFunctionWrapperCodePoolInstance() { 65 return GetDefaultCodePoolInstance(); 66 } 67 #endif 68 GetInstance()69DataPool* DataPool::GetInstance() { 70 static auto* g_data_pool = NewForever<DataPool>(); 71 return g_data_pool; 72 } 73 74 } // namespace berberis 75