gslice.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* GLIB sliced memory - fast threaded memory chunk allocator
  2. * Copyright (C) 2005 Tim Janik
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef __G_SLICE_H__
  20. #define __G_SLICE_H__
  21. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  22. #error "Only <glib.h> can be included directly."
  23. #endif
  24. #include <glib/gtypes.h>
  25. #include <string.h>
  26. G_BEGIN_DECLS
  27. /* slices - fast allocation/release of small memory blocks
  28. */
  29. GLIB_AVAILABLE_IN_ALL
  30. gpointer g_slice_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  31. GLIB_AVAILABLE_IN_ALL
  32. gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  33. GLIB_AVAILABLE_IN_ALL
  34. gpointer g_slice_copy (gsize block_size,
  35. gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
  36. GLIB_AVAILABLE_IN_ALL
  37. void g_slice_free1 (gsize block_size,
  38. gpointer mem_block);
  39. GLIB_AVAILABLE_IN_ALL
  40. void g_slice_free_chain_with_offset (gsize block_size,
  41. gpointer mem_chain,
  42. gsize next_offset);
  43. #define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type)))
  44. /* Allow the compiler to inline memset(). Since the size is a constant, this
  45. * can significantly improve performance. */
  46. #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
  47. # define g_slice_new0(type) \
  48. (type *) (G_GNUC_EXTENSION ({ \
  49. gsize __s = sizeof (type); \
  50. gpointer __p; \
  51. __p = g_slice_alloc (__s); \
  52. memset (__p, 0, __s); \
  53. __p; \
  54. }))
  55. #else
  56. # define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type)))
  57. #endif
  58. /* MemoryBlockType *
  59. * g_slice_dup (MemoryBlockType,
  60. * MemoryBlockType *mem_block);
  61. * g_slice_free (MemoryBlockType,
  62. * MemoryBlockType *mem_block);
  63. * g_slice_free_chain (MemoryBlockType,
  64. * MemoryBlockType *first_chain_block,
  65. * memory_block_next_field);
  66. * pseudo prototypes for the macro
  67. * definitions following below.
  68. */
  69. /* we go through extra hoops to ensure type safety */
  70. #define g_slice_dup(type, mem) \
  71. (1 ? (type*) g_slice_copy (sizeof (type), (mem)) \
  72. : ((void) ((type*) 0 == (mem)), (type*) 0))
  73. #define g_slice_free(type, mem) \
  74. G_STMT_START { \
  75. if (1) g_slice_free1 (sizeof (type), (mem)); \
  76. else (void) ((type*) 0 == (mem)); \
  77. } G_STMT_END
  78. #define g_slice_free_chain(type, mem_chain, next) \
  79. G_STMT_START { \
  80. if (1) g_slice_free_chain_with_offset (sizeof (type), \
  81. (mem_chain), G_STRUCT_OFFSET (type, next)); \
  82. else (void) ((type*) 0 == (mem_chain)); \
  83. } G_STMT_END
  84. /* --- internal debugging API --- */
  85. typedef enum {
  86. G_SLICE_CONFIG_ALWAYS_MALLOC = 1,
  87. G_SLICE_CONFIG_BYPASS_MAGAZINES,
  88. G_SLICE_CONFIG_WORKING_SET_MSECS,
  89. G_SLICE_CONFIG_COLOR_INCREMENT,
  90. G_SLICE_CONFIG_CHUNK_SIZES,
  91. G_SLICE_CONFIG_CONTENTION_COUNTER
  92. } GSliceConfig;
  93. GLIB_DEPRECATED_IN_2_34
  94. void g_slice_set_config (GSliceConfig ckey, gint64 value);
  95. GLIB_DEPRECATED_IN_2_34
  96. gint64 g_slice_get_config (GSliceConfig ckey);
  97. GLIB_DEPRECATED_IN_2_34
  98. gint64* g_slice_get_config_state (GSliceConfig ckey, gint64 address, guint *n_values);
  99. #ifndef __GI_SCANNER__
  100. #ifdef G_ENABLE_DEBUG
  101. GLIB_AVAILABLE_IN_ALL
  102. void g_slice_debug_tree_statistics (void);
  103. #endif
  104. #endif
  105. G_END_DECLS
  106. #endif /* __G_SLICE_H__ */