1 <!-- ##### SECTION Title ##### -->
4 <!-- ##### SECTION Short_Description ##### -->
5 general memory-handling.
7 <!-- ##### SECTION Long_Description ##### -->
9 These functions provide support for allocating and freeing memory.
13 If any call to allocate memory fails, the application is terminated.
14 This also means that there is no need to check if the call succeeded.
18 <!-- ##### SECTION See_Also ##### -->
23 <!-- ##### SECTION Stability_Level ##### -->
26 <!-- ##### MACRO g_new ##### -->
28 Allocates @n_structs elements of type @struct_type.
29 The returned pointer is cast to a pointer to the given type.
30 If @n_structs is 0 it returns %NULL.
33 @struct_type: the type of the elements to allocate.
34 @n_structs: the number of elements to allocate.
35 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
38 <!-- ##### MACRO g_new0 ##### -->
40 Allocates @n_structs elements of type @struct_type, initialized to 0's.
41 The returned pointer is cast to a pointer to the given type.
42 If @n_structs is 0 it returns %NULL.
45 @struct_type: the type of the elements to allocate.
46 @n_structs: the number of elements to allocate.
47 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
50 <!-- ##### MACRO g_renew ##### -->
52 Reallocates the memory pointed to by @mem, so that it now has space for
53 @n_structs elements of type @struct_type. It returns the new address of
54 the memory, which may have been moved.
57 @struct_type: the type of the elements to allocate.
58 @mem: the currently allocated memory.
59 @n_structs: the number of elements to allocate.
60 @Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type.
63 <!-- ##### MACRO g_try_new ##### -->
65 Attempts to allocate @n_structs elements of type @struct_type, and returns
66 %NULL on failure. Contrast with g_new(), which aborts the program on failure.
67 The returned pointer is cast to a pointer to the given type.
68 If @n_structs is 0 it returns %NULL.
71 @struct_type: the type of the elements to allocate.
72 @n_structs: the number of elements to allocate.
73 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
77 <!-- ##### MACRO g_try_new0 ##### -->
79 Attempts to allocate @n_structs elements of type @struct_type, initialized
80 to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
81 the program on failure.
82 The returned pointer is cast to a pointer to the given type.
83 If @n_counts is 0 it returns %NULL.
86 @struct_type: the type of the elements to allocate.
87 @n_structs: the number of elements to allocate.
88 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
92 <!-- ##### MACRO g_try_renew ##### -->
94 Attempts to reallocate the memory pointed to by @mem, so that it now has
95 space for @n_structs elements of type @struct_type, and returns %NULL on
96 failure. Contrast with g_renew(), which aborts the program on failure.
97 It returns the new address of the memory, which may have been moved.
100 @struct_type: the type of the elements to allocate.
101 @mem: the currently allocated memory.
102 @n_structs: the number of elements to allocate.
103 @Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type.
107 <!-- ##### FUNCTION g_malloc ##### -->
109 Allocates @n_bytes bytes of memory.
110 If @n_bytes is 0 it returns %NULL.
113 @n_bytes: the number of bytes to allocate.
114 @Returns: a pointer to the allocated memory.
117 <!-- ##### FUNCTION g_malloc0 ##### -->
119 Allocates @n_bytes bytes of memory, initialized to 0's.
120 If @n_bytes is 0 it returns %NULL.
123 @n_bytes: the number of bytes to allocate.
124 @Returns: a pointer to the allocated memory.
127 <!-- ##### FUNCTION g_realloc ##### -->
129 Reallocates the memory pointed to by @mem, so that it now has space for
130 @n_bytes bytes of memory. It returns the new address of the memory, which may
131 have been moved. @mem may be %NULL, in which case it's considered to
132 have zero-length. @n_bytes may be 0, in which case %NULL will be returned.
135 @mem: the memory to reallocate.
136 @n_bytes: new size of the memory in bytes.
137 @Returns: the new address of the allocated memory.
140 <!-- ##### FUNCTION g_try_malloc ##### -->
142 Attempts to allocate @n_bytes, and returns %NULL on failure.
143 Contrast with g_malloc(), which aborts the program on failure.
146 @n_bytes: number of bytes to allocate.
147 @Returns: the allocated memory, or %NULL.
150 <!-- ##### FUNCTION g_try_malloc0 ##### -->
152 Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
153 failure. Contrast with g_malloc0(), which aborts the program on failure.
156 @n_bytes: number of bytes to allocate.
157 @Returns: the allocated memory, or %NULL.
161 <!-- ##### FUNCTION g_try_realloc ##### -->
163 Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
164 on failure. Contrast with g_realloc(), which aborts the program
165 on failure. If @mem is %NULL, behaves the same as g_try_malloc().
168 @mem: previously-allocated memory, or %NULL.
169 @n_bytes: number of bytes to allocate.
170 @Returns: the allocated memory, or %NULL.
173 <!-- ##### FUNCTION g_free ##### -->
175 Frees the memory pointed to by @mem.
176 If @mem is %NULL it simply returns.
179 @mem: the memory to free.
182 <!-- ##### MACRO g_alloca ##### -->
184 Allocates @size bytes on the stack; these bytes will be freed when the current
185 stack frame is cleaned up. This macro essentially just wraps the
186 <function>alloca()</function> function present on most UNIX variants.
187 Thus it provides the same advantages and pitfalls as <function>alloca()</function>:
189 <varlistentry><term></term><listitem><para>
190 + <function>alloca()</function> is very fast, as on most systems it's implemented by just adjusting
191 the stack pointer register.
192 </para></listitem></varlistentry>
193 <varlistentry><term></term><listitem><para>
194 + It doesn't cause any memory fragmentation, within its scope, separate <function>alloca()</function>
195 blocks just build up and are released together at function end.
196 </para></listitem></varlistentry>
197 <varlistentry><term></term><listitem><para>
198 - Allocation sizes have to fit into the current stack frame. For instance in a
199 threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
200 so be sparse with <function>alloca()</function> uses.
201 </para></listitem></varlistentry>
202 <varlistentry><term></term><listitem><para>
203 - Allocation failure due to insufficient stack space is not indicated with a %NULL
204 return like e.g. with <function>malloc()</function>. Instead, most systems probably handle it the same
205 way as out of stack space situations from infinite function recursion, i.e.
206 with a segmentation fault.
207 </para></listitem></varlistentry>
208 <varlistentry><term></term><listitem><para>
209 - Special care has to be taken when mixing <function>alloca()</function> with GNU C variable sized arrays.
210 Stack space allocated with <function>alloca()</function> in the same scope as a variable sized array
211 will be freed together with the variable sized array upon exit of that scope, and
212 not upon exit of the enclosing function scope.
213 </para></listitem></varlistentry>
218 @size: number of bytes to allocate.
219 @Returns: space for @size bytes, allocated on the stack
222 <!-- ##### MACRO g_newa ##### -->
224 Wraps g_alloca() in a more typesafe manner.
227 @struct_type: Type of memory chunks to be allocated
228 @n_structs: Number of chunks to be allocated
229 @Returns: Pointer to stack space for @n_structs chunks of type @struct_type
232 <!-- ##### MACRO g_memmove ##### -->
242 <!-- ##### FUNCTION g_memdup ##### -->
244 Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
245 from @mem. If @mem is %NULL it returns %NULL.
248 @mem: the memory to copy.
249 @byte_size: the number of bytes to copy.
250 @Returns: a pointer to the newly-allocated copy of the memory, or %NULL if @mem
254 <!-- ##### STRUCT GMemVTable ##### -->
256 A set of functions used to perform memory allocation. The same #GMemVTable must
257 be used for all allocations in the same program; a call to g_mem_set_vtable(),
258 if it exists, should be prior to any use of GLib.
261 @malloc: function to use for allocating memory.
262 @realloc: function to use for reallocating memory.
263 @free: function to use to free memory.
264 @calloc: function to use for allocating zero-filled memory.
265 @try_malloc: function to use for allocating memory without a default error handler.
266 @try_realloc: function to use for reallocating memory without a default error handler.
268 <!-- ##### FUNCTION g_mem_set_vtable ##### -->
270 Sets the #GMemVTable to use for memory allocation. You can use this to provide
271 custom memory allocation routines. <emphasis>This function must be called before using any other GLib functions.</emphasis> The @vtable only needs to provide <function>malloc()</function>, <function>realloc()</function>, and <function>free()</function>
272 functions; GLib can provide default implementations of the others. The <function>malloc()</function>
273 and <function>realloc()</function> implementations should return %NULL on failure, GLib will handle
274 error-checking for you. @vtable is copied, so need not persist after this
275 function has been called.
278 @vtable: table of memory allocation routines.
281 <!-- ##### FUNCTION g_mem_is_system_malloc ##### -->
289 <!-- ##### VARIABLE glib_mem_profiler_table ##### -->
291 A #GMemVTable containing profiling variants of the memory
292 allocation functions. Use them together with g_mem_profile()
293 in order to get information about the memory allocation pattern
298 <!-- ##### FUNCTION g_mem_profile ##### -->
300 Outputs a summary of memory usage.
303 It outputs the frequency of allocations of different sizes,
304 the total number of bytes which have been allocated,
305 the total number of bytes which have been freed,
306 and the difference between the previous two values, i.e. the number of bytes
310 Note that this function will not output anything unless you have
311 previously installed the #glib_mem_profiler_table with g_mem_set_vtable().