Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / include / glib-compat.h
blob88403eb860bd4aec3321a4663c722fa4152292c2
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 #include <glib.h>
21 /* GLIB version compatibility flags */
22 #if !GLIB_CHECK_VERSION(2, 26, 0)
23 #define G_TIME_SPAN_SECOND (G_GINT64_CONSTANT(1000000))
24 #endif
26 #if !GLIB_CHECK_VERSION(2, 28, 0)
27 static inline gint64 qemu_g_get_monotonic_time(void)
29 /* g_get_monotonic_time() is best-effort so we can use the wall clock as a
30 * fallback.
33 GTimeVal time;
34 g_get_current_time(&time);
36 return time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
38 /* work around distro backports of this interface */
39 #define g_get_monotonic_time() qemu_g_get_monotonic_time()
40 #endif
42 #if defined(_WIN32) && !GLIB_CHECK_VERSION(2, 50, 0)
44 * g_poll has a problem on Windows when using
45 * timeouts < 10ms, so use wrapper.
47 #define g_poll(fds, nfds, timeout) g_poll_fixed(fds, nfds, timeout)
48 gint g_poll_fixed(GPollFD *fds, guint nfds, gint timeout);
49 #endif
51 #if !defined(_WIN32)
52 #if !GLIB_CHECK_VERSION(2, 30, 0)
53 /* Not a 100% compatible implementation, but good enough for most
54 * cases. Placeholders are only supported at the end of the
55 * template. */
56 static inline gchar *qemu_g_dir_make_tmp(gchar const *tmpl, GError **error)
58 gchar *path = g_build_filename(g_get_tmp_dir(), tmpl ?: ".XXXXXX", NULL);
60 if (mkdtemp(path) != NULL) {
61 return path;
63 /* Error occurred, clean up. */
64 g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
65 "mkdtemp() failed");
66 g_free(path);
67 return NULL;
70 #define g_dir_make_tmp(tmpl, error) qemu_g_dir_make_tmp(tmpl, error)
71 #endif /* glib 2.30 */
72 #endif /* !_WIN32 */
74 #if !GLIB_CHECK_VERSION(2, 31, 0)
75 /* before glib-2.31, GMutex and GCond was dynamic-only (there was a separate
76 * GStaticMutex, but it didn't work with condition variables).
78 * Our implementation uses GOnce to fake a static implementation that does
79 * not require separate initialization.
80 * We need to rename the types to avoid passing our CompatGMutex/CompatGCond
81 * by mistake to a function that expects GMutex/GCond. However, for ease
82 * of use we keep the GLib function names. GLib uses macros for the
83 * implementation, we use inline functions instead and undefine the macros.
86 typedef struct CompatGMutex {
87 GOnce once;
88 } CompatGMutex;
90 typedef struct CompatGCond {
91 GOnce once;
92 } CompatGCond;
94 static inline gpointer do_g_mutex_new(gpointer unused)
96 return (gpointer) g_mutex_new();
99 static inline void g_mutex_init(CompatGMutex *mutex)
101 mutex->once = (GOnce) G_ONCE_INIT;
104 static inline void g_mutex_clear(CompatGMutex *mutex)
106 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
107 if (mutex->once.retval) {
108 g_mutex_free((GMutex *) mutex->once.retval);
110 mutex->once = (GOnce) G_ONCE_INIT;
113 static inline void (g_mutex_lock)(CompatGMutex *mutex)
115 g_once(&mutex->once, do_g_mutex_new, NULL);
116 g_mutex_lock((GMutex *) mutex->once.retval);
118 #undef g_mutex_lock
120 static inline gboolean (g_mutex_trylock)(CompatGMutex *mutex)
122 g_once(&mutex->once, do_g_mutex_new, NULL);
123 return g_mutex_trylock((GMutex *) mutex->once.retval);
125 #undef g_mutex_trylock
128 static inline void (g_mutex_unlock)(CompatGMutex *mutex)
130 g_mutex_unlock((GMutex *) mutex->once.retval);
132 #undef g_mutex_unlock
134 static inline gpointer do_g_cond_new(gpointer unused)
136 return (gpointer) g_cond_new();
139 static inline void g_cond_init(CompatGCond *cond)
141 cond->once = (GOnce) G_ONCE_INIT;
144 static inline void g_cond_clear(CompatGCond *cond)
146 g_assert(cond->once.status != G_ONCE_STATUS_PROGRESS);
147 if (cond->once.retval) {
148 g_cond_free((GCond *) cond->once.retval);
150 cond->once = (GOnce) G_ONCE_INIT;
153 static inline void (g_cond_wait)(CompatGCond *cond, CompatGMutex *mutex)
155 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
156 g_once(&cond->once, do_g_cond_new, NULL);
157 g_cond_wait((GCond *) cond->once.retval, (GMutex *) mutex->once.retval);
159 #undef g_cond_wait
161 static inline void (g_cond_broadcast)(CompatGCond *cond)
163 g_once(&cond->once, do_g_cond_new, NULL);
164 g_cond_broadcast((GCond *) cond->once.retval);
166 #undef g_cond_broadcast
168 static inline void (g_cond_signal)(CompatGCond *cond)
170 g_once(&cond->once, do_g_cond_new, NULL);
171 g_cond_signal((GCond *) cond->once.retval);
173 #undef g_cond_signal
175 static inline gboolean (g_cond_timed_wait)(CompatGCond *cond,
176 CompatGMutex *mutex,
177 GTimeVal *time)
179 g_assert(mutex->once.status != G_ONCE_STATUS_PROGRESS);
180 g_once(&cond->once, do_g_cond_new, NULL);
181 return g_cond_timed_wait((GCond *) cond->once.retval,
182 (GMutex *) mutex->once.retval, time);
184 #undef g_cond_timed_wait
186 /* This is not a macro, because it didn't exist until 2.32. */
187 static inline gboolean g_cond_wait_until(CompatGCond *cond, CompatGMutex *mutex,
188 gint64 end_time)
190 GTimeVal time;
192 /* Convert from monotonic to CLOCK_REALTIME. */
193 end_time -= g_get_monotonic_time();
194 g_get_current_time(&time);
195 end_time += time.tv_sec * G_TIME_SPAN_SECOND + time.tv_usec;
197 time.tv_sec = end_time / G_TIME_SPAN_SECOND;
198 time.tv_usec = end_time % G_TIME_SPAN_SECOND;
199 return g_cond_timed_wait(cond, mutex, &time);
202 /* before 2.31 there was no g_thread_new() */
203 static inline GThread *g_thread_new(const char *name,
204 GThreadFunc func, gpointer data)
206 GThread *thread = g_thread_create(func, data, TRUE, NULL);
207 if (!thread) {
208 g_error("creating thread");
210 return thread;
212 #else
213 #define CompatGMutex GMutex
214 #define CompatGCond GCond
215 #endif /* glib 2.31 */
217 #if !GLIB_CHECK_VERSION(2, 32, 0)
218 /* Beware, function returns gboolean since 2.39.2, see GLib commit 9101915 */
219 static inline void g_hash_table_add(GHashTable *hash_table, gpointer key)
221 g_hash_table_replace(hash_table, key, key);
224 static inline gboolean g_hash_table_contains(GHashTable *hash_table,
225 gpointer key)
227 return g_hash_table_lookup_extended(hash_table, key, NULL, NULL);
229 #define G_SOURCE_CONTINUE TRUE
230 #define G_SOURCE_REMOVE FALSE
231 #endif
233 #ifndef g_assert_true
234 #define g_assert_true(expr) \
235 do { \
236 if (G_LIKELY(expr)) { \
237 } else { \
238 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
239 "'" #expr "' should be TRUE"); \
241 } while (0)
242 #endif
244 #ifndef g_assert_false
245 #define g_assert_false(expr) \
246 do { \
247 if (G_LIKELY(!(expr))) { \
248 } else { \
249 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
250 "'" #expr "' should be FALSE"); \
252 } while (0)
253 #endif
255 #ifndef g_assert_null
256 #define g_assert_null(expr) \
257 do { \
258 if (G_LIKELY((expr) == NULL)) { \
259 } else { \
260 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
261 "'" #expr "' should be NULL"); \
263 } while (0)
264 #endif
266 #ifndef g_assert_nonnull
267 #define g_assert_nonnull(expr) \
268 do { \
269 if (G_LIKELY((expr) != NULL)) { \
270 } else { \
271 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
272 "'" #expr "' should not be NULL"); \
274 } while (0)
275 #endif
277 #ifndef g_assert_cmpmem
278 #define g_assert_cmpmem(m1, l1, m2, l2) \
279 do { \
280 gconstpointer __m1 = m1, __m2 = m2; \
281 int __l1 = l1, __l2 = l2; \
282 if (__l1 != __l2) { \
283 g_assertion_message_cmpnum( \
284 G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
285 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", \
286 __l2, 'i'); \
287 } else if (memcmp(__m1, __m2, __l1) != 0) { \
288 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
289 "assertion failed (" #m1 " == " #m2 ")"); \
291 } while (0)
292 #endif
294 #if !GLIB_CHECK_VERSION(2, 28, 0)
295 static inline void g_list_free_full(GList *list, GDestroyNotify free_func)
297 GList *l;
299 for (l = list; l; l = l->next) {
300 free_func(l->data);
303 g_list_free(list);
306 static inline void g_slist_free_full(GSList *list, GDestroyNotify free_func)
308 GSList *l;
310 for (l = list; l; l = l->next) {
311 free_func(l->data);
314 g_slist_free(list);
316 #endif
318 #if !GLIB_CHECK_VERSION(2, 26, 0)
319 static inline void g_source_set_name(GSource *source, const char *name)
321 /* This is just a debugging aid, so leaving it a no-op */
323 static inline void g_source_set_name_by_id(guint tag, const char *name)
325 /* This is just a debugging aid, so leaving it a no-op */
327 #endif
329 #if !GLIB_CHECK_VERSION(2, 36, 0)
330 /* Always fail. This will not include error_report output in the test log,
331 * sending it instead to stderr.
333 #define g_test_initialized() (0)
334 #endif
335 #if !GLIB_CHECK_VERSION(2, 38, 0)
336 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
337 #error schizophrenic detection of glib subprocess testing
338 #endif
339 #define g_test_subprocess() (0)
340 #endif
343 #if !GLIB_CHECK_VERSION(2, 34, 0)
344 static inline void
345 g_test_add_data_func_full(const char *path,
346 gpointer data,
347 gpointer fn,
348 gpointer data_free_func)
350 #if GLIB_CHECK_VERSION(2, 26, 0)
351 /* back-compat casts, remove this once we can require new-enough glib */
352 g_test_add_vtable(path, 0, data, NULL,
353 (GTestFixtureFunc)fn, (GTestFixtureFunc) data_free_func);
354 #else
355 /* back-compat casts, remove this once we can require new-enough glib */
356 g_test_add_vtable(path, 0, data, NULL,
357 (void (*)(void)) fn, (void (*)(void)) data_free_func);
358 #endif
360 #endif
362 /* Small compat shim from glib 2.32 */
363 #ifndef G_SOURCE_CONTINUE
364 #define G_SOURCE_CONTINUE TRUE
365 #endif
366 #ifndef G_SOURCE_REMOVE
367 #define G_SOURCE_REMOVE FALSE
368 #endif
370 #endif