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