| 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 | #include <glib/gstdio.h> |
| 34 | #if defined(G_OS_UNIX) |
| 35 | #include <glib-unix.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <pwd.h> |
| 38 | #endif |
| 39 | |
| 40 | /* |
| 41 | * These functions perform function pointer casts which can cause function call |
| 42 | * failure on Emscripten. Use g_slist_sort_with_data and g_list_sort_with_data |
| 43 | * instead of these functions. |
| 44 | */ |
| 45 | #pragma GCC poison g_slist_sort g_list_sort |
| 46 | |
| 47 | /* |
| 48 | * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing |
| 49 | * use of functions from newer GLib via this compat header needs a little |
| 50 | * trickery to prevent warnings being emitted. |
| 51 | * |
| 52 | * Consider a function from newer glib-X.Y that we want to use |
| 53 | * |
| 54 | * int g_foo(const char *wibble) |
| 55 | * |
| 56 | * We must define a static inline function with the same signature that does |
| 57 | * what we need, but with a "_compat" suffix e.g. |
| 58 | * |
| 59 | * static inline void g_foo_compat(const char *wibble) |
| 60 | * { |
| 61 | * #if GLIB_CHECK_VERSION(X, Y, 0) |
| 62 | * g_foo(wibble) |
| 63 | * #else |
| 64 | * g_something_equivalent_in_older_glib(wibble); |
| 65 | * #endif |
| 66 | * } |
| 67 | * |
| 68 | * The #pragma at the top of this file turns off -Wdeprecated-declarations, |
| 69 | * ensuring this wrapper function impl doesn't trigger the compiler warning |
| 70 | * about using too new glib APIs. Finally we can do |
| 71 | * |
| 72 | * #define g_foo(a) g_foo_compat(a) |
| 73 | * |
| 74 | * So now the code elsewhere in QEMU, which *does* have the |
| 75 | * -Wdeprecated-declarations warning active, can call g_foo(...) as normal, |
| 76 | * without generating warnings. |
| 77 | */ |
| 78 | |
| 79 | /* |
| 80 | * g_memdup2_qemu: |
| 81 | * @mem: (nullable): the memory to copy. |
| 82 | * @byte_size: the number of bytes to copy. |
| 83 | * |
| 84 | * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it |
| 85 | * from @mem. If @mem is %NULL it returns %NULL. |
| 86 | * |
| 87 | * This replaces g_memdup(), which was prone to integer overflows when |
| 88 | * converting the argument from a #gsize to a #guint. |
| 89 | * |
| 90 | * This static inline version is a backport of the new public API from |
| 91 | * GLib 2.68, kept internal to GLib for backport to older stable releases. |
| 92 | * See https://gitlab.gnome.org/GNOME/glib/-/issues/2319. |
| 93 | * |
| 94 | * Returns: (nullable): a pointer to the newly-allocated copy of the memory, |
| 95 | * or %NULL if @mem is %NULL. |
| 96 | */ |
| 97 | static inline gpointer g_memdup2_qemu(gconstpointer mem, gsize byte_size) |
| 98 | { |
| 99 | #if GLIB_CHECK_VERSION(2, 68, 0) |
| 100 | return g_memdup2(mem, byte_size); |
| 101 | #else |
| 102 | gpointer new_mem; |
| 103 | |
| 104 | if (mem && byte_size != 0) { |
| 105 | new_mem = g_malloc(byte_size); |
| 106 | memcpy(new_mem, mem, byte_size); |
| 107 | } else { |
| 108 | new_mem = NULL; |
| 109 | } |
| 110 | |
| 111 | return new_mem; |
| 112 | #endif |
| 113 | } |
| 114 | #define g_memdup2(m, s) g_memdup2_qemu(m, s) |
| 115 | |
| 116 | static inline bool |
| 117 | qemu_g_test_slow(void) |
| 118 | { |
| 119 | static int cached = -1; |
| 120 | if (cached == -1) { |
| 121 | cached = g_test_slow() || getenv("G_TEST_SLOW") != NULL; |
| 122 | } |
| 123 | return cached; |
| 124 | } |
| 125 | |
| 126 | #undef g_test_slow |
| 127 | #undef g_test_thorough |
| 128 | #undef g_test_quick |
| 129 | #define g_test_slow() qemu_g_test_slow() |
| 130 | #define g_test_thorough() qemu_g_test_slow() |
| 131 | #define g_test_quick() (!qemu_g_test_slow()) |
| 132 | |
| 133 | static inline gboolean g_clear_fd_qemu(int *fd_ptr, GError **error) |
| 134 | { |
| 135 | #if GLIB_CHECK_VERSION(2, 76, 0) |
| 136 | return g_clear_fd(fd_ptr, error); |
| 137 | #else |
| 138 | int fd = *fd_ptr; |
| 139 | |
| 140 | *fd_ptr = -1; |
| 141 | |
| 142 | if (fd < 0) { |
| 143 | return TRUE; |
| 144 | } |
| 145 | |
| 146 | return g_close(fd, error); |
| 147 | #endif |
| 148 | } |
| 149 | #define g_clear_fd(fd, err) g_clear_fd_qemu(fd, err) |
| 150 | |
| 151 | #if !GLIB_CHECK_VERSION(2, 76, 0) |
| 152 | static inline void _g_clear_fd_ignore_error(int *fd_ptr) |
| 153 | { |
| 154 | int errsv = errno; |
| 155 | g_clear_fd(fd_ptr, NULL); |
| 156 | errno = errsv; |
| 157 | } |
| 158 | #define g_autofd __attribute__((cleanup(_g_clear_fd_ignore_error))) |
| 159 | #endif |
| 160 | |
| 161 | #pragma GCC diagnostic pop |
| 162 | |
| 163 | #ifndef G_NORETURN |
| 164 | #define G_NORETURN G_GNUC_NORETURN |
| 165 | #endif |
| 166 | |
| 167 | #endif |