r2d: fix build on mingw
[qemu.git] / include / glib-compat.h
blob8a078c52885161d284ba30a0d7e89daaa5cc5f20
1 /*
2 * GLIB Compatibility Functions
4 * Copyright IBM, Corp. 2013
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 * Michael Tokarev <mjt@tls.msk.ru>
9 * Paolo Bonzini <pbonzini@redhat.com>
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.
16 #ifndef QEMU_GLIB_COMPAT_H
17 #define QEMU_GLIB_COMPAT_H
19 /* Ask for warnings for anything that was marked deprecated in
20 * the defined version, or before. It is a candidate for rewrite.
22 #define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_40
24 /* Ask for warnings if code tries to use function that did not
25 * exist in the defined version. These risk breaking builds
27 #define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_40
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
32 #include <glib.h>
35 * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
36 * use of functions from newer GLib via this compat header needs a little
37 * trickery to prevent warnings being emitted.
39 * Consider a function from newer glib-X.Y that we want to use
41 * int g_foo(const char *wibble)
43 * We must define a static inline function with the same signature that does
44 * what we need, but with a "_qemu" suffix e.g.
46 * static inline void g_foo_qemu(const char *wibble)
47 * {
48 * #if GLIB_CHECK_VERSION(X, Y, 0)
49 * g_foo(wibble)
50 * #else
51 * g_something_equivalent_in_older_glib(wibble);
52 * #endif
53 * }
55 * The #pragma at the top of this file turns off -Wdeprecated-declarations,
56 * ensuring this wrapper function impl doesn't trigger the compiler warning
57 * about using too new glib APIs. Finally we can do
59 * #define g_foo(a) g_foo_qemu(a)
61 * So now the code elsewhere in QEMU, which *does* have the
62 * -Wdeprecated-declarations warning active, can call g_foo(...) as normal,
63 * without generating warnings.
66 static inline gboolean g_strv_contains_qemu(const gchar *const *strv,
67 const gchar *str)
69 #if GLIB_CHECK_VERSION(2, 44, 0)
70 return g_strv_contains(strv, str);
71 #else
72 g_return_val_if_fail(strv != NULL, FALSE);
73 g_return_val_if_fail(str != NULL, FALSE);
75 for (; *strv != NULL; strv++) {
76 if (g_str_equal(str, *strv)) {
77 return TRUE;
81 return FALSE;
82 #endif
84 #define g_strv_contains(a, b) g_strv_contains_qemu(a, b)
86 #if !GLIB_CHECK_VERSION(2, 58, 0)
87 typedef struct QemuGSpawnFds {
88 GSpawnChildSetupFunc child_setup;
89 gpointer user_data;
90 gint stdin_fd;
91 gint stdout_fd;
92 gint stderr_fd;
93 } QemuGSpawnFds;
95 static inline void
96 qemu_gspawn_fds_setup(gpointer user_data)
98 QemuGSpawnFds *q = (QemuGSpawnFds *)user_data;
100 dup2(q->stdin_fd, 0);
101 dup2(q->stdout_fd, 1);
102 dup2(q->stderr_fd, 2);
103 q->child_setup(q->user_data);
105 #endif
107 static inline gboolean
108 g_spawn_async_with_fds_qemu(const gchar *working_directory,
109 gchar **argv,
110 gchar **envp,
111 GSpawnFlags flags,
112 GSpawnChildSetupFunc child_setup,
113 gpointer user_data,
114 GPid *child_pid,
115 gint stdin_fd,
116 gint stdout_fd,
117 gint stderr_fd,
118 GError **error)
120 #if GLIB_CHECK_VERSION(2, 58, 0)
121 return g_spawn_async_with_fds(working_directory, argv, envp, flags,
122 child_setup, user_data,
123 child_pid, stdin_fd, stdout_fd, stderr_fd,
124 error);
125 #else
126 QemuGSpawnFds setup = {
127 .child_setup = child_setup,
128 .user_data = user_data,
129 .stdin_fd = stdin_fd,
130 .stdout_fd = stdout_fd,
131 .stderr_fd = stderr_fd,
134 return g_spawn_async(working_directory, argv, envp, flags,
135 qemu_gspawn_fds_setup, &setup,
136 child_pid, error);
137 #endif
140 #define g_spawn_async_with_fds(wd, argv, env, f, c, d, p, ifd, ofd, efd, err) \
141 g_spawn_async_with_fds_qemu(wd, argv, env, f, c, d, p, ifd, ofd, efd, err)
143 #if defined(_WIN32) && !GLIB_CHECK_VERSION(2, 50, 0)
145 * g_poll has a problem on Windows when using
146 * timeouts < 10ms, so use wrapper.
148 #define g_poll(fds, nfds, timeout) g_poll_fixed(fds, nfds, timeout)
149 gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
150 #endif
153 #ifndef g_assert_cmpmem
154 #define g_assert_cmpmem(m1, l1, m2, l2) \
155 do { \
156 gconstpointer __m1 = m1, __m2 = m2; \
157 int __l1 = l1, __l2 = l2; \
158 if (__l1 != __l2) { \
159 g_assertion_message_cmpnum( \
160 G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
161 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", \
162 __l2, 'i'); \
163 } else if (memcmp(__m1, __m2, __l1) != 0) { \
164 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
165 "assertion failed (" #m1 " == " #m2 ")"); \
167 } while (0)
168 #endif
170 #pragma GCC diagnostic pop
172 #endif