Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-22 10:14:06

0001 /* GLIB - Library of useful routines for C programming
0002  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
0003  *
0004  * SPDX-License-Identifier: LGPL-2.1-or-later
0005  *
0006  * This library is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Lesser General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2.1 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * Lesser General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU Lesser General Public
0017  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
0018  */
0019 
0020 /*
0021  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
0022  * file for a list of people on the GLib Team.  See the ChangeLog
0023  * files for a list of changes.  These files are distributed with
0024  * GLib at ftp://ftp.gtk.org/pub/gtk/.
0025  */
0026 
0027 #ifndef __G_MEM_H__
0028 #define __G_MEM_H__
0029 
0030 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
0031 #error "Only <glib.h> can be included directly."
0032 #endif
0033 
0034 #include <glib/gutils.h>
0035 #include <glib/glib-typeof.h>
0036 
0037 G_BEGIN_DECLS
0038 
0039 /**
0040  * GMemVTable:
0041  * @malloc: function to use for allocating memory.
0042  * @realloc: function to use for reallocating memory.
0043  * @free: function to use to free memory.
0044  * @calloc: function to use for allocating zero-filled memory.
0045  * @try_malloc: function to use for allocating memory without a default error handler.
0046  * @try_realloc: function to use for reallocating memory without a default error handler.
0047  * 
0048  * A set of functions used to perform memory allocation. The same #GMemVTable must
0049  * be used for all allocations in the same program; a call to g_mem_set_vtable(),
0050  * if it exists, should be prior to any use of GLib.
0051  *
0052  * This functions related to this has been deprecated in 2.46, and no longer work.
0053  */
0054 typedef struct _GMemVTable GMemVTable;
0055 
0056 
0057 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
0058 /**
0059  * G_MEM_ALIGN:
0060  *
0061  * Indicates the number of bytes to which memory will be aligned on the
0062  * current platform.
0063  */
0064 #  define G_MEM_ALIGN   GLIB_SIZEOF_VOID_P
0065 #else   /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
0066 #  define G_MEM_ALIGN   GLIB_SIZEOF_LONG
0067 #endif  /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
0068 
0069 
0070 /* Memory allocation functions
0071  */
0072 
0073 GLIB_AVAILABLE_IN_ALL
0074 void     (g_free)         (gpointer      mem);
0075 GLIB_AVAILABLE_IN_2_76
0076 void     g_free_sized     (gpointer      mem,
0077                            size_t        size);
0078 
0079 GLIB_AVAILABLE_IN_2_34
0080 void     g_clear_pointer  (gpointer      *pp,
0081                            GDestroyNotify destroy);
0082 
0083 GLIB_AVAILABLE_IN_ALL
0084 gpointer g_malloc         (gsize     n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
0085 GLIB_AVAILABLE_IN_ALL
0086 gpointer g_malloc0        (gsize     n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
0087 GLIB_AVAILABLE_IN_ALL
0088 gpointer g_realloc        (gpointer  mem,
0089                gsize     n_bytes) G_GNUC_WARN_UNUSED_RESULT;
0090 GLIB_AVAILABLE_IN_ALL
0091 gpointer g_try_malloc     (gsize     n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
0092 GLIB_AVAILABLE_IN_ALL
0093 gpointer g_try_malloc0    (gsize     n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
0094 GLIB_AVAILABLE_IN_ALL
0095 gpointer g_try_realloc    (gpointer  mem,
0096                gsize     n_bytes) G_GNUC_WARN_UNUSED_RESULT;
0097 
0098 GLIB_AVAILABLE_IN_ALL
0099 gpointer g_malloc_n       (gsize     n_blocks,
0100                gsize     n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
0101 GLIB_AVAILABLE_IN_ALL
0102 gpointer g_malloc0_n      (gsize     n_blocks,
0103                gsize     n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
0104 GLIB_AVAILABLE_IN_ALL
0105 gpointer g_realloc_n      (gpointer  mem,
0106                gsize     n_blocks,
0107                gsize     n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
0108 GLIB_AVAILABLE_IN_ALL
0109 gpointer g_try_malloc_n   (gsize     n_blocks,
0110                gsize     n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
0111 GLIB_AVAILABLE_IN_ALL
0112 gpointer g_try_malloc0_n  (gsize     n_blocks,
0113                gsize     n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
0114 GLIB_AVAILABLE_IN_ALL
0115 gpointer g_try_realloc_n  (gpointer  mem,
0116                gsize     n_blocks,
0117                gsize     n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
0118 
0119 GLIB_AVAILABLE_IN_2_72
0120 gpointer g_aligned_alloc  (gsize         n_blocks,
0121                            gsize         n_block_bytes,
0122                            gsize         alignment) G_GNUC_WARN_UNUSED_RESULT G_GNUC_ALLOC_SIZE2(1,2);
0123 GLIB_AVAILABLE_IN_2_72
0124 gpointer g_aligned_alloc0 (gsize         n_blocks,
0125                            gsize         n_block_bytes,
0126                            gsize         alignment) G_GNUC_WARN_UNUSED_RESULT G_GNUC_ALLOC_SIZE2(1,2);
0127 GLIB_AVAILABLE_IN_2_72
0128 void     g_aligned_free   (gpointer      mem);
0129 GLIB_AVAILABLE_IN_2_76
0130 void     g_aligned_free_sized (gpointer  mem,
0131                                size_t    alignment,
0132                                size_t    size);
0133 
0134 #if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
0135 #define g_clear_pointer(pp, destroy)                     \
0136   G_STMT_START                                           \
0137   {                                                      \
0138     G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
0139     glib_typeof ((pp)) _pp = (pp);                       \
0140     glib_typeof (*(pp)) _ptr = *_pp;                     \
0141     *_pp = NULL;                                         \
0142     if (_ptr)                                            \
0143       (destroy) (_ptr);                                  \
0144   }                                                      \
0145   G_STMT_END                                             \
0146   GLIB_AVAILABLE_MACRO_IN_2_34
0147 #else /* __GNUC__ */
0148 #define g_clear_pointer(pp, destroy) \
0149   G_STMT_START {                                                               \
0150     G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));                       \
0151     /* Only one access, please; work around type aliasing */                   \
0152     union { char *in; gpointer *out; } _pp;                                    \
0153     gpointer _p;                                                               \
0154     /* This assignment is needed to avoid a gcc warning */                     \
0155     GDestroyNotify _destroy = (GDestroyNotify) (destroy);                      \
0156                                                                                \
0157     _pp.in = (char *) (pp);                                                    \
0158     _p = *_pp.out;                                                             \
0159     if (_p)                                        \
0160       {                                        \
0161         *_pp.out = NULL;                                                       \
0162         _destroy (_p);                                                         \
0163       }                                                                        \
0164   } G_STMT_END                                                                 \
0165   GLIB_AVAILABLE_MACRO_IN_2_34
0166 #endif /* __GNUC__ */
0167 
0168 
0169 #if G_GNUC_CHECK_VERSION (4, 1) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_78 && defined(G_HAVE_FREE_SIZED)
0170 
0171 #define g_free(mem)                                                            \
0172   (__builtin_object_size ((mem), 0) != ((size_t) - 1)) ?                       \
0173     g_free_sized (mem, __builtin_object_size ((mem), 0)) : (g_free) (mem)
0174 
0175 #endif /* G_GNUC_CHECK_VERSION (4, 1) && && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_78 && defined(G_HAVE_FREE_SIZED) */
0176 
0177 /**
0178  * g_steal_pointer:
0179  * @pp: (not nullable): a pointer to a pointer
0180  *
0181  * Sets @pp to %NULL, returning the value that was there before.
0182  *
0183  * Conceptually, this transfers the ownership of the pointer from the
0184  * referenced variable to the "caller" of the macro (ie: "steals" the
0185  * reference).
0186  *
0187  * The return value will be properly typed, according to the type of
0188  * @pp.
0189  *
0190  * This can be very useful when combined with g_autoptr() to prevent the
0191  * return value of a function from being automatically freed.  Consider
0192  * the following example (which only works on GCC and clang):
0193  *
0194  * |[
0195  * GObject *
0196  * create_object (void)
0197  * {
0198  *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
0199  *
0200  *   if (early_error_case)
0201  *     return NULL;
0202  *
0203  *   return g_steal_pointer (&obj);
0204  * }
0205  * ]|
0206  *
0207  * It can also be used in similar ways for 'out' parameters and is
0208  * particularly useful for dealing with optional out parameters:
0209  *
0210  * |[
0211  * gboolean
0212  * get_object (GObject **obj_out)
0213  * {
0214  *   g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
0215  *
0216  *   if (early_error_case)
0217  *     return FALSE;
0218  *
0219  *   if (obj_out)
0220  *     *obj_out = g_steal_pointer (&obj);
0221  *
0222  *   return TRUE;
0223  * }
0224  * ]|
0225  *
0226  * In the above example, the object will be automatically freed in the
0227  * early error case and also in the case that %NULL was given for
0228  * @obj_out.
0229  *
0230  * Since: 2.44
0231  */
0232 GLIB_AVAILABLE_STATIC_INLINE_IN_2_44
0233 static inline gpointer
0234 g_steal_pointer (gpointer pp)
0235 {
0236   gpointer *ptr = (gpointer *) pp;
0237   gpointer ref;
0238 
0239   ref = *ptr;
0240   *ptr = NULL;
0241 
0242   return ref;
0243 }
0244 
0245 /* type safety */
0246 #if defined(glib_typeof) && GLIB_VERSION_MAX_ALLOWED >= GLIB_VERSION_2_58
0247 #define g_steal_pointer(pp) ((glib_typeof (*pp)) (g_steal_pointer) (pp))
0248 #else  /* __GNUC__ */
0249 /* This version does not depend on gcc extensions, but gcc does not warn
0250  * about incompatible-pointer-types: */
0251 #define g_steal_pointer(pp) \
0252   (0 ? (*(pp)) : (g_steal_pointer) (pp))
0253 #endif /* __GNUC__ */
0254 
0255 /* Optimise: avoid the call to the (slower) _n function if we can
0256  * determine at compile-time that no overflow happens.
0257  */
0258 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
0259 #  define _G_NEW(struct_type, n_structs, func) \
0260     (struct_type *) (G_GNUC_EXTENSION ({            \
0261       gsize __n = (gsize) (n_structs);          \
0262       gsize __s = sizeof (struct_type);         \
0263       gpointer __p;                     \
0264       if (__s == 1)                     \
0265         __p = g_##func (__n);               \
0266       else if (__builtin_constant_p (__n) &&        \
0267                (__s == 0 || __n <= G_MAXSIZE / __s))    \
0268         __p = g_##func (__n * __s);             \
0269       else                          \
0270         __p = g_##func##_n (__n, __s);          \
0271       __p;                          \
0272     }))
0273 #  define _G_RENEW(struct_type, mem, n_structs, func) \
0274     (struct_type *) (G_GNUC_EXTENSION ({            \
0275       gsize __n = (gsize) (n_structs);          \
0276       gsize __s = sizeof (struct_type);         \
0277       gpointer __p = (gpointer) (mem);          \
0278       if (__s == 1)                     \
0279         __p = g_##func (__p, __n);              \
0280       else if (__builtin_constant_p (__n) &&        \
0281                (__s == 0 || __n <= G_MAXSIZE / __s))    \
0282         __p = g_##func (__p, __n * __s);            \
0283       else                          \
0284         __p = g_##func##_n (__p, __n, __s);         \
0285       __p;                          \
0286     }))
0287 
0288 #else
0289 
0290 /* Unoptimised version: always call the _n() function. */
0291 
0292 #define _G_NEW(struct_type, n_structs, func) \
0293         ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
0294 #define _G_RENEW(struct_type, mem, n_structs, func) \
0295         ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
0296 
0297 #endif
0298 
0299 /**
0300  * g_new:
0301  * @struct_type: the type of the elements to allocate
0302  * @n_structs: the number of elements to allocate
0303  * 
0304  * Allocates @n_structs elements of type @struct_type.
0305  * The returned pointer is cast to a pointer to the given type.
0306  * If @n_structs is 0 it returns %NULL.
0307  * Care is taken to avoid overflow when calculating the size of the allocated block.
0308  * 
0309  * Since the returned pointer is already casted to the right type,
0310  * it is normally unnecessary to cast it explicitly, and doing
0311  * so might hide memory allocation errors.
0312  * 
0313  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
0314  */
0315 #define g_new(struct_type, n_structs)           _G_NEW (struct_type, n_structs, malloc)
0316 /**
0317  * g_new0:
0318  * @struct_type: the type of the elements to allocate.
0319  * @n_structs: the number of elements to allocate.
0320  * 
0321  * Allocates @n_structs elements of type @struct_type, initialized to 0's.
0322  * The returned pointer is cast to a pointer to the given type.
0323  * If @n_structs is 0 it returns %NULL.
0324  * Care is taken to avoid overflow when calculating the size of the allocated block.
0325  * 
0326  * Since the returned pointer is already casted to the right type,
0327  * it is normally unnecessary to cast it explicitly, and doing
0328  * so might hide memory allocation errors.
0329  * 
0330  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
0331  */
0332 #define g_new0(struct_type, n_structs)          _G_NEW (struct_type, n_structs, malloc0)
0333 /**
0334  * g_renew:
0335  * @struct_type: the type of the elements to allocate
0336  * @mem: the currently allocated memory
0337  * @n_structs: the number of elements to allocate
0338  * 
0339  * Reallocates the memory pointed to by @mem, so that it now has space for
0340  * @n_structs elements of type @struct_type. It returns the new address of
0341  * the memory, which may have been moved.
0342  * Care is taken to avoid overflow when calculating the size of the allocated block.
0343  * 
0344  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
0345  */
0346 #define g_renew(struct_type, mem, n_structs)        _G_RENEW (struct_type, mem, n_structs, realloc)
0347 /**
0348  * g_try_new:
0349  * @struct_type: the type of the elements to allocate
0350  * @n_structs: the number of elements to allocate
0351  * 
0352  * Attempts to allocate @n_structs elements of type @struct_type, and returns
0353  * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
0354  * The returned pointer is cast to a pointer to the given type.
0355  * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
0356  * 
0357  * Since: 2.8
0358  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
0359  */
0360 #define g_try_new(struct_type, n_structs)       _G_NEW (struct_type, n_structs, try_malloc)
0361 /**
0362  * g_try_new0:
0363  * @struct_type: the type of the elements to allocate
0364  * @n_structs: the number of elements to allocate
0365  * 
0366  * Attempts to allocate @n_structs elements of type @struct_type, initialized
0367  * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
0368  * the program on failure.
0369  * The returned pointer is cast to a pointer to the given type.
0370  * The function returns %NULL when @n_structs is 0 or if an overflow occurs.
0371  * 
0372  * Since: 2.8
0373  * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
0374  */
0375 #define g_try_new0(struct_type, n_structs)      _G_NEW (struct_type, n_structs, try_malloc0)
0376 /**
0377  * g_try_renew:
0378  * @struct_type: the type of the elements to allocate
0379  * @mem: the currently allocated memory
0380  * @n_structs: the number of elements to allocate
0381  * 
0382  * Attempts to reallocate the memory pointed to by @mem, so that it now has
0383  * space for @n_structs elements of type @struct_type, and returns %NULL on
0384  * failure. Contrast with g_renew(), which aborts the program on failure.
0385  * It returns the new address of the memory, which may have been moved.
0386  * The function returns %NULL if an overflow occurs.
0387  * 
0388  * Since: 2.8
0389  * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
0390  */
0391 #define g_try_renew(struct_type, mem, n_structs)    _G_RENEW (struct_type, mem, n_structs, try_realloc)
0392 
0393 
0394 /* Memory allocation virtualization for debugging purposes
0395  * g_mem_set_vtable() has to be the very first GLib function called
0396  * if being used
0397  */
0398 struct _GMemVTable {
0399   gpointer (*malloc)      (gsize    n_bytes);
0400   gpointer (*realloc)     (gpointer mem,
0401                gsize    n_bytes);
0402   void     (*free)        (gpointer mem);
0403   /* optional; set to NULL if not used ! */
0404   gpointer (*calloc)      (gsize    n_blocks,
0405                gsize    n_block_bytes);
0406   gpointer (*try_malloc)  (gsize    n_bytes);
0407   gpointer (*try_realloc) (gpointer mem,
0408                gsize    n_bytes);
0409 };
0410 GLIB_DEPRECATED_IN_2_46
0411 void     g_mem_set_vtable (GMemVTable   *vtable);
0412 GLIB_DEPRECATED_IN_2_46
0413 gboolean g_mem_is_system_malloc (void);
0414 
0415 GLIB_VAR gboolean g_mem_gc_friendly;
0416 
0417 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
0418  */
0419 GLIB_VAR GMemVTable *glib_mem_profiler_table;
0420 GLIB_DEPRECATED_IN_2_46
0421 void    g_mem_profile   (void);
0422 
0423 G_END_DECLS
0424 
0425 #endif /* __G_MEM_H__ */