gmem.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  19. * file for a list of people on the GLib Team. See the ChangeLog
  20. * files for a list of changes. These files are distributed with
  21. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  22. */
  23. #ifndef __G_MEM_H__
  24. #define __G_MEM_H__
  25. #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #include <glib/gutils.h>
  29. G_BEGIN_DECLS
  30. /**
  31. * GMemVTable:
  32. * @malloc: function to use for allocating memory.
  33. * @realloc: function to use for reallocating memory.
  34. * @free: function to use to free memory.
  35. * @calloc: function to use for allocating zero-filled memory.
  36. * @try_malloc: function to use for allocating memory without a default error handler.
  37. * @try_realloc: function to use for reallocating memory without a default error handler.
  38. *
  39. * A set of functions used to perform memory allocation. The same #GMemVTable must
  40. * be used for all allocations in the same program; a call to g_mem_set_vtable(),
  41. * if it exists, should be prior to any use of GLib.
  42. *
  43. * This functions related to this has been deprecated in 2.46, and no longer work.
  44. */
  45. typedef struct _GMemVTable GMemVTable;
  46. #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
  47. /**
  48. * G_MEM_ALIGN:
  49. *
  50. * Indicates the number of bytes to which memory will be aligned on the
  51. * current platform.
  52. */
  53. # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
  54. #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
  55. # define G_MEM_ALIGN GLIB_SIZEOF_LONG
  56. #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
  57. /* Memory allocation functions
  58. */
  59. GLIB_AVAILABLE_IN_ALL
  60. void g_free (gpointer mem);
  61. GLIB_AVAILABLE_IN_2_34
  62. void g_clear_pointer (gpointer *pp,
  63. GDestroyNotify destroy);
  64. GLIB_AVAILABLE_IN_ALL
  65. gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  66. GLIB_AVAILABLE_IN_ALL
  67. gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  68. GLIB_AVAILABLE_IN_ALL
  69. gpointer g_realloc (gpointer mem,
  70. gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
  71. GLIB_AVAILABLE_IN_ALL
  72. gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  73. GLIB_AVAILABLE_IN_ALL
  74. gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  75. GLIB_AVAILABLE_IN_ALL
  76. gpointer g_try_realloc (gpointer mem,
  77. gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
  78. GLIB_AVAILABLE_IN_ALL
  79. gpointer g_malloc_n (gsize n_blocks,
  80. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  81. GLIB_AVAILABLE_IN_ALL
  82. gpointer g_malloc0_n (gsize n_blocks,
  83. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  84. GLIB_AVAILABLE_IN_ALL
  85. gpointer g_realloc_n (gpointer mem,
  86. gsize n_blocks,
  87. gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
  88. GLIB_AVAILABLE_IN_ALL
  89. gpointer g_try_malloc_n (gsize n_blocks,
  90. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  91. GLIB_AVAILABLE_IN_ALL
  92. gpointer g_try_malloc0_n (gsize n_blocks,
  93. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  94. GLIB_AVAILABLE_IN_ALL
  95. gpointer g_try_realloc_n (gpointer mem,
  96. gsize n_blocks,
  97. gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
  98. #define g_clear_pointer(pp, destroy) \
  99. G_STMT_START { \
  100. G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
  101. /* Only one access, please */ \
  102. gpointer *_pp = (gpointer *) (pp); \
  103. gpointer _p; \
  104. /* This assignment is needed to avoid a gcc warning */ \
  105. GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
  106. \
  107. _p = *_pp; \
  108. if (_p) \
  109. { \
  110. *_pp = NULL; \
  111. _destroy (_p); \
  112. } \
  113. } G_STMT_END
  114. /**
  115. * g_steal_pointer:
  116. * @pp: (not nullable): a pointer to a pointer
  117. *
  118. * Sets @pp to %NULL, returning the value that was there before.
  119. *
  120. * Conceptually, this transfers the ownership of the pointer from the
  121. * referenced variable to the "caller" of the macro (ie: "steals" the
  122. * reference).
  123. *
  124. * The return value will be properly typed, according to the type of
  125. * @pp.
  126. *
  127. * This can be very useful when combined with g_autoptr() to prevent the
  128. * return value of a function from being automatically freed. Consider
  129. * the following example (which only works on GCC and clang):
  130. *
  131. * |[
  132. * GObject *
  133. * create_object (void)
  134. * {
  135. * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
  136. *
  137. * if (early_error_case)
  138. * return NULL;
  139. *
  140. * return g_steal_pointer (&obj);
  141. * }
  142. * ]|
  143. *
  144. * It can also be used in similar ways for 'out' parameters and is
  145. * particularly useful for dealing with optional out parameters:
  146. *
  147. * |[
  148. * gboolean
  149. * get_object (GObject **obj_out)
  150. * {
  151. * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
  152. *
  153. * if (early_error_case)
  154. * return FALSE;
  155. *
  156. * if (obj_out)
  157. * *obj_out = g_steal_pointer (&obj);
  158. *
  159. * return TRUE;
  160. * }
  161. * ]|
  162. *
  163. * In the above example, the object will be automatically freed in the
  164. * early error case and also in the case that %NULL was given for
  165. * @obj_out.
  166. *
  167. * Since: 2.44
  168. */
  169. static inline gpointer
  170. g_steal_pointer (gpointer pp)
  171. {
  172. gpointer *ptr = (gpointer *) pp;
  173. gpointer ref;
  174. ref = *ptr;
  175. *ptr = NULL;
  176. return ref;
  177. }
  178. /* type safety */
  179. #define g_steal_pointer(pp) \
  180. (0 ? (*(pp)) : (g_steal_pointer) (pp))
  181. /* Optimise: avoid the call to the (slower) _n function if we can
  182. * determine at compile-time that no overflow happens.
  183. */
  184. #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
  185. # define _G_NEW(struct_type, n_structs, func) \
  186. (struct_type *) (G_GNUC_EXTENSION ({ \
  187. gsize __n = (gsize) (n_structs); \
  188. gsize __s = sizeof (struct_type); \
  189. gpointer __p; \
  190. if (__s == 1) \
  191. __p = g_##func (__n); \
  192. else if (__builtin_constant_p (__n) && \
  193. (__s == 0 || __n <= G_MAXSIZE / __s)) \
  194. __p = g_##func (__n * __s); \
  195. else \
  196. __p = g_##func##_n (__n, __s); \
  197. __p; \
  198. }))
  199. # define _G_RENEW(struct_type, mem, n_structs, func) \
  200. (struct_type *) (G_GNUC_EXTENSION ({ \
  201. gsize __n = (gsize) (n_structs); \
  202. gsize __s = sizeof (struct_type); \
  203. gpointer __p = (gpointer) (mem); \
  204. if (__s == 1) \
  205. __p = g_##func (__p, __n); \
  206. else if (__builtin_constant_p (__n) && \
  207. (__s == 0 || __n <= G_MAXSIZE / __s)) \
  208. __p = g_##func (__p, __n * __s); \
  209. else \
  210. __p = g_##func##_n (__p, __n, __s); \
  211. __p; \
  212. }))
  213. #else
  214. /* Unoptimised version: always call the _n() function. */
  215. #define _G_NEW(struct_type, n_structs, func) \
  216. ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
  217. #define _G_RENEW(struct_type, mem, n_structs, func) \
  218. ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
  219. #endif
  220. /**
  221. * g_new:
  222. * @struct_type: the type of the elements to allocate
  223. * @n_structs: the number of elements to allocate
  224. *
  225. * Allocates @n_structs elements of type @struct_type.
  226. * The returned pointer is cast to a pointer to the given type.
  227. * If @n_structs is 0 it returns %NULL.
  228. * Care is taken to avoid overflow when calculating the size of the allocated block.
  229. *
  230. * Since the returned pointer is already casted to the right type,
  231. * it is normally unnecessary to cast it explicitly, and doing
  232. * so might hide memory allocation errors.
  233. *
  234. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  235. */
  236. #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
  237. /**
  238. * g_new0:
  239. * @struct_type: the type of the elements to allocate.
  240. * @n_structs: the number of elements to allocate.
  241. *
  242. * Allocates @n_structs elements of type @struct_type, initialized to 0's.
  243. * The returned pointer is cast to a pointer to the given type.
  244. * If @n_structs is 0 it returns %NULL.
  245. * Care is taken to avoid overflow when calculating the size of the allocated block.
  246. *
  247. * Since the returned pointer is already casted to the right type,
  248. * it is normally unnecessary to cast it explicitly, and doing
  249. * so might hide memory allocation errors.
  250. *
  251. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
  252. */
  253. #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
  254. /**
  255. * g_renew:
  256. * @struct_type: the type of the elements to allocate
  257. * @mem: the currently allocated memory
  258. * @n_structs: the number of elements to allocate
  259. *
  260. * Reallocates the memory pointed to by @mem, so that it now has space for
  261. * @n_structs elements of type @struct_type. It returns the new address of
  262. * the memory, which may have been moved.
  263. * Care is taken to avoid overflow when calculating the size of the allocated block.
  264. *
  265. * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
  266. */
  267. #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
  268. /**
  269. * g_try_new:
  270. * @struct_type: the type of the elements to allocate
  271. * @n_structs: the number of elements to allocate
  272. *
  273. * Attempts to allocate @n_structs elements of type @struct_type, and returns
  274. * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
  275. * The returned pointer is cast to a pointer to the given type.
  276. * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
  277. *
  278. * Since: 2.8
  279. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  280. */
  281. #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
  282. /**
  283. * g_try_new0:
  284. * @struct_type: the type of the elements to allocate
  285. * @n_structs: the number of elements to allocate
  286. *
  287. * Attempts to allocate @n_structs elements of type @struct_type, initialized
  288. * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
  289. * the program on failure.
  290. * The returned pointer is cast to a pointer to the given type.
  291. * The function returns %NULL when @n_structs is 0 or if an overflow occurs.
  292. *
  293. * Since: 2.8
  294. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  295. */
  296. #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
  297. /**
  298. * g_try_renew:
  299. * @struct_type: the type of the elements to allocate
  300. * @mem: the currently allocated memory
  301. * @n_structs: the number of elements to allocate
  302. *
  303. * Attempts to reallocate the memory pointed to by @mem, so that it now has
  304. * space for @n_structs elements of type @struct_type, and returns %NULL on
  305. * failure. Contrast with g_renew(), which aborts the program on failure.
  306. * It returns the new address of the memory, which may have been moved.
  307. * The function returns %NULL if an overflow occurs.
  308. *
  309. * Since: 2.8
  310. * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
  311. */
  312. #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
  313. /* Memory allocation virtualization for debugging purposes
  314. * g_mem_set_vtable() has to be the very first GLib function called
  315. * if being used
  316. */
  317. struct _GMemVTable {
  318. gpointer (*malloc) (gsize n_bytes);
  319. gpointer (*realloc) (gpointer mem,
  320. gsize n_bytes);
  321. void (*free) (gpointer mem);
  322. /* optional; set to NULL if not used ! */
  323. gpointer (*calloc) (gsize n_blocks,
  324. gsize n_block_bytes);
  325. gpointer (*try_malloc) (gsize n_bytes);
  326. gpointer (*try_realloc) (gpointer mem,
  327. gsize n_bytes);
  328. };
  329. GLIB_DEPRECATED_IN_2_46
  330. void g_mem_set_vtable (GMemVTable *vtable);
  331. GLIB_DEPRECATED_IN_2_46
  332. gboolean g_mem_is_system_malloc (void);
  333. GLIB_VAR gboolean g_mem_gc_friendly;
  334. /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
  335. */
  336. GLIB_VAR GMemVTable *glib_mem_profiler_table;
  337. GLIB_DEPRECATED_IN_2_46
  338. void g_mem_profile (void);
  339. G_END_DECLS
  340. #endif /* __G_MEM_H__ */