Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
gmem.h File Reference
#include <glib/gutils.h>
#include <glib/glib-typeof.h>

Go to the source code of this file.

Data Structures

struct  _GMemVTable
 

Macros

#define G_MEM_ALIGN   GLIB_SIZEOF_LONG
 
#define g_clear_pointer(pp, destroy)
 
#define g_steal_pointer(pp)    (0 ? (*(pp)) : (g_steal_pointer) (pp))
 
#define _G_NEW(struct_type, n_structs, func)    ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
 
#define _G_RENEW(struct_type, mem, n_structs, func)    ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
 
#define g_new(struct_type, n_structs)   _G_NEW (struct_type, n_structs, malloc)
 
#define g_new0(struct_type, n_structs)   _G_NEW (struct_type, n_structs, malloc0)
 
#define g_renew(struct_type, mem, n_structs)   _G_RENEW (struct_type, mem, n_structs, realloc)
 
#define g_try_new(struct_type, n_structs)   _G_NEW (struct_type, n_structs, try_malloc)
 
#define g_try_new0(struct_type, n_structs)   _G_NEW (struct_type, n_structs, try_malloc0)
 
#define g_try_renew(struct_type, mem, n_structs)   _G_RENEW (struct_type, mem, n_structs, try_realloc)
 

Typedefs

typedef typedefG_BEGIN_DECLS struct _GMemVTable GMemVTable
 

Functions

GLIB_AVAILABLE_IN_ALL void g_free (gpointer mem)
 
GLIB_AVAILABLE_IN_2_76 void g_free_sized (gpointer mem, size_t size)
 
GLIB_AVAILABLE_IN_2_34 void g_clear_pointer (gpointer *pp, GDestroyNotify destroy)
 
GLIB_AVAILABLE_IN_ALL gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1)
 
GLIB_AVAILABLE_IN_ALL gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1)
 
GLIB_AVAILABLE_IN_ALL gpointer g_realloc (gpointer mem, gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT
 
GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1)
 
GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1)
 
GLIB_AVAILABLE_IN_ALL gpointer g_try_realloc (gpointer mem, gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT
 
GLIB_AVAILABLE_IN_ALL gpointer g_malloc_n (gsize n_blocks, gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1
 
GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer g_malloc0_n (gsize n_blocks, gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1
 
GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer g_realloc_n (gpointer mem, gsize n_blocks, gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT
 
GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc_n (gsize n_blocks, gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1
 
GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc0_n (gsize n_blocks, gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1
 
GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer g_try_realloc_n (gpointer mem, gsize n_blocks, gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT
 
GLIB_AVAILABLE_IN_2_72 gpointer g_aligned_alloc (gsize n_blocks, gsize n_block_bytes, gsize alignment) G_GNUC_WARN_UNUSED_RESULT G_GNUC_ALLOC_SIZE2(1
 
GLIB_AVAILABLE_IN_2_72 gpointer GLIB_AVAILABLE_IN_2_72 gpointer g_aligned_alloc0 (gsize n_blocks, gsize n_block_bytes, gsize alignment) G_GNUC_WARN_UNUSED_RESULT G_GNUC_ALLOC_SIZE2(1
 
GLIB_AVAILABLE_IN_2_72 gpointer GLIB_AVAILABLE_IN_2_72 gpointer GLIB_AVAILABLE_IN_2_72 void g_aligned_free (gpointer mem)
 
GLIB_AVAILABLE_IN_2_76 void g_aligned_free_sized (gpointer mem, size_t alignment, size_t size)
 
static GLIB_AVAILABLE_STATIC_INLINE_IN_2_44 gpointer g_steal_pointer (gpointer pp)
 
GLIB_DEPRECATED_IN_2_46 void g_mem_set_vtable (GMemVTable *vtable)
 
GLIB_DEPRECATED_IN_2_46 gboolean g_mem_is_system_malloc (void)
 
GLIB_DEPRECATED_IN_2_46 void g_mem_profile (void)
 

Variables

GLIB_VAR gboolean g_mem_gc_friendly
 
GLIB_VAR GMemVTableglib_mem_profiler_table
 

Macro Definition Documentation

◆ _G_NEW

#define _G_NEW ( struct_type,
n_structs,
func )    ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))

Definition at line 292 of file gmem.h.

292#define _G_NEW(struct_type, n_structs, func) \
293 ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))

◆ _G_RENEW

#define _G_RENEW ( struct_type,
mem,
n_structs,
func )    ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))

Definition at line 294 of file gmem.h.

294#define _G_RENEW(struct_type, mem, n_structs, func) \
295 ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))

