📄 glib-memory-slices.html
字号:
<!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 Slices</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-data-types.html" title="GLib Data Types"><link rel="next" href="glib-Memory-Chunks.html" title="Memory Chunks"><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-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-Memory-Chunks.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="#id3229254" class="shortcut">Top</a>  |  <a href="#id3229458" class="shortcut">Description</a></nobr></td></tr></table><div class="refentry" lang="en"><a name="glib-Memory-Slices"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><a name="id3229254"></a><span class="refentrytitle">Memory Slices</span></h2><p>Memory Slices — 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 class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> <a class="link" href="glib-Memory-Slices.html#g-slice-alloc">g_slice_alloc</a> (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0">g_slice_alloc0</a> (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> <a class="link" href="glib-Memory-Slices.html#g-slice-copy">g_slice_copy</a> (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size, <a class="link" href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> mem_block);void <a class="link" href="glib-Memory-Slices.html#g-slice-free1">g_slice_free1</a> (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem_block);void <a class="link" href="glib-Memory-Slices.html#g-slice-free-chain-with-offset">g_slice_free_chain_with_offset</a> (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem_chain, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> next_offset);#define <a class="link" href="glib-Memory-Slices.html#g-slice-new">g_slice_new</a> (type)#define <a class="link" href="glib-Memory-Slices.html#g-slice-new0">g_slice_new0</a> (type)#define <a class="link" href="glib-Memory-Slices.html#g-slice-dup">g_slice_dup</a> (type, mem)#define <a class="link" href="glib-Memory-Slices.html#g-slice-free">g_slice_free</a> (type, mem)#define <a class="link" href="glib-Memory-Slices.html#g-slice-free-chain">g_slice_free_chain</a> (type, mem_chain, next)</pre></div><div class="refsect1" lang="en"><a name="id3229458"></a><h2>Description</h2><p>Memory slices provide a space-efficient and multi-processing scalableway to allocate equal-sized pieces of memory, just like the original<span class="type">GMemChunks</span> (from GLib <= 2.8), while avoiding their excessivememory-waste, scalability and performance problems.</p><p>To achieve these goals, the slice allocator uses a sophisticated, layered design that has been inspired by Bonwick's slab allocator<sup>[<a name="id3229491" href="#ftn.id3229491" class="footnote">6</a>]</sup>.It uses <code class="function">posix_memalign()</code> to optimize allocations of many equally-sized chunks, and has per-thread free lists (the so-called magazine layer) to quickly satisfy allocation requests of already known structure sizes. This is accompanied by extra caching logic to keep freed memory around for some time before returning it to the system. Memory that is unused due to alignment constraints is used for cache colorization (random distribution of chunk addresses) to improve CPU cache utilization. The caching layer of the slice allocator adapts itself to high lock contention to improve scalability.</p><p>The slice allocator can allocate blocks as small as two pointers, andunlike <code class="function">malloc()</code>, it does not reserve extra space per block. For large block sizes, <a class="link" href="glib-Memory-Slices.html#g-slice-new"><code class="function">g_slice_new()</code></a> and <a class="link" href="glib-Memory-Slices.html#g-slice-alloc"><code class="function">g_slice_alloc()</code></a> will automatically delegate to thesystem <code class="function">malloc()</code> implementation. For newly written code it is recommendedto use the new <code class="literal">g_slice</code> API instead of <a class="link" href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a> and friends, as long as objects are not resized during their lifetime and the object size used at allocation time is still available when freeing.</p><div class="example"><a name="id3229613"></a><p class="title"><b>Example 15. Using the slice allocator</b></p><div class="example-contents"><pre class="programlisting"> gchar *mem[10000]; gint i; /* Allocate 10000 blocks. */ for (i = 0; i < 10000; i++) { mem[i] = g_slice_alloc (50); /* Fill in the memory with some junk. */ for (j = 0; j < 50; j++) mem[i][j] = i * j; } /* Now free all of the blocks. */ for (i = 0; i < 10000; i++) { g_slice_free1 (50, mem[i]); }</pre></div></div><br class="example-break"><div class="example"><a name="id3229634"></a><p class="title"><b>Example 16. Using the slice allocator with data structures</b></p><div class="example-contents"><pre class="programlisting"> GRealArray *array; /* Allocate one block, using the g_slice_new() macro. */ array = g_slice_new (GRealArray); /* 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 block, so it can be reused. */ g_slice_free (GRealArray, array);</pre></div></div><br class="example-break"></div><div class="refsect1" lang="en"><a name="id3229658"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id3229668"></a><h3><a name="g-slice-alloc"></a>g_slice_alloc ()</h3><a class="indexterm" name="id3229683"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_slice_alloc (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size);</pre><p>Allocates a block of memory from the slice allocator.The block adress handed out can be expected to be alignedto at least <code class="literal">1 * sizeof (void*)</code>,though in general slices are 2 * sizeof (void*) bytes aligned,if a <code class="function">malloc()</code> fallback implementation is used instead,the alignment may be reduced in a libc dependent fashion.Note that the underlying slice allocation mechanism canbe changed with the <a class="link" href="glib-running.html#G_SLICE" title="G_SLICE">G_SLICE=always-malloc</a>environment variable.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td><td>the number of bytes to allocate</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>a pointer to the allocated memory block</td></tr></tbody></table></div><p class="since">Since 2.10</p></div><hr><div class="refsect2" lang="en"><a name="id3229779"></a><h3><a name="g-slice-alloc0"></a>g_slice_alloc0 ()</h3><a class="indexterm" name="id3229794"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_slice_alloc0 (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size);</pre><p>Allocates a block of memory via <a class="link" href="glib-Memory-Slices.html#g-slice-alloc"><code class="function">g_slice_alloc()</code></a>and initialize the returned memory to 0.Note that the underlying slice allocation mechanism canbe changed with the <a class="link" href="glib-running.html#G_SLICE" title="G_SLICE">G_SLICE=always-malloc</a>environment variable.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td><td>the number of bytes to allocate</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>a pointer to the allocated block</td></tr></tbody></table></div><p class="since">Since 2.10</p></div><hr><div class="refsect2" lang="en"><a name="id3229879"></a><h3><a name="g-slice-copy"></a>g_slice_copy ()</h3><a class="indexterm" name="id3229894"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_slice_copy (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size, <a class="link" href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> mem_block);</pre><p>Allocates a block of memory from the slice allocator and copies<em class="parameter"><code>block_size</code></em> bytes into it from <em class="parameter"><code>mem_block</code></em>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td><td>the number of bytes to allocate</td></tr><tr><td><p><span class="term"><em class="parameter"><code>mem_block</code></em> :</span></p></td><td>the memory 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 allocated memory block</td></tr></tbody></table></div><p class="since">Since 2.14</p></div><hr><div class="refsect2" lang="en"><a name="id3229995"></a><h3><a name="g-slice-free1"></a>g_slice_free1 ()</h3><a class="indexterm" name="id3230009"></a><pre class="programlisting">void g_slice_free1 (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size, <a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem_block);</pre><p>Frees a block of memory. The memory must have been allocated via<a class="link" href="glib-Memory-Slices.html#g-slice-alloc"><code class="function">g_slice_alloc()</code></a> or <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0"><code class="function">g_slice_alloc0()</code></a>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -