xref: /aosp_15_r20/art/compiler/optimizing/handle_cache.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1 /*
2  * Copyright (C) 2024 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 #ifndef ART_COMPILER_OPTIMIZING_HANDLE_CACHE_H_
18 #define ART_COMPILER_OPTIMIZING_HANDLE_CACHE_H_
19 
20 #include "base/macros.h"
21 #include "class_root.h"
22 #include "reference_type_info.h"
23 
24 namespace art HIDDEN {
25 
26 class VariableSizedHandleScope;
27 
28 class HandleCache {
29  public:
HandleCache(VariableSizedHandleScope * handles)30   explicit HandleCache(VariableSizedHandleScope* handles) : handles_(handles) { }
31 
GetHandles()32   VariableSizedHandleScope* GetHandles() { return handles_; }
33 
34   template <typename T>
35   MutableHandle<T> NewHandle(T* object) REQUIRES_SHARED(Locks::mutator_lock_);
36 
37   template <typename T>
38   MutableHandle<T> NewHandle(ObjPtr<T> object) REQUIRES_SHARED(Locks::mutator_lock_);
39 
GetObjectClassHandle()40   ReferenceTypeInfo::TypeHandle GetObjectClassHandle() {
41     return GetRootHandle(ClassRoot::kJavaLangObject, &object_class_handle_);
42   }
43 
GetClassClassHandle()44   ReferenceTypeInfo::TypeHandle GetClassClassHandle() {
45     return GetRootHandle(ClassRoot::kJavaLangClass, &class_class_handle_);
46   }
47 
GetMethodHandleClassHandle()48   ReferenceTypeInfo::TypeHandle GetMethodHandleClassHandle() {
49     return GetRootHandle(ClassRoot::kJavaLangInvokeMethodHandleImpl, &method_handle_class_handle_);
50   }
51 
GetMethodTypeClassHandle()52   ReferenceTypeInfo::TypeHandle GetMethodTypeClassHandle() {
53     return GetRootHandle(ClassRoot::kJavaLangInvokeMethodType, &method_type_class_handle_);
54   }
55 
GetStringClassHandle()56   ReferenceTypeInfo::TypeHandle GetStringClassHandle() {
57     return GetRootHandle(ClassRoot::kJavaLangString, &string_class_handle_);
58   }
59 
GetThrowableClassHandle()60   ReferenceTypeInfo::TypeHandle GetThrowableClassHandle() {
61     return GetRootHandle(ClassRoot::kJavaLangThrowable, &throwable_class_handle_);
62   }
63 
64  private:
GetRootHandle(ClassRoot class_root,ReferenceTypeInfo::TypeHandle * cache)65   inline ReferenceTypeInfo::TypeHandle GetRootHandle(ClassRoot class_root,
66                                                      ReferenceTypeInfo::TypeHandle* cache) {
67     if (UNLIKELY(!ReferenceTypeInfo::IsValidHandle(*cache))) {
68       *cache = CreateRootHandle(handles_, class_root);
69     }
70     return *cache;
71   }
72 
73   static ReferenceTypeInfo::TypeHandle CreateRootHandle(VariableSizedHandleScope* handles,
74                                                         ClassRoot class_root);
75 
76   VariableSizedHandleScope* handles_;
77 
78   ReferenceTypeInfo::TypeHandle object_class_handle_;
79   ReferenceTypeInfo::TypeHandle class_class_handle_;
80   ReferenceTypeInfo::TypeHandle method_handle_class_handle_;
81   ReferenceTypeInfo::TypeHandle method_type_class_handle_;
82   ReferenceTypeInfo::TypeHandle string_class_handle_;
83   ReferenceTypeInfo::TypeHandle throwable_class_handle_;
84 };
85 
86 }  // namespace art
87 
88 #endif  // ART_COMPILER_OPTIMIZING_HANDLE_CACHE_H_
89 
90