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 Since the returned pointer is already casted to the right type,
34 it is normally unnecessary to cast it explicitly, and doing
35 so might hide memory allocation errors.
38 @struct_type: the type of the elements to allocate.
39 @n_structs: the number of elements to allocate.
40 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
43 <!-- ##### MACRO g_new0 ##### -->
45 Allocates @n_structs elements of type @struct_type, initialized to 0's.
46 The returned pointer is cast to a pointer to the given type.
47 If @n_structs is 0 it returns %NULL.
50 Since the returned pointer is already casted to the right type,
51 it is normally unnecessary to cast it explicitly, and doing
52 so might hide memory allocation errors.
55 @struct_type: the type of the elements to allocate.
56 @n_structs: the number of elements to allocate.
57 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
60 <!-- ##### MACRO g_renew ##### -->
62 Reallocates the memory pointed to by @mem, so that it now has space for
63 @n_structs elements of type @struct_type. It returns the new address of
64 the memory, which may have been moved.
67 @struct_type: the type of the elements to allocate.
68 @mem: the currently allocated memory.
69 @n_structs: the number of elements to allocate.
70 @Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type.
73 <!-- ##### MACRO g_try_new ##### -->
75 Attempts to allocate @n_structs elements of type @struct_type, and returns
76 %NULL on failure. Contrast with g_new(), which aborts the program on failure.
77 The returned pointer is cast to a pointer to the given type.
78 If @n_structs is 0 it returns %NULL.
81 @struct_type: the type of the elements to allocate.
82 @n_structs: the number of elements to allocate.
83 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
87 <!-- ##### MACRO g_try_new0 ##### -->
89 Attempts to allocate @n_structs elements of type @struct_type, initialized
90 to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
91 the program on failure.
92 The returned pointer is cast to a pointer to the given type.
93 If @n_counts is 0 it returns %NULL.
96 @struct_type: the type of the elements to allocate.
97 @n_structs: the number of elements to allocate.
98 @Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
102 <!-- ##### MACRO g_try_renew ##### -->
104 Attempts to reallocate the memory pointed to by @mem, so that it now has
105 space for @n_structs elements of type @struct_type, and returns %NULL on
106 failure. Contrast with g_renew(), which aborts the program on failure.
107 It returns the new address of the memory, which may have been moved.
110 @struct_type: the type of the elements to allocate.
111 @mem: the currently allocated memory.
112 @n_structs: the number of elements to allocate.
113 @Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type.
117 <!-- ##### FUNCTION g_malloc ##### -->
119 Allocates @n_bytes bytes of memory.
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_malloc0 ##### -->
129 Allocates @n_bytes bytes of memory, initialized to 0's.
130 If @n_bytes is 0 it returns %NULL.
133 @n_bytes: the number of bytes to allocate.
134 @Returns: a pointer to the allocated memory.
137 <!-- ##### FUNCTION g_realloc ##### -->
139 Reallocates the memory pointed to by @mem, so that it now has space for
140 @n_bytes bytes of memory. It returns the new address of the memory, which may
141 have been moved. @mem may be %NULL, in which case it's considered to
142 have zero-length. @n_bytes may be 0, in which case %NULL will be returned.
145 @mem: the memory to reallocate.
146 @n_bytes: new size of the memory in bytes.
147 @Returns: the new address of the allocated memory.
150 <!-- ##### FUNCTION g_try_malloc ##### -->
152 Attempts to allocate @n_bytes, and returns %NULL on failure.
153 Contrast with g_malloc(), which aborts the program on failure.
156 @n_bytes: number of bytes to allocate.
157 @Returns: the allocated memory, or %NULL.
160 <!-- ##### FUNCTION g_try_malloc0 ##### -->
162 Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
163 failure. Contrast with g_malloc0(), which aborts the program on failure.
166 @n_bytes: number of bytes to allocate.
167 @Returns: the allocated memory, or %NULL.
171 <!-- ##### FUNCTION g_try_realloc ##### -->
173 Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
174 on failure. Contrast with g_realloc(), which aborts the program
175 on failure. If @mem is %NULL, behaves the same as g_try_malloc().
178 @mem: previously-allocated memory, or %NULL.
179 @n_bytes: number of bytes to allocate.
180 @Returns: the allocated memory, or %NULL.
183 <!-- ##### FUNCTION g_free ##### -->
185 Frees the memory pointed to by @mem.
186 If @mem is %NULL it simply returns.
189 @mem: the memory to free.
192 <!-- ##### MACRO g_alloca ##### -->
194 Allocates @size bytes on the stack; these bytes will be freed when the current
195 stack frame is cleaned up. This macro essentially just wraps the
196 <function>alloca()</function> function present on most UNIX variants.
197 Thus it provides the same advantages and pitfalls as <function>alloca()</function>:
199 <varlistentry><term></term><listitem><para>
200 + <function>alloca()</function> is very fast, as on most systems it's implemented by just adjusting
201 the stack pointer register.
202 </para></listitem></varlistentry>
203 <varlistentry><term></term><listitem><para>
204 + It doesn't cause any memory fragmentation, within its scope, separate <function>alloca()</function>
205 blocks just build up and are released together at function end.
206 </para></listitem></varlistentry>
207 <varlistentry><term></term><listitem><para>
208 - Allocation sizes have to fit into the current stack frame. For instance in a
209 threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes,
210 so be sparse with <function>alloca()</function> uses.
211 </para></listitem></varlistentry>
212 <varlistentry><term></term><listitem><para>
213 - Allocation failure due to insufficient stack space is not indicated with a %NULL
214 return like e.g. with <function>malloc()</function>. Instead, most systems probably handle it the same
215 way as out of stack space situations from infinite function recursion, i.e.
216 with a segmentation fault.
217 </para></listitem></varlistentry>
218 <varlistentry><term></term><listitem><para>
219 - Special care has to be taken when mixing <function>alloca()</function> with GNU C variable sized arrays.
220 Stack space allocated with <function>alloca()</function> in the same scope as a variable sized array
221 will be freed together with the variable sized array upon exit of that scope, and
222 not upon exit of the enclosing function scope.
223 </para></listitem></varlistentry>
228 @size: number of bytes to allocate.
229 @Returns: space for @size bytes, allocated on the stack
232 <!-- ##### MACRO g_newa ##### -->
234 Wraps g_alloca() in a more typesafe manner.
237 @struct_type: Type of memory chunks to be allocated
238 @n_structs: Number of chunks to be allocated
239 @Returns: Pointer to stack space for @n_structs chunks of type @struct_type
242 <!-- ##### MACRO g_memmove ##### -->
252 <!-- ##### FUNCTION g_memdup ##### -->
254 Allocates @byte_size bytes of memory, and copies @byte_size bytes into it
255 from @mem. If @mem is %NULL it returns %NULL.
258 @mem: the memory to copy.
259 @byte_size: the number of bytes to copy.
260 @Returns: a pointer to the newly-allocated copy of the memory, or %NULL if @mem
264 <!-- ##### STRUCT GMemVTable ##### -->
266 A set of functions used to perform memory allocation. The same #GMemVTable must
267 be used for all allocations in the same program; a call to g_mem_set_vtable(),
268 if it exists, should be prior to any use of GLib.
271 @malloc: function to use for allocating memory.
272 @realloc: function to use for reallocating memory.
273 @free: function to use to free memory.
274 @calloc: function to use for allocating zero-filled memory.
275 @try_malloc: function to use for allocating memory without a default error handler.
276 @try_realloc: function to use for reallocating memory without a default error handler.
278 <!-- ##### FUNCTION g_mem_set_vtable ##### -->
280 Sets the #GMemVTable to use for memory allocation. You can use this to provide
281 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>
282 functions; GLib can provide default implementations of the others. The <function>malloc()</function>
283 and <function>realloc()</function> implementations should return %NULL on failure, GLib will handle
284 error-checking for you. @vtable is copied, so need not persist after this
285 function has been called.
288 @vtable: table of memory allocation routines.
291 <!-- ##### FUNCTION g_mem_is_system_malloc ##### -->
299 <!-- ##### VARIABLE glib_mem_profiler_table ##### -->
301 A #GMemVTable containing profiling variants of the memory
302 allocation functions. Use them together with g_mem_profile()
303 in order to get information about the memory allocation pattern
308 <!-- ##### FUNCTION g_mem_profile ##### -->
310 Outputs a summary of memory usage.
313 It outputs the frequency of allocations of different sizes,
314 the total number of bytes which have been allocated,
315 the total number of bytes which have been freed,
316 and the difference between the previous two values, i.e. the number of bytes
320 Note that this function will not output anything unless you have
321 previously installed the #glib_mem_profiler_table with g_mem_set_vtable().