◆ g_clear_pointer

#define g_clear_pointer ( pp,
destroy )
Value:
G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
/* Only one access, please; work around type aliasing */ \
union { char *in; gpointer *out; } _pp; \
gpointer _p; \
/* This assignment is needed to avoid a gcc warning */ \
GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
\
_pp.in = (char *) (pp); \
_p = *_pp.out; \
if (_p) \
{ \
*_pp.out = NULL; \
_destroy (_p); \
} \
} G_STMT_END \
GLIB_AVAILABLE_MACRO_IN_2_34
#define NULL
Definition gmacros.h:924
#define G_STMT_START
Definition gmacros.h:989
void * gpointer
Definition gtypes.h:109
void(* GDestroyNotify)(gpointer data)
Definition gtypes.h:140

Definition at line 148 of file gmem.h.

148#define g_clear_pointer(pp, destroy) \
149 G_STMT_START { \
150 G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer)); \
151 /* Only one access, please; work around type aliasing */ \
152 union { char *in; gpointer *out; } _pp; \
153 gpointer _p; \
154 /* This assignment is needed to avoid a gcc warning */ \
155 GDestroyNotify _destroy = (GDestroyNotify) (destroy); \
156 \
157 _pp.in = (char *) (pp); \
158 _p = *_pp.out; \
159 if (_p) \
160 { \
161 *_pp.out = NULL; \
162 _destroy (_p); \
163 } \
164 } G_STMT_END \
165 GLIB_AVAILABLE_MACRO_IN_2_34

◆ G_MEM_ALIGN

#define G_MEM_ALIGN   GLIB_SIZEOF_LONG

Definition at line 66 of file gmem.h.

◆ g_new

#define g_new ( struct_type,
n_structs )   _G_NEW (struct_type, n_structs, malloc)

g_new: @struct_type: the type of the elements to allocate @n_structs: the number of elements to allocate

Allocates @n_structs elements of type @struct_type. The returned pointer is cast to a pointer to the given type. If @n_structs is 0 it returns NULL. Care is taken to avoid overflow when calculating the size of the allocated block.

Since the returned pointer is already casted to the right type, it is normally unnecessary to cast it explicitly, and doing so might hide memory allocation errors.

Returns: a pointer to the allocated memory, cast to a pointer to @struct_type

Definition at line 315 of file gmem.h.

Referenced by g_object_notify_queue_thaw().

◆ g_new0

#define g_new0 ( struct_type,
n_structs )   _G_NEW (struct_type, n_structs, malloc0)

g_new0: @struct_type: the type of the elements to allocate. @n_structs: the number of elements to allocate.

Allocates @n_structs elements of type @struct_type, initialized to 0's. The returned pointer is cast to a pointer to the given type. If @n_structs is 0 it returns NULL. Care is taken to avoid overflow when calculating the size of the allocated block.

Since the returned pointer is already casted to the right type, it is normally unnecessary to cast it explicitly, and doing so might hide memory allocation errors.

Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.

Definition at line 332 of file gmem.h.

Referenced by activate().

◆ g_renew

#define g_renew ( struct_type,
mem,
n_structs )   _G_RENEW (struct_type, mem, n_structs, realloc)

