📄 glib-memory-allocation.html
字号:
<td><p><span class="term"></span></p></td><td><p> - Allocation failure due to insufficient stack space is not indicated with a <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> return like e.g. with <code class="function">malloc()</code>. Instead, most systems probably handle it the same way as out of stack space situations from infinite function recursion, i.e. with a segmentation fault. </p></td></tr><tr><td><p><span class="term"></span></p></td><td><p> - Special care has to be taken when mixing <code class="function">alloca()</code> with GNU C variable sized arrays. Stack space allocated with <code class="function">alloca()</code> in the same scope as a variable sized array will be freed together with the variable sized array upon exit of that scope, and not upon exit of the enclosing function scope. </p></td></tr></tbody></table></div><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>size</code></em> :</span></p></td><td> number of bytes to allocate.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>space for <em class="parameter"><code>size</code></em> bytes, allocated on the stack</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2923295"></a><h3><a name="g-newa"></a>g_newa()</h3><a class="indexterm" name="id2923308"></a><pre class="programlisting">#define g_newa(struct_type, n_structs)</pre><p>Wraps <a class="link" href="glib-Memory-Allocation.html#g-alloca"><code class="function">g_alloca()</code></a> in a more typesafe manner.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>struct_type</code></em> :</span></p></td><td>Type of memory chunks to be allocated</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_structs</code></em> :</span></p></td><td> Number of chunks to be allocated</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td> Pointer to stack space for <em class="parameter"><code>n_structs</code></em> chunks of type <em class="parameter"><code>struct_type</code></em></td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2923389"></a><h3><a name="g-memmove"></a>g_memmove()</h3><a class="indexterm" name="id2923401"></a><pre class="programlisting">#define g_memmove(dest,src,len)</pre><p>Copies a block of memory <em class="parameter"><code>len</code></em> bytes long, from <em class="parameter"><code>src</code></em> to <em class="parameter"><code>dest</code></em>.The source and destination areas may overlap.</p><p>In order to use this function, you must include <code class="filename">string.h</code> yourself, because this macro will typically simply resolve to <code class="function">memmove()</code> and GLib does not include <code class="filename">string.h</code> for you.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>dest</code></em> :</span></p></td><td> the destination address to copy the bytes to.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>src</code></em> :</span></p></td><td> the source address to copy the bytes from.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>len</code></em> :</span></p></td><td> the number of bytes to copy.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2923513"></a><h3><a name="g-memdup"></a>g_memdup ()</h3><a class="indexterm" name="id2923526"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_memdup (<a class="link" href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> mem, <a class="link" href="glib-Basic-Types.html#guint">guint</a> byte_size);</pre><p>Allocates <em class="parameter"><code>byte_size</code></em> bytes of memory, and copies <em class="parameter"><code>byte_size</code></em> bytes into itfrom <em class="parameter"><code>mem</code></em>. If <em class="parameter"><code>mem</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> it returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td><td>the memory to copy.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>byte_size</code></em> :</span></p></td><td>the number of bytes to copy.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>a pointer to the newly-allocated copy of the memory, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> if <em class="parameter"><code>mem</code></em>is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2923677"></a><h3><a name="GMemVTable"></a>GMemVTable</h3><a class="indexterm" name="id2923689"></a><pre class="programlisting">typedef struct { gpointer (*malloc) (gsize n_bytes); gpointer (*realloc) (gpointer mem, gsize n_bytes); void (*free) (gpointer mem); /* optional; set to NULL if not used ! */ gpointer (*calloc) (gsize n_blocks, gsize n_block_bytes); gpointer (*try_malloc) (gsize n_bytes); gpointer (*try_realloc) (gpointer mem, gsize n_bytes);} GMemVTable;</pre><p>A set of functions used to perform memory allocation. The same <a class="link" href="glib-Memory-Allocation.html#GMemVTable"><span class="type">GMemVTable</span></a> mustbe used for all allocations in the same program; a call to <a class="link" href="glib-Memory-Allocation.html#g-mem-set-vtable"><code class="function">g_mem_set_vtable()</code></a>,if it exists, should be prior to any use of GLib.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="structfield"><code>malloc</code></em> ()</span></p></td><td>function to use for allocating memory.</td></tr><tr><td><p><span class="term"><em class="structfield"><code>realloc</code></em> ()</span></p></td><td>function to use for reallocating memory.</td></tr><tr><td><p><span class="term"><em class="structfield"><code>free</code></em> ()</span></p></td><td>function to use to free memory.</td></tr><tr><td><p><span class="term"><em class="structfield"><code>calloc</code></em> ()</span></p></td><td>function to use for allocating zero-filled memory.</td></tr><tr><td><p><span class="term"><em class="structfield"><code>try_malloc</code></em> ()</span></p></td><td>function to use for allocating memory without a default error handler.</td></tr><tr><td><p><span class="term"><em class="structfield"><code>try_realloc</code></em> ()</span></p></td><td>function to use for reallocating memory without a default error handler.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2923823"></a><h3><a name="g-mem-set-vtable"></a>g_mem_set_vtable ()</h3><a class="indexterm" name="id2923836"></a><pre class="programlisting">void g_mem_set_vtable (<a class="link" href="glib-Memory-Allocation.html#GMemVTable">GMemVTable</a> *vtable);</pre><p>Sets the <a class="link" href="glib-Memory-Allocation.html#GMemVTable"><span class="type">GMemVTable</span></a> to use for memory allocation. You can use this to providecustom memory allocation routines. <span class="emphasis"><em>This function must be called before using any other GLib functions.</em></span> The <em class="parameter"><code>vtable</code></em> only needs to provide <code class="function">malloc()</code>, <code class="function">realloc()</code>, and <code class="function">free()</code> functions; GLib can provide default implementations of the others. The <code class="function">malloc()</code> and <code class="function">realloc()</code> implementations should return <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> on failure, GLib will handle error-checking for you. <em class="parameter"><code>vtable</code></em> is copied, so need not persist after this function has been called.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>vtable</code></em> :</span></p></td><td>table of memory allocation routines.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2923976"></a><h3><a name="g-mem-is-system-malloc"></a>g_mem_is_system_malloc ()</h3><a class="indexterm" name="id2923989"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a> g_mem_is_system_malloc (void);</pre><p>Checks whether the allocator used by <a class="link" href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a> is the system'smalloc implementation. If it returns <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS"><code class="literal">TRUE</code></a> memory allocated with<code class="function">malloc()</code> can be used interchangeable with memory allocated using <a class="link" href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a>. This function is useful for avoiding an extra copy of allocated memory returnedby a non-GLib-based API.</p><p>A different allocator can be set using <a class="link" href="glib-Memory-Allocation.html#g-mem-set-vtable"><code class="function">g_mem_set_vtable()</code></a>.</p><p></p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td> if <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS"><code class="literal">TRUE</code></a>, <code class="function">malloc()</code> and <a class="link" href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a> can be mixed.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2924119"></a><h3><a name="glib-mem-profiler-table"></a>glib_mem_profiler_table</h3><a class="indexterm" name="id2924132"></a><pre class="programlisting">extern GMemVTable *glib_mem_profiler_table;</pre><p>A <a class="link" href="glib-Memory-Allocation.html#GMemVTable"><span class="type">GMemVTable</span></a> containing profiling variants of the memoryallocation functions. Use them together with <a class="link" href="glib-Memory-Allocation.html#g-mem-profile"><code class="function">g_mem_profile()</code></a>in order to get information about the memory allocation patternof your program.</p></div><hr><div class="refsect2" lang="en"><a name="id2924169"></a><h3><a name="g-mem-profile"></a>g_mem_profile ()</h3><a class="indexterm" name="id2924182"></a><pre class="programlisting">void g_mem_profile (void);</pre><p>Outputs a summary of memory usage.</p><p>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 bytesstill in use.</p><p>Note that this function will not output anything unless you havepreviously installed the <a class="link" href="glib-Memory-Allocation.html#glib-mem-profiler-table"><span class="type">glib_mem_profiler_table</span></a> with <a class="link" href="glib-Memory-Allocation.html#g-mem-set-vtable"><code class="function">g_mem_set_vtable()</code></a>.</p></div></div><div class="refsect1" lang="en"><a name="id2924236"></a><div class="refsect2" lang="en"><a name="id2924237"></a></div><hr><div class="refsect2" lang="en"><a name="id2924238"></a></div></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -