@@ -19,6 +19,15 @@ namespace xamarin::android::internal
19
19
{
20
20
class MonodroidDl
21
21
{
22
+ enum class CacheKind
23
+ {
24
+ // Access AOT cache
25
+ AOT,
26
+
27
+ // Access DSO cache
28
+ DSO,
29
+ };
30
+
22
31
static inline xamarin::android::mutex dso_handle_write_lock;
23
32
24
33
static unsigned int convert_dl_flags (int flags) noexcept
@@ -29,18 +38,20 @@ namespace xamarin::android::internal
29
38
return lflags;
30
39
}
31
40
32
- template <bool AotCache >
41
+ template <CacheKind WhichCache >
33
42
[[gnu::always_inline, gnu::flatten]]
34
43
static DSOCacheEntry* find_dso_cache_entry_common (hash_t hash) noexcept
35
44
{
45
+ static_assert (WhichCache == CacheKind::AOT || WhichCache == CacheKind::DSO, " Unknown cache type specified" );
46
+
36
47
DSOCacheEntry *arr;
37
48
size_t arr_size;
38
49
39
- if constexpr (AotCache ) {
50
+ if constexpr (WhichCache == CacheKind::AOT ) {
40
51
log_debug (LOG_ASSEMBLY, " Looking for hash 0x%x in AOT cache" , hash);
41
52
arr = aot_dso_cache;
42
53
arr_size = application_config.number_of_aot_cache_entries ;
43
- } else {
54
+ } else if constexpr (WhichCache == CacheKind::DSO) {
44
55
log_debug (LOG_ASSEMBLY, " Looking for hash 0x%x in DSO cache" , hash);
45
56
arr = dso_cache;
46
57
arr_size = application_config.number_of_dso_cache_entries ;
@@ -60,32 +71,13 @@ namespace xamarin::android::internal
60
71
[[gnu::always_inline, gnu::flatten]]
61
72
static DSOCacheEntry* find_only_aot_cache_entry (hash_t hash) noexcept
62
73
{
63
- constexpr bool IsAotCache = true ;
64
- return find_dso_cache_entry_common<IsAotCache> (hash);
74
+ return find_dso_cache_entry_common<CacheKind::AOT> (hash);
65
75
}
66
76
67
77
[[gnu::always_inline, gnu::flatten]]
68
78
static DSOCacheEntry* find_only_dso_cache_entry (hash_t hash) noexcept
69
79
{
70
- constexpr bool IsAotCache = false ;
71
- return find_dso_cache_entry_common<IsAotCache> (hash);
72
- }
73
-
74
- [[gnu::always_inline, gnu::flatten]]
75
- static DSOCacheEntry* find_any_dso_cache_entry (hash_t hash) noexcept
76
- {
77
- // If we're asked to look in the AOT DSO cache, do it first. This is because we're likely called from the
78
- // MonoVM's dlopen fallback handler and it will not be a request to resolved a p/invoke, but most likely to
79
- // find and load an AOT image for a managed assembly. Since there might be naming/hash conflicts in this
80
- // scenario, we look at the AOT cache first.
81
- //
82
- // See: https://github.com/dotnet/android/issues/9081
83
- DSOCacheEntry *ret = find_only_aot_cache_entry (hash);
84
- if (ret != nullptr ) {
85
- return ret;
86
- }
87
-
88
- return find_only_dso_cache_entry (hash);
80
+ return find_dso_cache_entry_common<CacheKind::DSO> (hash);
89
81
}
90
82
91
83
static void * monodroid_dlopen_log_and_return (void *handle, char **err, const char *full_name, bool free_memory)
@@ -150,7 +142,7 @@ namespace xamarin::android::internal
150
142
151
143
public:
152
144
[[gnu::flatten]]
153
- static void * monodroid_dlopen (const char *name, int flags, char **err, bool use_aot_cache ) noexcept
145
+ static void * monodroid_dlopen (const char *name, int flags, char **err, bool prefer_aot_cache ) noexcept
154
146
{
155
147
if (name == nullptr ) {
156
148
log_warn (LOG_ASSEMBLY, " monodroid_dlopen got a null name. This is not supported in NET+" );
@@ -159,7 +151,22 @@ namespace xamarin::android::internal
159
151
160
152
hash_t name_hash = xxhash::hash (name, strlen (name));
161
153
log_debug (LOG_ASSEMBLY, " monodroid_dlopen: hash for name '%s' is 0x%zx" , name, name_hash);
162
- DSOCacheEntry *dso = use_aot_cache ? find_any_dso_cache_entry (name_hash) : find_only_dso_cache_entry (name_hash);
154
+
155
+ DSOCacheEntry *dso = nullptr ;
156
+ if (prefer_aot_cache) {
157
+ // If we're asked to look in the AOT DSO cache, do it first. This is because we're likely called from the
158
+ // MonoVM's dlopen fallback handler and it will not be a request to resolved a p/invoke, but most likely to
159
+ // find and load an AOT image for a managed assembly. Since there might be naming/hash conflicts in this
160
+ // scenario, we look at the AOT cache first.
161
+ //
162
+ // See: https://github.com/dotnet/android/issues/9081
163
+ dso = find_only_aot_cache_entry (name_hash);
164
+ }
165
+
166
+ if (dso == nullptr ) {
167
+ dso = find_only_dso_cache_entry (name_hash);
168
+ }
169
+
163
170
log_debug (LOG_ASSEMBLY, " monodroid_dlopen: hash match %sfound, DSO name is '%s'" , dso == nullptr ? " not " : " " , dso == nullptr ? " <unknown>" : dso->name );
164
171
165
172
if (dso == nullptr ) {
@@ -213,8 +220,8 @@ namespace xamarin::android::internal
213
220
{
214
221
// We're called by MonoVM via a callback, we might need to return an AOT DSO.
215
222
// See: https://github.com/dotnet/android/issues/9081
216
- constexpr bool USE_AOT_CACHE = true ;
217
- return monodroid_dlopen (name, flags, err, USE_AOT_CACHE );
223
+ constexpr bool PREFER_AOT_CACHE = true ;
224
+ return monodroid_dlopen (name, flags, err, PREFER_AOT_CACHE );
218
225
}
219
226
220
227
[[gnu::flatten]]
0 commit comments