#include "mono/utils/mono-memory-model.h"
#include "mono/utils/atomic.h"
#include <mono/utils/mono-threads.h>
+#include <mono/utils/mono-error-internals.h>
#include <string.h>
#include <errno.h>
static void init_safe_handle (void);
+static void*
+ves_icall_marshal_alloc (gulong size);
+
/* MonoMethod pointers to SafeHandle::DangerousAddRef and ::DangerousRelease */
static MonoMethod *sh_dangerous_add_ref;
static MonoMethod *sh_dangerous_release;
register_icall (mono_ftnptr_to_delegate, "mono_ftnptr_to_delegate", "object ptr ptr", FALSE);
register_icall (mono_marshal_asany, "mono_marshal_asany", "ptr object int32 int32", FALSE);
register_icall (mono_marshal_free_asany, "mono_marshal_free_asany", "void object ptr int32 int32", FALSE);
- register_icall (mono_marshal_alloc, "mono_marshal_alloc", "ptr int32", FALSE);
+ register_icall (ves_icall_marshal_alloc, "ves_icall_marshal_alloc", "ptr int32", FALSE);
register_icall (mono_marshal_free, "mono_marshal_free", "void ptr", FALSE);
register_icall (mono_marshal_set_last_error, "mono_marshal_set_last_error", "void", FALSE);
register_icall (mono_marshal_set_last_error_windows, "mono_marshal_set_last_error_windows", "void int32", FALSE);
gchar*
mono_string_builder_to_utf8 (MonoStringBuilder *sb)
{
- GError *error = NULL;
+ MonoError error;
+ GError *gerror = NULL;
if (!sb)
return NULL;
guint str_len = mono_string_builder_string_length (sb);
- gchar *tmp = g_utf16_to_utf8 (str_utf16, str_len, NULL, NULL, &error);
+ gchar *tmp = g_utf16_to_utf8 (str_utf16, str_len, NULL, NULL, &gerror);
- if (error) {
- g_error_free (error);
+ if (gerror) {
+ g_error_free (gerror);
g_free (str_utf16);
mono_raise_exception (mono_get_exception_execution_engine ("Failed to convert StringBuilder from utf16 to utf8"));
return NULL;
} else {
guint len = mono_string_builder_capacity (sb) + 1;
- gchar *res = (gchar *)mono_marshal_alloc (len * sizeof (gchar));
+ gchar *res = (gchar *)mono_marshal_alloc (len * sizeof (gchar), &error);
+ mono_error_raise_exception (&error); /* FIXME don't raise here */
g_assert (str_len < len);
memcpy (res, tmp, str_len * sizeof (gchar));
res[str_len] = '\0';
gunichar2*
mono_string_builder_to_utf16 (MonoStringBuilder *sb)
{
+ MonoError error;
+
if (!sb)
return NULL;
if (len == 0)
len = 1;
- gunichar2 *str = (gunichar2 *)mono_marshal_alloc ((len + 1) * sizeof (gunichar2));
+ gunichar2 *str = (gunichar2 *)mono_marshal_alloc ((len + 1) * sizeof (gunichar2), &error);
+ mono_error_raise_exception (&error); /* FIXME don't raise here */
+
str[len] = '\0';
if (len == 0)
g_assert (m->retobj_var);
mono_mb_emit_icon (mb, mono_class_native_size (klass, NULL));
mono_mb_emit_byte (mb, CEE_CONV_I);
- mono_mb_emit_icall (mb, mono_marshal_alloc);
+ mono_mb_emit_icall (mb, ves_icall_marshal_alloc);
mono_mb_emit_stloc (mb, 1);
mono_mb_emit_ldloc (mb, 1);
mono_mb_emit_stloc (mb, m->retobj_var);
/* Allocate and set dest */
mono_mb_emit_icon (mb, mono_class_native_size (klass, NULL));
mono_mb_emit_byte (mb, CEE_CONV_I);
- mono_mb_emit_icall (mb, mono_marshal_alloc);
+ mono_mb_emit_icall (mb, ves_icall_marshal_alloc);
mono_mb_emit_stloc (mb, 1);
/* Update argument pointer */
/* Allocate and set dest */
mono_mb_emit_icon (mb, mono_class_native_size (klass, NULL));
mono_mb_emit_byte (mb, CEE_CONV_I);
- mono_mb_emit_icall (mb, mono_marshal_alloc);
+ mono_mb_emit_icall (mb, ves_icall_marshal_alloc);
mono_mb_emit_byte (mb, CEE_DUP);
mono_mb_emit_stloc (mb, 1);
mono_mb_emit_stloc (mb, 3);
mono_mb_emit_byte (mb, CEE_ADD);
}
mono_mb_emit_byte (mb, CEE_MUL);
- mono_mb_emit_icall (mb, mono_marshal_alloc);
+ mono_mb_emit_icall (mb, ves_icall_marshal_alloc);
mono_mb_emit_stloc (mb, dest);
mono_mb_emit_ldloc (mb, dest);
mono_mb_emit_stloc (mb, 3);
}
void*
-mono_marshal_alloc (gulong size)
+mono_marshal_alloc (gulong size, MonoError *error)
{
gpointer res;
+ mono_error_init (error);
+
#ifdef HOST_WIN32
res = CoTaskMemAlloc (size);
#else
res = g_try_malloc ((gulong)size);
if (!res)
- mono_gc_out_of_memory ((gulong)size);
+ mono_error_set_out_of_memory (error, "Could not allocate %i bytes", size);
#endif
return res;
}
+static void*
+ves_icall_marshal_alloc (gulong size)
+{
+ MonoError error;
+ void *ret = mono_marshal_alloc (size, &error);
+ mono_error_raise_exception (&error);
+
+ return ret;
+}
+
void
mono_marshal_free (gpointer ptr)
{
if (s == NULL) {
return NULL;
} else {
- gunichar2 *res = (gunichar2 *)mono_marshal_alloc ((mono_string_length (s) * 2) + 2);
+ MonoError error;
+ gunichar2 *res = (gunichar2 *)mono_marshal_alloc ((mono_string_length (s) * 2) + 2, &error);
+ mono_error_raise_exception (&error); /* FIXME don't raise here */
memcpy (res, mono_string_chars (s), mono_string_length (s) * 2);
res [mono_string_length (s)] = 0;
return res;
gpointer
mono_marshal_asany (MonoObject *o, MonoMarshalNative string_encoding, int param_attrs)
{
+ MonoError error;
MonoType *t;
MonoClass *klass;
klass->blittable || klass->enumtype))
return mono_object_unbox (o);
- res = mono_marshal_alloc (mono_class_native_size (klass, NULL));
+ res = mono_marshal_alloc (mono_class_native_size (klass, NULL), &error);
+ mono_error_raise_exception (&error); /* FIXME don't raise here */
if (!((param_attrs & PARAM_ATTRIBUTE_OUT) && !(param_attrs & PARAM_ATTRIBUTE_IN))) {
method = mono_marshal_get_struct_to_ptr (o->vtable->klass);