[netcore] Remove approx. 10 icalls. (#18018)
[mono-project.git] / mono / metadata / marshal-windows.c
blob70c340245a870a58a7d9f95b4796e054dc4de33a
1 /**
2 * \file
3 * Windows marshal support.
5 * Copyright 2016 Microsoft
6 * Licensed under the MIT license. See LICENSE file in the project root for full license information.
7 */
8 #include <config.h>
9 #include <glib.h>
11 #if defined(HOST_WIN32)
12 #include <winsock2.h>
13 #include <windows.h>
14 #include <objbase.h>
15 #include "mono/metadata/marshal-windows-internals.h"
16 #include "icall-decl.h"
18 #if G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT)
19 void*
20 mono_marshal_alloc_hglobal (size_t size, MonoError *error)
22 void* p = GlobalAlloc (GMEM_FIXED, size);
23 if (!p)
24 mono_error_set_out_of_memory (error, "");
25 return p;
28 gpointer
29 mono_marshal_realloc_hglobal (gpointer ptr, size_t size)
31 return GlobalReAlloc (ptr, size, GMEM_MOVEABLE);
34 void
35 mono_marshal_free_hglobal (gpointer ptr)
37 GlobalFree (ptr);
39 #endif /* G_HAVE_API_SUPPORT(HAVE_CLASSIC_WINAPI_SUPPORT) */
41 void*
42 mono_marshal_alloc_co_task_mem (size_t size)
44 return CoTaskMemAlloc (size);
47 void
48 mono_marshal_free_co_task_mem (void *ptr)
50 CoTaskMemFree (ptr);
53 gpointer
54 mono_marshal_realloc_co_task_mem (gpointer ptr, size_t size)
56 return CoTaskMemRealloc (ptr, size);
59 char*
60 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (const gunichar2 *s, int length, MonoError *error);
62 char*
63 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (const gunichar2 *s, int length, MonoError *error)
65 g_assert_not_netcore ();
67 // FIXME pass mono_utf16_to_utf8 an allocator to avoid double alloc/copy.
69 char* tres = mono_utf16_to_utf8 (s, length, error);
70 return_val_if_nok (error, NULL);
71 if (!tres)
72 return tres;
75 * mono_utf16_to_utf8() returns a memory area at least as large as length,
76 * even if it contains NULL characters. The copy we allocate here has to be equally
77 * large.
79 size_t len = MAX (strlen (tres) + 1, length);
80 char* ret = (char*)ves_icall_System_Runtime_InteropServices_Marshal_AllocHGlobal (len, error);
81 if (ret)
82 memcpy (ret, tres, len);
83 g_free (tres);
84 return ret;
87 gpointer
88 mono_string_to_utf8str_impl (MonoStringHandle s, MonoError *error)
90 char *as, *tmp;
91 glong len;
92 GError *gerror = NULL;
94 if (MONO_HANDLE_IS_NULL (s))
95 return NULL;
97 if (!mono_string_handle_length (s)) {
98 as = (char*)CoTaskMemAlloc (1);
99 g_assert (as);
100 as [0] = '\0';
101 return as;
104 // FIXME pass g_utf16_to_utf8 an allocator to avoid double alloc/copy.
106 uint32_t gchandle = 0;
107 tmp = g_utf16_to_utf8 (mono_string_handle_pin_chars (s, &gchandle), mono_string_handle_length (s), NULL, &len, &gerror);
108 mono_gchandle_free_internal (gchandle);
109 if (gerror) {
110 mono_error_set_argument (error, "string", gerror->message);
111 g_error_free (gerror);
112 return NULL;
113 } else {
114 as = (char*)CoTaskMemAlloc (len + 1);
115 g_assert (as);
116 memcpy (as, tmp, len + 1);
117 g_free (tmp);
118 return as;
122 #endif /* HOST_WIN32 */