Skip to content

Commit

Permalink
Enabled g_mem_set_vtable through the configure option --with-overrida…
Browse files Browse the repository at this point in the history
…ble-allocators and exposed through mono_set_allocator_vtable
  • Loading branch information
Dunati authored and kumpera committed Jul 11, 2016
1 parent c516dda commit 6474f8e
Show file tree
Hide file tree
Showing 16 changed files with 127 additions and 41 deletions.
13 changes: 12 additions & 1 deletion eglib/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,17 @@ AC_CHECK_FUNCS(strlcpy stpcpy strtok_r rewinddir vasprintf)
AC_CHECK_FUNCS(getrlimit)
AC_CHECK_FUNCS(fork execv execve)

AC_ARG_WITH([overidable-allocators], [ --with-overridable-allocators allow g_*alloc/g_free to call custom allocators set via g_mem_set_vtable])

if test x$with_overridable_allocators == xyes; then
ENABLE_OVERRIDABLE_ALLOCATORS="1"
AC_MSG_NOTICE([Overridable allocator support enabled])
else
ENABLE_OVERRIDABLE_ALLOCATORS="0"
AC_MSG_NOTICE([Overridable allocator support disabled])
fi
AC_SUBST(ENABLE_OVERRIDABLE_ALLOCATORS)

#
# Mono currently supports 10.6, but strndup is not available prior to 10.7; avoiding
# the detection of strndup on OS X so Mono built on 10.7+ still runs on 10.6. This can be
Expand All @@ -151,7 +162,7 @@ elif test x$target_ios = xno; then
AC_CHECK_FUNCS(strndup getpwuid_r)
fi

AM_CONDITIONAL(NEED_VASPRINTF, test x$ac_cv_func_vasprintf = xno )
AM_CONDITIONAL(NEED_VASPRINTF, test x$ac_cv_func_vasprintf = xno || test x$with_overridable_allocators == xyes)
AM_ICONV()
AC_SEARCH_LIBS(sqrtf, m)

Expand Down
4 changes: 4 additions & 0 deletions eglib/src/eglib-config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#define G_HAVE_ALLOCA_H
#endif

#if @ENABLE_OVERRIDABLE_ALLOCATORS@ == 1
#define G_OVERRIDABLE_ALLOCATORS
#endif

typedef unsigned @GSIZE@ gsize;
typedef signed @GSIZE@ gssize;

Expand Down
10 changes: 4 additions & 6 deletions eglib/src/gerror.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <config.h>
#include <glib.h>

#include "vasprintf.h"

