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 * Memory slices provide a space-efficient and multi-processing scalable 64 * way to allocate equal-sized pieces of memory, just like the original 65 * GMemChunks (from GLib 2.8), while avoiding their excessive 66 * memory-waste, scalability and performance problems. 67 * 68 * To achieve these goals, the slice allocator uses a sophisticated, 69 * layered design that has been inspired by Bonwick's slab allocator 70 * [1]. 71 * It uses posix_memalign() to optimize allocations of many equally-sized 72 * chunks, and has per-thread free lists (the so-called magazine layer) 73 * to quickly satisfy allocation requests of already known structure sizes. 74 * This is accompanied by extra caching logic to keep freed memory around 75 * for some time before returning it to the system. Memory that is unused 76 * due to alignment constraints is used for cache colorization (random 77 * distribution of chunk addresses) to improve CPU cache utilization. The 78 * caching layer of the slice allocator adapts itself to high lock contention 79 * to improve scalability. 80 * 81 * The slice allocator can allocate blocks as small as two pointers, and 82 * unlike malloc(), it does not reserve extra space per block. For large block 83 * sizes, g_slice_new() and g_slice_alloc() will automatically delegate to the 84 * system malloc() implementation. For newly written code it is recommended 85 * to use the new g_slice API instead of g_malloc() and 86 * friends, as long as objects are not resized during their lifetime and the 87 * object size used at allocation time is still available when freeing. 88 * 89 * $(DDOC_COMMENT example) 90 * 91 * $(DDOC_COMMENT example) 92 */ 93 public class MemorySlice 94 { 95 96 T* mewSlice(T)() 97 { 98 return cast(T*)g_slice_alloc(T.sizeof); 99 } 100 101 T* mewSlice0(T)() 102 { 103 return cast(T*)g_slice_alloc0(T.sizeof); 104 } 105 106 T* dup(T)(T* memBlock) 107 { 108 return cast(T*)g_slice_copy(T.sizeof, memBlock); 109 } 110 111 void free(T)(T* memBlock) 112 { 113 g_slice_free1(T.sizeof, memBlock); 114 } 115 116 /** 117 */ 118 119 /** 120 * Allocates a block of memory from the slice allocator. 121 * The block adress handed out can be expected to be aligned 122 * to at least 1 * sizeof (void*), 123 * though in general slices are 2 * sizeof (void*) bytes aligned, 124 * if a malloc() fallback implementation is used instead, 125 * the alignment may be reduced in a libc dependent fashion. 126 * Note that the underlying slice allocation mechanism can 127 * be changed with the G_SLICE=always-malloc 128 * environment variable. 129 * Since 2.10 130 * Params: 131 * blockSize = the number of bytes to allocate 132 * Returns: a pointer to the allocated memory block 133 */ 134 public static void* alloc(gsize blockSize) 135 { 136 // gpointer g_slice_alloc (gsize block_size); 137 return g_slice_alloc(blockSize); 138 } 139 140 /** 141 * Allocates a block of memory via g_slice_alloc() and initializes 142 * the returned memory to 0. Note that the underlying slice allocation 143 * mechanism can be changed with the 144 * G_SLICE=always-malloc 145 * environment variable. 146 * Since 2.10 147 * Params: 148 * blockSize = the number of bytes to allocate 149 * Returns: a pointer to the allocated block 150 */ 151 public static void* alloc0(gsize blockSize) 152 { 153 // gpointer g_slice_alloc0 (gsize block_size); 154 return g_slice_alloc0(blockSize); 155 } 156 157 /** 158 * Allocates a block of memory from the slice allocator 159 * and copies block_size bytes into it from mem_block. 160 * Since 2.14 161 * Params: 162 * blockSize = the number of bytes to allocate 163 * memBlock = the memory to copy 164 * Returns: a pointer to the allocated memory block 165 */ 166 public static void* copy(gsize blockSize, void* memBlock) 167 { 168 // gpointer g_slice_copy (gsize block_size, gconstpointer mem_block); 169 return g_slice_copy(blockSize, memBlock); 170 } 171 172 /** 173 * Frees a block of memory. 174 * The memory must have been allocated via g_slice_alloc() or 175 * g_slice_alloc0() and the block_size has to match the size 176 * specified upon allocation. Note that the exact release behaviour 177 * can be changed with the 178 * G_DEBUG=gc-friendly environment 179 * variable, also see G_SLICE for 180 * related debugging options. 181 * Since 2.10 182 * Params: 183 * blockSize = the size of the block 184 * memBlock = a pointer to the block to free 185 */ 186 public static void free1(gsize blockSize, void* memBlock) 187 { 188 // void g_slice_free1 (gsize block_size, gpointer mem_block); 189 g_slice_free1(blockSize, memBlock); 190 } 191 192 /** 193 * Frees a linked list of memory blocks of structure type type. 194 * The memory blocks must be equal-sized, allocated via 195 * g_slice_alloc() or g_slice_alloc0() and linked together by a 196 * next pointer (similar to GSList). The offset of the next 197 * field in each block is passed as third argument. 198 * Note that the exact release behaviour can be changed with the 199 * G_DEBUG=gc-friendly environment 200 * variable, also see G_SLICE for 201 * related debugging options. 202 * Since 2.10 203 * Params: 204 * blockSize = the size of the blocks 205 * memChain = a pointer to the first block of the chain 206 * nextOffset = the offset of the next field in the blocks 207 */ 208 public static void freeChainWithOffset(gsize blockSize, void* memChain, gsize nextOffset) 209 { 210 // void g_slice_free_chain_with_offset (gsize block_size, gpointer mem_chain, gsize next_offset); 211 g_slice_free_chain_with_offset(blockSize, memChain, nextOffset); 212 } 213 }