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-Allocation.html 27 * outPack = glib 28 * outFile = Memory 29 * strct = 30 * realStrct= 31 * ctorStrct= 32 * clss = Memory 33 * interf = 34 * class Code: No 35 * interface Code: No 36 * template for: 37 * extend = 38 * implements: 39 * prefixes: 40 * - g_ 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.Memory; 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 * Description 66 * These functions provide support for allocating and freeing memory. 67 * Note 68 * If any call to allocate memory fails, the application is terminated. 69 * This also means that there is no need to check if the call succeeded. 70 * Note 71 * It's important to match g_malloc() with g_free(), plain malloc() with free(), 72 * and (if you're using C++) new with delete and new[] with delete[]. Otherwise 73 * bad things can happen, since these allocators may use different memory 74 * pools (and new/delete call constructors and destructors). See also 75 * g_mem_set_vtable(). 76 */ 77 public class Memory 78 { 79 80 /** 81 */ 82 83 /** 84 * Allocates n_bytes bytes of memory. 85 * If n_bytes is 0 it returns NULL. 86 * Params: 87 * nBytes = the number of bytes to allocate 88 * Returns: a pointer to the allocated memory 89 */ 90 public static void* malloc(gsize nBytes) 91 { 92 // gpointer g_malloc (gsize n_bytes); 93 return g_malloc(nBytes); 94 } 95 96 /** 97 * Allocates n_bytes bytes of memory, initialized to 0's. 98 * If n_bytes is 0 it returns NULL. 99 * Params: 100 * nBytes = the number of bytes to allocate 101 * Returns: a pointer to the allocated memory 102 */ 103 public static void* malloc0(gsize nBytes) 104 { 105 // gpointer g_malloc0 (gsize n_bytes); 106 return g_malloc0(nBytes); 107 } 108 109 /** 110 * Reallocates the memory pointed to by mem, so that it now has space for 111 * n_bytes bytes of memory. It returns the new address of the memory, which may 112 * have been moved. mem may be NULL, in which case it's considered to 113 * have zero-length. n_bytes may be 0, in which case NULL will be returned 114 * and mem will be freed unless it is NULL. 115 * Params: 116 * mem = the memory to reallocate 117 * nBytes = new size of the memory in bytes 118 * Returns: the new address of the allocated memory 119 */ 120 public static void* realloc(void* mem, gsize nBytes) 121 { 122 // gpointer g_realloc (gpointer mem, gsize n_bytes); 123 return g_realloc(mem, nBytes); 124 } 125 126 /** 127 * Attempts to allocate n_bytes, and returns NULL on failure. 128 * Contrast with g_malloc(), which aborts the program on failure. 129 * Params: 130 * nBytes = number of bytes to allocate. 131 * Returns: the allocated memory, or NULL. 132 */ 133 public static void* tryMalloc(gsize nBytes) 134 { 135 // gpointer g_try_malloc (gsize n_bytes); 136 return g_try_malloc(nBytes); 137 } 138 139 /** 140 * Attempts to allocate n_bytes, initialized to 0's, and returns NULL on 141 * failure. Contrast with g_malloc0(), which aborts the program on failure. 142 * Since 2.8 143 * Params: 144 * nBytes = number of bytes to allocate 145 * Returns: the allocated memory, or NULL 146 */ 147 public static void* tryMalloc0(gsize nBytes) 148 { 149 // gpointer g_try_malloc0 (gsize n_bytes); 150 return g_try_malloc0(nBytes); 151 } 152 153 /** 154 * Attempts to realloc mem to a new size, n_bytes, and returns NULL 155 * on failure. Contrast with g_realloc(), which aborts the program 156 * on failure. If mem is NULL, behaves the same as g_try_malloc(). 157 * Params: 158 * mem = previously-allocated memory, or NULL. 159 * nBytes = number of bytes to allocate. 160 * Returns: the allocated memory, or NULL. 161 */ 162 public static void* tryRealloc(void* mem, gsize nBytes) 163 { 164 // gpointer g_try_realloc (gpointer mem, gsize n_bytes); 165 return g_try_realloc(mem, nBytes); 166 } 167 168 /** 169 * This function is similar to g_malloc(), allocating (n_blocks * n_block_bytes) bytes, 170 * but care is taken to detect possible overflow during multiplication. 171 * Since 2.24 172 * Params: 173 * nBlocks = the number of blocks to allocate 174 * nBlockBytes = the size of each block in bytes 175 * Returns: a pointer to the allocated memory 176 */ 177 public static void* mallocN(gsize nBlocks, gsize nBlockBytes) 178 { 179 // gpointer g_malloc_n (gsize n_blocks, gsize n_block_bytes); 180 return g_malloc_n(nBlocks, nBlockBytes); 181 } 182 183 /** 184 * This function is similar to g_malloc0(), allocating (n_blocks * n_block_bytes) bytes, 185 * but care is taken to detect possible overflow during multiplication. 186 * Since 2.24 187 * Params: 188 * nBlocks = the number of blocks to allocate 189 * nBlockBytes = the size of each block in bytes 190 * Returns: a pointer to the allocated memory 191 */ 192 public static void* malloc0_N(gsize nBlocks, gsize nBlockBytes) 193 { 194 // gpointer g_malloc0_n (gsize n_blocks, gsize n_block_bytes); 195 return g_malloc0_n(nBlocks, nBlockBytes); 196 } 197 198 /** 199 * This function is similar to g_realloc(), allocating (n_blocks * n_block_bytes) bytes, 200 * but care is taken to detect possible overflow during multiplication. 201 * Since 2.24 202 * Params: 203 * mem = the memory to reallocate 204 * nBlocks = the number of blocks to allocate 205 * nBlockBytes = the size of each block in bytes 206 * Returns: the new address of the allocated memory 207 */ 208 public static void* reallocN(void* mem, gsize nBlocks, gsize nBlockBytes) 209 { 210 // gpointer g_realloc_n (gpointer mem, gsize n_blocks, gsize n_block_bytes); 211 return g_realloc_n(mem, nBlocks, nBlockBytes); 212 } 213 214 /** 215 * This function is similar to g_try_malloc(), allocating (n_blocks * n_block_bytes) bytes, 216 * but care is taken to detect possible overflow during multiplication. 217 * Since 2.24 218 * Params: 219 * nBlocks = the number of blocks to allocate 220 * nBlockBytes = the size of each block in bytes 221 * Returns: the allocated memory, or NULL. 222 */ 223 public static void* tryMallocN(gsize nBlocks, gsize nBlockBytes) 224 { 225 // gpointer g_try_malloc_n (gsize n_blocks, gsize n_block_bytes); 226 return g_try_malloc_n(nBlocks, nBlockBytes); 227 } 228 229 /** 230 * This function is similar to g_try_malloc0(), allocating (n_blocks * n_block_bytes) bytes, 231 * but care is taken to detect possible overflow during multiplication. 232 * Since 2.24 233 * Params: 234 * nBlocks = the number of blocks to allocate 235 * nBlockBytes = the size of each block in bytes 236 * Returns: the allocated memory, or NULL 237 */ 238 public static void* tryMalloc0_N(gsize nBlocks, gsize nBlockBytes) 239 { 240 // gpointer g_try_malloc0_n (gsize n_blocks, gsize n_block_bytes); 241 return g_try_malloc0_n(nBlocks, nBlockBytes); 242 } 243 244 /** 245 * This function is similar to g_try_realloc(), allocating (n_blocks * n_block_bytes) bytes, 246 * but care is taken to detect possible overflow during multiplication. 247 * Since 2.24 248 * Params: 249 * mem = previously-allocated memory, or NULL. 250 * nBlocks = the number of blocks to allocate 251 * nBlockBytes = the size of each block in bytes 252 * Returns: the allocated memory, or NULL. 253 */ 254 public static void* tryReallocN(void* mem, gsize nBlocks, gsize nBlockBytes) 255 { 256 // gpointer g_try_realloc_n (gpointer mem, gsize n_blocks, gsize n_block_bytes); 257 return g_try_realloc_n(mem, nBlocks, nBlockBytes); 258 } 259 260 /** 261 * Frees the memory pointed to by mem. 262 * If mem is NULL it simply returns. 263 * Params: 264 * mem = the memory to free 265 */ 266 public static void free(void* mem) 267 { 268 // void g_free (gpointer mem); 269 g_free(mem); 270 } 271 272 /** 273 * Allocates byte_size bytes of memory, and copies byte_size bytes into it 274 * from mem. If mem is NULL it returns NULL. 275 * Params: 276 * mem = the memory to copy. 277 * byteSize = the number of bytes to copy. 278 * Returns: a pointer to the newly-allocated copy of the memory, or NULL if mem is NULL. 279 */ 280 public static void* memdup(void* mem, uint byteSize) 281 { 282 // gpointer g_memdup (gconstpointer mem, guint byte_size); 283 return g_memdup(mem, byteSize); 284 } 285 286 /** 287 * Sets the GMemVTable to use for memory allocation. You can use this to provide 288 * custom memory allocation routines. This function must be called 289 * before using any other GLib functions. The vtable only needs to 290 * provide malloc(), realloc(), and free() functions; GLib can provide default 291 * implementations of the others. The malloc() and realloc() implementations 292 * should return NULL on failure, GLib will handle error-checking for you. 293 * vtable is copied, so need not persist after this function has been called. 294 * Params: 295 * vtable = table of memory allocation routines. 296 */ 297 public static void memSetVtable(GMemVTable* vtable) 298 { 299 // void g_mem_set_vtable (GMemVTable *vtable); 300 g_mem_set_vtable(vtable); 301 } 302 303 /** 304 * Checks whether the allocator used by g_malloc() is the system's 305 * malloc implementation. If it returns TRUE memory allocated with 306 * malloc() can be used interchangeable with memory allocated using g_malloc(). 307 * This function is useful for avoiding an extra copy of allocated memory returned 308 * by a non-GLib-based API. 309 * A different allocator can be set using g_mem_set_vtable(). 310 * Returns: if TRUE, malloc() and g_malloc() can be mixed. 311 */ 312 public static int memIsSystemMalloc() 313 { 314 // gboolean g_mem_is_system_malloc (void); 315 return g_mem_is_system_malloc(); 316 } 317 318 /** 319 * Outputs a summary of memory usage. 320 * It outputs the frequency of allocations of different sizes, 321 * the total number of bytes which have been allocated, 322 * the total number of bytes which have been freed, 323 * and the difference between the previous two values, i.e. the number of bytes 324 * still in use. 325 * Note that this function will not output anything unless you have 326 * previously installed the glib_mem_profiler_table with g_mem_set_vtable(). 327 */ 328 public static void memProfile() 329 { 330 // void g_mem_profile (void); 331 g_mem_profile(); 332 } 333 }