Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c95d321
`-fstack-clash-protection` is a linker flag
grendello Jan 15, 2024
d98858c
Disable false negative warning for array bounds checking
grendello Jan 15, 2024
6a82a2d
Use `std::array<T,S>` for override_dirs
grendello Jan 15, 2024
de9a18d
Optimize startup a tiny bit
grendello Jan 16, 2024
fc5eff0
`std::string_view` all the way (even if it's fugly :P)
grendello Jan 16, 2024
f55b42a
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 17, 2024
1176d3d
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 17, 2024
81c05a4
Fix finding assembly blob entries in the apk
grendello Jan 17, 2024
6e90924
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 17, 2024
5f9be55
nicer, a bit. Still a fugly hack
grendello Jan 17, 2024
4e712cc
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 18, 2024
06329f8
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 25, 2024
0939db2
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 25, 2024
fb17eec
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 25, 2024
a4987ef
Migration from to continues
grendello Jan 25, 2024
383f887
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Jan 26, 2024
3ea6c6a
More std:string_view-ication
grendello Jan 26, 2024
66f3db0
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Feb 7, 2024
1216016
Remove all the WINDOWS, APPLE, desktop and !NET code
grendello Feb 7, 2024
154e70f
Merge branch 'main' into dev/grendel/cpp-tweaks
grendello Feb 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Optimize startup a tiny bit
Use `std::span` and `std::array` for application library directories in
a way that allows us to avoid memory allocation in the most common
scenario (where shared libraries aren't extraced to the filesystem but
reside in one of the split apks)
  • Loading branch information
grendello committed Jan 16, 2024
commit de9a18db07f5f5b9ee29ba4ec4718df2380c6f83
7 changes: 4 additions & 3 deletions src/monodroid/jni/android-system.cc
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ AndroidSystem::load_dso_from_specified_dirs (const char **directories, size_t nu
void*
AndroidSystem::load_dso_from_app_lib_dirs (const char *name, unsigned int dl_flags)
{
return load_dso_from_specified_dirs (static_cast<const char**> (app_lib_directories), app_lib_directories_size, name, dl_flags);
return load_dso_from_specified_dirs (app_lib_directories.data (), app_lib_directories.size (), name, dl_flags);
}

void*
Expand Down Expand Up @@ -448,9 +448,10 @@ AndroidSystem::get_full_dso_path_on_disk (const char *dso_name, dynamic_local_st
return true;
}
#endif
for (size_t i = 0; i < app_lib_directories_size; i++) {
if (get_existing_dso_path_on_disk (app_lib_directories [i], dso_name, path))
for (const char *app_lib_dir : app_lib_directories) {
if (get_existing_dso_path_on_disk (app_lib_dir, dso_name, path)) {
return true;
}
}

return false;
Expand Down
30 changes: 18 additions & 12 deletions src/monodroid/jni/basic-android-system.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
#include <cerrno>

#include "basic-android-system.hh"
#include "cpp-util.hh"
#include "globals.hh"

using namespace xamarin::android;
using namespace xamarin::android::internal;

const char **BasicAndroidSystem::app_lib_directories;
size_t BasicAndroidSystem::app_lib_directories_size = 0;
const char* BasicAndroidSystem::built_for_abi_name = nullptr;

void
BasicAndroidSystem::detect_embedded_dso_mode (jstring_array_wrapper& appDirs) noexcept
{
Expand All @@ -31,14 +25,21 @@ BasicAndroidSystem::setup_app_library_directories (jstring_array_wrapper& runtim
{
if (!is_embedded_dso_mode_enabled ()) {
log_debug (LOG_DEFAULT, "Setting up for DSO lookup in app data directories");
BasicAndroidSystem::app_lib_directories_size = 1;
BasicAndroidSystem::app_lib_directories = new const char*[app_lib_directories_size]();

BasicAndroidSystem::app_lib_directories = std::span<const char*> (single_app_lib_directory);
BasicAndroidSystem::app_lib_directories [0] = utils.strdup_new (appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX].get_cstr ());
log_debug (LOG_ASSEMBLY, "Added filesystem DSO lookup location: %s", appDirs[SharedConstants::APP_DIRS_DATA_DIR_INDEX].get_cstr ());
} else {
log_debug (LOG_DEFAULT, "Setting up for DSO lookup directly in the APK");
BasicAndroidSystem::app_lib_directories_size = runtimeApks.get_length ();
BasicAndroidSystem::app_lib_directories = new const char*[app_lib_directories_size]();

if (have_split_apks) {
// If split apks are used, then we will have just a single app library directory. Don't allocate any memory
// dynamically in this case
BasicAndroidSystem::app_lib_directories = std::span<const char*> (single_app_lib_directory);
} else {
size_t app_lib_directories_size = have_split_apks ? 1 : runtimeApks.get_length ();
BasicAndroidSystem::app_lib_directories = std::span<const char*> (new const char*[app_lib_directories_size], app_lib_directories_size);
}

unsigned short built_for_cpu = 0, running_on_cpu = 0;
unsigned char is64bit = 0;
Expand All @@ -61,7 +62,7 @@ BasicAndroidSystem::for_each_apk (jstring_array_wrapper &runtimeApks, ForEachApk
force_inline void
BasicAndroidSystem::add_apk_libdir (const char *apk, size_t &index, const char *abi) noexcept
{
abort_unless (index < app_lib_directories_size, "Index out of range");
abort_unless (index < app_lib_directories.size (), "Index out of range");
app_lib_directories [index] = utils.string_concat (apk, "!/lib/", abi);
log_debug (LOG_ASSEMBLY, "Added APK DSO lookup location: %s", app_lib_directories[index]);
index++;
Expand All @@ -87,7 +88,12 @@ BasicAndroidSystem::setup_apk_directories (unsigned short running_on_cpu, jstrin
}
}

app_lib_directories_size = number_of_added_directories;
if (app_lib_directories.size () == number_of_added_directories) [[likely]] {
return;
}

abort_unless (number_of_added_directories > 0, "At least a single application lib directory must be added");
app_lib_directories = app_lib_directories.subspan (0, number_of_added_directories);
}

char*
Expand Down
12 changes: 8 additions & 4 deletions src/monodroid/jni/basic-android-system.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <array>
#include <cstddef>
#include <cstdint>
#include <span>

#include "cpu-arch.hh"
#include "jni-wrappers.hh"
Expand Down Expand Up @@ -33,7 +34,7 @@ namespace xamarin::android::internal
#pragma clang diagnostic pop
#endif
static constexpr size_t ANDROID_ABI_NAMES_SIZE = sizeof(android_abi_names) / sizeof (android_abi_names[0]);
static const char* built_for_abi_name;
inline static const char* built_for_abi_name = nullptr;

public:
#ifdef ANDROID64
Expand All @@ -52,9 +53,12 @@ namespace xamarin::android::internal
static constexpr char SYSTEM_LIB_PATH[] = "";
#endif

inline static std::array<char*, 1> override_dirs;
static const char **app_lib_directories;
static size_t app_lib_directories_size;
inline static std::array<char*, 1> override_dirs{};

// This optimizes things a little bit. The array is allocated at build time, so we pay no cost for its
// allocation and at run time it allows us to skip dynamic memory allocation.
inline static std::array<const char*, 1> single_app_lib_directory{};
inline static std::span<const char*> app_lib_directories;
static const char* get_built_for_abi_name ();

public:
Expand Down
8 changes: 4 additions & 4 deletions src/monodroid/jni/debug-app-helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ get_libmonosgen_path ()
}
}

for (size_t i = 0; i < BasicAndroidSystem::app_lib_directories_size; i++) {
if (runtime_exists (BasicAndroidSystem::app_lib_directories [i], libmonoso)) {
for (const char *app_lib_dir : BasicAndroidSystem::app_lib_directories) {
if (runtime_exists (app_lib_dir, libmonoso)) {
return libmonoso;
}
}
Expand Down Expand Up @@ -270,8 +270,8 @@ get_libmonosgen_path ()
log_fatal (LOG_DEFAULT, " %s", od);
}

for (size_t i = 0; i < BasicAndroidSystem::app_lib_directories_size; i++) {
log_fatal (LOG_DEFAULT, " %s", BasicAndroidSystem::app_lib_directories [i]);
for (const char *app_lib_dir : BasicAndroidSystem::app_lib_directories) {
log_fatal (LOG_DEFAULT, " %s", app_lib_dir);
}

log_fatal (LOG_DEFAULT, "Do you have a shared runtime build of your app with AndroidManifest.xml android:minSdkVersion < 10 while running on a 64-bit Android 5.0 target? This combination is not supported.");
Expand Down