glib-compat.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. #ifndef QEMU_GLIB_COMPAT_H
  16. #define QEMU_GLIB_COMPAT_H
  17. /* Ask for warnings for anything that was marked deprecated in
  18. * the defined version, or before. It is a candidate for rewrite.
  19. */
  20. #define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_66
  21. /* Ask for warnings if code tries to use function that did not
  22. * exist in the defined version. These risk breaking builds
  23. */
  24. #define GLIB_VERSION_MAX_ALLOWED GLIB_VERSION_2_66
  25. #pragma GCC diagnostic push
  26. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  27. #include <glib.h>
  28. #if defined(G_OS_UNIX)
  29. #include <glib-unix.h>
  30. #include <sys/types.h>
  31. #include <pwd.h>
  32. #endif
  33. /*
  34. * Note that because of the GLIB_VERSION_MAX_ALLOWED constant above, allowing
  35. * use of functions from newer GLib via this compat header needs a little
  36. * trickery to prevent warnings being emitted.
  37. *
  38. * Consider a function from newer glib-X.Y that we want to use
  39. *
  40. * int g_foo(const char *wibble)
  41. *
  42. * We must define a static inline function with the same signature that does
  43. * what we need, but with a "_compat" suffix e.g.
  44. *
  45. * static inline void g_foo_compat(const char *wibble)
  46. * {
  47. * #if GLIB_CHECK_VERSION(X, Y, 0)
  48. * g_foo(wibble)
  49. * #else
  50. * g_something_equivalent_in_older_glib(wibble);
  51. * #endif
  52. * }
  53. *
  54. * The #pragma at the top of this file turns off -Wdeprecated-declarations,
  55. * ensuring this wrapper function impl doesn't trigger the compiler warning
  56. * about using too new glib APIs. Finally we can do
  57. *
  58. * #define g_foo(a) g_foo_compat(a)
  59. *
  60. * So now the code elsewhere in QEMU, which *does* have the
  61. * -Wdeprecated-declarations warning active, can call g_foo(...) as normal,
  62. * without generating warnings.
  63. */
  64. /*
  65. * g_memdup2_qemu:
  66. * @mem: (nullable): the memory to copy.
  67. * @byte_size: the number of bytes to copy.
  68. *
  69. * Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
  70. * from @mem. If @mem is %NULL it returns %NULL.
  71. *
  72. * This replaces g_memdup(), which was prone to integer overflows when
  73. * converting the argument from a #gsize to a #guint.
  74. *
  75. * This static inline version is a backport of the new public API from
  76. * GLib 2.68, kept internal to GLib for backport to older stable releases.
  77. * See https://gitlab.gnome.org/GNOME/glib/-/issues/2319.
  78. *
  79. * Returns: (nullable): a pointer to the newly-allocated copy of the memory,
  80. * or %NULL if @mem is %NULL.
  81. */
  82. static inline gpointer g_memdup2_qemu(gconstpointer mem, gsize byte_size)
  83. {
  84. #if GLIB_CHECK_VERSION(2, 68, 0)
  85. return g_memdup2(mem, byte_size);
  86. #else
  87. gpointer new_mem;
  88. if (mem && byte_size != 0) {
  89. new_mem = g_malloc(byte_size);
  90. memcpy(new_mem, mem, byte_size);
  91. } else {
  92. new_mem = NULL;
  93. }
  94. return new_mem;
  95. #endif
  96. }
  97. #define g_memdup2(m, s) g_memdup2_qemu(m, s)
  98. static inline bool
  99. qemu_g_test_slow(void)
  100. {
  101. static int cached = -1;
  102. if (cached == -1) {
  103. cached = g_test_slow() || getenv("G_TEST_SLOW") != NULL;
  104. }
  105. return cached;
  106. }
  107. #undef g_test_slow
  108. #undef g_test_thorough
  109. #undef g_test_quick
  110. #define g_test_slow() qemu_g_test_slow()
  111. #define g_test_thorough() qemu_g_test_slow()
  112. #define g_test_quick() (!qemu_g_test_slow())
  113. #pragma GCC diagnostic pop
  114. #ifndef G_NORETURN
  115. #define G_NORETURN G_GNUC_NORETURN
  116. #endif
  117. #endif