grcbox.h 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* grcbox.h: Reference counted data
  2. *
  3. * Copyright 2018 Emmanuele Bassi
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #pragma once
  21. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  22. #error "Only <glib.h> can be included directly."
  23. #endif
  24. #include <glib/gmem.h>
  25. #include <glib/glib-typeof.h>
  26. G_BEGIN_DECLS
  27. GLIB_AVAILABLE_IN_2_58
  28. gpointer g_rc_box_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  29. GLIB_AVAILABLE_IN_2_58
  30. gpointer g_rc_box_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  31. GLIB_AVAILABLE_IN_2_58
  32. gpointer g_rc_box_dup (gsize block_size,
  33. gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
  34. GLIB_AVAILABLE_IN_2_58
  35. gpointer g_rc_box_acquire (gpointer mem_block);
  36. GLIB_AVAILABLE_IN_2_58
  37. void g_rc_box_release (gpointer mem_block);
  38. GLIB_AVAILABLE_IN_2_58
  39. void g_rc_box_release_full (gpointer mem_block,
  40. GDestroyNotify clear_func);
  41. GLIB_AVAILABLE_IN_2_58
  42. gsize g_rc_box_get_size (gpointer mem_block);
  43. GLIB_AVAILABLE_IN_2_58
  44. gpointer g_atomic_rc_box_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  45. GLIB_AVAILABLE_IN_2_58
  46. gpointer g_atomic_rc_box_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  47. GLIB_AVAILABLE_IN_2_58
  48. gpointer g_atomic_rc_box_dup (gsize block_size,
  49. gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
  50. GLIB_AVAILABLE_IN_2_58
  51. gpointer g_atomic_rc_box_acquire (gpointer mem_block);
  52. GLIB_AVAILABLE_IN_2_58
  53. void g_atomic_rc_box_release (gpointer mem_block);
  54. GLIB_AVAILABLE_IN_2_58
  55. void g_atomic_rc_box_release_full (gpointer mem_block,
  56. GDestroyNotify clear_func);
  57. GLIB_AVAILABLE_IN_2_58
  58. gsize g_atomic_rc_box_get_size (gpointer mem_block);
  59. #define g_rc_box_new(type) \
  60. ((type *) g_rc_box_alloc (sizeof (type)))
  61. #define g_rc_box_new0(type) \
  62. ((type *) g_rc_box_alloc0 (sizeof (type)))
  63. #define g_atomic_rc_box_new(type) \
  64. ((type *) g_atomic_rc_box_alloc (sizeof (type)))
  65. #define g_atomic_rc_box_new0(type) \
  66. ((type *) g_atomic_rc_box_alloc0 (sizeof (type)))
  67. #if defined(glib_typeof)
  68. /* Type check to avoid assigning references to different types */
  69. #define g_rc_box_acquire(mem_block) \
  70. ((glib_typeof (mem_block)) (g_rc_box_acquire) (mem_block))
  71. #define g_atomic_rc_box_acquire(mem_block) \
  72. ((glib_typeof (mem_block)) (g_atomic_rc_box_acquire) (mem_block))
  73. /* Type check to avoid duplicating data to different types */
  74. #define g_rc_box_dup(block_size, mem_block) \
  75. ((glib_typeof (mem_block)) (g_rc_box_dup) (block_size, mem_block))
  76. #define g_atomic_rc_box_dup(block_size, mem_block) \
  77. ((glib_typeof (mem_block)) (g_atomic_rc_box_dup) (block_size, mem_block))
  78. #endif
  79. G_END_DECLS