Skip to content

Commit c7a8651

Browse files
committed
[CLR] Enable more p/invokes
1 parent 2173aca commit c7a8651

16 files changed

+1418
-37
lines changed

src/Mono.Android/Android.Runtime/RuntimeNativeMethods.cs

-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ enum TraceKind : uint
2020

2121
internal static class RuntimeNativeMethods
2222
{
23-
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
24-
internal extern static void monodroid_log_traces (TraceKind kind, string first_line);
25-
2623
[DllImport (RuntimeConstants.InternalDllName, CallingConvention = CallingConvention.Cdecl)]
2724
internal extern static void monodroid_log (LogLevel level, LogCategories category, string message);
2825

src/native/clr/host/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ set(XAMARIN_MONODROID_SOURCES
3636
jni-remapping.cc
3737
os-bridge.cc
3838
pinvoke-override.cc
39+
runtime-util.cc
3940
typemap.cc
41+
xamarin_getifaddrs.cc
4042
)
4143

4244
list(APPEND LOCAL_CLANG_CHECK_SOURCES

src/native/clr/host/generate-pinvoke-tables.cc

+13-13
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ const std::vector<std::string> internal_pinvoke_names = {
3636
// "java_interop_free",
3737
// "monodroid_clear_gdb_wait",
3838
// "_monodroid_counters_dump",
39-
// "_monodroid_detect_cpu_and_architecture",
39+
"_monodroid_detect_cpu_and_architecture",
4040
// "monodroid_dylib_mono_free",
4141
// "monodroid_dylib_mono_init",
4242
// "monodroid_dylib_mono_new",
4343
// "monodroid_embedded_assemblies_set_assemblies_prefix",
4444
// "monodroid_fopen",
4545
"monodroid_free",
46-
// "_monodroid_freeifaddrs",
47-
// "_monodroid_gc_wait_for_bridge_processing",
46+
"_monodroid_freeifaddrs",
47+
"_monodroid_gc_wait_for_bridge_processing",
4848
// "_monodroid_get_dns_servers",
4949
// "monodroid_get_dylib",
50-
// "_monodroid_getifaddrs",
50+
"_monodroid_getifaddrs",
5151
// "monodroid_get_namespaced_system_property",
5252
// "_monodroid_get_network_interface_supports_multicast",
5353
// "_monodroid_get_network_interface_up_state",
@@ -60,21 +60,21 @@ const std::vector<std::string> internal_pinvoke_names = {
6060
// "monodroid_log_traces",
6161
"_monodroid_lookup_replacement_type",
6262
"_monodroid_lookup_replacement_method_info",
63-
// "_monodroid_lref_log_delete",
64-
// "_monodroid_lref_log_new",
65-
// "_monodroid_max_gref_get",
63+
"_monodroid_lref_log_delete",
64+
"_monodroid_lref_log_new",
65+
"_monodroid_max_gref_get",
6666
// "monodroid_strdup_printf",
6767
// "monodroid_strfreev",
6868
// "monodroid_strsplit",
69-
// "_monodroid_timezone_get_default_id",
70-
// "monodroid_timing_start",
71-
// "monodroid_timing_stop",
69+
"_monodroid_timezone_get_default_id",
70+
"monodroid_timing_start",
71+
"monodroid_timing_stop",
7272
"monodroid_TypeManager_get_java_class_name",
7373
"clr_typemap_managed_to_java",
7474
"clr_typemap_java_to_managed",
75-
// "_monodroid_weak_gref_delete",
76-
// "_monodroid_weak_gref_get",
77-
// "_monodroid_weak_gref_new",
75+
"_monodroid_weak_gref_delete",
76+
"_monodroid_weak_gref_get",
77+
"_monodroid_weak_gref_new",
7878
// "path_combine",
7979
// "recv_uninterrupted",
8080
// "send_uninterrupted",

src/native/clr/host/host.cc

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <host/host-jni.hh>
1414
#include <host/host-util.hh>
1515
#include <host/os-bridge.hh>
16+
#include <host/runtime-util.hh>
1617
#include <runtime-base/android-system.hh>
1718
#include <runtime-base/jni-wrappers.hh>
1819
#include <runtime-base/logger.hh>
@@ -264,7 +265,7 @@ void Host::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass runtimeCl
264265

265266
size_t total_time_index;
266267
if (FastTiming::enabled ()) [[unlikely]] {
267-
_timing = std::make_unique<Timing> ();
268+
_timing = std::make_shared<Timing> ();
268269
total_time_index = internal_timing.start_event (TimingEventKind::TotalRuntimeInit);
269270
}
270271

@@ -278,6 +279,8 @@ void Host::Java_mono_android_Runtime_initInternal (JNIEnv *env, jclass runtimeCl
278279
Util::set_environment_variable_for_directory ("HOME", home);
279280
create_xdg_directories_and_environment (home);
280281

282+
java_TimeZone = RuntimeUtil::get_class_from_runtime_field (env, runtimeClass, "java_util_TimeZone", true);
283+
281284
AndroidSystem::detect_embedded_dso_mode (applicationDirs);
282285
AndroidSystem::set_running_in_emulator (isEmulator);
283286
AndroidSystem::set_primary_override_dir (home);

src/native/clr/host/internal-pinvokes.cc

+104
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <host/host.hh>
22
#include <host/os-bridge.hh>
33
#include <host/typemap.hh>
4+
#include <runtime-base/android-system.hh>
5+
#include <runtime-base/cpu-arch.hh>
46
#include <runtime-base/internal-pinvokes.hh>
57
#include <runtime-base/jni-remapping.hh>
68

@@ -88,3 +90,105 @@ _monodroid_lookup_replacement_method_info (const char *jniSourceType, const char
8890
{
8991
return JniRemapping::lookup_replacement_method_info (jniSourceType, jniMethodName, jniMethodSignature);
9092
}
93+
94+
managed_timing_sequence* monodroid_timing_start (const char *message)
95+
{
96+
// Technically a reference here is against the idea of shared pointers, but
97+
// in this instance it's fine since we know we won't be storing the pointer
98+
// and this way things are slightly faster.
99+
std::shared_ptr<Timing> const &timing = Host::get_timing ();
100+
if (!timing) {
101+
return nullptr;
102+
}
103+
104+
managed_timing_sequence *ret = timing->get_available_sequence ();
105+
if (message != nullptr) {
106+
log_write (LOG_TIMING, LogLevel::Info, message);
107+
}
108+
ret->period.mark_start ();
109+
return ret;
110+
}
111+
112+
void monodroid_timing_stop (managed_timing_sequence *sequence, const char *message)
113+
{
114+
static constexpr const char DEFAULT_MESSAGE[] = "Managed Timing";
115+
if (sequence == nullptr) {
116+
return;
117+
}
118+
119+
std::shared_ptr<Timing> const &timing = Host::get_timing ();
120+
if (!timing) [[unlikely]] {
121+
return;
122+
}
123+
124+
sequence->period.mark_end ();
125+
Timing::info (sequence->period, message == nullptr ? DEFAULT_MESSAGE : message);
126+
timing->release_sequence (sequence);
127+
}
128+
129+
void _monodroid_weak_gref_new (jobject curHandle, char curType, jobject newHandle, char newType, const char *threadName, int threadId, const char *from, int from_writable)
130+
{
131+
OSBridge::_monodroid_weak_gref_new (curHandle, curType, newHandle, newType, threadName, threadId, from, from_writable);
132+
}
133+
134+
int _monodroid_weak_gref_get ()
135+
{
136+
return OSBridge::get_gc_weak_gref_count ();
137+
}
138+
139+
int _monodroid_max_gref_get ()
140+
{
141+
return static_cast<int>(AndroidSystem::get_max_gref_count ());
142+
}
143+
144+
void _monodroid_weak_gref_delete (jobject handle, char type, const char *threadName, int threadId, const char *from, int from_writable)
145+
{
146+
OSBridge::_monodroid_weak_gref_delete (handle, type, threadName, threadId, from, from_writable);
147+
}
148+
149+
void _monodroid_lref_log_new (int lrefc, jobject handle, char type, const char *threadName, int threadId, const char *from, int from_writable)
150+
{
151+
OSBridge::_monodroid_lref_log_new (lrefc, handle, type, threadName, threadId, from, from_writable);
152+
}
153+
154+
void _monodroid_lref_log_delete (int lrefc, jobject handle, char type, const char *threadName, int threadId, const char *from, int from_writable)
155+
{
156+
OSBridge::_monodroid_lref_log_delete (lrefc, handle, type, threadName, threadId, from, from_writable);
157+
}
158+
159+
void _monodroid_gc_wait_for_bridge_processing ()
160+
{
161+
// mono_gc_wait_for_bridge_processing (); - replace with the new GC bridge call, when we have it
162+
}
163+
164+
void _monodroid_detect_cpu_and_architecture (uint16_t *built_for_cpu, uint16_t *running_on_cpu, unsigned char *is64bit)
165+
{
166+
abort_if_invalid_pointer_argument (built_for_cpu, "built_for_cpu");
167+
abort_if_invalid_pointer_argument (running_on_cpu, "running_on_cpu");
168+
abort_if_invalid_pointer_argument (is64bit, "is64bit");
169+
170+
bool _64bit;
171+
monodroid_detect_cpu_and_architecture (*built_for_cpu, *running_on_cpu, _64bit);
172+
*is64bit = _64bit;
173+
}
174+
175+
void* _monodroid_timezone_get_default_id ()
176+
{
177+
JNIEnv *env = OSBridge::ensure_jnienv ();
178+
jmethodID getDefault = env->GetStaticMethodID (Host::get_java_class_TimeZone (), "getDefault", "()Ljava/util/TimeZone;");
179+
jmethodID getID = env->GetMethodID (Host::get_java_class_TimeZone (), "getID", "()Ljava/lang/String;");
180+
jobject d = env->CallStaticObjectMethod (Host::get_java_class_TimeZone (), getDefault);
181+
jstring id = reinterpret_cast<jstring> (env->CallObjectMethod (d, getID));
182+
const char *mutf8 = env->GetStringUTFChars (id, nullptr);
183+
if (mutf8 == nullptr) {
184+
log_error (LOG_DEFAULT, "Failed to convert Java TimeZone ID to UTF8 (out of memory?)"sv);
185+
env->DeleteLocalRef (id);
186+
env->DeleteLocalRef (d);
187+
return nullptr;
188+
}
189+
char *def_id = strdup (mutf8);
190+
env->ReleaseStringUTFChars (id, mutf8);
191+
env->DeleteLocalRef (id);
192+
env->DeleteLocalRef (d);
193+
return def_id;
194+
}

0 commit comments

Comments
 (0)