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