GError *
g_error_new (gpointer domain, gint code, const char *format, ...)
{
Expand All @@ -42,7 +40,7 @@ g_error_new (gpointer domain, gint code, const char *format, ...)
err->code = code;

va_start (args, format);
if (vasprintf (&err->message, format, args) == -1)
if (g_vasprintf (&err->message, format, args) == -1)
err->message = g_strdup_printf ("internal: invalid format string %s", format);
va_end (args);

Expand All @@ -57,7 +55,7 @@ g_error_vnew (gpointer domain, gint code, const char *format, va_list ap)
err->domain = domain;
err->code = code;

if (vasprintf (&err->message, format, ap) == -1)
if (g_vasprintf (&err->message, format, ap) == -1)
err->message = g_strdup_printf ("internal: invalid format string %s", format);

return err;
Expand All @@ -77,7 +75,7 @@ g_error_free (GError *error)
{
g_return_if_fail (error != NULL);

free (error->message);
g_free (error->message);
g_free (error);
}

Expand Down
17 changes: 12 additions & 5 deletions eglib/src/glib.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#ifndef __GLIB_H
#define __GLIB_H

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -9,6 +8,7 @@
#include <ctype.h>
#include <limits.h>


#ifdef _MSC_VER
#pragma include_alias(<eglib-config.h>, <eglib-config.hw>)
#endif
Expand Down Expand Up @@ -126,6 +126,7 @@ void g_free (void *ptr);
gpointer g_realloc (gpointer obj, gsize size);
gpointer g_malloc (gsize x);
gpointer g_malloc0 (gsize x);
gpointer g_calloc (gsize n, gsize x);
gpointer g_try_malloc (gsize x);
gpointer g_try_realloc (gpointer obj, gsize size);

Expand All @@ -138,19 +139,21 @@ gpointer g_try_realloc (gpointer obj, gsize size);
#define g_alloca(size) alloca (size)

gpointer g_memdup (gconstpointer mem, guint byte_size);
static inline gchar *g_strdup (const gchar *str) { if (str) {return strdup (str);} return NULL; }
static inline gchar *g_strdup (const gchar *str) { if (str) { return (gchar*) g_memdup(str, (guint)strlen (str) + 1); } return NULL; }
gchar **g_strdupv (gchar **str_array);

typedef struct {
gpointer (*malloc) (gsize n_bytes);
gpointer (*realloc) (gpointer mem, gsize n_bytes);
void (*free) (gpointer mem);
gpointer (*calloc) (gsize n_blocks, gsize n_block_bytes);
gpointer (*try_malloc) (gsize n_bytes);
gpointer (*try_realloc) (gpointer mem, gsize n_bytes);
} GMemVTable;

#define g_mem_set_vtable(x)
#if defined (G_OVERRIDABLE_ALLOCATORS)
void g_mem_set_vtable (GMemVTable* vtable);
#else
#define g_mem_set_vtable (x)
#endif

struct _GMemChunk {
guint alloc_size;
Expand Down Expand Up @@ -227,7 +230,11 @@ gint g_snprintf (gchar *string, gulong n, gchar const *format, ..
#define g_vfprintf vfprintf
#define g_vsprintf vsprintf
#define g_vsnprintf vsnprintf
#if defined (G_OVERRIDABLE_ALLOCATORS) || !defined (HAVE_VASPRINTF)
gint g_vasprintf (gchar **ret, const gchar *fmt, va_list ap);
#else
#define g_vasprintf vasprintf
#endif

gsize g_strlcpy (gchar *dest, const gchar *src, gsize dest_size);
gchar *g_stpcpy (gchar *dest, const char *src);
Expand Down
2 changes: 1 addition & 1 deletion eglib/src/gmarkup.c
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ g_markup_parse_context_parse (GMarkupParseContext *context,
if (context->parser.end_element != NULL && context->state == START_ELEMENT){
context->parser.end_element (context, ename, context->user_data, error);
if (error != NULL && *error != NULL){
free (ename);
g_free (ename);
goto fail;
}
}
Expand Down
52 changes: 39 additions & 13 deletions eglib/src/gmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,33 @@
#include <string.h>
#include <glib.h>

#if defined (G_OVERRIDABLE_ALLOCATORS)

static GMemVTable sGMemVTable = { malloc, realloc, free, calloc };

void
g_mem_set_vtable (GMemVTable* vtable)
{
sGMemVTable.calloc = vtable->calloc ? vtable->calloc : calloc;
sGMemVTable.realloc = vtable->realloc ? vtable->realloc : realloc;
sGMemVTable.malloc = vtable->malloc ? vtable->malloc : malloc;
sGMemVTable.free = vtable->free ? vtable->free : free;
}
#define G_FREE_INTERNAL sGMemVTable.free
#define G_REALLOC_INTERNAL sGMemVTable.realloc
#define G_CALLOC_INTERNAL sGMemVTable.calloc
#define G_MALLOC_INTERNAL sGMemVTable.malloc
#else
#define G_FREE_INTERNAL free
#define G_REALLOC_INTERNAL realloc
#define G_CALLOC_INTERNAL calloc
#define G_MALLOC_INTERNAL malloc
#endif
void
g_free (void *ptr)
{
if (ptr != NULL)
free (ptr);
G_FREE_INTERNAL (ptr);
}

gpointer
Expand All @@ -58,7 +80,7 @@ gpointer g_realloc (gpointer obj, gsize size)
g_free (obj);
return 0;
}
ptr = realloc (obj, size);
ptr = G_REALLOC_INTERNAL (obj, size);
if (ptr)
return ptr;
g_error ("Could not allocate %i bytes", size);
Expand All @@ -70,36 +92,40 @@ g_malloc (gsize x)
gpointer ptr;
if (!x)
return 0;
ptr = malloc (x);
ptr = G_MALLOC_INTERNAL (x);
if (ptr)
return ptr;
g_error ("Could not allocate %i bytes", x);
}

gpointer g_calloc (gsize n, gsize x)
{
gpointer ptr;
if (!x || !n)
return 0;
ptr = G_CALLOC_INTERNAL (n, x);
if (ptr)
return ptr;
g_error ("Could not allocate %i (%i * %i) bytes", x*n, n, x);
}
gpointer g_malloc0 (gsize x)
{
gpointer ptr;
if (!x)
return 0;
ptr = calloc(1,x);
if (ptr)
return ptr;
g_error ("Could not allocate %i bytes", x);
return g_calloc (1,x);
}

gpointer g_try_malloc (gsize x)
{
if (x)
return malloc (x);
return G_MALLOC_INTERNAL (x);
return 0;
}


gpointer g_try_realloc (gpointer obj, gsize size)
{
if (!size) {
g_free (obj);
G_FREE_INTERNAL (obj);
return 0;
}
return realloc (obj, size);
return G_REALLOC_INTERNAL (obj, size);
}
5 changes: 4 additions & 1 deletion eglib/src/gmisc-win32.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ g_win32_getlocale(void)
gint ccBuf = GetLocaleInfo(lcid, LOCALE_SISO639LANGNAME, buf, 9);
buf[ccBuf - 1] = '-';
ccBuf += GetLocaleInfo(lcid, LOCALE_SISO3166CTRYNAME, buf + ccBuf, 9);
return strdup(buf);
return g_strdup (buf);
}

gboolean
Expand Down Expand Up @@ -128,6 +128,9 @@ g_get_home_dir (void)
}
}

