widget/gtk/compat/glib/gmem.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #ifndef GMEM_WRAPPER_H
michael@0 6 #define GMEM_WRAPPER_H
michael@0 7
michael@0 8 #define g_malloc_n g_malloc_n_
michael@0 9 #define g_malloc0_n g_malloc0_n_
michael@0 10 #define g_realloc_n g_realloc_n_
michael@0 11 #include_next <glib/gmem.h>
michael@0 12 #undef g_malloc_n
michael@0 13 #undef g_malloc0_n
michael@0 14 #undef g_realloc_n
michael@0 15
michael@0 16 #include <glib/gmessages.h>
michael@0 17
michael@0 18 #undef g_new
michael@0 19 #define g_new(type, num) \
michael@0 20 ((type *) g_malloc_n((num), sizeof(type)))
michael@0 21
michael@0 22 #undef g_new0
michael@0 23 #define g_new0(type, num) \
michael@0 24 ((type *) g_malloc0_n((num), sizeof(type)))
michael@0 25
michael@0 26 #undef g_renew
michael@0 27 #define g_renew(type, ptr, num) \
michael@0 28 ((type *) g_realloc_n(ptr, (num), sizeof(type)))
michael@0 29
michael@0 30 #define _CHECK_OVERFLOW(num, type_size) \
michael@0 31 if (G_UNLIKELY(type_size > 0 && num > G_MAXSIZE / type_size)) { \
michael@0 32 g_error("%s: overflow allocating %" G_GSIZE_FORMAT "*%" G_GSIZE_FORMAT " bytes", \
michael@0 33 G_STRLOC, num, type_size); \
michael@0 34 }
michael@0 35
michael@0 36 static inline gpointer
michael@0 37 g_malloc_n(gsize num, gsize type_size)
michael@0 38 {
michael@0 39 _CHECK_OVERFLOW(num, type_size)
michael@0 40 return g_malloc(num * type_size);
michael@0 41 }
michael@0 42
michael@0 43 static inline gpointer
michael@0 44 g_malloc0_n(gsize num, gsize type_size)
michael@0 45 {
michael@0 46 _CHECK_OVERFLOW(num, type_size)
michael@0 47 return g_malloc0(num * type_size);
michael@0 48 }
michael@0 49
michael@0 50 static inline gpointer
michael@0 51 g_realloc_n(gpointer ptr, gsize num, gsize type_size)
michael@0 52 {
michael@0 53 _CHECK_OVERFLOW(num, type_size)
michael@0 54 return g_realloc(ptr, num * type_size);
michael@0 55 }
michael@0 56 #endif /* GMEM_WRAPPER_H */

mercurial