diff --git a/configure.ac b/configure.ac index 0066dbfbb..be2586997 100644 --- a/configure.ac +++ b/configure.ac @@ -25,6 +25,21 @@ dnl Keep this check close to the beginning, so that the users dnl without any glib won't have their time wasted by other checks. dnl +AC_DEFINE(WITH_GLIB, 1, [Use Glib functions]) + +dnl TODO: make option --with-glib like this: +dnl +dnl AC_ARG_WITH(glib, +dnl [AS_HELP_STRING([--with-glib], [Use glib (default is yes)])], +dnl [with_glib=$enableval], +dnl [with_glib=yes]) +dnl +dnl if test x${with_glib} = xyes; then +dnl dnl some checking stuff +dnl AC_DEFINE(WITH_GLIB, 1, [Use Glib functions]) +dnl fi + + AC_ARG_WITH(glib12, [ --with-glib12 Force using glib 1.2.x [[no]]]) diff --git a/mhl/memory.h b/mhl/memory.h index b00617700..20db6c023 100644 --- a/mhl/memory.h +++ b/mhl/memory.h @@ -1,28 +1,100 @@ #ifndef __MHL_MEM #define __MHL_MEM +#if defined _AIX && !defined REGEX_MALLOC + #pragma alloca +#endif + +#include #include #include -/* allocate a chunk of stack memory, uninitialized */ -#define mhl_mem_alloc_u(sz) (malloc(sz)) +#ifdef __GNUC__ +# define alloca __builtin_alloca +#else +# ifdef _MSC_VER +# include +# define alloca _alloca +# else +# if HAVE_ALLOCA_H +# include +# else +# ifdef _AIX +#pragma alloca +# else +# ifndef alloca /* predefined by HP cc +Olibcalls */ +char *alloca (); +# endif +# endif +# endif +# endif +#endif -/* allocate a chunk of stack memory, zeroed */ -#define mhl_mem_alloc_z(sz) (calloc(1,sz)) +#ifndef HAVE_ALLOCA +# include +# if !defined(STDC_HEADERS) && defined(HAVE_MALLOC_H) +# include +# endif +# define alloca malloc +#endif + +#ifdef WITH_GLIB +# include + +# if GLIB_MAJOR_VERSION >= 2 + /* allocate a chunk of stack memory, uninitialized */ +# define mhl_mem_alloc_u(sz) (g_try_malloc(sz)) + + /* allocate a chunk of stack memory, zeroed */ +# define mhl_mem_alloc_z(sz) (g_try_malloc0(sz)) + + /* re-alloc memory chunk */ +# define mhl_mem_realloc(ptr,sz) (g_try_realloc(ptr,sz)) + +# else + + /* allocate a chunk of stack memory, uninitialized */ +# define mhl_mem_alloc_u(sz) (g_malloc(sz)) + + /* allocate a chunk of stack memory, zeroed */ +# define mhl_mem_alloc_z(sz) (g_malloc0(sz)) + + /* re-alloc memory chunk */ +# define mhl_mem_realloc(ptr,sz) (g_realloc(ptr,sz)) + +# endif + + /* allocate a chunk on stack - automatically free'd on function exit */ +# define mhl_stack_alloc(sz) (g_alloca(sz)) + +#else + + /* allocate a chunk of stack memory, uninitialized */ +# define mhl_mem_alloc_u(sz) (malloc(sz)) + + /* allocate a chunk of stack memory, zeroed */ +# define mhl_mem_alloc_z(sz) (calloc(1,sz)) + + /* re-alloc memory chunk */ +# define mhl_mem_realloc(ptr,sz) (realloc(ptr,sz)) + + /* allocate a chunk on stack - automatically free'd on function exit */ +# define mhl_stack_alloc(sz) (alloca(sz)) + +#endif /* free a chunk of memory from stack, passing NULL does no harm */ static inline void mhl_mem_free(void* ptr) { - if (ptr) free(ptr); + if (ptr) +#ifdef WITH_GLIB + g_free(ptr); +#else + free(ptr); +#endif } /* free an ptr and NULL it */ #define MHL_PTR_FREE(ptr) do { mhl_mem_free(ptr); (ptr) = NULL; } while (0); -/* allocate a chunk on stack - automatically free'd on function exit */ -#define mhl_stack_alloc(sz) (alloca(sz)) - -/* re-alloc memory chunk */ -#define mhl_mem_realloc(ptr,sz) (realloc(ptr,sz)) - #endif