⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glib-memory-chunks.html

📁 最新gtk中文资料集
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Memory Chunks</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><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-Memory-Slices.html" title="Memory Slices"><link rel="next" href="glib-Doubly-Linked-Lists.html" title="Doubly-Linked Lists"><meta name="generator" content="GTK-Doc V1.9 (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"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Memory-Slices.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><tr><td colspan="5" class="shortcuts"><nobr><a href="#id3232621" class="shortcut">Top</a>                  &#160;|&#160;                  <a href="#id3232940" class="shortcut">Description</a></nobr></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><a name="id3232621"></a><span class="refentrytitle">Memory Chunks</span></h2><p>Memory Chunks &#8212; deprecated 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 &lt;glib.h&gt;                    <a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a>;#define             <a class="link" href="glib-Memory-Chunks.html#G-ALLOC-AND-FREE:CAPS">G_ALLOC_AND_FREE</a>#define             <a class="link" href="glib-Memory-Chunks.html#G-ALLOC-ONLY:CAPS">G_ALLOC_ONLY</a><a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a>*          <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-new">g_mem_chunk_new</a>                     (const <a class="link" href="glib-Basic-Types.html#gchar">gchar</a> *name,                                                         <a class="link" href="glib-Basic-Types.html#gint">gint</a> atom_size,                                                         <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> area_size,                                                         <a class="link" href="glib-Basic-Types.html#gint">gint</a> type);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-alloc">g_mem_chunk_alloc</a>                   (<a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-alloc0">g_mem_chunk_alloc0</a>                  (<a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);void                <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-free">g_mem_chunk_free</a>                    (<a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk,                                                         <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem);void                <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-destroy">g_mem_chunk_destroy</a>                 (<a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);#define             <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-create">g_mem_chunk_create</a>                  (type, pre_alloc, alloc_type)#define             <a class="link" href="glib-Memory-Chunks.html#g-chunk-new">g_chunk_new</a>                         (type, chunk)#define             <a class="link" href="glib-Memory-Chunks.html#g-chunk-new0">g_chunk_new0</a>                        (type, chunk)#define             <a class="link" href="glib-Memory-Chunks.html#g-chunk-free">g_chunk_free</a>                        (mem, mem_chunk)void                <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-reset">g_mem_chunk_reset</a>                   (<a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);void                <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-clean">g_mem_chunk_clean</a>                   (<a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);void                <a class="link" href="glib-Memory-Chunks.html#g-blow-chunks">g_blow_chunks</a>                       (void);void                <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-info">g_mem_chunk_info</a>                    (void);void                <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-print">g_mem_chunk_print</a>                   (<a class="link" href="glib-Memory-Chunks.html#GMemChunk">GMemChunk</a> *mem_chunk);</pre></div><div class="refsect1" lang="en"><a name="id3232940"></a><h2>Description</h2><p>Memory chunks provide an space-efficient way to allocate equal-sized pieces of memory, called atoms. However, due to the administrative overhead (in particular for <a class="link" href="glib-Memory-Chunks.html#G-ALLOC-AND-FREE:CAPS"><span class="type">G_ALLOC_AND_FREE</span></a>, and when used from multiple threads), they are in practise often slower than direct use of <a class="link" href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a>.Therefore, memory chunks have been deprecated in favor of the <a class="link" href="glib-Memory-Slices.html" title="Memory Slices">slice allocator</a>,which has been added in 2.10. All internal uses of memory chunks inGLib have been converted to the <code class="literal">g_slice</code> API.</p><p>There are two types of memory chunks, <a class="link" href="glib-Memory-Chunks.html#G-ALLOC-ONLY:CAPS"><span class="type">G_ALLOC_ONLY</span></a>, and <a class="link" 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 class="link" 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 class="link" 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 class="link" href="glib-Memory-Chunks.html#g-mem-chunk-new"><code class="function">g_mem_chunk_new()</code></a> or the convenience macro<a class="link" 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 class="link" href="glib-Memory-Chunks.html#g-mem-chunk-alloc"><code class="function">g_mem_chunk_alloc()</code></a>, <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-alloc0"><code class="function">g_mem_chunk_alloc0()</code></a>,or the convenience macros <a class="link" href="glib-Memory-Chunks.html#g-chunk-new"><code class="function">g_chunk_new()</code></a> or <a class="link" href="glib-Memory-Chunks.html#g-chunk-new0"><code class="function">g_chunk_new0()</code></a>. </p><p>To free an atom use <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-free"><code class="function">g_mem_chunk_free()</code></a>, or the convenience macro<a class="link" 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 class="link" 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 class="link" 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 class="link" 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 class="link" 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 class="link" 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 class="link" href="glib-Memory-Chunks.html#g-mem-chunk-info"><code class="function">g_mem_chunk_info()</code></a> and <a class="link" href="glib-Memory-Chunks.html#g-mem-chunk-print"><code class="function">g_mem_chunk_print()</code></a>.</p><div class="example"><a name="id3233236"></a><p class="title"><b>Example&#160;17.&#160;Using a <span class="structname">GMemChunk</span></b></p><div class="example-contents"><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 &lt; 10000; i++)    {      mem[i] = g_chunk_new (gchar, mem_chunk);      /* Fill in the atom memory with some junk. */      for (j = 0; j &lt; 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 &lt; 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><br class="example-break"><div class="example"><a name="id3233281"></a><p class="title"><b>Example&#160;18.&#160;Using a <span class="structname">GMemChunk</span> with data structures</b></p><div class="example-contents"><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-&gt;data            = NULL;  array-&gt;len             = 0;  array-&gt;alloc           = 0;  array-&gt;zero_terminated = (zero_terminated ? 1 : 0);  array-&gt;clear           = (clear ? 1 : 0);  array-&gt;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><br class="example-break"></div><div class="refsect1" lang="en"><a name="id3233318"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3233328"></a><h3><a name="GMemChunk"></a>GMemChunk</h3><a class="indexterm" name="id3233343"></a><pre class="programlisting">typedef struct _GMemChunk GMemChunk;</pre><div class="warning" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Warning</h3><p><code class="literal">GMemChunk</code> is deprecated and should not be used in newly-written code.</p></div><p>The <a class="link" 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="id3233381"></a><h3><a name="G-ALLOC-AND-FREE:CAPS"></a>G_ALLOC_AND_FREE</h3>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -