gmem.h 16 KB

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