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