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