Blame docs/reference/glib/html/glib-Memory-Allocation.html

Packit ae235b
Packit ae235b
<html>
Packit ae235b
<head>
Packit ae235b
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
Packit ae235b
<title>Memory Allocation: GLib Reference Manual</title>
Packit ae235b
<meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
Packit ae235b
<link rel="home" href="index.html" title="GLib Reference Manual">
Packit ae235b
<link rel="up" href="glib-core.html" title="GLib Core Application Support">
Packit ae235b
<link rel="prev" href="glib-Dynamic-Loading-of-Modules.html" title="Dynamic Loading of Modules">
Packit ae235b
<link rel="next" href="glib-Memory-Slices.html" title="Memory Slices">
Packit ae235b
<meta name="generator" content="GTK-Doc V1.27 (XML mode)">
Packit ae235b
<link rel="stylesheet" href="style.css" type="text/css">
Packit ae235b
</head>
Packit ae235b
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
Packit ae235b
Packit ae235b
Packit ae235b
Top  | 
Packit ae235b
                  Description
Packit ae235b
Packit ae235b
Home
Packit ae235b
Up
Packit ae235b
Prev
Packit ae235b
Next
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Memory Allocation

Packit ae235b

Memory Allocation — general memory-handling

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Functions

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_new()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_new0()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_renew()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_try_new()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_try_new0()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_try_renew()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_malloc ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_malloc0 ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_realloc ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_try_malloc ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_try_malloc0 ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_try_realloc ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_malloc_n ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_malloc0_n ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_realloc_n ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_try_malloc_n ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_try_malloc0_n ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_try_realloc_n ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
void
Packit ae235b
Packit ae235b
Packit ae235b
g_free ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
void
Packit ae235b
Packit ae235b
Packit ae235b
g_clear_pointer ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_steal_pointer ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_alloca()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_newa()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
#define
Packit ae235b
Packit ae235b
g_memmove()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gpointer
Packit ae235b
Packit ae235b
Packit ae235b
g_memdup ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
void
Packit ae235b
Packit ae235b
Packit ae235b
g_mem_set_vtable ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
gboolean
Packit ae235b
Packit ae235b
Packit ae235b
g_mem_is_system_malloc ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
void
Packit ae235b
Packit ae235b
Packit ae235b
g_mem_profile ()
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Types and Values

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
extern gboolean 
Packit ae235b
g_mem_gc_friendly
Packit ae235b
Packit ae235b
Packit ae235b
struct
Packit ae235b
GMemVTable
Packit ae235b
Packit ae235b
Packit ae235b
extern GMemVTable	*
Packit ae235b
glib_mem_profiler_table
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Includes

Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Description

Packit ae235b

These functions provide support for allocating and freeing memory.

Packit ae235b

If any call to allocate memory using functions g_new(), g_new0(), g_renew(),

Packit ae235b
g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n()
Packit ae235b
fails, the application is terminated. This also means that there is no
Packit ae235b
need to check if the call succeeded. On the other hand, g_try_...() family
Packit ae235b
of functions returns NULL on failure that can be used as a check
Packit ae235b
for unsuccessful memory allocation. The application is not terminated
Packit ae235b
in this case.

Packit ae235b

It's important to match g_malloc() (and wrappers such as g_new()) with

