1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/gtk/compat/glib/gmem.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,56 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef GMEM_WRAPPER_H 1.9 +#define GMEM_WRAPPER_H 1.10 + 1.11 +#define g_malloc_n g_malloc_n_ 1.12 +#define g_malloc0_n g_malloc0_n_ 1.13 +#define g_realloc_n g_realloc_n_ 1.14 +#include_next <glib/gmem.h> 1.15 +#undef g_malloc_n 1.16 +#undef g_malloc0_n 1.17 +#undef g_realloc_n 1.18 + 1.19 +#include <glib/gmessages.h> 1.20 + 1.21 +#undef g_new 1.22 +#define g_new(type, num) \ 1.23 + ((type *) g_malloc_n((num), sizeof(type))) 1.24 + 1.25 +#undef g_new0 1.26 +#define g_new0(type, num) \ 1.27 + ((type *) g_malloc0_n((num), sizeof(type))) 1.28 + 1.29 +#undef g_renew 1.30 +#define g_renew(type, ptr, num) \ 1.31 + ((type *) g_realloc_n(ptr, (num), sizeof(type))) 1.32 + 1.33 +#define _CHECK_OVERFLOW(num, type_size) \ 1.34 + if (G_UNLIKELY(type_size > 0 && num > G_MAXSIZE / type_size)) { \ 1.35 + g_error("%s: overflow allocating %" G_GSIZE_FORMAT "*%" G_GSIZE_FORMAT " bytes", \ 1.36 + G_STRLOC, num, type_size); \ 1.37 + } 1.38 + 1.39 +static inline gpointer 1.40 +g_malloc_n(gsize num, gsize type_size) 1.41 +{ 1.42 + _CHECK_OVERFLOW(num, type_size) 1.43 + return g_malloc(num * type_size); 1.44 +} 1.45 + 1.46 +static inline gpointer 1.47 +g_malloc0_n(gsize num, gsize type_size) 1.48 +{ 1.49 + _CHECK_OVERFLOW(num, type_size) 1.50 + return g_malloc0(num * type_size); 1.51 +} 1.52 + 1.53 +static inline gpointer 1.54 +g_realloc_n(gpointer ptr, gsize num, gsize type_size) 1.55 +{ 1.56 + _CHECK_OVERFLOW(num, type_size) 1.57 + return g_realloc(ptr, num * type_size); 1.58 +} 1.59 +#endif /* GMEM_WRAPPER_H */