Skip to content

Commit 7efda5c

Browse files
committed
Address feedback
1 parent 7cba241 commit 7efda5c

File tree

2 files changed

+37
-30
lines changed

2 files changed

+37
-30
lines changed

src/native/pinvoke-override/pinvoke-override-api-impl.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace xamarin::android {
2020

2121
if (lib_handle == nullptr) {
2222
// We're being called as part of the p/invoke mechanism, we don't need to look in the AOT cache
23-
constexpr bool USE_AOT_CACHE = false;
24-
lib_handle = internal::MonodroidDl::monodroid_dlopen (library_name, MONO_DL_LOCAL, nullptr, USE_AOT_CACHE);
23+
constexpr bool PREFER_AOT_CACHE = false;
24+
lib_handle = internal::MonodroidDl::monodroid_dlopen (library_name, MONO_DL_LOCAL, nullptr, PREFER_AOT_CACHE);
2525
if (lib_handle == nullptr) {
2626
log_warn (LOG_ASSEMBLY, "Shared library '%s' not loaded, p/invoke '%s' may fail", library_name, symbol_name);
2727
return nullptr;

src/native/runtime-base/monodroid-dl.hh

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ namespace xamarin::android::internal
1919
{
2020
class MonodroidDl
2121
{
22+
enum class CacheKind
23+
{
24+
// Access AOT cache
25+
AOT,
26+
27+
// Access DSO cache
28+
DSO,
29+
};
30+
2231
static inline xamarin::android::mutex dso_handle_write_lock;
2332

2433
static unsigned int convert_dl_flags (int flags) noexcept
@@ -29,18 +38,20 @@ namespace xamarin::android::internal
2938
return lflags;
3039
}
3140

32-
template<bool AotCache>
41+
template<CacheKind WhichCache>
3342
[[gnu::always_inline, gnu::flatten]]
3443
static DSOCacheEntry* find_dso_cache_entry_common (hash_t hash) noexcept
3544
{
45+
static_assert (WhichCache == CacheKind::AOT || WhichCache == CacheKind::DSO, "Unknown cache type specified");
46+
3647
DSOCacheEntry *arr;
3748
size_t arr_size;
3849

39-
if constexpr (AotCache) {
50+
if constexpr (WhichCache == CacheKind::AOT) {
4051
log_debug (LOG_ASSEMBLY, "Looking for hash 0x%x in AOT cache", hash);
4152
arr = aot_dso_cache;
4253
arr_size = application_config.number_of_aot_cache_entries;
43-
} else {
54+
} else if constexpr (WhichCache == CacheKind::DSO) {
4455
log_debug (LOG_ASSEMBLY, "Looking for hash 0x%x in DSO cache", hash);
4556
arr = dso_cache;
4657
arr_size = application_config.number_of_dso_cache_entries;
@@ -60,32 +71,13 @@ namespace xamarin::android::internal
6071
[[gnu::always_inline, gnu::flatten]]
6172
static DSOCacheEntry* find_only_aot_cache_entry (hash_t hash) noexcept
6273
{
63-
constexpr bool IsAotCache = true;
64-
return find_dso_cache_entry_common<IsAotCache> (hash);
74+
return find_dso_cache_entry_common<CacheKind::AOT> (hash);
6575
}
6676

6777
[[gnu::always_inline, gnu::flatten]]
6878
static DSOCacheEntry* find_only_dso_cache_entry (hash_t hash) noexcept
6979
{
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);
8981
}
9082

9183
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
150142

151143
public:
152144
[[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
154146
{
155147
if (name == nullptr) {
156148
log_warn (LOG_ASSEMBLY, "monodroid_dlopen got a null name. This is not supported in NET+");
@@ -159,7 +151,22 @@ namespace xamarin::android::internal
159151

160152
hash_t name_hash = xxhash::hash (name, strlen (name));
161153
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+
163170
log_debug (LOG_ASSEMBLY, "monodroid_dlopen: hash match %sfound, DSO name is '%s'", dso == nullptr ? "not " : "", dso == nullptr ? "<unknown>" : dso->name);
164171

165172
if (dso == nullptr) {
@@ -213,8 +220,8 @@ namespace xamarin::android::internal
213220
{
214221
// We're called by MonoVM via a callback, we might need to return an AOT DSO.
215222
// 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);
218225
}
219226

220227
[[gnu::flatten]]

0 commit comments

Comments
 (0)