1 /* 2 * This file is part of gtkD. 3 * 4 * gtkD is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License 6 * as published by the Free Software Foundation; either version 3 7 * of the License, or (at your option) any later version, with 8 * some exceptions, please read the COPYING file. 9 * 10 * gtkD is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU Lesser General Public License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with gtkD; if not, write to the Free Software 17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA 18 */ 19 20 // generated automatically - do not change 21 // find conversion definition on APILookup.txt 22 // implement new conversion functionalities on the wrap.utils pakage 23 24 /* 25 * Conversion parameters: 26 * inFile = glib-Memory-Slices.html 27 * outPack = glib 28 * outFile = MemorySlice 29 * strct = 30 * realStrct= 31 * ctorStrct= 32 * clss = MemorySlice 33 * interf = 34 * class Code: Yes 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_slice_ 41 * omit structs: 42 * omit prefixes: 43 * omit code: 44 * omit signals: 45 * imports: 46 * structWrap: 47 * module aliases: 48 * local aliases: 49 * overrides: 50 */ 51 52 module glib.MemorySlice; 53 54 public import gtkc.glibtypes; 55 56 private import gtkc.glib; 57 private import glib.ConstructionException; 58 59 60 61 62 63 64 /** 65 * Memory slices provide a space-efficient and multi-processing scalable 66 * way to allocate equal-sized pieces of memory, just like the original 67 * GMemChunks (from GLib 2.8), while avoiding their excessive 68 * memory-waste, scalability and performance problems. 69 * 70 * To achieve these goals, the slice allocator uses a sophisticated, 71 * layered design that has been inspired by Bonwick's slab allocator 72 * [1]. 73 * It uses posix_memalign() to optimize allocations of many equally-sized 74 * chunks, and has per-thread free lists (the so-called magazine layer) 75 * to quickly satisfy allocation requests of already known structure sizes. 76 * This is accompanied by extra caching logic to keep freed memory around 77 * for some time before returning it to the system. Memory that is unused 78 * due to alignment constraints is used for cache colorization (random 79 * distribution of chunk addresses) to improve CPU cache utilization. The 80 * caching layer of the slice allocator adapts itself to high lock contention 81 * to improve scalability. 82 * 83 * The slice allocator can allocate blocks as small as two pointers, and 84 * unlike malloc(), it does not reserve extra space per block. For large block 85 * sizes, g_slice_new() and g_slice_alloc() will automatically delegate to the 86 * system malloc() implementation. For newly written code it is recommended 87 * to use the new g_slice API instead of g_malloc() and 88 * friends, as long as objects are not resized during their lifetime and the 89 * object size used at allocation time is still available when freeing. 90 * 91 * $(DDOC_COMMENT example) 92 * 93 * $(DDOC_COMMENT example) 94 */ 95 public class MemorySlice 96 { 97 98 T* mewSlice(T)() 99 { 100 return cast(T*)g_slice_alloc(T.sizeof); 101 } 102 103 T* mewSlice0(T)() 104 { 105 return cast(T*)g_slice_alloc0(T.sizeof); 106 } 107 108 T* dup(T)(T* memBlock) 109 { 110 return cast(T*)g_slice_copy(T.sizeof, memBlock); 111 } 112 113 void free(T)(T* memBlock) 114 { 115 g_slice_free1(T.sizeof, memBlock); 116 } 117 118 /** 119 */ 120 121 /** 122 * Allocates a block of memory from the slice allocator. 123 * The block adress handed out can be expected to be aligned 124 * to at least 1 * sizeof (void*), 125 * though in general slices are 2 * sizeof (void*) bytes aligned, 126 * if a malloc() fallback implementation is used instead, 127 * the alignment may be reduced in a libc dependent fashion. 128 * Note that the underlying slice allocation mechanism can 129 * be changed with the G_SLICE=always-malloc 130 * environment variable. 131 * Since 2.10 132 * Params: 133 * blockSize = the number of bytes to allocate 134 * Returns: a pointer to the allocated memory block 135 */ 136 public static void* alloc(gsize blockSize) 137 { 138 // gpointer g_slice_alloc (gsize block_size); 139 return g_slice_alloc(blockSize); 140 } 141 142 /** 143 * Allocates a block of memory via g_slice_alloc() and initializes 144 * the returned memory to 0. Note that the underlying slice allocation 145 * mechanism can be changed with the 146 * G_SLICE=always-malloc 147 * environment variable. 148 * Since 2.10 149 * Params: 150 * blockSize = the number of bytes to allocate 151 * Returns: a pointer to the allocated block 152 */ 153 public static void* alloc0(gsize blockSize) 154 { 155 // gpointer g_slice_alloc0 (gsize block_size); 156 return g_slice_alloc0(blockSize); 157 } 158 159 /** 160 * Allocates a block of memory from the slice allocator 161 * and copies block_size bytes into it from mem_block. 162 * Since 2.14 163 * Params: 164 * blockSize = the number of bytes to allocate 165 * memBlock = the memory to copy 166 * Returns: a pointer to the allocated memory block 167 */ 168 public static void* copy(gsize blockSize, void* memBlock) 169 { 170 // gpointer g_slice_copy (gsize block_size, gconstpointer mem_block); 171 return g_slice_copy(blockSize, memBlock); 172 } 173 174 /** 175 * Frees a block of memory. 176 * The memory must have been allocated via g_slice_alloc() or 177 * g_slice_alloc0() and the block_size has to match the size 178 * specified upon allocation. Note that the exact release behaviour 179 * can be changed with the 180 * G_DEBUG=gc-friendly environment 181 * variable, also see G_SLICE for 182 * related debugging options. 183 * Since 2.10 184 * Params: 185 * blockSize = the size of the block 186 * memBlock = a pointer to the block to free 187 */ 188 public static void free1(gsize blockSize, void* memBlock) 189 { 190 // void g_slice_free1 (gsize block_size, gpointer mem_block); 191 g_slice_free1(blockSize, memBlock); 192 } 193 194 /** 195 * Frees a linked list of memory blocks of structure type type. 196 * The memory blocks must be equal-sized, allocated via 197 * g_slice_alloc() or g_slice_alloc0() and linked together by a 198 * next pointer (similar to GSList). The offset of the next 199 * field in each block is passed as third argument. 200 * Note that the exact release behaviour can be changed with the 201 * G_DEBUG=gc-friendly environment 202 * variable, also see G_SLICE for 203 * related debugging options. 204 * Since 2.10 205 * Params: 206 * blockSize = the size of the blocks 207 * memChain = a pointer to the first block of the chain 208 * nextOffset = the offset of the next field in the blocks 209 */ 210 public static void freeChainWithOffset(gsize blockSize, void* memChain, gsize nextOffset) 211 { 212 // void g_slice_free_chain_with_offset (gsize block_size, gpointer mem_chain, gsize next_offset); 213 g_slice_free_chain_with_offset(blockSize, memChain, nextOffset); 214 } 215 }