blob: c49cf87196f9376cd0ff032fd4cb347113894cbf [file] [log] [blame]
Anthony Liguorid63c9472013-03-25 10:23:56 -05001/*
2 * GLIB Compatibility Functions
3 *
4 * Copyright IBM, Corp. 2013
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
Michael Tokarev86946a22014-05-08 12:30:46 +04008 * Michael Tokarev <mjt@tls.msk.ru>
9 * Paolo Bonzini <pbonzini@redhat.com>
Anthony Liguorid63c9472013-03-25 10:23:56 -050010 *
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#include <glib.h>
20
Stefan Hajnoczi89b516d2014-10-15 14:29:30 +020021/* GLIB version compatibility flags */
22#if !GLIB_CHECK_VERSION(2, 26, 0)
23#define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
24#endif
25
Stefan Hajnoczi89b516d2014-10-15 14:29:30 +020026#if !GLIB_CHECK_VERSION(2, 28, 0)
Cornelia Huck14655e92015-04-02 17:17:45 +020027static inline gint64 qemu_g_get_monotonic_time(void)
Stefan Hajnoczi89b516d2014-10-15 14:29:30 +020028{
29 /* g_get_monotonic_time() is best-effort so we can use the wall clock as a
30 * fallback.
31 */
32
33 GTimeVal time;
34 g_get_current_time(&time);
35
36 return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
37}
Cornelia Huck14655e92015-04-02 17:17:45 +020038/* work around distro backports of this interface */
39#define g_get_monotonic_time() qemu_g_get_monotonic_time()
Stefan Hajnoczi89b516d2014-10-15 14:29:30 +020040#endif
41
Marc-André Lureau1706e9d2017-01-03 20:19:33 +010042#if defined(_WIN32) && !GLIB_CHECK_VERSION(2, 50, 0)
Sangho Park5a007542014-05-08 12:47:10 +040043/*
44 * g_poll has a problem on Windows when using
45 * timeouts < 10ms, so use wrapper.
46 */
47#define g_poll(fds, nfds, timeout) g_poll_fixed(fds, nfds, timeout)
48gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
Stefan Hajnoczif95c9672014-05-02 18:35:56 +040049#endif
50
Sascha Silbe50455702016-08-18 20:46:02 +020051#if !GLIB_CHECK_VERSION(2, 30, 0)
52/* Not a 100% compatible implementation, but good enough for most
53 * cases. Placeholders are only supported at the end of the
54 * template. */
55static inline gchar *qemu_g_dir_make_tmp(gchar const *tmpl, GError **error)
56{
57 gchar *path = g_build_filename(g_get_tmp_dir(), tmpl ?: ".XXXXXX", NULL);
58
59 if (mkdtemp(path) != NULL) {
60 return path;
61 }
62 /* Error occurred, clean up. */
63 g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
64 "mkdtemp() failed");
65 g_free(path);
66 return NULL;
67}
68#define g_dir_make_tmp(tmpl, error) qemu_g_dir_make_tmp(tmpl, error)
69#endif /* glib 2.30 */
70
Michael Tokarev86946a22014-05-08 12:30:46 +040071#if !GLIB_CHECK_VERSION(2, 31, 0)
72/* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate
73 * GStaticMutex, but it didn't work with condition variables).
74 *
75 * Our implementation uses GOnce to fake a static implementation that does
76 * not require separate initialization.
77 * We need to rename the types to avoid passing our CompatGMutex/CompatGCond
78 * by mistake to a function that expects GMutex/GCond. However, for ease
79 * of use we keep the GLib function names. GLib uses macros for the
80 * implementation, we use inline functions instead and undefine the macros.
81 */
82
83typedef struct CompatGMutex {
84 GOnce once;
85} CompatGMutex;
86
87typedef struct CompatGCond {
88 GOnce once;
89} CompatGCond;
90
91static inline gpointer do_g_mutex_new(gpointer unused)
92{
93 return (gpointer) g_mutex_new();
94}
95
96static inline void g_mutex_init(CompatGMutex *mutex)
97{
98 mutex->once = (GOnce) G_ONCE_INIT;
99}
100
101static inline void g_mutex_clear(CompatGMutex *mutex)
102{
Michael Tokarevf20f2a12015-05-07 13:38:02 +0300103 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
Michael Tokarev86946a22014-05-08 12:30:46 +0400104 if (mutex->once.retval) {
105 g_mutex_free((GMutex *) mutex->once.retval);
106 }
107 mutex->once = (GOnce) G_ONCE_INIT;
108}
109
110static inline void (g_mutex_lock)(CompatGMutex *mutex)
111{
112 g_once(&mutex->once, do_g_mutex_new, NULL);
113 g_mutex_lock((GMutex *) mutex->once.retval);
114}
115#undef g_mutex_lock
116
117static inline gboolean (g_mutex_trylock)(CompatGMutex *mutex)
118{
119 g_once(&mutex->once, do_g_mutex_new, NULL);
120 return g_mutex_trylock((GMutex *) mutex->once.retval);
121}
122#undef g_mutex_trylock
123
124
125static inline void (g_mutex_unlock)(CompatGMutex *mutex)
126{
127 g_mutex_unlock((GMutex *) mutex->once.retval);
128}
129#undef g_mutex_unlock
130
131static inline gpointer do_g_cond_new(gpointer unused)
132{
133 return (gpointer) g_cond_new();
134}
135
136static inline void g_cond_init(CompatGCond *cond)
137{
138 cond->once = (GOnce) G_ONCE_INIT;
139}
140
141static inline void g_cond_clear(CompatGCond *cond)
142{
Michael Tokarevf20f2a12015-05-07 13:38:02 +0300143 g_assert(cond->once.status != G_ONCE_STATUS_PROGRESS);
Michael Tokarev86946a22014-05-08 12:30:46 +0400144 if (cond->once.retval) {
145 g_cond_free((GCond *) cond->once.retval);
146 }
147 cond->once = (GOnce) G_ONCE_INIT;
148}
149
150static inline void (g_cond_wait)(CompatGCond *cond, CompatGMutex *mutex)
151{
Michael Tokarevf20f2a12015-05-07 13:38:02 +0300152 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
Michael Tokarev86946a22014-05-08 12:30:46 +0400153 g_once(&cond->once, do_g_cond_new, NULL);
154 g_cond_wait((GCond *) cond->once.retval, (GMutex *) mutex->once.retval);
155}
156#undef g_cond_wait
157
158static inline void (g_cond_broadcast)(CompatGCond *cond)
159{
160 g_once(&cond->once, do_g_cond_new, NULL);
161 g_cond_broadcast((GCond *) cond->once.retval);
162}
163#undef g_cond_broadcast
164
165static inline void (g_cond_signal)(CompatGCond *cond)
166{
167 g_once(&cond->once, do_g_cond_new, NULL);
168 g_cond_signal((GCond *) cond->once.retval);
169}
170#undef g_cond_signal
171
Paolo Bonzini634d39b2016-06-28 18:32:42 +0200172static inline gboolean (g_cond_timed_wait)(CompatGCond *cond,
173 CompatGMutex *mutex,
174 GTimeVal *time)
175{
176 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
177 g_once(&cond->once, do_g_cond_new, NULL);
178 return g_cond_timed_wait((GCond *) cond->once.retval,
179 (GMutex *) mutex->once.retval, time);
180}
181#undef g_cond_timed_wait
182
183/* This is not a macro, because it didn't exist until 2.32. */
184static inline gboolean g_cond_wait_until(CompatGCond *cond, CompatGMutex *mutex,
185 gint64 end_time)
186{
187 GTimeVal time;
188
189 /* Convert from monotonic to CLOCK_REALTIME. */
190 end_time -= g_get_monotonic_time();
191 g_get_current_time(&time);
192 end_time += time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
193
194 time.tv_sec = end_time / G_TIME_SPAN_SECOND;
195 time.tv_usec = end_time % G_TIME_SPAN_SECOND;
196 return g_cond_timed_wait(cond, mutex, &time);
197}
Michael Tokarev86946a22014-05-08 12:30:46 +0400198
199/* before 2.31 there was no g_thread_new() */
200static inline GThread *g_thread_new(const char *name,
201 GThreadFunc func, gpointer data)
202{
203 GThread *thread = g_thread_create(func, data, TRUE, NULL);
204 if (!thread) {
205 g_error("creating thread");
206 }
207 return thread;
208}
209#else
210#define CompatGMutex GMutex
211#define CompatGCond GCond
212#endif /* glib 2.31 */
213
Markus Armbruster8681dff2015-10-27 15:44:00 +0100214#if !GLIB_CHECK_VERSION(2, 32, 0)
215/* Beware, function returns gboolean since 2.39.2, see GLib commit 9101915 */
216static inline void g_hash_table_add(GHashTable *hash_table, gpointer key)
217{
218 g_hash_table_replace(hash_table, key, key);
219}
Vinzenz Feenstra161a56a2017-04-19 11:26:15 +0200220
221static inline gboolean g_hash_table_contains(GHashTable *hash_table,
222 gpointer key)
223{
224 return g_hash_table_lookup_extended(hash_table, key, NULL, NULL);
225}
Juan Quintela2a543bf2017-07-24 12:51:59 +0200226#define G_SOURCE_CONTINUE TRUE
227#define G_SOURCE_REMOVE FALSE
Markus Armbruster8681dff2015-10-27 15:44:00 +0100228#endif
229
Marc-André Lureau8a0b5422015-10-02 14:58:17 +0200230#ifndef g_assert_true
231#define g_assert_true(expr) \
232 do { \
233 if (G_LIKELY(expr)) { \
234 } else { \
235 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
236 "'" #expr "' should be TRUE"); \
237 } \
238 } while (0)
239#endif
240
241#ifndef g_assert_false
242#define g_assert_false(expr) \
243 do { \
244 if (G_LIKELY(!(expr))) { \
245 } else { \
246 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
247 "'" #expr "' should be FALSE"); \
248 } \
249 } while (0)
250#endif
251
252#ifndef g_assert_null
253#define g_assert_null(expr) \
254 do { \
255 if (G_LIKELY((expr) == NULL)) { \
256 } else { \
257 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
258 "'" #expr "' should be NULL"); \
259 } \
260 } while (0)
261#endif
262
263#ifndef g_assert_nonnull
264#define g_assert_nonnull(expr) \
265 do { \
266 if (G_LIKELY((expr) != NULL)) { \
267 } else { \
268 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
269 "'" #expr "' should not be NULL"); \
270 } \
271 } while (0)
272#endif
273
274#ifndef g_assert_cmpmem
275#define g_assert_cmpmem(m1, l1, m2, l2) \
276 do { \
277 gconstpointer __m1 = m1, __m2 = m2; \
278 int __l1 = l1, __l2 = l2; \
279 if (__l1 != __l2) { \
280 g_assertion_message_cmpnum( \
281 G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
282 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", \
283 __l2, 'i'); \
284 } else if (memcmp(__m1, __m2, __l1) != 0) { \
285 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
286 "assertion failed (" #m1 " == " #m2 ")"); \
287 } \
288 } while (0)
289#endif
290
Marc-André Lureau5c7e3e92016-08-05 11:16:07 +0400291#if !GLIB_CHECK_VERSION(2, 28, 0)
292static inline void g_list_free_full(GList *list, GDestroyNotify free_func)
293{
294 GList *l;
295
296 for (l = list; l; l = l->next) {
297 free_func(l->data);
298 }
299
300 g_list_free(list);
301}
302
303static inline void g_slist_free_full(GSList *list, GDestroyNotify free_func)
304{
305 GSList *l;
306
307 for (l = list; l; l = l->next) {
308 free_func(l->data);
309 }
310
311 g_slist_free(list);
312}
313#endif
314
Daniel P. Berrange20f4aa22016-09-30 11:50:18 +0100315#if !GLIB_CHECK_VERSION(2, 26, 0)
316static inline void g_source_set_name(GSource *source, const char *name)
317{
318 /* This is just a debugging aid, so leaving it a no-op */
319}
Daniel P. Berrangee93a68e2016-09-30 11:57:14 +0100320static inline void g_source_set_name_by_id(guint tag, const char *name)
321{
322 /* This is just a debugging aid, so leaving it a no-op */
323}
Daniel P. Berrange20f4aa22016-09-30 11:50:18 +0100324#endif
325
Paolo Bonzini28017e02016-10-24 18:31:03 +0200326#if !GLIB_CHECK_VERSION(2, 36, 0)
327/* Always fail. This will not include error_report output in the test log,
328 * sending it instead to stderr.
329 */
330#define g_test_initialized() (0)
331#endif
332#if !GLIB_CHECK_VERSION(2, 38, 0)
333#ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
334#error schizophrenic detection of glib subprocess testing
335#endif
336#define g_test_subprocess() (0)
337#endif
338
Marc-André Lureau461a8622017-02-07 18:00:49 +0400339
340#if !GLIB_CHECK_VERSION(2, 34, 0)
341static inline void
342g_test_add_data_func_full(const char *path,
343 gpointer data,
344 gpointer fn,
345 gpointer data_free_func)
346{
347#if GLIB_CHECK_VERSION(2, 26, 0)
348 /* back-compat casts, remove this once we can require new-enough glib */
349 g_test_add_vtable(path, 0, data, NULL,
350 (GTestFixtureFunc)fn, (GTestFixtureFunc) data_free_func);
351#else
352 /* back-compat casts, remove this once we can require new-enough glib */
353 g_test_add_vtable(path, 0, data, NULL,
354 (void (*)(void)) fn, (void (*)(void)) data_free_func);
355#endif
356}
357#endif
358
Marc-André Lureau660db842017-08-09 19:46:09 +0200359/* Small compat shim from glib 2.32 */
360#ifndef G_SOURCE_CONTINUE
361#define G_SOURCE_CONTINUE TRUE
362#endif
363#ifndef G_SOURCE_REMOVE
364#define G_SOURCE_REMOVE FALSE
365#endif
Marc-André Lureau461a8622017-02-07 18:00:49 +0400366
Anthony Liguorid63c9472013-03-25 10:23:56 -0500367#endif