?? glib-memory-chunks.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 Chunks</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-data-types.html" title="GLib Data Types"><link rel="next" href="glib-Doubly-Linked-Lists.html" title="Doubly-Linked Lists"><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-data-types.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="glib-Doubly-Linked-Lists.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-Chunks"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Memory Chunks</span></h2><p>Memory Chunks — efficient way to allocate groups of equal-sized chunks of memory.</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-Chunks.html#GMemChunk">GMemChunk</a>;#define <a href="glib-Memory-Chunks.html#G-ALLOC-AND-FREE:CAPS">G_ALLOC_AND_FREE</a>#define <a href="glib-Memory-Chunks.html#G-ALLOC-ONLY:CAPS">G_ALLOC_ONLY</a><a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a>* <a href="glib-Memory-Chunks.html#g-mem-chunk-new">g_mem_chunk_new</a> (const <a href="glib-Basic-Types.html#gchar">gchar</a> *name, <a href="glib-Basic-Types.html#gint">gint</a> atom_size, <a href="glib-Basic-Types.html#gulong">gulong</a> area_size, <a href="glib-Basic-Types.html#gint">gint</a> type);<a href="glib-Basic-Types.html#gpointer">gpointer</a> <a href="glib-Memory-Chunks.html#g-mem-chunk-alloc">g_mem_chunk_alloc</a> (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);<a href="glib-Basic-Types.html#gpointer">gpointer</a> <a href="glib-Memory-Chunks.html#g-mem-chunk-alloc0">g_mem_chunk_alloc0</a> (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);void <a href="glib-Memory-Chunks.html#g-mem-chunk-free">g_mem_chunk_free</a> (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk, <a href="glib-Basic-Types.html#gpointer">gpointer</a> mem);void <a href="glib-Memory-Chunks.html#g-mem-chunk-destroy">g_mem_chunk_destroy</a> (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);#define <a href="glib-Memory-Chunks.html#g-mem-chunk-create">g_mem_chunk_create</a> (type, pre_alloc, alloc_type)#define <a href="glib-Memory-Chunks.html#g-chunk-new">g_chunk_new</a> (type, chunk)#define <a href="glib-Memory-Chunks.html#g-chunk-new0">g_chunk_new0</a> (type, chunk)#define <a href="glib-Memory-Chunks.html#g-chunk-free">g_chunk_free</a> (mem, mem_chunk)void <a href="glib-Memory-Chunks.html#g-mem-chunk-reset">g_mem_chunk_reset</a> (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);void <a href="glib-Memory-Chunks.html#g-mem-chunk-clean">g_mem_chunk_clean</a> (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);void <a href="glib-Memory-Chunks.html#g-blow-chunks">g_blow_chunks</a> (void);void <a href="glib-Memory-Chunks.html#g-mem-chunk-info">g_mem_chunk_info</a> (void);void <a href="glib-Memory-Chunks.html#g-mem-chunk-print">g_mem_chunk_print</a> (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);</pre></div><div class="refsect1" lang="en"><a name="id3100773"></a><h2>Description</h2><p>Memory chunks provide an efficient way to allocate equal-sized pieces ofmemory, called atoms. They are used extensively within GLib itself.For example, theDoubly Linked Listsuse memory chunks to allocate space for elements of the lists.</p><p>There are two types of memory chunks, <a href="glib-Memory-Chunks.html#G-ALLOC-ONLY:CAPS"><span class="type">G_ALLOC_ONLY</span></a>, and <a href="glib-Memory-Chunks.html#G-ALLOC-AND-FREE:CAPS"><span class="type">G_ALLOC_AND_FREE</span></a>.</p><div class="itemizedlist"><ul type="disc"><li><p><a href="glib-Memory-Chunks.html#G-ALLOC-ONLY:CAPS"><span class="type">G_ALLOC_ONLY</span></a> chunks only allow allocation of atoms. The atoms can neverbe freed individually. The memory chunk can only be free in its entirety.</p></li><li><p><a href="glib-Memory-Chunks.html#G-ALLOC-AND-FREE:CAPS"><span class="type">G_ALLOC_AND_FREE</span></a> chunks do allow atoms to be freed individually.The disadvantage of this is that the memory chunk has to keep track of whichatoms have been freed. This results in more memory being used and a slightdegradation in performance.</p></li></ul></div><p></p><p>To create a memory chunk use <a href="glib-Memory-Chunks.html#g-mem-chunk-new"><code class="function">g_mem_chunk_new()</code></a> or the convenience macro<a href="glib-Memory-Chunks.html#g-mem-chunk-create"><code class="function">g_mem_chunk_create()</code></a>.</p><p>To allocate a new atom use <a href="glib-Memory-Chunks.html#g-mem-chunk-alloc"><code class="function">g_mem_chunk_alloc()</code></a>, <a href="glib-Memory-Chunks.html#g-mem-chunk-alloc0"><code class="function">g_mem_chunk_alloc0()</code></a>,or the convenience macros <a href="glib-Memory-Chunks.html#g-chunk-new"><code class="function">g_chunk_new()</code></a> or <a href="glib-Memory-Chunks.html#g-chunk-new0"><code class="function">g_chunk_new0()</code></a>. </p><p>To free an atom use <a href="glib-Memory-Chunks.html#g-mem-chunk-free"><code class="function">g_mem_chunk_free()</code></a>, or the convenience macro<a href="glib-Memory-Chunks.html#g-chunk-free"><code class="function">g_chunk_free()</code></a>. (Atoms can only be freed if the memory chunk is createdwith the type set to <a href="glib-Memory-Chunks.html#G-ALLOC-AND-FREE:CAPS"><span class="type">G_ALLOC_AND_FREE</span></a>.)</p><p>To free any blocks of memory which are no longer being used, use<a href="glib-Memory-Chunks.html#g-mem-chunk-clean"><code class="function">g_mem_chunk_clean()</code></a>. To clean all memory chunks, use <a href="glib-Memory-Chunks.html#g-blow-chunks"><code class="function">g_blow_chunks()</code></a>.</p><p>To reset the memory chunk, freeing all of the atoms, use <a href="glib-Memory-Chunks.html#g-mem-chunk-reset"><code class="function">g_mem_chunk_reset()</code></a>.</p><p>To destroy a memory chunk, use <a href="glib-Memory-Chunks.html#g-mem-chunk-destroy"><code class="function">g_mem_chunk_destroy()</code></a>.</p><p>To help debug memory chunks, use <a href="glib-Memory-Chunks.html#g-mem-chunk-info"><code class="function">g_mem_chunk_info()</code></a> and <a href="glib-Memory-Chunks.html#g-mem-chunk-print"><code class="function">g_mem_chunk_print()</code></a>.</p><div class="example"><a name="id3101036"></a><p class="title"><b>Example 1. Using a <span class="structname">GMemChunk</span></b></p><pre class="programlisting"> GMemChunk *mem_chunk; gchar *mem[10000]; gint i; /* Create a GMemChunk with atoms 50 bytes long, and memory blocks holding 100 bytes. Note that this means that only 2 atoms fit into each memory block and so isn't very efficient. */ mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE); /* Now allocate 10000 atoms. */ for (i = 0; i < 10000; i++) { mem[i] = g_chunk_new (gchar, mem_chunk); /* Fill in the atom memory with some junk. */ for (j = 0; j < 50; j++) mem[i][j] = i * j; } /* Now free all of the atoms. Note that since we are going to destroy the GMemChunk, this wouldn't normally be used. */ for (i = 0; i < 10000; i++) { g_mem_chunk_free (mem_chunk, mem[i]); } /* We are finished with the GMemChunk, so we destroy it. */ g_mem_chunk_destroy (mem_chunk);</pre></div><div class="example"><a name="id3101080"></a><p class="title"><b>Example 2. Using a <span class="structname">GMemChunk</span> with data structures</b></p><pre class="programlisting"> GMemChunk *array_mem_chunk; GRealArray *array; /* Create a GMemChunk to hold GRealArray structures, using the g_mem_chunk_create() convenience macro. We want 1024 atoms in each memory block, and we want to be able to free individual atoms. */ array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE); /* Allocate one atom, using the g_chunk_new() convenience macro. */ array = g_chunk_new (GRealArray, array_mem_chunk); /* We can now use array just like a normal pointer to a structure. */ array->data = NULL; array->len = 0; array->alloc = 0; array->zero_terminated = (zero_terminated ? 1 : 0); array->clear = (clear ? 1 : 0); array->elt_size = elt_size; /* We can free the element, so it can be reused. */ g_chunk_free (array, array_mem_chunk); /* We destroy the GMemChunk when we are finished with it. */ g_mem_chunk_destroy (array_mem_chunk);</pre></div></div><div class="refsect1" lang="en"><a name="id3100322"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3100327"></a><h3><a name="GMemChunk"></a>GMemChunk</h3><a class="indexterm" name="id3100337"></a><pre class="programlisting">typedef struct _GMemChunk GMemChunk;</pre><p>The <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a> struct is an opaque data structure representing a memorychunk. It should be accessed only through the use of the following functions.</p></div><hr><div class="refsect2" lang="en"><a name="id3100363"></a><h3><a name="G-ALLOC-AND-FREE:CAPS"></a>G_ALLOC_AND_FREE</h3><a class="indexterm" name="id3100374"></a><pre class="programlisting">#define G_ALLOC_AND_FREE 2</pre><p>Specifies the type of a <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>.Used in <a href="glib-Memory-Chunks.html#g-mem-chunk-new"><code class="function">g_mem_chunk_new()</code></a> and <a href="glib-Memory-Chunks.html#g-mem-chunk-create"><code class="function">g_mem_chunk_create()</code></a> to specify that atomswill be freed individually.</p></div><hr><div class="refsect2" lang="en"><a name="id3100420"></a><h3><a name="G-ALLOC-ONLY:CAPS"></a>G_ALLOC_ONLY</h3><a class="indexterm" name="id3100430"></a><pre class="programlisting">#define G_ALLOC_ONLY 1</pre><p>Specifies the type of a <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>.Used in <a href="glib-Memory-Chunks.html#g-mem-chunk-new"><code class="function">g_mem_chunk_new()</code></a> and <a href="glib-Memory-Chunks.html#g-mem-chunk-create"><code class="function">g_mem_chunk_create()</code></a> to specify that atomswill never be freed individually.</p></div><hr><div class="refsect2" lang="en"><a name="id3101409"></a><h3><a name="g-mem-chunk-new"></a>g_mem_chunk_new ()</h3><a class="indexterm" name="id3101419"></a><pre class="programlisting"><a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a>* g_mem_chunk_new (const <a href="glib-Basic-Types.html#gchar">gchar</a> *name, <a href="glib-Basic-Types.html#gint">gint</a> atom_size, <a href="glib-Basic-Types.html#gulong">gulong</a> area_size, <a href="glib-Basic-Types.html#gint">gint</a> type);</pre><p>Creates a new <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</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>a string to identify the <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>. It is not copied so itshould be valid for the lifetime of the <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>. It is only used in<a href="glib-Memory-Chunks.html#g-mem-chunk-print"><code class="function">g_mem_chunk_print()</code></a>, which is used for debugging.</td></tr><tr><td><span class="term"><em class="parameter"><code>atom_size</code></em> :</span></td><td>the size, in bytes, of each element in the <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>.</td></tr><tr><td><span class="term"><em class="parameter"><code>area_size</code></em> :</span></td><td>the size, in bytes, of each block of memory allocated to containthe atoms.</td></tr><tr><td><span class="term"><em class="parameter"><code>type</code></em> :</span></td><td>the type of the <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>.<a href="glib-Memory-Chunks.html#G-ALLOC-AND-FREE:CAPS"><span class="type">G_ALLOC_AND_FREE</span></a> is used if the atoms will be freed individually.<a href="glib-Memory-Chunks.html#G-ALLOC-ONLY:CAPS"><span class="type">G_ALLOC_ONLY</span></a> should be used if atoms will never be freed individually.<a href="glib-Memory-Chunks.html#G-ALLOC-ONLY:CAPS"><span class="type">G_ALLOC_ONLY</span></a> is quicker, since it does not need to track free atoms,but it obviously wastes memory if you no longer need many of the atoms.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>the new <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3101632"></a><h3><a name="g-mem-chunk-alloc"></a>g_mem_chunk_alloc ()</h3><a class="indexterm" name="id3101642"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a> g_mem_chunk_alloc (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);</pre><p>Allocates an atom of memory from a <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>mem_chunk</code></em> :</span></td><td>a <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></td><td>a pointer to the allocated atom.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id3101715"></a><h3><a name="g-mem-chunk-alloc0"></a>g_mem_chunk_alloc0 ()</h3><a class="indexterm" name="id3101726"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a> g_mem_chunk_alloc0 (<a href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);</pre><p>Allocates an atom of memory from a <a href="glib-Memory-Chunks.html#GMemChunk"><span class="type">GMemChunk</span></a>, setting the memory to 0.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -