• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 /*
19  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GLib Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GLib at ftp://ftp.gtk.org/pub/gtk/.
23  */
24 
25 /*
26  * MT safe
27  */
28 
29 #include "config.h"
30 
31 #include "gmem.h"
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <signal.h>
36 
37 #include "gslice.h"
38 #include "gbacktrace.h"
39 #include "gtestutils.h"
40 #include "gthread.h"
41 #include "glib_trace.h"
42 
43 /* notes on macros:
44  * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
45  * g_mem_profile().
46  * If g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
47  */
48 
49 /* --- variables --- */
50 static GMemVTable glib_mem_vtable = {
51   malloc,
52   realloc,
53   free,
54   calloc,
55   malloc,
56   realloc,
57 };
58 
59 /**
60  * SECTION:memory
61  * @Short_Description: general memory-handling
62  * @Title: Memory Allocation
63  *
64  * These functions provide support for allocating and freeing memory.
65  *
66  * If any call to allocate memory using functions g_new(), g_new0(), g_renew(),
67  * g_malloc(), g_malloc0(), g_malloc0_n(), g_realloc(), and g_realloc_n()
68  * fails, the application is terminated. This also means that there is no
69  * need to check if the call succeeded. On the other hand, the `g_try_...()` family
70  * of functions returns %NULL on failure that can be used as a check
71  * for unsuccessful memory allocation. The application is not terminated
72  * in this case.
73  *
74  * As all GLib functions and data structures use `g_malloc()` internally, unless
75  * otherwise specified, any allocation failure will result in the application
76  * being terminated.
77  *
78  * It's important to match g_malloc() (and wrappers such as g_new()) with
79  * g_free(), g_slice_alloc() (and wrappers such as g_slice_new()) with
80  * g_slice_free(), plain malloc() with free(), and (if you're using C++)
81  * new with delete and new[] with delete[]. Otherwise bad things can happen,
82  * since these allocators may use different memory pools (and new/delete call
83  * constructors and destructors).
84  *
85  * Since GLib 2.46 g_malloc() is hardcoded to always use the system malloc
86  * implementation.
87  */
88 
89 /* --- functions --- */
90 /**
91  * g_malloc:
92  * @n_bytes: the number of bytes to allocate
93  *
94  * Allocates @n_bytes bytes of memory.
95  * If @n_bytes is 0 it returns %NULL.
96  *
97  * Returns: a pointer to the allocated memory
98  */
99 gpointer
g_malloc(gsize n_bytes)100 g_malloc (gsize n_bytes)
101 {
102   if (G_LIKELY (n_bytes))
103     {
104       gpointer mem;
105 
106       mem = malloc (n_bytes);
107       TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 0));
108       if (mem)
109 	return mem;
110 
111       g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
112                G_STRLOC, n_bytes);
113     }
114 
115   TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 0, 0));
116 
117   return NULL;
118 }
119 
120 /**
121  * g_malloc0:
122  * @n_bytes: the number of bytes to allocate
123  *
124  * Allocates @n_bytes bytes of memory, initialized to 0's.
125  * If @n_bytes is 0 it returns %NULL.
126  *
127  * Returns: a pointer to the allocated memory
128  */
129 gpointer
g_malloc0(gsize n_bytes)130 g_malloc0 (gsize n_bytes)
131 {
132   if (G_LIKELY (n_bytes))
133     {
134       gpointer mem;
135 
136       mem = calloc (1, n_bytes);
137       TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 1, 0));
138       if (mem)
139 	return mem;
140 
141       g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
142                G_STRLOC, n_bytes);
143     }
144 
145   TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 1, 0));
146 
147   return NULL;
148 }
149 
150 /**
151  * g_realloc:
152  * @mem: (nullable): the memory to reallocate
153  * @n_bytes: new size of the memory in bytes
154  *
155  * Reallocates the memory pointed to by @mem, so that it now has space for
156  * @n_bytes bytes of memory. It returns the new address of the memory, which may
157  * have been moved. @mem may be %NULL, in which case it's considered to
158  * have zero-length. @n_bytes may be 0, in which case %NULL will be returned
159  * and @mem will be freed unless it is %NULL.
160  *
161  * Returns: the new address of the allocated memory
162  */
163 gpointer
g_realloc(gpointer mem,gsize n_bytes)164 g_realloc (gpointer mem,
165 	   gsize    n_bytes)
166 {
167   gpointer newmem;
168 
169   if (G_LIKELY (n_bytes))
170     {
171       newmem = realloc (mem, n_bytes);
172       TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 0));
173       if (newmem)
174 	return newmem;
175 
176       g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
177                G_STRLOC, n_bytes);
178     }
179 
180   free (mem);
181 
182   TRACE (GLIB_MEM_REALLOC((void*) NULL, (void*)mem, 0, 0));
183 
184   return NULL;
185 }
186 
187 /**
188  * g_free:
189  * @mem: (nullable): the memory to free
190  *
191  * Frees the memory pointed to by @mem.
192  *
193  * If @mem is %NULL it simply returns, so there is no need to check @mem
194  * against %NULL before calling this function.
195  */
196 void
g_free(gpointer mem)197 g_free (gpointer mem)
198 {
199   free (mem);
200   TRACE(GLIB_MEM_FREE((void*) mem));
201 }
202 
203 /**
204  * g_clear_pointer: (skip)
205  * @pp: (not nullable): a pointer to a variable, struct member etc. holding a
206  *    pointer
207  * @destroy: a function to which a gpointer can be passed, to destroy *@pp
208  *
209  * Clears a reference to a variable.
210  *
211  * @pp must not be %NULL.
212  *
213  * If the reference is %NULL then this function does nothing.
214  * Otherwise, the variable is destroyed using @destroy and the
215  * pointer is set to %NULL.
216  *
217  * A macro is also included that allows this function to be used without
218  * pointer casts. This will mask any warnings about incompatible function types
219  * or calling conventions, so you must ensure that your @destroy function is
220  * compatible with being called as `GDestroyNotify` using the standard calling
221  * convention for the platform that GLib was compiled for; otherwise the program
222  * will experience undefined behaviour.
223  *
224  * Since: 2.34
225  **/
226 #undef g_clear_pointer
227 void
g_clear_pointer(gpointer * pp,GDestroyNotify destroy)228 g_clear_pointer (gpointer      *pp,
229                  GDestroyNotify destroy)
230 {
231   gpointer _p;
232 
233   _p = *pp;
234   if (_p)
235     {
236       *pp = NULL;
237       destroy (_p);
238     }
239 }
240 
241 /**
242  * g_try_malloc:
243  * @n_bytes: number of bytes to allocate.
244  *
245  * Attempts to allocate @n_bytes, and returns %NULL on failure.
246  * Contrast with g_malloc(), which aborts the program on failure.
247  *
248  * Returns: the allocated memory, or %NULL.
249  */
250 gpointer
g_try_malloc(gsize n_bytes)251 g_try_malloc (gsize n_bytes)
252 {
253   gpointer mem;
254 
255   if (G_LIKELY (n_bytes))
256     mem = malloc (n_bytes);
257   else
258     mem = NULL;
259 
260   TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1));
261 
262   return mem;
263 }
264 
265 /**
266  * g_try_malloc0:
267  * @n_bytes: number of bytes to allocate
268  *
269  * Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
270  * failure. Contrast with g_malloc0(), which aborts the program on failure.
271  *
272  * Since: 2.8
273  * Returns: the allocated memory, or %NULL
274  */
275 gpointer
g_try_malloc0(gsize n_bytes)276 g_try_malloc0 (gsize n_bytes)
277 {
278   gpointer mem;
279 
280   if (G_LIKELY (n_bytes))
281     mem = calloc (1, n_bytes);
282   else
283     mem = NULL;
284 
285   return mem;
286 }
287 
288 /**
289  * g_try_realloc:
290  * @mem: (nullable): previously-allocated memory, or %NULL.
291  * @n_bytes: number of bytes to allocate.
292  *
293  * Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
294  * on failure. Contrast with g_realloc(), which aborts the program
295  * on failure.
296  *
297  * If @mem is %NULL, behaves the same as g_try_malloc().
298  *
299  * Returns: the allocated memory, or %NULL.
300  */
301 gpointer
g_try_realloc(gpointer mem,gsize n_bytes)302 g_try_realloc (gpointer mem,
303 	       gsize    n_bytes)
304 {
305   gpointer newmem;
306 
307   if (G_LIKELY (n_bytes))
308     newmem = realloc (mem, n_bytes);
309   else
310     {
311       newmem = NULL;
312       free (mem);
313     }
314 
315   TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 1));
316 
317   return newmem;
318 }
319 
320 
321 #define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((b) > 0 && (a) > G_MAXSIZE / (b)))
322 
323 /**
324  * g_malloc_n:
325  * @n_blocks: the number of blocks to allocate
326  * @n_block_bytes: the size of each block in bytes
327  *
328  * This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
329  * but care is taken to detect possible overflow during multiplication.
330  *
331  * Since: 2.24
332  * Returns: a pointer to the allocated memory
333  */
334 gpointer
g_malloc_n(gsize n_blocks,gsize n_block_bytes)335 g_malloc_n (gsize n_blocks,
336 	    gsize n_block_bytes)
337 {
338   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
339     {
340       g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
341                G_STRLOC, n_blocks, n_block_bytes);
342     }
343 
344   return g_malloc (n_blocks * n_block_bytes);
345 }
346 
347 /**
348  * g_malloc0_n:
349  * @n_blocks: the number of blocks to allocate
350  * @n_block_bytes: the size of each block in bytes
351  *
352  * This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
353  * but care is taken to detect possible overflow during multiplication.
354  *
355  * Since: 2.24
356  * Returns: a pointer to the allocated memory
357  */
358 gpointer
g_malloc0_n(gsize n_blocks,gsize n_block_bytes)359 g_malloc0_n (gsize n_blocks,
360 	     gsize n_block_bytes)
361 {
362   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
363     {
364       g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
365                G_STRLOC, n_blocks, n_block_bytes);
366     }
367 
368   return g_malloc0 (n_blocks * n_block_bytes);
369 }
370 
371 /**
372  * g_realloc_n:
373  * @mem: (nullable): the memory to reallocate
374  * @n_blocks: the number of blocks to allocate
375  * @n_block_bytes: the size of each block in bytes
376  *
377  * This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
378  * but care is taken to detect possible overflow during multiplication.
379  *
380  * Since: 2.24
381  * Returns: the new address of the allocated memory
382  */
383 gpointer
g_realloc_n(gpointer mem,gsize n_blocks,gsize n_block_bytes)384 g_realloc_n (gpointer mem,
385 	     gsize    n_blocks,
386 	     gsize    n_block_bytes)
387 {
388   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
389     {
390       g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
391                G_STRLOC, n_blocks, n_block_bytes);
392     }
393 
394   return g_realloc (mem, n_blocks * n_block_bytes);
395 }
396 
397 /**
398  * g_try_malloc_n:
399  * @n_blocks: the number of blocks to allocate
400  * @n_block_bytes: the size of each block in bytes
401  *
402  * This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
403  * but care is taken to detect possible overflow during multiplication.
404  *
405  * Since: 2.24
406  * Returns: the allocated memory, or %NULL.
407  */
408 gpointer
g_try_malloc_n(gsize n_blocks,gsize n_block_bytes)409 g_try_malloc_n (gsize n_blocks,
410 		gsize n_block_bytes)
411 {
412   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
413     return NULL;
414 
415   return g_try_malloc (n_blocks * n_block_bytes);
416 }
417 
418 /**
419  * g_try_malloc0_n:
420  * @n_blocks: the number of blocks to allocate
421  * @n_block_bytes: the size of each block in bytes
422  *
423  * This function is similar to g_try_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
424  * but care is taken to detect possible overflow during multiplication.
425  *
426  * Since: 2.24
427  * Returns: the allocated memory, or %NULL
428  */
429 gpointer
g_try_malloc0_n(gsize n_blocks,gsize n_block_bytes)430 g_try_malloc0_n (gsize n_blocks,
431 		 gsize n_block_bytes)
432 {
433   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
434     return NULL;
435 
436   return g_try_malloc0 (n_blocks * n_block_bytes);
437 }
438 
439 /**
440  * g_try_realloc_n:
441  * @mem: (nullable): previously-allocated memory, or %NULL.
442  * @n_blocks: the number of blocks to allocate
443  * @n_block_bytes: the size of each block in bytes
444  *
445  * This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
446  * but care is taken to detect possible overflow during multiplication.
447  *
448  * Since: 2.24
449  * Returns: the allocated memory, or %NULL.
450  */
451 gpointer
g_try_realloc_n(gpointer mem,gsize n_blocks,gsize n_block_bytes)452 g_try_realloc_n (gpointer mem,
453 		 gsize    n_blocks,
454 		 gsize    n_block_bytes)
455 {
456   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
457     return NULL;
458 
459   return g_try_realloc (mem, n_blocks * n_block_bytes);
460 }
461 
462 /**
463  * g_mem_is_system_malloc:
464  *
465  * Checks whether the allocator used by g_malloc() is the system's
466  * malloc implementation. If it returns %TRUE memory allocated with
467  * malloc() can be used interchangeably with memory allocated using g_malloc().
468  * This function is useful for avoiding an extra copy of allocated memory returned
469  * by a non-GLib-based API.
470  *
471  * Returns: if %TRUE, malloc() and g_malloc() can be mixed.
472  *
473  * Deprecated: 2.46: GLib always uses the system malloc, so this function always
474  * returns %TRUE.
475  **/
476 gboolean
g_mem_is_system_malloc(void)477 g_mem_is_system_malloc (void)
478 {
479   return TRUE;
480 }
481 
482 /**
483  * g_mem_set_vtable:
484  * @vtable: table of memory allocation routines.
485  *
486  * This function used to let you override the memory allocation function.
487  * However, its use was incompatible with the use of global constructors
488  * in GLib and GIO, because those use the GLib allocators before main is
489  * reached. Therefore this function is now deprecated and is just a stub.
490  *
491  * Deprecated: 2.46: This function now does nothing. Use other memory
492  * profiling tools instead
493  */
494 void
g_mem_set_vtable(GMemVTable * vtable)495 g_mem_set_vtable (GMemVTable *vtable)
496 {
497   g_warning (G_STRLOC ": custom memory allocation vtable not supported");
498 }
499 
500 
501 /**
502  * glib_mem_profiler_table:
503  *
504  * Used to be a #GMemVTable containing profiling variants of the memory
505  * allocation functions, but this variable shouldn't be modified anymore.
506  *
507  * Deprecated: 2.46: Use other memory profiling tools instead
508  */
509 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
510 
511 /**
512  * g_mem_profile:
513  *
514  * GLib used to support some tools for memory profiling, but this
515  * no longer works. There are many other useful tools for memory
516  * profiling these days which can be used instead.
517  *
518  * Deprecated: 2.46: Use other memory profiling tools instead
519  */
520 void
g_mem_profile(void)521 g_mem_profile (void)
522 {
523   g_warning (G_STRLOC ": memory profiling not supported");
524 }
525