g_free (drive);
g_free (path);

return home_dir;
}

Expand Down
12 changes: 6 additions & 6 deletions eglib/src/goutput.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ g_print (const gchar *format, ...)
va_list args;

va_start (args, format);
if (vasprintf (&msg, format, args) < 0)
if (g_vasprintf (&msg, format, args) < 0)
return;
va_end (args);

if (!stdout_handler)
stdout_handler = default_stdout_handler;

stdout_handler (msg);
free (msg);
g_free (msg);
}

void
Expand All @@ -67,15 +67,15 @@ g_printerr (const gchar *format, ...)
va_list args;

va_start (args, format);
if (vasprintf (&msg, format, args) < 0)
if (g_vasprintf (&msg, format, args) < 0)
return;
va_end (args);

if (!stderr_handler)
stderr_handler = default_stderr_handler;

stderr_handler (msg);
free (msg);
g_free (msg);
}

GLogLevelFlags
Expand Down Expand Up @@ -107,11 +107,11 @@ g_logv (const gchar *log_domain, GLogLevelFlags log_level, const gchar *format,
if (!default_log_func)
default_log_func = g_log_default_handler;

if (vasprintf (&msg, format, args) < 0)
if (g_vasprintf (&msg, format, args) < 0)
return;

default_log_func (log_domain, log_level, msg, default_log_func_user_data);
free (msg);
g_free (msg);
}

void
Expand Down
6 changes: 3 additions & 3 deletions eglib/src/gstr.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
gchar *
g_strndup (const gchar *str, gsize n)
{
#ifdef HAVE_STRNDUP
#if defined (HAVE_STRNDUP) && !defined (G_OVERRIDABLE_ALLOCATORS)
return strndup (str, n);
#else
if (str) {
Expand Down Expand Up @@ -133,7 +133,7 @@ g_strdup_vprintf (const gchar *format, va_list args)
int n;
char *ret;

n = vasprintf (&ret, format, args);
n = g_vasprintf (&ret, format, args);
if (n == -1)
return NULL;

Expand All @@ -148,7 +148,7 @@ g_strdup_printf (const gchar *format, ...)
int n;

va_start (args, format);
n = vasprintf (&ret, format, args);
n = g_vasprintf (&ret, format, args);
va_end (args);
if (n == -1)
return NULL;
Expand Down
5 changes: 3 additions & 2 deletions eglib/src/vasprintf.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>

int vasprintf(char **ret, const char *fmt, va_list ap)
gint g_vasprintf (gchar **ret, const gchar *fmt, va_list ap)
{
char *buf;
int len;
Expand All @@ -17,7 +18,7 @@ int vasprintf(char **ret, const char *fmt, va_list ap)
len = vsnprintf(NULL, 0, fmt, ap2);
#endif

if (len >= 0 && (buf = malloc ((buflen = (size_t) (len + 1)))) != NULL) {
if (len >= 0 && (buf = g_malloc ((buflen = (size_t) (len + 1)))) != NULL) {
len = vsnprintf(buf, buflen, fmt, ap);
*ret = buf;
} else {
Expand Down
5 changes: 2 additions & 3 deletions eglib/src/vasprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
#define __VASPRINTF_H

#include <stdarg.h>
#include <config.h>

#ifndef HAVE_VASPRINTF
int vasprintf(char **ret, const char *fmt, va_list ap);
#if !defined (HAVE_VASPRINTF) || defined (G_OVERRIDABLE_ALLOCATORS)
int g_vasprintf(char **ret, const char *fmt, va_list ap);
#endif

#endif /* __VASPRINTF_H */
6 changes: 6 additions & 0 deletions mono/utils/mono-publib.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ mono_free (void *ptr)
g_free (ptr);
}

void
mono_set_allocator_vtable (MonoAllocatorVTable* vtable)
{
GMemVTable g_mem_vtable = { vtable->malloc, vtable->realloc, vtable->free, vtable->calloc};
g_mem_set_vtable (&g_mem_vtable);
}
Loading

0 comments on commit 6474f8e

Please sign in to comment.