1 /*
2 * GLIB Compatibility Functions
3 *
4 * Copyright IBM, Corp. 2013
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Michael Tokarev <mjt@tls.msk.ru>
9 * Paolo Bonzini <pbonzini@redhat.com>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2 or later.
12 * See the COPYING file in the top-level directory.
13 *
14 */
15
16 #ifndef QEMU_GLIB_COMPAT_H
17 #define QEMU_GLIB_COMPAT_H
18
19 /* Ask for warnings for anything that was marked deprecated in
20 * the defined version, or before. It is a candidate for rewrite.
21 */
22 #define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_66
23
24 /* Ask for warnings if code tries to use function that did not
25 * exist in the defined version. These risk breaking builds
26 */
27 #define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_66
28
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
31
32 #include <glib.h>
33 #if defined(G_OS_UNIX)
34 #include <glib-unix.h>
35 #include <sys/types.h>
36 #include <pwd.h>
37 #endif
38
39 /*
40 * These functions perform function pointer casts which can cause function call
41 * failure on Emscripten. Use g_slist_sort_with_data and g_list_sort_with_data
42 * instead of these functions.
43 */
44 #pragma GCC poison g_slist_sort g_list_sort
45
46 /*
47 * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
48 * use of functions from newer GLib via this compat header needs a little
49 * trickery to prevent warnings being emitted.
50 *
51 * Consider a function from newer glib-X.Y that we want to use
52 *
53 * int g_foo(const char *wibble)
54 *
55 * We must define a static inline function with the same signature that does
56 * what we need, but with a "_compat" suffix e.g.
57 *
58 * static inline void g_foo_compat(const char *wibble)
59 * {
60 * #if GLIB_CHECK_VERSION(X, Y, 0)
61 * g_foo(wibble)
62 * #else
63 * g_something_equivalent_in_older_glib(wibble);
64 * #endif
65 * }
66 *
67 * The #pragma at the top of this file turns off -Wdeprecated-declarations,
68 * ensuring this wrapper function impl doesn't trigger the compiler warning
69 * about using too new glib APIs. Finally we can do
70 *
71 * #define g_foo(a) g_foo_compat(a)
72 *
73 * So now the code elsewhere in QEMU, which *does* have the
74 * -Wdeprecated-declarations warning active, can call g_foo(...) as normal,
75 * without generating warnings.
76 */
77
78 /*
79 * g_memdup2_qemu:
80 * @mem: (nullable): the memory to copy.
81 * @byte_size: the number of bytes to copy.
82 *
83 * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
84 * from @mem. If @mem is %NULL it returns %NULL.
85 *
86 * This replaces g_memdup(), which was prone to integer overflows when
87 * converting the argument from a #gsize to a #guint.
88 *
89 * This static inline version is a backport of the new public API from
90 * GLib 2.68, kept internal to GLib for backport to older stable releases.
91 * See https://gitlab.gnome.org/GNOME/glib/-/issues/2319.
92 *
93 * Returns: (nullable): a pointer to the newly-allocated copy of the memory,
94 * or %NULL if @mem is %NULL.
95 */
g_memdup2_qemu(gconstpointer mem,gsize byte_size)96 static inline gpointer g_memdup2_qemu(gconstpointer mem, gsize byte_size)
97 {
98 #if GLIB_CHECK_VERSION(2, 68, 0)
99 return g_memdup2(mem, byte_size);
100 #else
101 gpointer new_mem;
102
103 if (mem && byte_size != 0) {
104 new_mem = g_malloc(byte_size);
105 memcpy(new_mem, mem, byte_size);
106 } else {
107 new_mem = NULL;
108 }
109
110 return new_mem;
111 #endif
112 }
113 #define g_memdup2(m, s) g_memdup2_qemu(m, s)
114
115 static inline bool
qemu_g_test_slow(void)116 qemu_g_test_slow(void)
117 {
118 static int cached = -1;
119 if (cached == -1) {
120 cached = g_test_slow() || getenv("G_TEST_SLOW") != NULL;
121 }
122 return cached;
123 }
124
125 #undef g_test_slow
126 #undef g_test_thorough
127 #undef g_test_quick
128 #define g_test_slow() qemu_g_test_slow()
129 #define g_test_thorough() qemu_g_test_slow()
130 #define g_test_quick() (!qemu_g_test_slow())
131
132 #pragma GCC diagnostic pop
133
134 #ifndef G_NORETURN
135 #define G_NORETURN G_GNUC_NORETURN
136 #endif
137
138 #endif
139