gmem.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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.1 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. #if defined(g_has_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
  99. #define g_clear_pointer(pp, destroy) \
  100. G_STMT_START { \
  101. G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
  102. __typeof__((pp)) _pp = (pp); \
  103. __typeof__(*(pp)) _ptr = *_pp; \
  104. *_pp = NULL; \
  105. if (_ptr) \
  106. (destroy) (_ptr); \
  107. } G_STMT_END \
  108. GLIB_AVAILABLE_MACRO_IN_2_34
  109. #else /* __GNUC__ */
  110. #define g_clear_pointer(pp, destroy) \
  111. G_STMT_START { \
  112. G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
  113. /* Only one access, please; work around type aliasing */ \
  114. union { char *in; gpointer *out; } _pp; \
  115. gpointer _p; \
  116. /* This assignment is needed to avoid a gcc warning */ \
  117. GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
  118. \
  119. _pp.in = (char *) (pp); \
  120. _p = *_pp.out; \
  121. if (_p) \
  122. { \
  123. *_pp.out = NULL; \
  124. _destroy (_p); \
  125. } \
  126. } G_STMT_END \
  127. GLIB_AVAILABLE_MACRO_IN_2_34
  128. #endif /* __GNUC__ */
  129. /**
  130. * g_steal_pointer:
  131. * @pp: (not nullable): a pointer to a pointer
  132. *
  133. * Sets @pp to %NULL, returning the value that was there before.
  134. *
  135. * Conceptually, this transfers the ownership of the pointer from the
  136. * referenced variable to the "caller" of the macro (ie: "steals" the
  137. * reference).
  138. *
  139. * The return value will be properly typed, according to the type of
  140. * @pp.
  141. *
  142. * This can be very useful when combined with g_autoptr() to prevent the
  143. * return value of a function from being automatically freed. Consider
  144. * the following example (which only works on GCC and clang):
  145. *
  146. * |[
  147. * GObject *
  148. * create_object (void)
  149. * {
  150. * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
  151. *
  152. * if (early_error_case)
  153. * return NULL;
  154. *
  155. * return g_steal_pointer (&obj);
  156. * }
  157. * ]|
  158. *
  159. * It can also be used in similar ways for 'out' parameters and is
  160. * particularly useful for dealing with optional out parameters:
  161. *
  162. * |[
  163. * gboolean
  164. * get_object (GObject **obj_out)
  165. * {
  166. * g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
  167. *
  168. * if (early_error_case)
  169. * return FALSE;
  170. *
  171. * if (obj_out)
  172. * *obj_out = g_steal_pointer (&obj);
  173. *
  174. * return TRUE;
  175. * }
  176. * ]|
  177. *
  178. * In the above example, the object will be automatically freed in the
  179. * early error case and also in the case that %NULL was given for
  180. * @obj_out.
  181. *
  182. * Since: 2.44
  183. */
  184. static inline gpointer
  185. g_steal_pointer (gpointer pp)
  186. {
  187. gpointer *ptr = (gpointer *) pp;
  188. gpointer ref;
  189. ref = *ptr;
  190. *ptr = NULL;
  191. return ref;
  192. }
  193. /* type safety */
  194. #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)) && !defined(__cplusplus) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
  195. #define g_steal_pointer(pp) ((__typeof__(*pp)) (g_steal_pointer) (pp))
  196. #else /* __GNUC__ */
  197. /* This version does not depend on gcc extensions, but gcc does not warn
  198. * about incompatible-pointer-types: */
  199. #define g_steal_pointer(pp) \
  200. (0 ? (*(pp)) : (g_steal_pointer) (pp))
  201. #endif /* __GNUC__ */
  202. /* Optimise: avoid the call to the (slower) _n function if we can
  203. * determine at compile-time that no overflow happens.
  204. */
  205. #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
  206. # define _G_NEW(struct_type, n_structs, func) \
  207. (struct_type *) (G_GNUC_EXTENSION ({ \
  208. gsize __n = (gsize) (n_structs); \
  209. gsize __s = sizeof (struct_type); \
  210. gpointer __p; \
  211. if (__s == 1) \
  212. __p = g_##func (__n); \
  213. else if (__builtin_constant_p (__n) && \
  214. (__s == 0 || __n <= G_MAXSIZE / __s)) \
  215. __p = g_##func (__n * __s); \
  216. else \
  217. __p = g_##func##_n (__n, __s); \
  218. __p; \
  219. }))
  220. # define _G_RENEW(struct_type, mem, n_structs, func) \
  221. (struct_type *) (G_GNUC_EXTENSION ({ \
  222. gsize __n = (gsize) (n_structs); \
  223. gsize __s = sizeof (struct_type); \
  224. gpointer __p = (gpointer) (mem); \
  225. if (__s == 1) \
  226. __p = g_##func (__p, __n); \
  227. else if (__builtin_constant_p (__n) && \
  228. (__s == 0 || __n <= G_MAXSIZE / __s)) \
  229. __p = g_##func (__p, __n * __s); \
  230. else \
  231. __p = g_##func##_n (__p, __n, __s); \
  232. __p; \
  233. }))
  234. #else
  235. /* Unoptimised version: always call the _n() function. */
  236. #define _G_NEW(struct_type, n_structs, func) \
  237. ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
  238. #define _G_RENEW(struct_type, mem, n_structs, func) \
  239. ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
  240. #endif
  241. /**
  242. * g_new:
  243. * @struct_type: the type of the elements to allocate
  244. * @n_structs: the number of elements to allocate
  245. *
  246. * Allocates @n_structs elements of type @struct_type.
  247. * The returned pointer is cast to a pointer to the given type.
  248. * If @n_structs is 0 it returns %NULL.
  249. * Care is taken to avoid overflow when calculating the size of the allocated block.
  250. *
  251. * Since the returned pointer is already casted to the right type,
  252. * it is normally unnecessary to cast it explicitly, and doing
  253. * so might hide memory allocation errors.
  254. *
  255. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  256. */
  257. #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
  258. /**
  259. * g_new0:
  260. * @struct_type: the type of the elements to allocate.
  261. * @n_structs: the number of elements to allocate.
  262. *
  263. * Allocates @n_structs elements of type @struct_type, initialized to 0's.
  264. * The returned pointer is cast to a pointer to the given type.
  265. * If @n_structs is 0 it returns %NULL.
  266. * Care is taken to avoid overflow when calculating the size of the allocated block.
  267. *
  268. * Since the returned pointer is already casted to the right type,
  269. * it is normally unnecessary to cast it explicitly, and doing
  270. * so might hide memory allocation errors.
  271. *
  272. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
  273. */
  274. #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
  275. /**
  276. * g_renew:
  277. * @struct_type: the type of the elements to allocate
  278. * @mem: the currently allocated memory
  279. * @n_structs: the number of elements to allocate
  280. *
  281. * Reallocates the memory pointed to by @mem, so that it now has space for
  282. * @n_structs elements of type @struct_type. It returns the new address of
  283. * the memory, which may have been moved.
  284. * Care is taken to avoid overflow when calculating the size of the allocated block.
  285. *
  286. * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
  287. */
  288. #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
  289. /**
  290. * g_try_new:
  291. * @struct_type: the type of the elements to allocate
  292. * @n_structs: the number of elements to allocate
  293. *
  294. * Attempts to allocate @n_structs elements of type @struct_type, and returns
  295. * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
  296. * The returned pointer is cast to a pointer to the given type.
  297. * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
  298. *
  299. * Since: 2.8
  300. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  301. */
  302. #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
  303. /**
  304. * g_try_new0:
  305. * @struct_type: the type of the elements to allocate
  306. * @n_structs: the number of elements to allocate
  307. *
  308. * Attempts to allocate @n_structs elements of type @struct_type, initialized
  309. * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
  310. * the program on failure.
  311. * The returned pointer is cast to a pointer to the given type.
  312. * The function returns %NULL when @n_structs is 0 or if an overflow occurs.
  313. *
  314. * Since: 2.8
  315. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  316. */
  317. #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
  318. /**
  319. * g_try_renew:
  320. * @struct_type: the type of the elements to allocate
  321. * @mem: the currently allocated memory
  322. * @n_structs: the number of elements to allocate
  323. *
  324. * Attempts to reallocate the memory pointed to by @mem, so that it now has
  325. * space for @n_structs elements of type @struct_type, and returns %NULL on
  326. * failure. Contrast with g_renew(), which aborts the program on failure.
  327. * It returns the new address of the memory, which may have been moved.
  328. * The function returns %NULL if an overflow occurs.
  329. *
  330. * Since: 2.8
  331. * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
  332. */
  333. #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
  334. /* Memory allocation virtualization for debugging purposes
  335. * g_mem_set_vtable() has to be the very first GLib function called
  336. * if being used
  337. */
  338. struct _GMemVTable {
  339. gpointer (*malloc) (gsize n_bytes);
  340. gpointer (*realloc) (gpointer mem,
  341. gsize n_bytes);
  342. void (*free) (gpointer mem);
  343. /* optional; set to NULL if not used ! */
  344. gpointer (*calloc) (gsize n_blocks,
  345. gsize n_block_bytes);
  346. gpointer (*try_malloc) (gsize n_bytes);
  347. gpointer (*try_realloc) (gpointer mem,
  348. gsize n_bytes);
  349. };
  350. GLIB_DEPRECATED_IN_2_46
  351. void g_mem_set_vtable (GMemVTable *vtable);
  352. GLIB_DEPRECATED_IN_2_46
  353. gboolean g_mem_is_system_malloc (void);
  354. GLIB_VAR gboolean g_mem_gc_friendly;
  355. /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
  356. */
  357. GLIB_VAR GMemVTable *glib_mem_profiler_table;
  358. GLIB_DEPRECATED_IN_2_46
  359. void g_mem_profile (void);
  360. G_END_DECLS
  361. #endif /* __G_MEM_H__ */