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.
11 #if defined(HOST_WIN32)
12 #include <mono/utils/mono-compiler.h>
13 MONO_PRAGMA_WARNING_PUSH()
14 MONO_PRAGMA_WARNING_DISABLE (4115) // warning C4115: 'IRpcStubBuffer': named type definition in parentheses
18 MONO_PRAGMA_WARNING_POP()
19 #include "mono/metadata/marshal-internals.h"
20 #include <mono/utils/w32subset.h>
21 #include "icall-decl.h"
23 #if HAVE_API_SUPPORT_WIN32_GLOBAL_ALLOC_FREE
26 mono_marshal_alloc_hglobal (size_t size
)
28 return GlobalAlloc (GMEM_FIXED
, size
);
32 mono_marshal_realloc_hglobal (gpointer ptr
, size_t size
)
34 return GlobalReAlloc (ptr
, size
, GMEM_MOVEABLE
);
38 mono_marshal_free_hglobal (gpointer ptr
)
42 #elif !HAVE_EXTERN_DEFINED_WIN32_GLOBAL_ALLOC_FREE
44 mono_marshal_alloc_hglobal (size_t size
)
46 return HeapAlloc (GetProcessHeap (), 0, size
);
50 mono_marshal_realloc_hglobal (gpointer ptr
, size_t size
)
52 return HeapReAlloc (GetProcessHeap (), 0, ptr
, size
);
56 mono_marshal_free_hglobal (gpointer ptr
)
58 HeapFree (GetProcessHeap (), 0, ptr
);
60 #endif /* HAVE_API_SUPPORT_WIN32_GLOBAL_ALLOC_FREE */
63 mono_marshal_alloc_co_task_mem (size_t size
)
65 return CoTaskMemAlloc (size
);
69 mono_marshal_free_co_task_mem (void *ptr
)
75 mono_marshal_realloc_co_task_mem (gpointer ptr
, size_t size
)
77 return CoTaskMemRealloc (ptr
, size
);
81 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (const gunichar2
*s
, int length
);
84 ves_icall_System_Runtime_InteropServices_Marshal_StringToHGlobalAnsi (const gunichar2
*s
, int length
)
86 // FIXME pass mono_utf16_to_utf8 an allocator to avoid double alloc/copy.
91 char* tres
= mono_utf16_to_utf8 (s
, length
, error
);
92 if (!tres
|| !is_ok (error
))
96 * mono_utf16_to_utf8() returns a memory area at least as large as length,
97 * even if it contains NULL characters. The copy we allocate here has to be equally
100 len
= MAX (strlen (tres
) + 1, length
);
101 ret
= (char*)mono_marshal_alloc_hglobal_error (len
, error
);
103 memcpy (ret
, tres
, len
);
106 mono_error_set_pending_exception (error
);
111 mono_string_to_utf8str_impl (MonoStringHandle s
, MonoError
*error
)
115 GError
*gerror
= NULL
;
117 if (MONO_HANDLE_IS_NULL (s
))
120 if (!mono_string_handle_length (s
)) {
121 as
= (char*)CoTaskMemAlloc (1);
127 // FIXME pass g_utf16_to_utf8 an allocator to avoid double alloc/copy.
129 MonoGCHandle gchandle
= NULL
;
130 tmp
= g_utf16_to_utf8 (mono_string_handle_pin_chars (s
, &gchandle
), mono_string_handle_length (s
), NULL
, &len
, &gerror
);
131 mono_gchandle_free_internal (gchandle
);
133 mono_error_set_argument (error
, "string", gerror
->message
);
134 g_error_free (gerror
);
137 as
= (char*)CoTaskMemAlloc (len
+ 1);
139 memcpy (as
, tmp
, len
+ 1);
145 #endif /* HOST_WIN32 */