Packit ae235b
g_free(), g_slice_alloc() (and wrappers such as g_slice_new()) with
Packit ae235b
g_slice_free(), plain malloc() with free(), and (if you're using C++)
Packit ae235b
new with delete and new[] with delete[]. Otherwise bad things can happen,
Packit ae235b
since these allocators may use different memory pools (and new/delete call
Packit ae235b
constructors and destructors).

Packit ae235b
Packit ae235b
Packit ae235b

Functions

Packit ae235b
Packit ae235b

g_new()

Packit ae235b
#define             g_new(struct_type, n_structs)
Packit ae235b

Allocates n_structs

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

Packit ae235b

Since the returned pointer is already casted to the right type,

Packit ae235b
it is normally unnecessary to cast it explicitly, and doing
Packit ae235b
so might hide memory allocation errors.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

struct_type

Packit ae235b

the type of the elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_structs

Packit ae235b

the number of elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory, cast to a pointer to struct_type

Packit ae235b

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_new0()

Packit ae235b
#define             g_new0(struct_type, n_structs)
Packit ae235b

Allocates n_structs

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

Packit ae235b

Since the returned pointer is already casted to the right type,

Packit ae235b
it is normally unnecessary to cast it explicitly, and doing
Packit ae235b
so might hide memory allocation errors.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

struct_type

Packit ae235b

the type of the elements to allocate.

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_structs

Packit ae235b

the number of elements to allocate.

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory, cast to a pointer to struct_type

Packit ae235b
.

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_renew()

Packit ae235b
#define             g_renew(struct_type, mem, n_structs)
Packit ae235b

Reallocates the memory pointed to by mem

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

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

struct_type

Packit ae235b

the type of the elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

mem

Packit ae235b

the currently allocated memory

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_structs

Packit ae235b

the number of elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the new allocated memory, cast to a pointer to struct_type

Packit ae235b

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_new()

Packit ae235b
#define             g_try_new(struct_type, n_structs)
Packit ae235b

Attempts to allocate n_structs

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

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

struct_type

Packit ae235b

the type of the elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_structs

Packit ae235b

the number of elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory, cast to a pointer to struct_type

Packit ae235b

Packit ae235b
Packit ae235b

Since: 2.8

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_new0()

Packit ae235b
#define             g_try_new0(struct_type, n_structs)
Packit ae235b

Attempts to allocate n_structs

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

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

struct_type

Packit ae235b

the type of the elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_structs

Packit ae235b

the number of elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory, cast to a pointer to struct_type

Packit ae235b

Packit ae235b
Packit ae235b

Since: 2.8

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_renew()

Packit ae235b
#define             g_try_renew(struct_type, mem, n_structs)
Packit ae235b

Attempts to reallocate the memory pointed to by mem

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

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

struct_type

Packit ae235b

the type of the elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

mem

Packit ae235b

the currently allocated memory

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_structs

Packit ae235b

the number of elements to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the new allocated memory, cast to a pointer to struct_type

Packit ae235b

Packit ae235b
Packit ae235b

Since: 2.8

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_malloc ()

Packit ae235b
gpointer
Packit ae235b
g_malloc (gsize n_bytes);
Packit ae235b

Allocates n_bytes

Packit ae235b
 bytes of memory.
Packit ae235b
If n_bytes
Packit ae235b
 is 0 it returns NULL.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_bytes

Packit ae235b

the number of bytes to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_malloc0 ()

Packit ae235b
gpointer
Packit ae235b
g_malloc0 (gsize n_bytes);
Packit ae235b

Allocates n_bytes

Packit ae235b
 bytes of memory, initialized to 0's.
Packit ae235b
If n_bytes
Packit ae235b
 is 0 it returns NULL.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_bytes

Packit ae235b

the number of bytes to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_realloc ()

Packit ae235b
gpointer
Packit ae235b
g_realloc (gpointer mem,
Packit ae235b
           gsize n_bytes);
Packit ae235b

Reallocates the memory pointed to by mem

Packit ae235b
, so that it now has space for
Packit ae235b
n_bytes
Packit ae235b
 bytes of memory. It returns the new address of the memory, which may
Packit ae235b
have been moved. mem
Packit ae235b
 may be NULL, in which case it's considered to
Packit ae235b
have zero-length. n_bytes
Packit ae235b
 may be 0, in which case NULL will be returned
Packit ae235b
and mem
Packit ae235b
 will be freed unless it is NULL.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

mem

Packit ae235b

the memory to reallocate.

Packit ae235b
[nullable]
Packit ae235b
Packit ae235b
Packit ae235b

n_bytes

Packit ae235b

new size of the memory in bytes

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the new address of the allocated memory

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_malloc ()

Packit ae235b
gpointer
Packit ae235b
g_try_malloc (gsize n_bytes);
Packit ae235b

Attempts to allocate n_bytes

Packit ae235b
, and returns NULL on failure.
Packit ae235b
Contrast with g_malloc(), which aborts the program on failure.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_bytes

Packit ae235b

number of bytes to allocate.

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the allocated memory, or NULL.

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_malloc0 ()

Packit ae235b
gpointer
Packit ae235b
g_try_malloc0 (gsize n_bytes);
Packit ae235b

Attempts to allocate n_bytes

Packit ae235b
, initialized to 0's, and returns NULL on
Packit ae235b
failure. Contrast with g_malloc0(), which aborts the program on failure.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_bytes

Packit ae235b

number of bytes to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the allocated memory, or NULL

Packit ae235b
Packit ae235b

Since: 2.8

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_realloc ()

Packit ae235b
gpointer
Packit ae235b
g_try_realloc (gpointer mem,
Packit ae235b
               gsize n_bytes);
Packit ae235b

Attempts to realloc mem

Packit ae235b
 to a new size, n_bytes
Packit ae235b
, and returns NULL
Packit ae235b
on failure. Contrast with g_realloc(), which aborts the program
Packit ae235b
on failure.

Packit ae235b

If mem

Packit ae235b
 is NULL, behaves the same as g_try_malloc().

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

mem

Packit ae235b

previously-allocated memory, or NULL.

Packit ae235b
[nullable]
Packit ae235b
Packit ae235b
Packit ae235b

n_bytes

Packit ae235b

number of bytes to allocate.

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the allocated memory, or NULL.

Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_malloc_n ()

Packit ae235b
gpointer
Packit ae235b
g_malloc_n (gsize n_blocks,
Packit ae235b
            gsize n_block_bytes);
Packit ae235b

This function is similar to g_malloc(), allocating (n_blocks

Packit ae235b
 * n_block_bytes
Packit ae235b
) bytes,
Packit ae235b
but care is taken to detect possible overflow during multiplication.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_blocks

Packit ae235b

the number of blocks to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_block_bytes

Packit ae235b

the size of each block in bytes

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory

Packit ae235b
Packit ae235b

Since: 2.24

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_malloc0_n ()

Packit ae235b
gpointer
Packit ae235b
g_malloc0_n (gsize n_blocks,
Packit ae235b
             gsize n_block_bytes);
Packit ae235b

This function is similar to g_malloc0(), allocating (n_blocks

Packit ae235b
 * n_block_bytes
Packit ae235b
) bytes,
Packit ae235b
but care is taken to detect possible overflow during multiplication.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_blocks

Packit ae235b

the number of blocks to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_block_bytes

Packit ae235b

the size of each block in bytes

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

a pointer to the allocated memory

Packit ae235b
Packit ae235b

Since: 2.24

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_realloc_n ()

Packit ae235b
gpointer
Packit ae235b
g_realloc_n (gpointer mem,
Packit ae235b
             gsize n_blocks,
Packit ae235b
             gsize n_block_bytes);
Packit ae235b

This function is similar to g_realloc(), allocating (n_blocks

Packit ae235b
 * n_block_bytes
Packit ae235b
) bytes,
Packit ae235b
but care is taken to detect possible overflow during multiplication.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

mem

Packit ae235b

the memory to reallocate.

Packit ae235b
[nullable]
Packit ae235b
Packit ae235b
Packit ae235b

n_blocks

Packit ae235b

the number of blocks to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_block_bytes

Packit ae235b

the size of each block in bytes

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the new address of the allocated memory

Packit ae235b
Packit ae235b

Since: 2.24

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_malloc_n ()

Packit ae235b
gpointer
Packit ae235b
g_try_malloc_n (gsize n_blocks,
Packit ae235b
                gsize n_block_bytes);
Packit ae235b

This function is similar to g_try_malloc(), allocating (n_blocks

Packit ae235b
 * n_block_bytes
Packit ae235b
) bytes,
Packit ae235b
but care is taken to detect possible overflow during multiplication.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_blocks

Packit ae235b

the number of blocks to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_block_bytes

Packit ae235b

the size of each block in bytes

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the allocated memory, or NULL.

Packit ae235b
Packit ae235b

Since: 2.24

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_malloc0_n ()

Packit ae235b
gpointer
Packit ae235b
g_try_malloc0_n (gsize n_blocks,
Packit ae235b
                 gsize n_block_bytes);
Packit ae235b

This function is similar to g_try_malloc0(), allocating (n_blocks

Packit ae235b
 * n_block_bytes
Packit ae235b
) bytes,
Packit ae235b
but care is taken to detect possible overflow during multiplication.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

n_blocks

Packit ae235b

the number of blocks to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_block_bytes

Packit ae235b

the size of each block in bytes

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the allocated memory, or NULL

Packit ae235b
Packit ae235b

Since: 2.24

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_try_realloc_n ()

Packit ae235b
gpointer
Packit ae235b
g_try_realloc_n (gpointer mem,
Packit ae235b
                 gsize n_blocks,
Packit ae235b
                 gsize n_block_bytes);
Packit ae235b

This function is similar to g_try_realloc(), allocating (n_blocks

Packit ae235b
 * n_block_bytes
Packit ae235b
) bytes,
Packit ae235b
but care is taken to detect possible overflow during multiplication.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

mem

Packit ae235b

previously-allocated memory, or NULL.

Packit ae235b
[nullable]
Packit ae235b
Packit ae235b
Packit ae235b

n_blocks

Packit ae235b

the number of blocks to allocate

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b

n_block_bytes

Packit ae235b

the size of each block in bytes

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Returns

Packit ae235b

the allocated memory, or NULL.

Packit ae235b
Packit ae235b

Since: 2.24

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_free ()

Packit ae235b
void
Packit ae235b
g_free (gpointer mem);
Packit ae235b

Frees the memory pointed to by mem

Packit ae235b
.

Packit ae235b

If mem

Packit ae235b
 is NULL it simply returns, so there is no need to check mem
Packit ae235b
Packit ae235b
against NULL before calling this function.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

mem

Packit ae235b

the memory to free.

Packit ae235b
[nullable]
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_clear_pointer ()

Packit ae235b
void
Packit ae235b
g_clear_pointer (gpointer *pp,
Packit ae235b
                 GDestroyNotify destroy);
Packit ae235b

Clears a reference to a variable.

Packit ae235b

pp

Packit ae235b
 must not be NULL.

Packit ae235b

If the reference is NULL then this function does nothing.

Packit ae235b
Otherwise, the variable is destroyed using destroy
Packit ae235b
 and the
Packit ae235b
pointer is set to NULL.

Packit ae235b

A macro is also included that allows this function to be used without

Packit ae235b
pointer casts.

Packit ae235b

[skip]

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

pp

Packit ae235b

a pointer to a variable, struct member etc. holding a

Packit ae235b
pointer. 

Packit ae235b
[not nullable]
Packit ae235b
Packit ae235b
Packit ae235b

destroy

Packit ae235b

a function to which a gpointer can be passed, to destroy *pp

Packit ae235b

Packit ae235b
 
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Since: 2.34

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_steal_pointer ()

Packit ae235b
gpointer
Packit ae235b
g_steal_pointer (gpointer pp);
Packit ae235b

Sets pp

Packit ae235b
 to NULL, returning the value that was there before.

Packit ae235b

Conceptually, this transfers the ownership of the pointer from the

Packit ae235b
referenced variable to the "caller" of the macro (ie: "steals" the
Packit ae235b
reference).

Packit ae235b

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

Packit ae235b
pp
Packit ae235b
.

Packit ae235b

This can be very useful when combined with g_autoptr() to prevent the

Packit ae235b
return value of a function from being automatically freed.  Consider
Packit ae235b
the following example (which only works on GCC and clang):

Packit ae235b
Packit ae235b
  
Packit ae235b
    
Packit ae235b
      
Packit ae235b
        
1
Packit ae235b
2
Packit ae235b
3
Packit ae235b
4
Packit ae235b
5
Packit ae235b
6
Packit ae235b
7
Packit ae235b
8
Packit ae235b
9
Packit ae235b
10
Packit ae235b
        
GObject *
Packit ae235b
create_object (void)
Packit ae235b
{
Packit ae235b
  g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
Packit ae235b
Packit ae235b
  if (early_error_case)
Packit ae235b
    return NULL;
Packit ae235b
Packit ae235b
  return g_steal_pointer (&obj);
Packit ae235b
}
Packit ae235b
      
Packit ae235b
    
Packit ae235b
  
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b

It can also be used in similar ways for 'out' parameters and is

Packit ae235b
particularly useful for dealing with optional out parameters:

Packit ae235b
Packit ae235b
  
Packit ae235b
    
Packit ae235b
      
Packit ae235b
        
1
Packit ae235b
2
Packit ae235b
3
Packit ae235b
4
Packit ae235b
5
Packit ae235b
6
Packit ae235b
7
Packit ae235b
8
Packit ae235b
9
Packit ae235b
10
Packit ae235b
11
Packit ae235b
12
Packit ae235b
13
Packit ae235b
        
gboolean
Packit ae235b
get_object (GObject **obj_out)
Packit ae235b
{
Packit ae235b
  g_autoptr(GObject) obj = g_object_new (G_TYPE_OBJECT, NULL);
Packit ae235b
Packit ae235b
  if (early_error_case)
Packit ae235b
    return FALSE;
Packit ae235b
Packit ae235b
  if (obj_out)
Packit ae235b
    *obj_out = g_steal_pointer (&obj);
Packit ae235b
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
      
Packit ae235b
    
Packit ae235b
  
Packit ae235b
Packit ae235b
Packit ae235b

Packit ae235b

In the above example, the object will be automatically freed in the

Packit ae235b
early error case and also in the case that NULL was given for
Packit ae235b
obj_out
Packit ae235b
.

Packit ae235b
Packit ae235b

Parameters

Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

pp

Packit ae235b

a pointer to a pointer.

Packit ae235b
[not nullable]
Packit ae235b
Packit ae235b
Packit ae235b
Packit ae235b

Since: 2.44

Packit ae235b
Packit ae235b

Packit ae235b
Packit ae235b

g_alloca()

Packit ae235b
#define             g_alloca(size)
Packit ae235b

Allocates size

Packit ae235b
 bytes on the stack; these bytes will be freed when the current
Packit ae235b
stack frame is cleaned up. This macro essentially just wraps the alloca()
Packit ae235b
function present on most UNIX variants.
Packit ae235b
Thus it provides the same advantages and pitfalls as alloca():

Packit ae235b
    Packit ae235b
  • alloca() is very fast, as on most systems it's implemented by just adjusting

  • Packit ae235b
    the stack pointer register.

    Packit ae235b
  • It doesn't cause any memory fragmentation, within its scope, separate alloca()

  • Packit ae235b
    blocks just build up and are released together at function end.

    Packit ae235b
  • Allocation sizes have to fit into the current stack frame. For instance in a

  • Packit ae235b
    threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
    Packit ae235b
    so be sparse with alloca() uses.

    Packit ae235b
  • Allocation failure due to insufficient stack space is not indicated with a NULL

  • Packit ae235b
    return like e.g. with malloc(). Instead, most systems probably handle it the same
    Packit ae235b
    way as out of stack space situations from infinite function recursion, i.e.
    Packit ae235b
    with a segmentation fault.

    Packit ae235b
  • Special care has to be taken when mixing alloca() with GNU C variable sized arrays.

  • Packit ae235b
    Stack space allocated with alloca() in the same scope as a variable sized array
    Packit ae235b
    will be freed together with the variable sized array upon exit of that scope, and
    Packit ae235b
    not upon exit of the enclosing function scope.

    Packit ae235b
    Packit ae235b
    Packit ae235b

    Parameters

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    size

    Packit ae235b

    number of bytes to allocate.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Returns

    Packit ae235b

    space for size

    Packit ae235b
    bytes, allocated on the stack

    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    g_newa()

    Packit ae235b
    #define             g_newa(struct_type, n_structs)
    Packit ae235b

    Wraps g_alloca() in a more typesafe manner.

    Packit ae235b
    Packit ae235b

    Parameters

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    struct_type

    Packit ae235b

    Type of memory chunks to be allocated

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    n_structs

    Packit ae235b

    Number of chunks to be allocated

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Returns

    Packit ae235b

    Pointer to stack space for n_structs

    Packit ae235b
    chunks of type struct_type
    Packit ae235b

    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    g_memmove()

    Packit ae235b
    #define             g_memmove(dest,src,len)
    Packit ae235b
    Packit ae235b

    g_memmove has been deprecated since version 2.40 and should not be used in newly-written code.

    Packit ae235b

    Just use memmove().

    Packit ae235b
    Packit ae235b

    Copies a block of memory len

    Packit ae235b
     bytes long, from src
    Packit ae235b
     to dest
    Packit ae235b
    .
    Packit ae235b
    The source and destination areas may overlap.

    Packit ae235b
    Packit ae235b

    Parameters

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    dest

    Packit ae235b

    the destination address to copy the bytes to.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    src

    Packit ae235b

    the source address to copy the bytes from.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    len

    Packit ae235b

    the number of bytes to copy.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    g_memdup ()

    Packit ae235b
    gpointer
    Packit ae235b
    g_memdup (gconstpointer mem,
    Packit ae235b
              guint byte_size);
    Packit ae235b

    Allocates byte_size

    Packit ae235b
     bytes of memory, and copies byte_size
    Packit ae235b
     bytes into it
    Packit ae235b
    from mem
    Packit ae235b
    . If mem
    Packit ae235b
     is NULL it returns NULL.

    Packit ae235b
    Packit ae235b

    Parameters

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    mem

    Packit ae235b

    the memory to copy.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    byte_size

    Packit ae235b

    the number of bytes to copy.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Returns

    Packit ae235b

    a pointer to the newly-allocated copy of the memory, or NULL if mem

    Packit ae235b
    is NULL.

    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    g_mem_set_vtable ()

    Packit ae235b
    void
    Packit ae235b
    g_mem_set_vtable (GMemVTable *vtable);
    Packit ae235b
    Packit ae235b

    g_mem_set_vtable has been deprecated since version 2.46 and should not be used in newly-written code.

    Packit ae235b

    This function now does nothing. Use other memory

    Packit ae235b
    profiling tools instead

    Packit ae235b
    Packit ae235b

    This function used to let you override the memory allocation function.

    Packit ae235b
    However, its use was incompatible with the use of global constructors
    Packit ae235b
    in GLib and GIO, because those use the GLib allocators before main is
    Packit ae235b
    reached. Therefore this function is now deprecated and is just a stub.

    Packit ae235b
    Packit ae235b

    Parameters

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    vtable

    Packit ae235b

    table of memory allocation routines.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    g_mem_is_system_malloc ()

    Packit ae235b
    gboolean
    Packit ae235b
    g_mem_is_system_malloc (void);
    Packit ae235b
    Packit ae235b

    g_mem_is_system_malloc has been deprecated since version 2.46 and should not be used in newly-written code.

    Packit ae235b

    GLib always uses the system malloc, so this function always

    Packit ae235b
    returns TRUE.

    Packit ae235b
    Packit ae235b

    Checks whether the allocator used by g_malloc() is the system's

    Packit ae235b
    malloc implementation. If it returns TRUE memory allocated with
    Packit ae235b
    malloc() can be used interchangeable with memory allocated using g_malloc().
    Packit ae235b
    This function is useful for avoiding an extra copy of allocated memory returned
    Packit ae235b
    by a non-GLib-based API.

    Packit ae235b
    Packit ae235b

    Returns

    Packit ae235b

    if TRUE, malloc() and g_malloc() can be mixed.

    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    g_mem_profile ()

    Packit ae235b
    void
    Packit ae235b
    g_mem_profile (void);
    Packit ae235b
    Packit ae235b

    g_mem_profile has been deprecated since version 2.46 and should not be used in newly-written code.

    Packit ae235b

    Use other memory profiling tools instead

    Packit ae235b
    Packit ae235b

    GLib used to support some tools for memory profiling, but this

    Packit ae235b
    no longer works. There are many other useful tools for memory
    Packit ae235b
    profiling these days which can be used instead.

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Types and Values

    Packit ae235b
    Packit ae235b

    g_mem_gc_friendly

    Packit ae235b
    extern gboolean g_mem_gc_friendly;
    Packit ae235b
    Packit ae235b

    This variable is TRUE if the G_DEBUG environment variable

    Packit ae235b
    includes the key gc-friendly.

    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    struct GMemVTable

    Packit ae235b
    struct GMemVTable {
    Packit ae235b
      gpointer (*malloc)      (gsize    n_bytes);
    Packit ae235b
      gpointer (*realloc)     (gpointer mem,
    Packit ae235b
    			   gsize    n_bytes);
    Packit ae235b
      void     (*free)        (gpointer mem);
    Packit ae235b
      /* optional; set to NULL if not used ! */
    Packit ae235b
      gpointer (*calloc)      (gsize    n_blocks,
    Packit ae235b
    			   gsize    n_block_bytes);
    Packit ae235b
      gpointer (*try_malloc)  (gsize    n_bytes);
    Packit ae235b
      gpointer (*try_realloc) (gpointer mem,
    Packit ae235b
    			   gsize    n_bytes);
    Packit ae235b
    };
    Packit ae235b
    Packit ae235b

    A set of functions used to perform memory allocation. The same GMemVTable must

    Packit ae235b
    be used for all allocations in the same program; a call to g_mem_set_vtable(),
    Packit ae235b
    if it exists, should be prior to any use of GLib.

    Packit ae235b

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

    Packit ae235b
    Packit ae235b

    Members

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    malloc ()

    Packit ae235b

    function to use for allocating memory.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    realloc ()

    Packit ae235b

    function to use for reallocating memory.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    free ()

    Packit ae235b

    function to use to free memory.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    calloc ()

    Packit ae235b

    function to use for allocating zero-filled memory.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    try_malloc ()

    Packit ae235b

    function to use for allocating memory without a default error handler.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b

    try_realloc ()

    Packit ae235b

    function to use for reallocating memory without a default error handler.

    Packit ae235b
     
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Packit ae235b
    Packit ae235b

    glib_mem_profiler_table

    Packit ae235b
    extern GMemVTable *glib_mem_profiler_table;
    Packit ae235b
    Packit ae235b
    Packit ae235b

    glib_mem_profiler_table has been deprecated since version 2.46 and should not be used in newly-written code.

    Packit ae235b

    Use other memory profiling tools instead

    Packit ae235b
    Packit ae235b

    Used to be a GMemVTable containing profiling variants of the memory

    Packit ae235b
    allocation functions, but this variable shouldn't be modified anymore.

    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b
    Packit ae235b

    Generated by GTK-Doc V1.27
    Packit ae235b
    </body>
    Packit ae235b
    </html>