• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<!-- ##### SECTION Title ##### -->
2Memory Slices
3
4<!-- ##### SECTION Short_Description ##### -->
5efficient way to allocate groups of equal-sized chunks of memory
6
7<!-- ##### SECTION Long_Description ##### -->
8<para>
9Memory slices provide a space-efficient and multi-processing scalable
10way to allocate equal-sized pieces of memory, just like the original
11#GMemChunks (from GLib &lt;= 2.8), while avoiding their excessive
12memory-waste, scalability and performance problems.
13</para>
14
15<para>
16To achieve these goals, the slice allocator uses a sophisticated,
17layered design that has been inspired by Bonwick's slab allocator
18<footnote><para>
19<ulink url="http://citeseer.ist.psu.edu/bonwick94slab.html">[Bonwick94]</ulink> Jeff Bonwick, The slab allocator: An object-caching kernel
20memory allocator. USENIX 1994, and
21<ulink url="http://citeseer.ist.psu.edu/bonwick01magazines.html">[Bonwick01]</ulink> Bonwick and Jonathan Adams, Magazines and vmem: Extending the
22slab allocator to many cpu's and arbitrary resources. USENIX 2001
23</para></footnote>.
24It uses posix_memalign() to optimize allocations of many equally-sized
25chunks, and has per-thread free lists (the so-called magazine layer)
26to quickly satisfy allocation requests of already known structure sizes.
27This is accompanied by extra caching logic to keep freed memory around
28for some time before returning it to the system. Memory that is unused
29due to alignment constraints is used for cache colorization (random
30distribution of chunk addresses) to improve CPU cache utilization. The
31caching layer of the slice allocator adapts itself to high lock contention
32to improve scalability.
33</para>
34
35<para>
36The slice allocator can allocate blocks as small as two pointers, and
37unlike malloc(), it does not reserve extra space per block. For large block
38sizes, g_slice_new() and g_slice_alloc() will automatically delegate to the
39system malloc() implementation. For newly written code it is recommended
40to use the new <literal>g_slice</literal> API instead of g_malloc() and
41friends, as long as objects are not resized during their lifetime and the
42object size used at allocation time is still available when freeing.
43</para>
44
45<example>
46<title>Using the slice allocator</title>
47<programlisting>
48  gchar *mem[10000];
49  gint i;
50
51  /* Allocate 10000 blocks. */
52  for (i = 0; i &lt; 10000; i++)
53    {
54      mem[i] = g_slice_alloc (50);
55
56      /* Fill in the memory with some junk. */
57      for (j = 0; j &lt; 50; j++)
58	mem[i][j] = i * j;
59    }
60
61  /* Now free all of the blocks. */
62  for (i = 0; i &lt; 10000; i++)
63    {
64      g_slice_free1 (50, mem[i]);
65    }
66</programlisting></example>
67
68<example>
69<title>Using the slice allocator with data structures</title>
70<programlisting>
71  GRealArray *array;
72
73  /* Allocate one block, using the g_slice_new(<!-- -->) macro. */
74  array = g_slice_new (GRealArray);
75
76  /* We can now use array just like a normal pointer to a structure. */
77  array->data            = NULL;
78  array->len             = 0;
79  array->alloc           = 0;
80  array->zero_terminated = (zero_terminated ? 1 : 0);
81  array->clear           = (clear ? 1 : 0);
82  array->elt_size        = elt_size;
83
84  /* We can free the block, so it can be reused. */
85  g_slice_free (GRealArray, array);
86</programlisting></example>
87
88<!-- ##### SECTION See_Also ##### -->
89<para>
90</para>
91
92<!-- ##### SECTION Stability_Level ##### -->
93
94
95<!-- ##### FUNCTION g_slice_alloc ##### -->
96<para>
97Allocates a block of memory from the slice allocator.
98The block adress handed out can be expected to be aligned
99to at least <literal>1 * sizeof (void*)</literal>,
100though in general slices are 2 * sizeof (void*) bytes aligned,
101if a malloc() fallback implementation is used instead,
102the alignment may be reduced in a libc dependent fashion.
103Note that the underlying slice allocation mechanism can
104be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
105environment variable.
106</para>
107
108@block_size: the number of bytes to allocate
109@Returns: a pointer to the allocated memory block
110@Since: 2.10
111
112
113<!-- ##### FUNCTION g_slice_alloc0 ##### -->
114<para>
115Allocates a block of memory via g_slice_alloc()
116and initialize the returned memory to 0.
117Note that the underlying slice allocation mechanism can
118be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
119environment variable.
120</para>
121
122@block_size: the number of bytes to allocate
123@Returns: a pointer to the allocated block
124@Since: 2.10
125
126
127<!-- ##### FUNCTION g_slice_copy ##### -->
128<para>
129Allocates a block of memory from the slice allocator and copies
130@block_size bytes into it from @mem_block.
131</para>
132
133@block_size: the number of bytes to allocate
134@mem_block: the memory to copy
135@Returns: a pointer to the allocated memory block
136@Since: 2.14
137
138
139<!-- ##### FUNCTION g_slice_free1 ##### -->
140<para>
141Frees a block of memory. The memory must have been allocated via
142g_slice_alloc() or g_slice_alloc0()
143and the @block_size has to match the size specified upon allocation.
144Note that the exact release behaviour can be changed with the
145<link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment variable,
146also see <link linkend="G_SLICE">G_SLICE</link> for related debugging options.
147</para>
148
149@block_size: the size of the block
150@mem_block: a pointer to the block to free
151@Since: 2.10
152
153
154<!-- ##### FUNCTION g_slice_free_chain_with_offset ##### -->
155<para>
156Frees a linked list of memory blocks of structure type @type.
157The memory blocks must be equal-sized, allocated via
158g_slice_alloc() or g_slice_alloc0()
159and linked together by a @next pointer (similar to #GSList). The offset
160of the @next field in each block is passed as third argument.
161Note that the exact release behaviour can be changed with the
162<link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment variable,
163also see <link linkend="G_SLICE">G_SLICE</link> for related debugging options.
164</para>
165
166@block_size: the size of the blocks
167@mem_chain:  a pointer to the first block of the chain
168@next_offset: the offset of the @next field in the blocks
169@Since: 2.10
170
171
172<!-- ##### MACRO g_slice_new ##### -->
173<para>
174A convenience macro to allocate a block of memory from the slice allocator.
175It calls g_slice_alloc() with <literal>sizeof (@type)</literal> and casts
176the returned pointer to a pointer of the given type, avoiding a type cast
177in the source code.
178Note that the underlying slice allocation mechanism can
179be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
180environment variable.
181</para>
182
183@type: the type to allocate, typically a structure name
184@Returns: a pointer to the allocated block, cast to a pointer to @type.
185@Since: 2.10
186
187
188<!-- ##### MACRO g_slice_new0 ##### -->
189<para>
190A convenience macro to allocate a block of memory from the slice allocator
191and set the memory to 0. It calls g_slice_alloc0() with
192<literal>sizeof (@type)</literal> and casts the returned pointer to a pointer
193of the given type, avoiding a type cast in the source code.
194Note that the underlying slice allocation mechanism can
195be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
196environment variable.
197</para>
198
199@type: the type to allocate, typically a structure name
200@Returns: a pointer to the allocated block, cast to a pointer to @type.
201@Since: 2.10
202
203
204<!-- ##### MACRO g_slice_dup ##### -->
205<para>
206A convenience macro to duplicate a block of memory using the slice allocator.
207It calls g_slice_copy() with <literal>sizeof (@type)</literal> and casts
208the returned pointer to a pointer of the given type, avoiding a type cast
209in the source code.
210Note that the underlying slice allocation mechanism can
211be changed with the <link linkend="G_SLICE">G_SLICE=always-malloc</link>
212environment variable.
213</para>
214
215@type: the type to duplicate, typically a structure name
216@mem: the memory to copy into the allocated block
217@Returns: a pointer to the allocated block, cast to a pointer to @type.
218@Since: 2.14
219
220
221<!-- ##### MACRO g_slice_free ##### -->
222<para>
223A convenience macro to free a block of memory that has been allocated
224from the slice allocator. It calls g_slice_free1() using
225<literal>sizeof (type)</literal> as the block size.
226Note that the exact release behaviour can be changed with the
227<link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment variable,
228also see <link linkend="G_SLICE">G_SLICE</link> for related debugging options.
229</para>
230
231@type: the type of the block to free, typically a structure name
232@mem: a pointer to the block to free
233@Since: 2.10
234
235
236<!-- ##### MACRO g_slice_free_chain ##### -->
237<para>
238Frees a linked list of memory blocks of structure type @type.
239The memory blocks must be equal-sized, allocated via
240g_slice_alloc() or g_slice_alloc0() and linked together by a
241@next pointer (similar to #GSList). The name of the
242@next field in @type is passed as third argument.
243Note that the exact release behaviour can be changed with the
244<link linkend="G_DEBUG">G_DEBUG=gc-friendly</link> environment variable,
245also see <link linkend="G_SLICE">G_SLICE</link> for related debugging options.
246</para>
247
248@type:        the type of the @mem_chain blocks
249@mem_chain:   a pointer to the first block of the chain
250@next:        the field name of the next pointer in @type
251@Since: 2.10
252
253
254