alternative to assert
[gtkD.git] / gtkD / src / glib / MemorySlice.d
blobc3db0bb2bfca4bc0a7b0ab752c71496b215b05a3
1 /*
2 * This file is part of gtkD.
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 // generated automatically - do not change
20 // find conversion definition on APILookup.txt
21 // implement new conversion functionalities on the wrap.utils pakage
24 * Conversion parameters:
25 * inFile = glib-Memory-Slices.html
26 * outPack = glib
27 * outFile = MemorySlice
28 * strct =
29 * realStrct=
30 * ctorStrct=
31 * clss = MemorySlice
32 * interf =
33 * class Code: No
34 * interface Code: No
35 * template for:
36 * extend =
37 * implements:
38 * prefixes:
39 * - g_slice_
40 * omit structs:
41 * omit prefixes:
42 * omit code:
43 * imports:
44 * structWrap:
45 * module aliases:
46 * local aliases:
49 module glib.MemorySlice;
51 version(noAssert)
53 version(Tango)
55 import tango.io.Stdout; // use the tango loging?
59 private import gtkc.glibtypes;
61 private import gtkc.glib;
68 /**
69 * Description
70 * Memory slices provide a space-efficient and multi-processing scalable
71 * way to allocate equal-sized pieces of memory, just like the original
72 * GMemChunks (from GLib <= 2.8), while avoiding their excessive
73 * memory-waste, scalability and performance problems.
74 * To achieve these goals, the slice allocator uses a sophisticated,
75 * layered design that has been inspired by Bonwick's slab allocator
76 * [5].
77 * It uses posix_memalign() to optimize allocations of many equally-sized
78 * chunks, and has per-thread free lists (the so-called magazine layer)
79 * to quickly satisfy allocation requests of already known structure sizes.
80 * This is accompanied by extra caching logic to keep freed memory around
81 * for some time before returning it to the system. Memory that is unused
82 * due to alignment constraints is used for cache colorization (random
83 * distribution of chunk addresses) to improve CPU cache utilization. The
84 * caching layer of the slice allocator adapts itself to high lock contention
85 * to improve scalability.
86 * The slice allocator can allocate blocks as small as two pointers, and
87 * unlike malloc(), it does not reserve extra space per block. For large block
88 * sizes, g_slice_new() and g_slice_alloc() will automatically delegate to the
89 * system malloc() implementation. For newly written code it is recommended
90 * to use the new g_slice API instead of g_malloc() and
91 * friends, as long as objects are not resized during their lifetime and the
92 * object size used at allocation time is still available when freeing.
93 * Example1.Using the slice allocator
94 * gchar *mem[10000];
95 * gint i;
96 * /+* Allocate 10000 blocks. +/
97 * for (i = 0; i < 10000; i++)
98 * {
99 * mem[i] = g_slice_alloc (50);
100 * /+* Fill in the memory with some junk. +/
101 * for (j = 0; j < 50; j++)
102 * mem[i][j] = i * j;
104 * /+* Now free all of the blocks. +/
105 * for (i = 0; i < 10000; i++)
107 * g_slice_free1 (50, mem[i]);
109 * Example2.Using the slice allocator with data structures
110 * GRealArray *array;
111 * /+* Allocate one block, using the g_slice_new() macro. +/
112 * array = g_slice_new (GRealArray);
113 * /+* We can now use array just like a normal pointer to a structure. +/
114 * array->data = NULL;
115 * array->len = 0;
116 * array->alloc = 0;
117 * array->zero_terminated = (zero_terminated ? 1 : 0);
118 * array->clear = (clear ? 1 : 0);
119 * array->elt_size = elt_size;
120 * /+* We can free the block, so it can be reused. +/
121 * g_slice_free (GRealArray, array);
123 public class MemorySlice
130 * Allocates a block of memory from the slice allocator.
131 * The block adress handed out is guaranteed to be aligned
132 * to at least 2 * sizeof (void*).
133 * Note that the underlying slice allocation mechanism can
134 * be changed with the G_SLICE=always-malloc
135 * environment variable.
136 * block_size:
137 * the number of bytes to allocate
138 * Returns:
139 * a pointer to the allocated memory block
140 * Since 2.10
142 public static void* alloc(uint blockSize)
144 // gpointer g_slice_alloc (gsize block_size);
145 return g_slice_alloc(blockSize);
149 * Allocates a block of memory via g_slice_alloc()
150 * and initialize the returned memory to 0.
151 * Note that the underlying slice allocation mechanism can
152 * be changed with the G_SLICE=always-malloc
153 * environment variable.
154 * block_size:
155 * the number of bytes to allocate
156 * Returns:
157 * a pointer to the allocated block
158 * Since 2.10
160 public static void* alloc0(uint blockSize)
162 // gpointer g_slice_alloc0 (gsize block_size);
163 return g_slice_alloc0(blockSize);
167 * Frees a block of memory. The memory must have been allocated via
168 * g_slice_alloc() or g_slice_alloc0()
169 * and the block_size has to match the size specified upon allocation.
170 * Note that the exact release behaviour can be changed with the
171 * G_DEBUG=gc-friendly environment variable,
172 * also see G_SLICE for related debugging options.
173 * block_size:
174 * the size of the block
175 * mem_block:
176 * a pointer to the block to free
177 * Since 2.10
179 public static void free1(uint blockSize, void* memBlock)
181 // void g_slice_free1 (gsize block_size, gpointer mem_block);
182 g_slice_free1(blockSize, memBlock);
186 * Frees a linked list of memory blocks of structure type type.
187 * The memory blocks must be equal-sized, allocated via
188 * g_slice_alloc() or g_slice_alloc0()
189 * and linked together by a next pointer (similar to GSList). The offset
190 * of the next field in each block is passed as third argument.
191 * Note that the exact release behaviour can be changed with the
192 * G_DEBUG=gc-friendly environment variable,
193 * also see G_SLICE for related debugging options.
194 * block_size:
195 * the size of the blocks
196 * mem_chain:
197 * a pointer to the first block of the chain
198 * next_offset:
199 * the offset of the next field in the blocks
200 * Since 2.10
202 public static void freeChainWithOffset(uint blockSize, void* memChain, uint nextOffset)
204 // void g_slice_free_chain_with_offset (gsize block_size, gpointer mem_chain, gsize next_offset);
205 g_slice_free_chain_with_offset(blockSize, memChain, nextOffset);