File indexing completed on 2025-12-22 10:14:06
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
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
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 typedef struct _GMemVTable GMemVTable;
0055
0056
0057 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
0058
0059
0060
0061
0062
0063
0064 # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
0065 #else
0066 # define G_MEM_ALIGN GLIB_SIZEOF_LONG
0067 #endif
0068
0069
0070
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
0148 #define g_clear_pointer(pp, destroy) \
0149 G_STMT_START { \
0150 G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
0151 \
0152 union { char *in; gpointer *out; } _pp; \
0153 gpointer _p; \
0154 \
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
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
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205
0206
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
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
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
0249
0250
0251 #define g_steal_pointer(pp) \
0252 (0 ? (*(pp)) : (g_steal_pointer) (pp))
0253 #endif
0254
0255
0256
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
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
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311
0312
0313
0314
0315 #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332 #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
0333
0334
0335
0336
0337
0338
0339
0340
0341
0342
0343
0344
0345
0346 #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360 #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375 #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391 #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
0392
0393
0394
0395
0396
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
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
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