• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/exec_region_anonymous.h"
23 
24 #if defined(__BIONIC__)
25 #include "berberis/base/exec_region_elf_backed.h"
26 #endif
27 
28 namespace berberis {
29 
AddRaw(const void * ptr,uint32_t size)30 void* DataPool::AddRaw(const void* ptr, uint32_t size) {
31   void* result;
32   // Take the lock to allocate in arena.
33   {
34     std::lock_guard<std::mutex> lock(mutex_);
35     result = arena_.Alloc(size, /* align = */ 16);
36   }
37   memcpy(result, ptr, size);
38   return result;
39 }
40 
ResetAllExecRegions()41 void ResetAllExecRegions() {
42   GetDefaultCodePoolInstance()->ResetExecRegion();
43   GetFunctionWrapperCodePoolInstance()->ResetExecRegion();
44 }
45 
GetDefaultCodePoolInstance()46 CodePool<ExecRegionAnonymousFactory>* GetDefaultCodePoolInstance() {
47   static CodePool<ExecRegionAnonymousFactory> g_code_pool;
48   return &g_code_pool;
49 }
50 
51 #if defined(__BIONIC__)
GetFunctionWrapperCodePoolInstance()52 CodePool<ExecRegionElfBackedFactory>* GetFunctionWrapperCodePoolInstance() {
53   static CodePool<ExecRegionElfBackedFactory> g_code_pool;
54   return &g_code_pool;
55 }
56 #else
GetFunctionWrapperCodePoolInstance()57 CodePool<ExecRegionAnonymousFactory>* GetFunctionWrapperCodePoolInstance() {
58   return GetDefaultCodePoolInstance();
59 }
60 #endif
61 
GetInstance()62 DataPool* DataPool::GetInstance() {
63   static DataPool g_data_pool;
64   return &g_data_pool;
65 }
66 
67 }  // namespace berberis
68