grcbox.h 3.7 KB

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