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.
14 If any call to allocate memory fails, the application is terminated.
15 This also means that there is no need to check if the call succeeded.
21 It's important to match g_malloc() with g_free(), plain malloc() with free(),
22 and (if you're using C++) new with delete and new[] with delete[]. Otherwise
23 bad things can happen, since these allocators may use different memory
24 pools (and new/delete call constructors and destructors). See also
29 <!-- ##### SECTION See_Also ##### -->
34 <!-- ##### SECTION Stability_Level ##### -->
37 <!-- ##### MACRO g_new ##### -->
39 Allocates @n_structs elements of type @struct_type.
40 The returned pointer is cast to a pointer to the given type.
41 If @n_structs is 0 it returns %NULL.
42 Care is taken to avoid overflow when calculating the size of the allocated block.
45 Since the returned pointer is already casted to the right type,
46 it is normally unnecessary to cast it explicitly, and doing
47 so might hide memory allocation errors.
50 @struct_type: the type of the elements to allocate
51 @n_structs: the number of elements to allocate
52 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
55 <!-- ##### MACRO g_new0 ##### -->
57 Allocates @n_structs elements of type @struct_type, initialized to 0's.
58 The returned pointer is cast to a pointer to the given type.
59 If @n_structs is 0 it returns %NULL.
60 Care is taken to avoid overflow when calculating the size of the allocated block.
63 Since the returned pointer is already casted to the right type,
64 it is normally unnecessary to cast it explicitly, and doing
65 so might hide memory allocation errors.
68 @struct_type: the type of the elements to allocate.
69 @n_structs: the number of elements to allocate.
70 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
73 <!-- ##### MACRO g_renew ##### -->
75 Reallocates the memory pointed to by @mem, so that it now has space for
76 @n_structs elements of type @struct_type. It returns the new address of
77 the memory, which may have been moved.
78 Care is taken to avoid overflow when calculating the size of the allocated block.
81 @struct_type: the type of the elements to allocate
82 @mem: the currently allocated memory
83 @n_structs: the number of elements to allocate
84 @Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
87 <!-- ##### MACRO g_try_new ##### -->
89 Attempts to allocate @n_structs elements of type @struct_type, and returns
90 %NULL on failure. Contrast with g_new(), which aborts the program on failure.
91 The returned pointer is cast to a pointer to the given type.
92 The function returns %NULL when @n_structs is 0 of if an overflow occurs.
95 @struct_type: the type of the elements to allocate
96 @n_structs: the number of elements to allocate
97 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
101 <!-- ##### MACRO g_try_new0 ##### -->
103 Attempts to allocate @n_structs elements of type @struct_type, initialized
104 to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
105 the program on failure.
106 The returned pointer is cast to a pointer to the given type.
107 The function returns %NULL when @n_structs is 0 of if an overflow occurs.
110 @struct_type: the type of the elements to allocate
111 @n_structs: the number of elements to allocate
112 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
116 <!-- ##### MACRO g_try_renew ##### -->
118 Attempts to reallocate the memory pointed to by @mem, so that it now has
119 space for @n_structs elements of type @struct_type, and returns %NULL on
120 failure. Contrast with g_renew(), which aborts the program on failure.
121 It returns the new address of the memory, which may have been moved.
122 The function returns %NULL if an overflow occurs.
125 @struct_type: the type of the elements to allocate
126 @mem: the currently allocated memory
127 @n_structs: the number of elements to allocate
128 @Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
132 <!-- ##### FUNCTION g_malloc ##### -->
134 Allocates @n_bytes bytes of memory.
135 If @n_bytes is 0 it returns %NULL.
138 @n_bytes: the number of bytes to allocate
139 @Returns: a pointer to the allocated memory
142 <!-- ##### FUNCTION g_malloc0 ##### -->
144 Allocates @n_bytes bytes of memory, initialized to 0's.
145 If @n_bytes is 0 it returns %NULL.
148 @n_bytes: the number of bytes to allocate
149 @Returns: a pointer to the allocated memory
152 <!-- ##### FUNCTION g_realloc ##### -->
154 Reallocates the memory pointed to by @mem, so that it now has space for
155 @n_bytes bytes of memory. It returns the new address of the memory, which may
156 have been moved. @mem may be %NULL, in which case it's considered to
157 have zero-length. @n_bytes may be 0, in which case %NULL will be returned
158 and @mem will be freed unless it is %NULL.
161 @mem: the memory to reallocate
162 @n_bytes: new size of the memory in bytes
163 @Returns: the new address of the allocated memory
166 <!-- ##### FUNCTION g_try_malloc ##### -->
168 Attempts to allocate @n_bytes, and returns %NULL on failure.
169 Contrast with g_malloc(), which aborts the program on failure.
172 @n_bytes: number of bytes to allocate.
173 @Returns: the allocated memory, or %NULL.
176 <!-- ##### FUNCTION g_try_malloc0 ##### -->
178 Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
179 failure. Contrast with g_malloc0(), which aborts the program on failure.
182 @n_bytes: number of bytes to allocate
183 @Returns: the allocated memory, or %NULL
187 <!-- ##### FUNCTION g_try_realloc ##### -->
189 Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
190 on failure. Contrast with g_realloc(), which aborts the program
191 on failure. If @mem is %NULL, behaves the same as g_try_malloc().
194 @mem: previously-allocated memory, or %NULL.
195 @n_bytes: number of bytes to allocate.
196 @Returns: the allocated memory, or %NULL.
199 <!-- ##### FUNCTION g_malloc_n ##### -->
201 This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
202 but care is taken to detect possible overflow during multiplication.
205 @n_blocks: the number of blocks to allocate
206 @n_block_bytes: the size of each block in bytes
207 @Returns: a pointer to the allocated memory
211 <!-- ##### FUNCTION g_malloc0_n ##### -->
213 This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
214 but care is taken to detect possible overflow during multiplication.
217 @n_blocks: the number of blocks to allocate
218 @n_block_bytes: the size of each block in bytes
219 @Returns: a pointer to the allocated memory
223 <!-- ##### FUNCTION g_realloc_n ##### -->
225 This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
226 but care is taken to detect possible overflow during multiplication.
229 @mem: the memory to reallocate
230 @n_blocks: the number of blocks to allocate
231 @n_block_bytes: the size of each block in bytes
232 @Returns: the new address of the allocated memory
236 <!-- ##### FUNCTION g_try_malloc_n ##### -->
238 This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
239 but care is taken to detect possible overflow during multiplication.
242 @n_blocks: the number of blocks to allocate
243 @n_block_bytes: the size of each block in bytes
244 @Returns: the allocated memory, or %NULL.
248 <!-- ##### FUNCTION g_try_malloc0_n ##### -->
250 This function is similar to g_try_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
251 but care is taken to detect possible overflow during multiplication.
254 @n_blocks: the number of blocks to allocate
255 @n_block_bytes: the size of each block in bytes
256 @Returns: the allocated memory, or %NULL
260 <!-- ##### FUNCTION g_try_realloc_n ##### -->
262 This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
263 but care is taken to detect possible overflow during multiplication.
266 @mem: previously-allocated memory, or %NULL.
267 @n_blocks: the number of blocks to allocate
268 @n_block_bytes: the size of each block in bytes
269 @Returns: the allocated memory, or %NULL.
273 <!-- ##### FUNCTION g_free ##### -->
275 Frees the memory pointed to by @mem.
276 If @mem is %NULL it simply returns.
279 @mem: the memory to free
282 <!-- ##### VARIABLE g_mem_gc_friendly ##### -->
284 This variable is %TRUE if the <envar>G_DEBUG</envar> environment variable
285 includes the key <link linkend="G_DEBUG">gc-friendly</link>.
289 <!-- ##### MACRO g_alloca ##### -->
291 Allocates @size bytes on the stack; these bytes will be freed when the current
292 stack frame is cleaned up. This macro essentially just wraps the alloca()
293 function present on most UNIX variants.
294 Thus it provides the same advantages and pitfalls as alloca():
296 <varlistentry><term></term><listitem><para>
297 + alloca() is very fast, as on most systems it's implemented by just adjusting
298 the stack pointer register.
299 </para></listitem></varlistentry>
300 <varlistentry><term></term><listitem><para>
301 + It doesn't cause any memory fragmentation, within its scope, separate alloca()
302 blocks just build up and are released together at function end.
303 </para></listitem></varlistentry>
304 <varlistentry><term></term><listitem><para>
305 - Allocation sizes have to fit into the current stack frame. For instance in a
306 threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
307 so be sparse with alloca() uses.
308 </para></listitem></varlistentry>
309 <varlistentry><term></term><listitem><para>
310 - Allocation failure due to insufficient stack space is not indicated with a %NULL
311 return like e.g. with malloc(). Instead, most systems probably handle it the same
312 way as out of stack space situations from infinite function recursion, i.e.
313 with a segmentation fault.
314 </para></listitem></varlistentry>
315 <varlistentry><term></term><listitem><para>
316 - Special care has to be taken when mixing alloca() with GNU C variable sized arrays.
317 Stack space allocated with alloca() in the same scope as a variable sized array
318 will be freed together with the variable sized array upon exit of that scope, and
319 not upon exit of the enclosing function scope.
320 </para></listitem></varlistentry>
325 @size: number of bytes to allocate.
326 @Returns: space for @size bytes, allocated on the stack
329 <!-- ##### MACRO g_newa ##### -->
331 Wraps g_alloca() in a more typesafe manner.
334 @struct_type: Type of memory chunks to be allocated
335 @n_structs: Number of chunks to be allocated
336 @Returns: Pointer to stack space for @n_structs chunks of type @struct_type
339 <!-- ##### MACRO g_memmove ##### -->
349 <!-- ##### FUNCTION g_memdup ##### -->
351 Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
352 from @mem. If @mem is %NULL it returns %NULL.
355 @mem: the memory to copy.
356 @byte_size: the number of bytes to copy.
357 @Returns: a pointer to the newly-allocated copy of the memory, or %NULL if @mem
361 <!-- ##### STRUCT GMemVTable ##### -->
363 A set of functions used to perform memory allocation. The same #GMemVTable must
364 be used for all allocations in the same program; a call to g_mem_set_vtable(),
365 if it exists, should be prior to any use of GLib.
368 @malloc: function to use for allocating memory.
369 @realloc: function to use for reallocating memory.
370 @free: function to use to free memory.
371 @calloc: function to use for allocating zero-filled memory.
372 @try_malloc: function to use for allocating memory without a default error handler.
373 @try_realloc: function to use for reallocating memory without a default error handler.
375 <!-- ##### FUNCTION g_mem_set_vtable ##### -->
377 Sets the #GMemVTable to use for memory allocation. You can use this to provide
378 custom memory allocation routines. <emphasis>This function must be called
379 before using any other GLib functions.</emphasis> The @vtable only needs to
380 provide malloc(), realloc(), and free() functions; GLib can provide default
381 implementations of the others. The malloc() and realloc() implementations
382 should return %NULL on failure, GLib will handle error-checking for you.
383 @vtable is copied, so need not persist after this function has been called.
386 @vtable: table of memory allocation routines.
389 <!-- ##### FUNCTION g_mem_is_system_malloc ##### -->
397 <!-- ##### VARIABLE glib_mem_profiler_table ##### -->
399 A #GMemVTable containing profiling variants of the memory
400 allocation functions. Use them together with g_mem_profile()
401 in order to get information about the memory allocation pattern
406 <!-- ##### FUNCTION g_mem_profile ##### -->
408 Outputs a summary of memory usage.
411 It outputs the frequency of allocations of different sizes,
412 the total number of bytes which have been allocated,
413 the total number of bytes which have been freed,
414 and the difference between the previous two values, i.e. the number of bytes
418 Note that this function will not output anything unless you have
419 previously installed the #glib_mem_profiler_table with g_mem_set_vtable().