Memory Allocation

Name

Memory Allocation -- general memory-handling.

Synopsis


#include <glib.h>


#define     g_new                           (type, count)
#define     g_new0                          (type, count)
#define     g_renew                         (type, mem, count)

gpointer    g_malloc                        (gulong size);
gpointer    g_malloc0                       (gulong size);
gpointer    g_realloc                       (gpointer mem,
                                             gulong size);

void        g_free                          (gpointer mem);

#define     g_memmove                       (d,s,n)
gpointer    g_memdup                        (gconstpointer mem,
                                             guint byte_size);

void        g_mem_profile                   (void);
void        g_mem_check                     (gpointer mem);

Description

These functions provide support for allocating and freeing memory.

Note: If any call to allocate memory fails, the application is terminated. This also means that there is no need to check if the call succeeded.

Details

g_new()

#define     g_new(type, count)

Allocates count elements of type type. The returned pointer is cast to a pointer to the given type. If count is 0 it returns NULL.

type :the type of the elements to allocate.
count :the number of elements to allocate.
Returns :a pointer to the allocated memory, cast to a pointer to type.


g_new0()

#define     g_new0(type, count)

Allocates count elements of type type, initialized to 0's. The returned pointer is cast to a pointer to the given type. If count is 0 it returns NULL.

type :the type of the elements to allocate.
count :the number of elements to allocate.
Returns :a pointer to the allocated memory, cast to a pointer to type.


g_renew()

#define     g_renew(type, mem, count)

Reallocates the memory pointed to by mem, so that it now has space for count elements of type type. It returns the new address of the memory, which may have been moved.

type :the type of the elements to allocate.
mem :the currently allocated memory.
count :the number of elements to allocate.
Returns :a pointer to the new allocated memory, cast to a pointer to type.


g_malloc ()

gpointer    g_malloc                        (gulong size);

Allocates size bytes of memory. If size is 0 it returns NULL.

size :the number of bytes to allocate.
Returns :a pointer to the allocated memory.


g_malloc0 ()

gpointer    g_malloc0                       (gulong size);

Allocates size bytes of memory, initialized to 0's. If size is 0 it returns NULL.

size :the number of bytes to allocate.
Returns :a pointer to the allocated memory.


g_realloc ()

gpointer    g_realloc                       (gpointer mem,
                                             gulong size);

Reallocates the memory pointed to by mem, so that it now has space for size bytes of memory. It returns the new address of the memory, which may have been moved.

mem :the memory to reallocate.
size :the new size of the allocated memory, in bytes.
Returns :the new address of the allocated memory.


g_free ()

void        g_free                          (gpointer mem);

Frees the memory pointed to by mem. If mem is NULL it simply returns.

mem :the memory to free.


g_memmove()

#define     g_memmove(d,s,n)

Copies a block of memory n bytes long, from s to d. The source and destination areas may overlap.

Note: On architectures where memmove() is not available, this function is implemented using bcopy(), which may not be able to handle overlapping areas.

d :the destination address to copy the bytes to.
s :the source address to copy the bytes from.
n :the number of bytes to copy.


g_memdup ()

gpointer    g_memdup                        (gconstpointer mem,
                                             guint byte_size);

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

mem :the memory to copy.
byte_size :the number of bytes to copy.
Returns :a pointer to the newly allocated copy of the memory, or NULL if mem is NULL.


g_mem_profile ()

void        g_mem_profile                   (void);

Outputs a summary of memory usage. To use this function you must configure glib with the flag '--enable-mem-profile=yes' before compiling.

It outputs the frequency of allocations of different sizes, the total number of bytes which have been allocated, the total number of bytes which have been freed, and the difference between the previous two values, i.e. the number of bytes still in use.


g_mem_check ()

void        g_mem_check                     (gpointer mem);

Checks if the given memory has already been freed. If it has it outputs a warning message. To use this function you must configure glib with the flag '--enable-mem-check=yes' before compiling.

mem :the memory to check.