Skip to content

Commit

Permalink
More consistent use of std::string_view + remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
grendello committed Nov 7, 2024
1 parent 958a16d commit a299f4c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 83 deletions.
17 changes: 11 additions & 6 deletions src/native/monodroid/embedded-assemblies.hh
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,28 @@ namespace xamarin::android::internal {

static constexpr std::string_view zip_path_separator { "/" };
static constexpr std::string_view apk_lib_dir_name { "lib" };

static constexpr size_t assemblies_prefix_size = calc_size(apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator);
static constexpr auto assemblies_prefix = concat_string_views<assemblies_prefix_size> (apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator);
static constexpr auto assemblies_prefix_array = concat_string_views<assemblies_prefix_size> (apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator);
static constexpr std::string_view assemblies_prefix { assemblies_prefix_array };

// We have two records for each assembly, for names with and without the extension
static constexpr uint32_t assembly_store_index_entries_per_assembly = 2;
static constexpr uint32_t number_of_assembly_store_files = 1;
static constexpr std::string_view dso_suffix { ".so" };

static constexpr auto apk_lib_prefix = assemblies_prefix; // concat_const (apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator);
static constexpr std::string_view apk_lib_prefix = assemblies_prefix; // concat_const (apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator);
static constexpr std::string_view assembly_store_prefix { "libassemblies." };
static constexpr std::string_view assembly_store_extension { ".blob" };

static constexpr size_t assembly_store_file_name_size = calc_size (assembly_store_prefix, SharedConstants::android_lib_abi, assembly_store_extension, dso_suffix);
static constexpr auto assembly_store_file_name = concat_string_views<assembly_store_file_name_size> (assembly_store_prefix, SharedConstants::android_lib_abi, assembly_store_extension, dso_suffix);
static constexpr auto assembly_store_file_name_array = concat_string_views<assembly_store_file_name_size> (assembly_store_prefix, SharedConstants::android_lib_abi, assembly_store_extension, dso_suffix);
static constexpr std::string_view assembly_store_file_name { assembly_store_file_name_array };

static constexpr size_t assembly_store_file_path_size = calc_size(apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator, assembly_store_prefix, SharedConstants::android_lib_abi, assembly_store_extension, dso_suffix);
static constexpr auto assembly_store_file_path = concat_string_views<assembly_store_file_path_size> (apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator, assembly_store_prefix, SharedConstants::android_lib_abi, assembly_store_extension, dso_suffix);
static constexpr auto assembly_store_file_path_array = concat_string_views<assembly_store_file_path_size> (apk_lib_dir_name, zip_path_separator, SharedConstants::android_lib_abi, zip_path_separator, assembly_store_prefix, SharedConstants::android_lib_abi, assembly_store_extension, dso_suffix);
static constexpr std::string_view assembly_store_file_path { assembly_store_file_path_array };

static constexpr size_t dso_size_overhead = ArchiveDSOStubConfig::PayloadSectionOffset + (ArchiveDSOStubConfig::SectionHeaderEntryCount * ArchiveDSOStubConfig::SectionHeaderEntrySize);

public:
Expand Down Expand Up @@ -162,8 +167,8 @@ namespace xamarin::android::internal {
abort_unless (
runtime_config_data_size < std::numeric_limits<uint32_t>::max (),
[] {
return Util::monodroid_strdup_printf ("Runtime config binary blob size exceeds %u bytes",
std::numeric_limits<uint32_t>::max ());
return detail::_format_message ("Runtime config binary blob size exceeds %u bytes",
std::numeric_limits<uint32_t>::max ());
}
);
size = static_cast<uint32_t>(runtime_config_data_size);
Expand Down
7 changes: 5 additions & 2 deletions src/native/monodroid/monodroid-glue.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,10 @@ MonodroidRuntime::init_android_runtime (JNIEnv *env, jclass runtimeClass, jobjec
abort_unless (
registerType != nullptr,
[&error] {
return Util::monodroid_strdup_printf("INTERNAL ERROR: Unable to find Android.Runtime.JNIEnvInit.RegisterJniNatives! %s", mono_error_get_message (&error));
return detail::_format_message (
"INTERNAL ERROR: Unable to find Android.Runtime.JNIEnvInit.RegisterJniNatives! %s",
mono_error_get_message (&error)
);
}
);

Expand Down Expand Up @@ -939,7 +942,7 @@ MonodroidRuntime::init_android_runtime (JNIEnv *env, jclass runtimeClass, jobjec
abort_unless (
initialize != nullptr,
[&error] {
return Util::monodroid_strdup_printf (
return detail::_format_message (
"Failed to obtain unmanaged-callers-only pointer to the Android.Runtime.JNIEnvInit.Initialize method. %s",
mono_error_get_message (&error)
);
Expand Down
6 changes: 2 additions & 4 deletions src/native/monodroid/osbridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -850,9 +850,7 @@ OSBridge::gc_cleanup_after_java_collection (JNIEnv *env, int num_sccs, MonoGCBri
if (j > 0) {
abort_unless (
sccs [i]->is_alive,
[&i] {
return Util::monodroid_strdup_printf ("Bridge SCC at index %d must be alive", i);
}
[&i] { return detail::_format_message ("Bridge SCC at index %d must be alive", i); }
);
}
sccs [i]->is_alive = 1;
Expand All @@ -878,7 +876,7 @@ OSBridge::gc_cleanup_after_java_collection (JNIEnv *env, int num_sccs, MonoGCBri
} else {
abort_unless (
!sccs [i]->is_alive,
[&i] { return Util::monodroid_strdup_printf ("Bridge SCC at index %d must NOT be alive", i); }
[&i] { return detail::_format_message ("Bridge SCC at index %d must NOT be alive", i); }
);
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/native/runtime-base/shared-constants.hh
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,13 @@ namespace xamarin::android::internal
static constexpr std::string_view DLL_EXTENSION { ".dll" };
static constexpr std::string_view PDB_EXTENSION { ".pdb" };

private:
static constexpr std::string_view RUNTIME_CONFIG_BLOB_BASE_NAME { "libarc.bin" };
static constexpr size_t runtime_config_blob_name_size = calc_size (RUNTIME_CONFIG_BLOB_BASE_NAME, MANGLED_ASSEMBLY_NAME_EXT);
static constexpr auto RUNTIME_CONFIG_BLOB_NAME = concat_string_views<runtime_config_blob_name_size> (RUNTIME_CONFIG_BLOB_BASE_NAME, MANGLED_ASSEMBLY_NAME_EXT);
static constexpr auto RUNTIME_CONFIG_BLOB_NAME_ARRAY = concat_string_views<runtime_config_blob_name_size> (RUNTIME_CONFIG_BLOB_BASE_NAME, MANGLED_ASSEMBLY_NAME_EXT);

public:
static constexpr std::string_view RUNTIME_CONFIG_BLOB_NAME { RUNTIME_CONFIG_BLOB_NAME_ARRAY };
static constexpr std::string_view MONO_SGEN_SO { "libmonosgen-2.0.so" };
static constexpr std::string_view MONO_SGEN_ARCH_SO { "libmonosgen-" __BITNESS__ "-2.0.so" };
static constexpr std::string_view OVERRIDE_DIRECTORY_NAME { ".__override__" };
Expand Down
23 changes: 0 additions & 23 deletions src/native/runtime-base/util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ namespace xamarin::android
{
class Util
{
static constexpr std::array<char, 16> hex_chars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
static constexpr uint32_t ms_in_nsec = 1000000ULL;

public:
static void initialize () noexcept;

Expand Down Expand Up @@ -197,13 +194,6 @@ namespace xamarin::android
return p != nullptr && p [N - 1] == '\0';
}

template<size_t N>
static bool ends_with (const char *str, helper_char_array<N> const& end) noexcept
{
char *p = const_cast<char*> (strstr (str, end.data ()));
return p != nullptr && p [N - 1] == '\0';
}

template<size_t N, size_t MaxStackSize, typename TStorage, typename TChar = char>
static bool ends_with (internal::string_base<MaxStackSize, TStorage, TChar> const& str, const char (&end)[N]) noexcept
{
Expand All @@ -230,19 +220,6 @@ namespace xamarin::android
return memcmp (str.get () + len - end_length, end.data (), end_length) == 0;
}

template<size_t N, size_t MaxStackSize, typename TStorage, typename TChar = char>
static bool ends_with (internal::string_base<MaxStackSize, TStorage, TChar> const& str, helper_char_array<N> const& end) noexcept
{
constexpr size_t end_length = N - 1uz;

size_t len = str.length ();
if (len < end_length) [[unlikely]] {
return false;
}

return memcmp (str.get () + len - end_length, end.data (), end_length) == 0;
}

template<size_t MaxStackSize, typename TStorage, typename TChar = char>
static const TChar* find_last (internal::string_base<MaxStackSize, TStorage, TChar> const& str, const char ch) noexcept
{
Expand Down
72 changes: 25 additions & 47 deletions src/native/shared/cpp-util.hh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <array>
#include <cstdarg>
#include <cstdlib>
#include <cstdio>
#include <concepts>
#include <memory>
#include <source_location>
Expand All @@ -15,35 +16,33 @@

#include "helpers.hh"

namespace xamarin::android::detail {
[[gnu::always_inline, gnu::flatten]]
static inline const char*
_format_message (const char *format, ...) noexcept
{
va_list ap;
va_start (ap, format);

static inline void
do_abort_unless (const char* fmt, ...)
{
va_list ap;

va_start (ap, fmt);
char *message = nullptr;
int n = vasprintf (&message, fmt, ap);
va_end (ap);
char *message;
int ret = vasprintf (&message, format, ap);

xamarin::android::Helpers::abort_application (n == -1 ? "Unable to allocate memory for abort message" : message);
va_end (ap);
return ret == -1 ? "Out of memory" : message;
}
}

// #define abort_unless(_condition_, _fmt_, ...) \
// if (!(_condition_)) [[unlikely]] { \
// do_abort_unless ("%s:%d (%s): " _fmt_, __FILE__, __LINE__, __FUNCTION__, ## __VA_ARGS__); \
// }

template<std::invocable<> F>
[[gnu::always_inline, gnu::flatten]]
static inline void
abort_unless (bool condition, F&& get_message, std::source_location sloc = std::source_location::current ()) noexcept
{
static_assert (std::is_same<typename std::invoke_result<F>::type, const char*>::value, "get_message must return 'const char*'");

if (condition) [[likely]] {
return;
}

// static_assert (std::is_same<std::invoke_result<F>, char*>::value, "get_message must return 'char*'");
xamarin::android::Helpers::abort_application (std::invoke (get_message), true /* log_location */, sloc);
}

Expand All @@ -57,22 +56,27 @@ abort_unless (bool condition, const char *message, std::source_location sloc = s
xamarin::android::Helpers::abort_application (message, true /* log_location */, sloc);
}

// #define abort_if_invalid_pointer_argument(_ptr_) abort_unless ((_ptr_) != nullptr, "Parameter '%s' must be a valid pointer", #_ptr_)
// #define abort_if_negative_integer_argument(_arg_) abort_unless ((_arg_) > 0, "Parameter '%s' must be larger than 0", #_arg_)

template<typename T>
[[gnu::always_inline, gnu::flatten]]
static inline void
abort_if_invalid_pointer_argument (T *ptr, const char *ptr_name, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_unless (ptr != nullptr, "Parameter '%s' must be a valid pointer", sloc);
abort_unless (
ptr != nullptr,
[&ptr_name] { return xamarin::android::detail::_format_message ("Parameter '%s' must be a valid pointer", ptr_name); },
sloc
);
}

[[gnu::always_inline, gnu::flatten]]
static inline void
abort_if_negative_integer_argument (int arg, const char *arg_name, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_unless (arg > 0, "Parameter '%s' must be a valid pointer", sloc);
abort_unless (
arg > 0,
[&arg_name] { return xamarin::android::detail::_format_message ("Parameter '%s' must be a valid pointer", arg_name); },
sloc
);
}

// Helper to use in "printf debugging". Normally not used in code anywhere. No code should be shipped with any
Expand Down Expand Up @@ -106,32 +110,6 @@ namespace xamarin::android
template <typename T>
using c_unique_ptr = std::unique_ptr<T, CDeleter<T>>;

template<size_t Size>
struct helper_char_array final
{
constexpr char* data () noexcept
{
return _elems;
}

constexpr const char* data () const noexcept
{
return _elems;
}

constexpr char const& operator[] (size_t n) const noexcept
{
return _elems[n];
}

constexpr char& operator[] (size_t n) noexcept
{
return _elems[n];
}

char _elems[Size]{};
};

template<size_t Size>
using char_array = std::array<char, Size>;

Expand Down

0 comments on commit a299f4c

Please sign in to comment.