g_renew: @struct_type: the type of the elements to allocate @mem: the currently allocated memory @n_structs: the number of elements to allocate

Reallocates the memory pointed to by @mem, so that it now has space for @n_structs elements of type @struct_type. It returns the new address of the memory, which may have been moved. Care is taken to avoid overflow when calculating the size of the allocated block.

Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type

Definition at line 346 of file gmem.h.

◆ g_steal_pointer

#define g_steal_pointer ( pp)     (0 ? (*(pp)) : (g_steal_pointer) (pp))

Definition at line 251 of file gmem.h.

251#define g_steal_pointer(pp) \
252 (0 ? (*(pp)) : (g_steal_pointer) (pp))

◆ g_try_new

#define g_try_new ( struct_type,
n_structs )   _G_NEW (struct_type, n_structs, try_malloc)

g_try_new: @struct_type: the type of the elements to allocate @n_structs: the number of elements to allocate

Attempts to allocate @n_structs elements of type @struct_type, and returns NULL on failure. Contrast with g_new(), which aborts the program on failure. The returned pointer is cast to a pointer to the given type. The function returns NULL when @n_structs is 0 of if an overflow occurs.

Since: 2.8 Returns: a pointer to the allocated memory, cast to a pointer to @struct_type

Definition at line 360 of file gmem.h.

◆ g_try_new0

#define g_try_new0 ( struct_type,
n_structs )   _G_NEW (struct_type, n_structs, try_malloc0)

g_try_new0: @struct_type: the type of the elements to allocate @n_structs: the number of elements to allocate

Attempts to allocate @n_structs elements of type @struct_type, initialized to 0's, and returns NULL on failure. Contrast with g_new0(), which aborts the program on failure. The returned pointer is cast to a pointer to the given type. The function returns NULL when @n_structs is 0 or if an overflow occurs.

Since: 2.8 Returns: a pointer to the allocated memory, cast to a pointer to @struct_type

Definition at line 375 of file gmem.h.

◆ g_try_renew

#define g_try_renew ( struct_type,
mem,
n_structs )   _G_RENEW (struct_type, mem, n_structs, try_realloc)

g_try_renew: @struct_type: the type of the elements to allocate @mem: the currently allocated memory @n_structs: the number of elements to allocate

Attempts to reallocate the memory pointed to by @mem, so that it now has space for @n_structs elements of type @struct_type, and returns NULL on failure. Contrast with g_renew(), which aborts the program on failure. It returns the new address of the memory, which may have been moved. The function returns NULL if an overflow occurs.

Since: 2.8 Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type

Definition at line 391 of file gmem.h.

Typedef Documentation

◆ GMemVTable

typedef typedefG_BEGIN_DECLS struct _GMemVTable GMemVTable

GMemVTable: @malloc: function to use for allocating memory. @realloc: function to use for reallocating memory. @free: function to use to free memory. @calloc: function to use for allocating zero-filled memory. @try_malloc: function to use for allocating memory without a default error handler. @try_realloc: function to use for reallocating memory without a default error handler.

A set of functions used to perform memory allocation. The same GMemVTable must be used for all allocations in the same program; a call to g_mem_set_vtable(), if it exists, should be prior to any use of GLib.

This functions related to this has been deprecated in 2.46, and no longer work.

Definition at line 54 of file gmem.h.

Function Documentation

◆ g_aligned_alloc()

GLIB_AVAILABLE_IN_2_72 gpointer g_aligned_alloc ( gsize n_blocks,
gsize n_block_bytes,
gsize alignment )

◆ g_aligned_alloc0()

GLIB_AVAILABLE_IN_2_72 gpointer GLIB_AVAILABLE_IN_2_72 gpointer g_aligned_alloc0 ( gsize n_blocks,
gsize n_block_bytes,
gsize alignment )

◆ g_aligned_free()

◆ g_aligned_free_sized()

GLIB_AVAILABLE_IN_2_76 void g_aligned_free_sized ( gpointer mem,
size_t alignment,
size_t size )

