michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef GMEM_WRAPPER_H michael@0: #define GMEM_WRAPPER_H michael@0: michael@0: #define g_malloc_n g_malloc_n_ michael@0: #define g_malloc0_n g_malloc0_n_ michael@0: #define g_realloc_n g_realloc_n_ michael@0: #include_next michael@0: #undef g_malloc_n michael@0: #undef g_malloc0_n michael@0: #undef g_realloc_n michael@0: michael@0: #include michael@0: michael@0: #undef g_new michael@0: #define g_new(type, num) \ michael@0: ((type *) g_malloc_n((num), sizeof(type))) michael@0: michael@0: #undef g_new0 michael@0: #define g_new0(type, num) \ michael@0: ((type *) g_malloc0_n((num), sizeof(type))) michael@0: michael@0: #undef g_renew michael@0: #define g_renew(type, ptr, num) \ michael@0: ((type *) g_realloc_n(ptr, (num), sizeof(type))) michael@0: michael@0: #define _CHECK_OVERFLOW(num, type_size) \ michael@0: if (G_UNLIKELY(type_size > 0 && num > G_MAXSIZE / type_size)) { \ michael@0: g_error("%s: overflow allocating %" G_GSIZE_FORMAT "*%" G_GSIZE_FORMAT " bytes", \ michael@0: G_STRLOC, num, type_size); \ michael@0: } michael@0: michael@0: static inline gpointer michael@0: g_malloc_n(gsize num, gsize type_size) michael@0: { michael@0: _CHECK_OVERFLOW(num, type_size) michael@0: return g_malloc(num * type_size); michael@0: } michael@0: michael@0: static inline gpointer michael@0: g_malloc0_n(gsize num, gsize type_size) michael@0: { michael@0: _CHECK_OVERFLOW(num, type_size) michael@0: return g_malloc0(num * type_size); michael@0: } michael@0: michael@0: static inline gpointer michael@0: g_realloc_n(gpointer ptr, gsize num, gsize type_size) michael@0: { michael@0: _CHECK_OVERFLOW(num, type_size) michael@0: return g_realloc(ptr, num * type_size); michael@0: } michael@0: #endif /* GMEM_WRAPPER_H */