Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / include / glib-compat.h
blobc2b3910795c635738e7d8ba741ede08d101701d0
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 #endif
231 #ifndef g_assert_true
232 #define g_assert_true(expr) \
233 do { \
234 if (G_LIKELY(expr)) { \
235 } else { \
236 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
237 "'" #expr "' should be TRUE"); \
239 } while (0)
240 #endif
242 #ifndef g_assert_false
243 #define g_assert_false(expr) \
244 do { \
245 if (G_LIKELY(!(expr))) { \
246 } else { \
247 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
248 "'" #expr "' should be FALSE"); \
250 } while (0)
251 #endif
253 #ifndef g_assert_null
254 #define g_assert_null(expr) \
255 do { \
256 if (G_LIKELY((expr) == NULL)) { \
257 } else { \
258 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
259 "'" #expr "' should be NULL"); \
261 } while (0)
262 #endif
264 #ifndef g_assert_nonnull
265 #define g_assert_nonnull(expr) \
266 do { \
267 if (G_LIKELY((expr) != NULL)) { \
268 } else { \
269 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
270 "'" #expr "' should not be NULL"); \
272 } while (0)
273 #endif
275 #ifndef g_assert_cmpmem
276 #define g_assert_cmpmem(m1, l1, m2, l2) \
277 do { \
278 gconstpointer __m1 = m1, __m2 = m2; \
279 int __l1 = l1, __l2 = l2; \
280 if (__l1 != __l2) { \
281 g_assertion_message_cmpnum( \
282 G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
283 #l1 " (len(" #m1 ")) == " #l2 " (len(" #m2 "))", __l1, "==", \
284 __l2, 'i'); \
285 } else if (memcmp(__m1, __m2, __l1) != 0) { \
286 g_assertion_message(G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, \
287 "assertion failed (" #m1 " == " #m2 ")"); \
289 } while (0)
290 #endif
292 #if !GLIB_CHECK_VERSION(2, 28, 0)
293 static inline void g_list_free_full(GList *list, GDestroyNotify free_func)
295 GList *l;
297 for (l = list; l; l = l->next) {
298 free_func(l->data);
301 g_list_free(list);
304 static inline void g_slist_free_full(GSList *list, GDestroyNotify free_func)
306 GSList *l;
308 for (l = list; l; l = l->next) {
309 free_func(l->data);
312 g_slist_free(list);
314 #endif
316 #if !GLIB_CHECK_VERSION(2, 26, 0)
317 static inline void g_source_set_name(GSource *source, const char *name)
319 /* This is just a debugging aid, so leaving it a no-op */
321 static inline void g_source_set_name_by_id(guint tag, const char *name)
323 /* This is just a debugging aid, so leaving it a no-op */
325 #endif
327 #if !GLIB_CHECK_VERSION(2, 36, 0)
328 /* Always fail. This will not include error_report output in the test log,
329 * sending it instead to stderr.
331 #define g_test_initialized() (0)
332 #endif
333 #if !GLIB_CHECK_VERSION(2, 38, 0)
334 #ifdef CONFIG_HAS_GLIB_SUBPROCESS_TESTS
335 #error schizophrenic detection of glib subprocess testing
336 #endif
337 #define g_test_subprocess() (0)
338 #endif
341 #if !GLIB_CHECK_VERSION(2, 34, 0)
342 static inline void
343 g_test_add_data_func_full(const char *path,
344 gpointer data,
345 gpointer fn,
346 gpointer data_free_func)
348 #if GLIB_CHECK_VERSION(2, 26, 0)
349 /* back-compat casts, remove this once we can require new-enough glib */
350 g_test_add_vtable(path, 0, data, NULL,
351 (GTestFixtureFunc)fn, (GTestFixtureFunc) data_free_func);
352 #else
353 /* back-compat casts, remove this once we can require new-enough glib */
354 g_test_add_vtable(path, 0, data, NULL,
355 (void (*)(void)) fn, (void (*)(void)) data_free_func);
356 #endif
358 #endif
361 #endif