📄 glib-memory-allocators.html
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Memory Allocators</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-data-types.html" title="GLib Data Types"><link rel="prev" href="glib-Caches.html" title="Caches"><link rel="next" href="tools.html" title="GLib Tools"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Caches.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-data-types.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="tools.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Memory-Allocators"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Memory Allocators</span></h2><p>Memory Allocators — allocates chunks of memory for <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>, <a href="glib-Singly-Linked-Lists.html#GSList"><span class="type">GSList</span></a> and <a href="glib-N-ary-Trees.html#GNode"><span class="type">GNode</span></a>.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include <glib.h> <a href="glib-Memory-Allocators.html#GAllocator">GAllocator</a>;<a href="glib-Memory-Allocators.html#GAllocator">GAllocator</a>* <a href="glib-Memory-Allocators.html#g-allocator-new">g_allocator_new</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *name, <a href="glib-Basic-Types.html#guint">guint</a> n_preallocs);void <a href="glib-Memory-Allocators.html#g-allocator-free">g_allocator_free</a> (<a href="glib-Memory-Allocators.html#GAllocator">GAllocator</a> *allocator);</pre></div><div class="refsect1" lang="en"><a name="id3206115"></a><h2>Description</h2><p>The <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a> is used as an efficient way to allocate small pieces ofmemory for use with the <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>, <a href="glib-Singly-Linked-Lists.html#GSList"><span class="type">GSList</span></a> and <a href="glib-N-ary-Trees.html#GNode"><span class="type">GNode</span></a> data structures.It uses a <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a> so elements are allocated in groups, rather thanindividually.</p><p>The <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>, <a href="glib-Singly-Linked-Lists.html#GSList"><span class="type">GSList</span></a> and <a href="glib-N-ary-Trees.html#GNode"><span class="type">GNode</span></a> implementations create default <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>objects, which are probably sufficient for most purposes. These defaultallocators use blocks of 128 elements.</p><p>To use your own <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>, create it with <a href="glib-Memory-Allocators.html#g-allocator-new"><code class="function">g_allocator_new()</code></a>. Thenuse <a href="glib-Doubly-Linked-Lists.html#g-list-push-allocator"><code class="function">g_list_push_allocator()</code></a>, <a href="glib-Singly-Linked-Lists.html#g-slist-push-allocator"><code class="function">g_slist_push_allocator()</code></a> or<a href="glib-N-ary-Trees.html#g-node-push-allocator"><code class="function">g_node_push_allocator()</code></a> before any code which allocates new <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>, <a href="glib-Singly-Linked-Lists.html#GSList"><span class="type">GSList</span></a>or <a href="glib-N-ary-Trees.html#GNode"><span class="type">GNode</span></a> elements respectively. After allocating the new elements, you mustuse <a href="glib-Doubly-Linked-Lists.html#g-list-pop-allocator"><code class="function">g_list_pop_allocator()</code></a>, <a href="glib-Singly-Linked-Lists.html#g-slist-pop-allocator"><code class="function">g_slist_pop_allocator()</code></a> or <a href="glib-N-ary-Trees.html#g-node-pop-allocator"><code class="function">g_node_pop_allocator()</code></a>to restore the previous allocators.</p><p>Note that you cannot use the same allocator for <a href="glib-Doubly-Linked-Lists.html#GList"><span class="type">GList</span></a>, <a href="glib-Singly-Linked-Lists.html#GSList"><span class="type">GSList</span></a> and <a href="glib-N-ary-Trees.html#GNode"><span class="type">GNode</span></a>elements. Each must use separate allocators.</p></div><div class="refsect1" lang="en"><a name="id3206348"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3206353"></a><h3><a name="GAllocator"></a>GAllocator</h3><a class="indexterm" name="id3206363"></a><pre class="programlisting">typedef struct _GAllocator GAllocator;</pre><p>The <span class="structname">GAllocator</span> struct contains private data. and should only be accessedusing the following functions.</p></div><hr><div class="refsect2" lang="en"><a name="id3206384"></a><h3><a name="g-allocator-new"></a>g_allocator_new ()</h3><a class="indexterm" name="id3206394"></a><pre class="programlisting"><a href="glib-Memory-Allocators.html#GAllocator">GAllocator</a>* g_allocator_new (const <a href="glib-Basic-Types.html#gchar">gchar</a> *name, <a href="glib-Basic-Types.html#guint">guint</a> n_preallocs);</pre><p>Creates a new <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>name</code></em> :</span></td><td>the name of the <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>. This name is used to set the name of the<a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a> used by the <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>, and is only used for debugging.</td></tr><tr><td><span class="term"><em class="parameter"><code>n_preallocs</code></em> :</span></td><td>the number of elements in each block of memory allocated.Larger blocks mean less calls to <a href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a>, but some memory may be wasted.(GLib uses 128 elements per block by default.) The value must be between 1and 65535.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a new <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3206527"></a><h3><a name="g-allocator-free"></a>g_allocator_free ()</h3><a class="indexterm" name="id3206537"></a><pre class="programlisting">void g_allocator_free (<a href="glib-Memory-Allocators.html#GAllocator">GAllocator</a> *allocator);</pre><p>Frees all of the memory allocated by the <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>allocator</code></em> :</span></td><td>a <a href="glib-Memory-Allocators.html#GAllocator"><span class="type">GAllocator</span></a>.</td></tr></tbody></table></div></div></div></div><table class="navigation" width="100%" summary="Navigation footer" cellpadding="2" cellspacing="0"><tr valign="middle"><td align="left"><a accesskey="p" href="glib-Caches.html"><b><< Caches</b></a></td><td align="right"><a accesskey="n" href="tools.html"><b>GLib Tools >></b></a></td></tr></table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -