GLib Reference Manual | |||
---|---|---|---|
<<< Previous Page | Home | Up | Next Page >>> |
#include <glib.h> #define g_new (struct_type, n_structs) #define g_new0 (struct_type, n_structs) #define g_renew (struct_type, mem, n_structs) gpointer g_malloc (gulong n_bytes); gpointer g_malloc0 (gulong n_bytes); gpointer g_realloc (gpointer mem, gulong n_bytes); gpointer g_try_malloc (gulong n_bytes); gpointer g_try_realloc (gpointer mem, gulong n_bytes); void g_free (gpointer mem); #define g_alloca (size) #define g_memmove (d,s,n) gpointer g_memdup (gconstpointer mem, guint byte_size); struct GMemVTable; void g_mem_set_vtable (GMemVTable *vtable); extern GMemVTable *glib_mem_profiler_table; void g_mem_profile (void); |
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.
#define g_new(struct_type, n_structs) |
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.
#define g_new0(struct_type, n_structs) |
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.
#define g_renew(struct_type, mem, n_structs) |
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.
gpointer g_malloc (gulong n_bytes); |
Allocates size bytes of memory. If size is 0 it returns NULL.
gpointer g_malloc0 (gulong n_bytes); |
Allocates size bytes of memory, initialized to 0's. If size is 0 it returns NULL.
gpointer g_realloc (gpointer mem, gulong n_bytes); |
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.
void g_free (gpointer mem); |
Frees the memory pointed to by mem. If mem is NULL it simply returns.
#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.
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.
struct GMemVTable { gpointer (*malloc) (gsize n_bytes); gpointer (*realloc) (gpointer mem, gsize n_bytes); void (*free) (gpointer mem); /* optional */ gpointer (*calloc) (gsize n_blocks, gsize n_block_bytes); gpointer (*try_malloc) (gsize n_bytes); gpointer (*try_realloc) (gpointer mem, gsize n_bytes); }; |
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.