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