1
1
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
4
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5
5
<title>Memory Slices</title>
6
<meta name="generator" content="DocBook XSL Stylesheets V1.73.2">
7
<link rel="start" href="index.html" title="GLib Reference Manual">
6
<meta name="generator" content="DocBook XSL Stylesheets V1.74.0">
7
<link rel="home" href="index.html" title="GLib Reference Manual">
8
8
<link rel="up" href="glib-data-types.html" title="GLib Data Types">
9
9
<link rel="prev" href="glib-data-types.html" title="GLib Data Types">
10
10
<link rel="next" href="glib-Memory-Chunks.html" title="Memory Chunks">
11
<meta name="generator" content="GTK-Doc V1.9 (XML mode)">
11
<meta name="generator" content="GTK-Doc V1.11 (XML mode)">
12
12
<link rel="stylesheet" href="style.css" type="text/css">
13
13
<link rel="chapter" href="glib.html" title="GLib Overview">
14
14
<link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals">
36
37
<th width="100%" align="center">GLib Reference Manual</th>
37
38
<td><a accesskey="n" href="glib-Memory-Chunks.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td>
39
<tr><td colspan="5" class="shortcuts"><nobr><a href="#id3286227" class="shortcut">Top</a>
41
<a href="#id3286431" class="shortcut">Description</a></nobr></td></tr>
40
<tr><td colspan="5" class="shortcuts">
41
<a href="#glib-Memory-Slices.synopsis" class="shortcut">Top</a>
43
<a href="#glib-Memory-Slices.description" class="shortcut">Description</a>
43
46
<div class="refentry" lang="en">
44
47
<a name="glib-Memory-Slices"></a><div class="titlepage"></div>
45
48
<div class="refnamediv"><table width="100%"><tr>
48
<a name="id3286227"></a><span class="refentrytitle">Memory Slices</span>
50
<p>Memory Slices — efficient way to allocate groups of equal-sized chunks of memory</p>
50
<h2><span class="refentrytitle"><a name="glib-Memory-Slices.top_of_page"></a>Memory Slices</span></h2>
51
<p>Memory Slices — efficient way to allocate groups of equal-sized chunks of memory</p>
52
53
<td valign="top" align="right"></td>
53
54
</tr></table></div>
54
55
<div class="refsynopsisdiv">
56
<a name="glib-Memory-Slices.synopsis"></a><h2>Synopsis</h2>
56
57
<pre class="synopsis">
58
59
#include <glib.h>
61
<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);
62
<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);
63
<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,
64
<a class="link" href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> mem_block);
65
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,
66
<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem_block);
67
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,
68
<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem_chain,
69
<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> next_offset);
71
#define <a class="link" href="glib-Memory-Slices.html#g-slice-new">g_slice_new</a> (type)
72
#define <a class="link" href="glib-Memory-Slices.html#g-slice-new0">g_slice_new0</a> (type)
73
#define <a class="link" href="glib-Memory-Slices.html#g-slice-dup">g_slice_dup</a> (type, mem)
74
#define <a class="link" href="glib-Memory-Slices.html#g-slice-free">g_slice_free</a> (type, mem)
75
#define <a class="link" href="glib-Memory-Slices.html#g-slice-free-chain">g_slice_free_chain</a> (type, mem_chain, next)
61
<a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> <a class="link" href="glib-Memory-Slices.html#g-slice-alloc" title="g_slice_alloc ()">g_slice_alloc</a> (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size);
62
<a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0" title="g_slice_alloc0 ()">g_slice_alloc0</a> (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size);
63
<a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> <a class="link" href="glib-Memory-Slices.html#g-slice-copy" title="g_slice_copy ()">g_slice_copy</a> (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size,
64
<a class="link" href="glib-Basic-Types.html#gconstpointer" title="gconstpointer">gconstpointer</a> mem_block);
65
void <a class="link" href="glib-Memory-Slices.html#g-slice-free1" title="g_slice_free1 ()">g_slice_free1</a> (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size,
66
<a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> mem_block);
67
void <a class="link" href="glib-Memory-Slices.html#g-slice-free-chain-with-offset" title="g_slice_free_chain_with_offset ()">g_slice_free_chain_with_offset</a> (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size,
68
<a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> mem_chain,
69
<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> next_offset);
71
#define <a class="link" href="glib-Memory-Slices.html#g-slice-new" title="g_slice_new()">g_slice_new</a> (type)
72
#define <a class="link" href="glib-Memory-Slices.html#g-slice-new0" title="g_slice_new0()">g_slice_new0</a> (type)
73
#define <a class="link" href="glib-Memory-Slices.html#g-slice-dup" title="g_slice_dup()">g_slice_dup</a> (type, mem)
74
#define <a class="link" href="glib-Memory-Slices.html#g-slice-free" title="g_slice_free()">g_slice_free</a> (type, mem)
75
#define <a class="link" href="glib-Memory-Slices.html#g-slice-free-chain" title="g_slice_free_chain()">g_slice_free_chain</a> (type, mem_chain, next)
79
78
<div class="refsect1" lang="en">
80
<a name="id3286431"></a><h2>Description</h2>
79
<a name="glib-Memory-Slices.description"></a><h2>Description</h2>
82
81
Memory slices provide a space-efficient and multi-processing scalable
83
82
way to allocate equal-sized pieces of memory, just like the original
85
84
memory-waste, scalability and performance problems.
88
To achieve these goals, the slice allocator uses a sophisticated,
87
To achieve these goals, the slice allocator uses a sophisticated,
89
88
layered design that has been inspired by Bonwick's slab allocator
90
<sup>[<a name="id3286464" href="#ftn.id3286464" class="footnote">6</a>]</sup>.
91
It uses <code class="function">posix_memalign()</code> to optimize allocations of many equally-sized
92
chunks, and has per-thread free lists (the so-called magazine layer)
93
to quickly satisfy allocation requests of already known structure sizes.
94
This is accompanied by extra caching logic to keep freed memory around
95
for some time before returning it to the system. Memory that is unused
96
due to alignment constraints is used for cache colorization (random
97
distribution of chunk addresses) to improve CPU cache utilization. The
98
caching layer of the slice allocator adapts itself to high lock contention
89
<sup>[<a name="id3337918" href="#ftn.id3337918" class="footnote">6</a>]</sup>.
90
It uses <code class="function">posix_memalign()</code> to optimize allocations of many equally-sized
91
chunks, and has per-thread free lists (the so-called magazine layer)
92
to quickly satisfy allocation requests of already known structure sizes.
93
This is accompanied by extra caching logic to keep freed memory around
94
for some time before returning it to the system. Memory that is unused
95
due to alignment constraints is used for cache colorization (random
96
distribution of chunk addresses) to improve CPU cache utilization. The
97
caching layer of the slice allocator adapts itself to high lock contention
99
98
to improve scalability.
102
101
The slice allocator can allocate blocks as small as two pointers, and
103
unlike <code class="function">malloc()</code>, it does not reserve extra space per block. For large block
104
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 the
102
unlike <code class="function">malloc()</code>, it does not reserve extra space per block. For large block
103
sizes, <a class="link" href="glib-Memory-Slices.html#g-slice-new" title="g_slice_new()"><code class="function">g_slice_new()</code></a> and <a class="link" href="glib-Memory-Slices.html#g-slice-alloc" title="g_slice_alloc ()"><code class="function">g_slice_alloc()</code></a> will automatically delegate to the
105
104
system <code class="function">malloc()</code> implementation. For newly written code it is recommended
106
to 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
107
friends, as long as objects are not resized during their lifetime and the
105
to use the new <code class="literal">g_slice</code> API instead of <a class="link" href="glib-Memory-Allocation.html#g-malloc" title="g_malloc ()"><code class="function">g_malloc()</code></a> and
106
friends, as long as objects are not resized during their lifetime and the
108
107
object size used at allocation time is still available when freeing.
110
109
<div class="example">
111
<a name="id3286586"></a><p class="title"><b>Example 15. Using the slice allocator</b></p>
110
<a name="id3338038"></a><p class="title"><b>Example 15. Using the slice allocator</b></p>
112
111
<div class="example-contents"><pre class="programlisting">
113
112
gchar *mem[10000];
116
114
/* Allocate 10000 blocks. */
117
115
for (i = 0; i < 10000; i++)
119
117
mem[i] = g_slice_alloc (50);
121
118
/* Fill in the memory with some junk. */
122
119
for (j = 0; j < 50; j++)
123
120
mem[i][j] = i * j;
126
122
/* Now free all of the blocks. */
127
123
for (i = 0; i < 10000; i++)
153
146
<br class="example-break">
155
148
<div class="refsect1" lang="en">
156
<a name="id3286631"></a><h2>Details</h2>
149
<a name="glib-Memory-Slices.details"></a><h2>Details</h2>
157
150
<div class="refsect2" lang="en">
158
<a name="id3286641"></a><h3>
159
<a name="g-slice-alloc"></a>g_slice_alloc ()</h3>
160
<a class="indexterm" name="id3286656"></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>
151
<a name="g-slice-alloc"></a><h3>g_slice_alloc ()</h3>
152
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> g_slice_alloc (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size);</pre>
162
154
Allocates a block of memory from the slice allocator.
163
155
The block adress handed out can be expected to be aligned
173
165
<col align="left" valign="top">
176
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
168
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
177
169
<td>the number of bytes to allocate
181
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
173
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
182
174
<td>a pointer to the allocated memory block
187
<p class="since">Since 2.10
179
<p class="since">Since 2.10</p>
193
182
<div class="refsect2" lang="en">
194
<a name="id3286752"></a><h3>
195
<a name="g-slice-alloc0"></a>g_slice_alloc0 ()</h3>
196
<a class="indexterm" name="id3286767"></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>
183
<a name="g-slice-alloc0"></a><h3>g_slice_alloc0 ()</h3>
184
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> g_slice_alloc0 (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size);</pre>
198
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>
186
Allocates a block of memory via <a class="link" href="glib-Memory-Slices.html#g-slice-alloc" title="g_slice_alloc ()"><code class="function">g_slice_alloc()</code></a>
199
187
and initialize the returned memory to 0.
200
188
Note that the underlying slice allocation mechanism can
201
189
be changed with the <a class="link" href="glib-running.html#G_SLICE" title="G_SLICE">G_SLICE=always-malloc</a>
205
193
<col align="left" valign="top">
208
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
196
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
209
197
<td>the number of bytes to allocate
213
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
201
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
214
202
<td>a pointer to the allocated block
219
<p class="since">Since 2.10
207
<p class="since">Since 2.10</p>
225
210
<div class="refsect2" lang="en">
226
<a name="id3286852"></a><h3>
227
<a name="g-slice-copy"></a>g_slice_copy ()</h3>
228
<a class="indexterm" name="id3286868"></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,
229
<a class="link" href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> mem_block);</pre>
211
<a name="g-slice-copy"></a><h3>g_slice_copy ()</h3>
212
<pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> g_slice_copy (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size,
213
<a class="link" href="glib-Basic-Types.html#gconstpointer" title="gconstpointer">gconstpointer</a> mem_block);</pre>
231
215
Allocates a block of memory from the slice allocator and copies
232
216
<em class="parameter"><code>block_size</code></em> bytes into it from <em class="parameter"><code>mem_block</code></em>.
235
219
<col align="left" valign="top">
238
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
222
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
239
223
<td>the number of bytes to allocate
243
<td><p><span class="term"><em class="parameter"><code>mem_block</code></em> :</span></p></td>
227
<td><p><span class="term"><em class="parameter"><code>mem_block</code></em> :</span></p></td>
244
228
<td>the memory to copy
248
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
232
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
249
233
<td>a pointer to the allocated memory block
254
<p class="since">Since 2.14
238
<p class="since">Since 2.14</p>
260
241
<div class="refsect2" lang="en">
261
<a name="id3286968"></a><h3>
262
<a name="g-slice-free1"></a>g_slice_free1 ()</h3>
263
<a class="indexterm" name="id3286982"></a><pre class="programlisting">void g_slice_free1 (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size,
264
<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem_block);</pre>
242
<a name="g-slice-free1"></a><h3>g_slice_free1 ()</h3>
243
<pre class="programlisting">void g_slice_free1 (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size,
244
<a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> mem_block);</pre>
266
246
Frees a block of memory. The memory must have been allocated via
267
<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>
247
<a class="link" href="glib-Memory-Slices.html#g-slice-alloc" title="g_slice_alloc ()"><code class="function">g_slice_alloc()</code></a> or <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0" title="g_slice_alloc0 ()"><code class="function">g_slice_alloc0()</code></a>
268
248
and the <em class="parameter"><code>block_size</code></em> has to match the size specified upon allocation.
269
249
Note that the exact release behaviour can be changed with the
270
250
<a class="link" href="glib-running.html#G_DEBUG" title="G_DEBUG">G_DEBUG=gc-friendly</a> environment variable,
274
254
<col align="left" valign="top">
277
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
257
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
278
258
<td>the size of the block
282
<td><p><span class="term"><em class="parameter"><code>mem_block</code></em> :</span></p></td>
262
<td><p><span class="term"><em class="parameter"><code>mem_block</code></em> :</span></p></td>
283
263
<td>a pointer to the block to free
288
<p class="since">Since 2.10
268
<p class="since">Since 2.10</p>
294
271
<div class="refsect2" lang="en">
295
<a name="id3287104"></a><h3>
296
<a name="g-slice-free-chain-with-offset"></a>g_slice_free_chain_with_offset ()</h3>
297
<a class="indexterm" name="id3287119"></a><pre class="programlisting">void g_slice_free_chain_with_offset (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> block_size,
298
<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem_chain,
299
<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> next_offset);</pre>
272
<a name="g-slice-free-chain-with-offset"></a><h3>g_slice_free_chain_with_offset ()</h3>
273
<pre class="programlisting">void g_slice_free_chain_with_offset (<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> block_size,
274
<a class="link" href="glib-Basic-Types.html#gpointer" title="gpointer">gpointer</a> mem_chain,
275
<a class="link" href="glib-Basic-Types.html#gsize" title="gsize">gsize</a> next_offset);</pre>
301
277
Frees a linked list of memory blocks of structure type <em class="parameter"><code>type</code></em>.
302
278
The memory blocks must be equal-sized, allocated via
303
<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>
304
and linked together by a <em class="parameter"><code>next</code></em> pointer (similar to <a class="link" href="glib-Singly-Linked-Lists.html#GSList"><span class="type">GSList</span></a>). The offset
279
<a class="link" href="glib-Memory-Slices.html#g-slice-alloc" title="g_slice_alloc ()"><code class="function">g_slice_alloc()</code></a> or <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0" title="g_slice_alloc0 ()"><code class="function">g_slice_alloc0()</code></a>
280
and linked together by a <em class="parameter"><code>next</code></em> pointer (similar to <a class="link" href="glib-Singly-Linked-Lists.html#GSList" title="GSList"><span class="type">GSList</span></a>). The offset
305
281
of the <em class="parameter"><code>next</code></em> field in each block is passed as third argument.
306
282
Note that the exact release behaviour can be changed with the
307
283
<a class="link" href="glib-running.html#G_DEBUG" title="G_DEBUG">G_DEBUG=gc-friendly</a> environment variable,
311
287
<col align="left" valign="top">
314
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
290
<td><p><span class="term"><em class="parameter"><code>block_size</code></em> :</span></p></td>
315
291
<td>the size of the blocks
319
<td><p><span class="term"><em class="parameter"><code>mem_chain</code></em> :</span></p></td>
295
<td><p><span class="term"><em class="parameter"><code>mem_chain</code></em> :</span></p></td>
320
296
<td> a pointer to the first block of the chain
324
<td><p><span class="term"><em class="parameter"><code>next_offset</code></em> :</span></p></td>
300
<td><p><span class="term"><em class="parameter"><code>next_offset</code></em> :</span></p></td>
325
301
<td>the offset of the <em class="parameter"><code>next</code></em> field in the blocks
330
<p class="since">Since 2.10
306
<p class="since">Since 2.10</p>
336
309
<div class="refsect2" lang="en">
337
<a name="id3287290"></a><h3>
338
<a name="g-slice-new"></a>g_slice_new()</h3>
339
<a class="indexterm" name="id3287305"></a><pre class="programlisting">#define g_slice_new(type)</pre>
310
<a name="g-slice-new"></a><h3>g_slice_new()</h3>
311
<pre class="programlisting">#define g_slice_new(type)</pre>
341
313
A convenience macro to allocate a block of memory from the slice allocator.
342
It calls <a class="link" href="glib-Memory-Slices.html#g-slice-alloc"><code class="function">g_slice_alloc()</code></a> with <code class="literal">sizeof (<em class="parameter"><code>type</code></em>)</code> and casts
343
the returned pointer to a pointer of the given type, avoiding a type cast
314
It calls <a class="link" href="glib-Memory-Slices.html#g-slice-alloc" title="g_slice_alloc ()"><code class="function">g_slice_alloc()</code></a> with <code class="literal">sizeof (<em class="parameter"><code>type</code></em>)</code> and casts
315
the returned pointer to a pointer of the given type, avoiding a type cast
344
316
in the source code.
345
317
Note that the underlying slice allocation mechanism can
346
318
be changed with the <a class="link" href="glib-running.html#G_SLICE" title="G_SLICE">G_SLICE=always-malloc</a>
350
322
<col align="left" valign="top">
353
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
325
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
354
326
<td>the type to allocate, typically a structure name
358
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
330
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
359
331
<td>a pointer to the allocated block, cast to a pointer to <em class="parameter"><code>type</code></em>.
364
<p class="since">Since 2.10
336
<p class="since">Since 2.10</p>
370
339
<div class="refsect2" lang="en">
371
<a name="id3287398"></a><h3>
372
<a name="g-slice-new0"></a>g_slice_new0()</h3>
373
<a class="indexterm" name="id3287413"></a><pre class="programlisting">#define g_slice_new0(type)</pre>
340
<a name="g-slice-new0"></a><h3>g_slice_new0()</h3>
341
<pre class="programlisting">#define g_slice_new0(type)</pre>
375
343
A convenience macro to allocate a block of memory from the slice allocator
376
and set the memory to 0. It calls <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0"><code class="function">g_slice_alloc0()</code></a> with
377
<code class="literal">sizeof (<em class="parameter"><code>type</code></em>)</code> and casts the returned pointer to a pointer
344
and set the memory to 0. It calls <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0" title="g_slice_alloc0 ()"><code class="function">g_slice_alloc0()</code></a> with
345
<code class="literal">sizeof (<em class="parameter"><code>type</code></em>)</code> and casts the returned pointer to a pointer
378
346
of the given type, avoiding a type cast in the source code.
379
347
Note that the underlying slice allocation mechanism can
380
348
be changed with the <a class="link" href="glib-running.html#G_SLICE" title="G_SLICE">G_SLICE=always-malloc</a>
384
352
<col align="left" valign="top">
387
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
355
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
388
356
<td>the type to allocate, typically a structure name
392
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
360
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
393
361
<td>a pointer to the allocated block, cast to a pointer to <em class="parameter"><code>type</code></em>.
398
<p class="since">Since 2.10
366
<p class="since">Since 2.10</p>
404
369
<div class="refsect2" lang="en">
405
<a name="id3287506"></a><h3>
406
<a name="g-slice-dup"></a>g_slice_dup()</h3>
407
<a class="indexterm" name="id3287520"></a><pre class="programlisting">#define g_slice_dup(type, mem)</pre>
370
<a name="g-slice-dup"></a><h3>g_slice_dup()</h3>
371
<pre class="programlisting">#define g_slice_dup(type, mem)</pre>
409
373
A convenience macro to duplicate a block of memory using the slice allocator.
410
It calls <a class="link" href="glib-Memory-Slices.html#g-slice-copy"><code class="function">g_slice_copy()</code></a> with <code class="literal">sizeof (<em class="parameter"><code>type</code></em>)</code> and casts
411
the returned pointer to a pointer of the given type, avoiding a type cast
374
It calls <a class="link" href="glib-Memory-Slices.html#g-slice-copy" title="g_slice_copy ()"><code class="function">g_slice_copy()</code></a> with <code class="literal">sizeof (<em class="parameter"><code>type</code></em>)</code> and casts
375
the returned pointer to a pointer of the given type, avoiding a type cast
412
376
in the source code.
413
377
Note that the underlying slice allocation mechanism can
414
378
be changed with the <a class="link" href="glib-running.html#G_SLICE" title="G_SLICE">G_SLICE=always-malloc</a>
418
382
<col align="left" valign="top">
421
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
385
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
422
386
<td>the type to duplicate, typically a structure name
426
<td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td>
390
<td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td>
427
391
<td>the memory to copy into the allocated block
431
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
395
<td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td>
432
396
<td>a pointer to the allocated block, cast to a pointer to <em class="parameter"><code>type</code></em>.
437
<p class="since">Since 2.14
401
<p class="since">Since 2.14</p>
443
404
<div class="refsect2" lang="en">
444
<a name="id3287627"></a><h3>
445
<a name="g-slice-free"></a>g_slice_free()</h3>
446
<a class="indexterm" name="id3287642"></a><pre class="programlisting">#define g_slice_free(type, mem)</pre>
405
<a name="g-slice-free"></a><h3>g_slice_free()</h3>
406
<pre class="programlisting">#define g_slice_free(type, mem)</pre>
448
408
A convenience macro to free a block of memory that has been allocated
449
from the slice allocator. It calls <a class="link" href="glib-Memory-Slices.html#g-slice-free1"><code class="function">g_slice_free1()</code></a> using
409
from the slice allocator. It calls <a class="link" href="glib-Memory-Slices.html#g-slice-free1" title="g_slice_free1 ()"><code class="function">g_slice_free1()</code></a> using
450
410
<code class="literal">sizeof (type)</code> as the block size.
451
411
Note that the exact release behaviour can be changed with the
452
412
<a class="link" href="glib-running.html#G_DEBUG" title="G_DEBUG">G_DEBUG=gc-friendly</a> environment variable,
456
416
<col align="left" valign="top">
459
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
419
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
460
420
<td>the type of the block to free, typically a structure name
464
<td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td>
424
<td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td>
465
425
<td>a pointer to the block to free
470
<p class="since">Since 2.10
430
<p class="since">Since 2.10</p>
476
433
<div class="refsect2" lang="en">
477
<a name="id3287732"></a><h3>
478
<a name="g-slice-free-chain"></a>g_slice_free_chain()</h3>
479
<a class="indexterm" name="id3287747"></a><pre class="programlisting">#define g_slice_free_chain(type, mem_chain, next)</pre>
434
<a name="g-slice-free-chain"></a><h3>g_slice_free_chain()</h3>
435
<pre class="programlisting">#define g_slice_free_chain(type, mem_chain, next)</pre>
481
437
Frees a linked list of memory blocks of structure type <em class="parameter"><code>type</code></em>.
482
438
The memory blocks must be equal-sized, allocated via
483
<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> and linked together by a
484
<em class="parameter"><code>next</code></em> pointer (similar to <a class="link" href="glib-Singly-Linked-Lists.html#GSList"><span class="type">GSList</span></a>). The name of the
439
<a class="link" href="glib-Memory-Slices.html#g-slice-alloc" title="g_slice_alloc ()"><code class="function">g_slice_alloc()</code></a> or <a class="link" href="glib-Memory-Slices.html#g-slice-alloc0" title="g_slice_alloc0 ()"><code class="function">g_slice_alloc0()</code></a> and linked together by a
440
<em class="parameter"><code>next</code></em> pointer (similar to <a class="link" href="glib-Singly-Linked-Lists.html#GSList" title="GSList"><span class="type">GSList</span></a>). The name of the
485
441
<em class="parameter"><code>next</code></em> field in <em class="parameter"><code>type</code></em> is passed as third argument.
486
442
Note that the exact release behaviour can be changed with the
487
443
<a class="link" href="glib-running.html#G_DEBUG" title="G_DEBUG">G_DEBUG=gc-friendly</a> environment variable,
491
447
<col align="left" valign="top">
494
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
450
<td><p><span class="term"><em class="parameter"><code>type</code></em> :</span></p></td>
495
451
<td> the type of the <em class="parameter"><code>mem_chain</code></em> blocks
499
<td><p><span class="term"><em class="parameter"><code>mem_chain</code></em> :</span></p></td>
455
<td><p><span class="term"><em class="parameter"><code>mem_chain</code></em> :</span></p></td>
500
456
<td> a pointer to the first block of the chain
504
<td><p><span class="term"><em class="parameter"><code>next</code></em> :</span></p></td>
460
<td><p><span class="term"><em class="parameter"><code>next</code></em> :</span></p></td>
505
461
<td> the field name of the next pointer in <em class="parameter"><code>type</code></em>
510
<p class="since">Since 2.10
516
<div class="refsect1" lang="en">
517
<a name="id3287902"></a><div class="refsect2" lang="en"><a name="id3287903"></a></div>
519
<div class="refsect2" lang="en"><a name="id3287904"></a></div>
466
<p class="since">Since 2.10</p>
521
469
<div class="footnotes">
522
470
<br><hr width="100" align="left">
523
<div class="footnote"><p><sup>[<a name="ftn.id3286464" href="#id3286464" class="para">6</a>] </sup>
471
<div class="footnote"><p><sup>[<a name="ftn.id3337918" href="#id3337918" class="para">6</a>] </sup>
524
472
<a class="ulink" href="http://citeseer.ist.psu.edu/bonwick94slab.html" target="_top">[Bonwick94]</a> Jeff Bonwick, The slab allocator: An object-caching kernel
525
memory allocator. USENIX 1994, and
473
memory allocator. USENIX 1994, and
526
474
<a class="ulink" href="http://citeseer.ist.psu.edu/bonwick01magazines.html" target="_top">[Bonwick01]</a> Bonwick and Jonathan Adams, Magazines and vmem: Extending the
527
475
slab allocator to many cpu's and arbitrary resources. USENIX 2001
481
Generated by GTK-Doc V1.11</div>