◆ g_clear_pointer()

GLIB_AVAILABLE_IN_2_34 void g_clear_pointer ( gpointer * pp,
GDestroyNotify destroy )

◆ g_free()

◆ g_free_sized()

GLIB_AVAILABLE_IN_2_76 void g_free_sized ( gpointer mem,
size_t size )

◆ g_malloc()

GLIB_AVAILABLE_IN_ALL gpointer g_malloc ( gsize n_bytes)

Referenced by g_strjoin().

◆ g_malloc0()

GLIB_AVAILABLE_IN_ALL gpointer g_malloc0 ( gsize n_bytes)

◆ g_malloc0_n()

GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer g_malloc0_n ( gsize n_blocks,
gsize n_block_bytes )

◆ g_malloc_n()

GLIB_AVAILABLE_IN_ALL gpointer g_malloc_n ( gsize n_blocks,
gsize n_block_bytes )

◆ g_mem_is_system_malloc()

GLIB_DEPRECATED_IN_2_46 gboolean g_mem_is_system_malloc ( void )

◆ g_mem_profile()

GLIB_DEPRECATED_IN_2_46 void g_mem_profile ( void )

◆ g_mem_set_vtable()

GLIB_DEPRECATED_IN_2_46 void g_mem_set_vtable ( GMemVTable * vtable)

◆ g_realloc()

GLIB_AVAILABLE_IN_ALL gpointer g_realloc ( gpointer mem,
gsize n_bytes )

◆ g_realloc_n()

◆ g_steal_pointer()

static GLIB_AVAILABLE_STATIC_INLINE_IN_2_44 gpointer g_steal_pointer ( gpointer pp)
inlinestatic

g_steal_pointer: @pp: (not nullable): a pointer to a pointer

Sets @pp to NULL, returning the value that was there before.

Conceptually, this transfers the ownership of the pointer from the referenced variable to the "caller" of the macro (ie: "steals" the reference).

The return value will be properly typed, according to the type of @pp.

This can be very useful when combined with g_autoptr() to prevent the return value of a function from being automatically freed. Consider the following example (which only works on GCC and clang):

|[ GObject * create_object (void) { g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);

if (early_error_case) return NULL;

return g_steal_pointer (&obj); } ]|

It can also be used in similar ways for 'out' parameters and is particularly useful for dealing with optional out parameters:

|[ gboolean get_object (GObject **obj_out) { g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);

if (early_error_case) return FALSE;

if (obj_out) *obj_out = g_steal_pointer (&obj);

return TRUE; } ]|

In the above example, the object will be automatically freed in the early error case and also in the case that NULL was given for @obj_out.

Since: 2.44

Definition at line 234 of file gmem.h.

235{
236 gpointer *ptr = (gpointer *) pp;
237 gpointer ref;
238
239 ref = *ptr;
240 *ptr = NULL;
241
242 return ref;
243}

References NULL.

◆ g_try_malloc()

GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc ( gsize n_bytes)

◆ g_try_malloc0()

GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc0 ( gsize n_bytes)

◆ g_try_malloc0_n()

GLIB_AVAILABLE_IN_ALL gpointer GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc0_n ( gsize n_blocks,
gsize n_block_bytes )

◆ g_try_malloc_n()

GLIB_AVAILABLE_IN_ALL gpointer g_try_malloc_n ( gsize n_blocks,
gsize n_block_bytes )

◆ g_try_realloc()

GLIB_AVAILABLE_IN_ALL gpointer g_try_realloc ( gpointer mem,
gsize n_bytes )

◆ g_try_realloc_n()

Variable Documentation

◆ g_mem_gc_friendly

GLIB_VAR gboolean g_mem_gc_friendly

Definition at line 415 of file gmem.h.

◆ glib_mem_profiler_table

GLIB_VAR GMemVTable* glib_mem_profiler_table

Definition at line 419 of file gmem.h.