• 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, 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  * It's important to match g_malloc() (and wrappers such as g_new()) with
75  * g_free(), g_slice_alloc() (and wrappers such as g_slice_new()) with
76  * g_slice_free(), plain malloc() with free(), and (if you're using C++)
77  * new with delete and new[] with delete[]. Otherwise bad things can happen,
78  * since these allocators may use different memory pools (and new/delete call
79  * constructors and destructors).
80  */
81 
82 /* --- functions --- */
83 /**
84  * g_malloc:
85  * @n_bytes: the number of bytes to allocate
86  *
87  * Allocates @n_bytes bytes of memory.
88  * If @n_bytes is 0 it returns %NULL.
89  *
90  * Returns: a pointer to the allocated memory
91  */
92 gpointer
g_malloc(gsize n_bytes)93 g_malloc (gsize n_bytes)
94 {
95   if (G_LIKELY (n_bytes))
96     {
97       gpointer mem;
98 
99       mem = malloc (n_bytes);
100       TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 0));
101       if (mem)
102 	return mem;
103 
104       g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
105                G_STRLOC, n_bytes);
106     }
107 
108   TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 0, 0));
109 
110   return NULL;
111 }
112 
113 /**
114  * g_malloc0:
115  * @n_bytes: the number of bytes to allocate
116  *
117  * Allocates @n_bytes bytes of memory, initialized to 0's.
118  * If @n_bytes is 0 it returns %NULL.
119  *
120  * Returns: a pointer to the allocated memory
121  */
122 gpointer
g_malloc0(gsize n_bytes)123 g_malloc0 (gsize n_bytes)
124 {
125   if (G_LIKELY (n_bytes))
126     {
127       gpointer mem;
128 
129       mem = calloc (1, n_bytes);
130       TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 1, 0));
131       if (mem)
132 	return mem;
133 
134       g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
135                G_STRLOC, n_bytes);
136     }
137 
138   TRACE(GLIB_MEM_ALLOC((void*) NULL, (int) n_bytes, 1, 0));
139 
140   return NULL;
141 }
142 
143 /**
144  * g_realloc:
145  * @mem: (nullable): the memory to reallocate
146  * @n_bytes: new size of the memory in bytes
147  *
148  * Reallocates the memory pointed to by @mem, so that it now has space for
149  * @n_bytes bytes of memory. It returns the new address of the memory, which may
150  * have been moved. @mem may be %NULL, in which case it's considered to
151  * have zero-length. @n_bytes may be 0, in which case %NULL will be returned
152  * and @mem will be freed unless it is %NULL.
153  *
154  * Returns: the new address of the allocated memory
155  */
156 gpointer
g_realloc(gpointer mem,gsize n_bytes)157 g_realloc (gpointer mem,
158 	   gsize    n_bytes)
159 {
160   gpointer newmem;
161 
162   if (G_LIKELY (n_bytes))
163     {
164       newmem = realloc (mem, n_bytes);
165       TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 0));
166       if (newmem)
167 	return newmem;
168 
169       g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
170                G_STRLOC, n_bytes);
171     }
172 
173   free (mem);
174 
175   TRACE (GLIB_MEM_REALLOC((void*) NULL, (void*)mem, 0, 0));
176 
177   return NULL;
178 }
179 
180 /**
181  * g_free:
182  * @mem: (nullable): the memory to free
183  *
184  * Frees the memory pointed to by @mem.
185  *
186  * If @mem is %NULL it simply returns, so there is no need to check @mem
187  * against %NULL before calling this function.
188  */
189 void
g_free(gpointer mem)190 g_free (gpointer mem)
191 {
192   free (mem);
193   TRACE(GLIB_MEM_FREE((void*) mem));
194 }
195 
196 /**
197  * g_clear_pointer: (skip)
198  * @pp: (not nullable): a pointer to a variable, struct member etc. holding a
199  *    pointer
200  * @destroy: a function to which a gpointer can be passed, to destroy *@pp
201  *
202  * Clears a reference to a variable.
203  *
204  * @pp must not be %NULL.
205  *
206  * If the reference is %NULL then this function does nothing.
207  * Otherwise, the variable is destroyed using @destroy and the
208  * pointer is set to %NULL.
209  *
210  * A macro is also included that allows this function to be used without
211  * pointer casts. This will mask any warnings about incompatible function types
212  * or calling conventions, so you must ensure that your @destroy function is
213  * compatible with being called as `GDestroyNotify` using the standard calling
214  * convention for the platform that GLib was compiled for; otherwise the program
215  * will experience undefined behaviour.
216  *
217  * Since: 2.34
218  **/
219 #undef g_clear_pointer
220 void
g_clear_pointer(gpointer * pp,GDestroyNotify destroy)221 g_clear_pointer (gpointer      *pp,
222                  GDestroyNotify destroy)
223 {
224   gpointer _p;
225 
226   _p = *pp;
227   if (_p)
228     {
229       *pp = NULL;
230       destroy (_p);
231     }
232 }
233 
234 /**
235  * g_try_malloc:
236  * @n_bytes: number of bytes to allocate.
237  *
238  * Attempts to allocate @n_bytes, and returns %NULL on failure.
239  * Contrast with g_malloc(), which aborts the program on failure.
240  *
241  * Returns: the allocated memory, or %NULL.
242  */
243 gpointer
g_try_malloc(gsize n_bytes)244 g_try_malloc (gsize n_bytes)
245 {
246   gpointer mem;
247 
248   if (G_LIKELY (n_bytes))
249     mem = malloc (n_bytes);
250   else
251     mem = NULL;
252 
253   TRACE (GLIB_MEM_ALLOC((void*) mem, (unsigned int) n_bytes, 0, 1));
254 
255   return mem;
256 }
257 
258 /**
259  * g_try_malloc0:
260  * @n_bytes: number of bytes to allocate
261  *
262  * Attempts to allocate @n_bytes, initialized to 0's, and returns %NULL on
263  * failure. Contrast with g_malloc0(), which aborts the program on failure.
264  *
265  * Since: 2.8
266  * Returns: the allocated memory, or %NULL
267  */
268 gpointer
g_try_malloc0(gsize n_bytes)269 g_try_malloc0 (gsize n_bytes)
270 {
271   gpointer mem;
272 
273   if (G_LIKELY (n_bytes))
274     mem = calloc (1, n_bytes);
275   else
276     mem = NULL;
277 
278   return mem;
279 }
280 
281 /**
282  * g_try_realloc:
283  * @mem: (nullable): previously-allocated memory, or %NULL.
284  * @n_bytes: number of bytes to allocate.
285  *
286  * Attempts to realloc @mem to a new size, @n_bytes, and returns %NULL
287  * on failure. Contrast with g_realloc(), which aborts the program
288  * on failure.
289  *
290  * If @mem is %NULL, behaves the same as g_try_malloc().
291  *
292  * Returns: the allocated memory, or %NULL.
293  */
294 gpointer
g_try_realloc(gpointer mem,gsize n_bytes)295 g_try_realloc (gpointer mem,
296 	       gsize    n_bytes)
297 {
298   gpointer newmem;
299 
300   if (G_LIKELY (n_bytes))
301     newmem = realloc (mem, n_bytes);
302   else
303     {
304       newmem = NULL;
305       free (mem);
306     }
307 
308   TRACE (GLIB_MEM_REALLOC((void*) newmem, (void*)mem, (unsigned int) n_bytes, 1));
309 
310   return newmem;
311 }
312 
313 
314 #define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((b) > 0 && (a) > G_MAXSIZE / (b)))
315 
316 /**
317  * g_malloc_n:
318  * @n_blocks: the number of blocks to allocate
319  * @n_block_bytes: the size of each block in bytes
320  *
321  * This function is similar to g_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
322  * but care is taken to detect possible overflow during multiplication.
323  *
324  * Since: 2.24
325  * Returns: a pointer to the allocated memory
326  */
327 gpointer
g_malloc_n(gsize n_blocks,gsize n_block_bytes)328 g_malloc_n (gsize n_blocks,
329 	    gsize n_block_bytes)
330 {
331   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
332     {
333       g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
334                G_STRLOC, n_blocks, n_block_bytes);
335     }
336 
337   return g_malloc (n_blocks * n_block_bytes);
338 }
339 
340 /**
341  * g_malloc0_n:
342  * @n_blocks: the number of blocks to allocate
343  * @n_block_bytes: the size of each block in bytes
344  *
345  * This function is similar to g_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
346  * but care is taken to detect possible overflow during multiplication.
347  *
348  * Since: 2.24
349  * Returns: a pointer to the allocated memory
350  */
351 gpointer
g_malloc0_n(gsize n_blocks,gsize n_block_bytes)352 g_malloc0_n (gsize n_blocks,
353 	     gsize n_block_bytes)
354 {
355   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
356     {
357       g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
358                G_STRLOC, n_blocks, n_block_bytes);
359     }
360 
361   return g_malloc0 (n_blocks * n_block_bytes);
362 }
363 
364 /**
365  * g_realloc_n:
366  * @mem: (nullable): the memory to reallocate
367  * @n_blocks: the number of blocks to allocate
368  * @n_block_bytes: the size of each block in bytes
369  *
370  * This function is similar to g_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
371  * but care is taken to detect possible overflow during multiplication.
372  *
373  * Since: 2.24
374  * Returns: the new address of the allocated memory
375  */
376 gpointer
g_realloc_n(gpointer mem,gsize n_blocks,gsize n_block_bytes)377 g_realloc_n (gpointer mem,
378 	     gsize    n_blocks,
379 	     gsize    n_block_bytes)
380 {
381   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
382     {
383       g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
384                G_STRLOC, n_blocks, n_block_bytes);
385     }
386 
387   return g_realloc (mem, n_blocks * n_block_bytes);
388 }
389 
390 /**
391  * g_try_malloc_n:
392  * @n_blocks: the number of blocks to allocate
393  * @n_block_bytes: the size of each block in bytes
394  *
395  * This function is similar to g_try_malloc(), allocating (@n_blocks * @n_block_bytes) bytes,
396  * but care is taken to detect possible overflow during multiplication.
397  *
398  * Since: 2.24
399  * Returns: the allocated memory, or %NULL.
400  */
401 gpointer
g_try_malloc_n(gsize n_blocks,gsize n_block_bytes)402 g_try_malloc_n (gsize n_blocks,
403 		gsize n_block_bytes)
404 {
405   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
406     return NULL;
407 
408   return g_try_malloc (n_blocks * n_block_bytes);
409 }
410 
411 /**
412  * g_try_malloc0_n:
413  * @n_blocks: the number of blocks to allocate
414  * @n_block_bytes: the size of each block in bytes
415  *
416  * This function is similar to g_try_malloc0(), allocating (@n_blocks * @n_block_bytes) bytes,
417  * but care is taken to detect possible overflow during multiplication.
418  *
419  * Since: 2.24
420  * Returns: the allocated memory, or %NULL
421  */
422 gpointer
g_try_malloc0_n(gsize n_blocks,gsize n_block_bytes)423 g_try_malloc0_n (gsize n_blocks,
424 		 gsize n_block_bytes)
425 {
426   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
427     return NULL;
428 
429   return g_try_malloc0 (n_blocks * n_block_bytes);
430 }
431 
432 /**
433  * g_try_realloc_n:
434  * @mem: (nullable): previously-allocated memory, or %NULL.
435  * @n_blocks: the number of blocks to allocate
436  * @n_block_bytes: the size of each block in bytes
437  *
438  * This function is similar to g_try_realloc(), allocating (@n_blocks * @n_block_bytes) bytes,
439  * but care is taken to detect possible overflow during multiplication.
440  *
441  * Since: 2.24
442  * Returns: the allocated memory, or %NULL.
443  */
444 gpointer
g_try_realloc_n(gpointer mem,gsize n_blocks,gsize n_block_bytes)445 g_try_realloc_n (gpointer mem,
446 		 gsize    n_blocks,
447 		 gsize    n_block_bytes)
448 {
449   if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
450     return NULL;
451 
452   return g_try_realloc (mem, n_blocks * n_block_bytes);
453 }
454 
455 /**
456  * g_mem_is_system_malloc:
457  *
458  * Checks whether the allocator used by g_malloc() is the system's
459  * malloc implementation. If it returns %TRUE memory allocated with
460  * malloc() can be used interchangeable with memory allocated using g_malloc().
461  * This function is useful for avoiding an extra copy of allocated memory returned
462  * by a non-GLib-based API.
463  *
464  * Returns: if %TRUE, malloc() and g_malloc() can be mixed.
465  *
466  * Deprecated: 2.46: GLib always uses the system malloc, so this function always
467  * returns %TRUE.
468  **/
469 gboolean
g_mem_is_system_malloc(void)470 g_mem_is_system_malloc (void)
471 {
472   return TRUE;
473 }
474 
475 /**
476  * g_mem_set_vtable:
477  * @vtable: table of memory allocation routines.
478  *
479  * This function used to let you override the memory allocation function.
480  * However, its use was incompatible with the use of global constructors
481  * in GLib and GIO, because those use the GLib allocators before main is
482  * reached. Therefore this function is now deprecated and is just a stub.
483  *
484  * Deprecated: 2.46: This function now does nothing. Use other memory
485  * profiling tools instead
486  */
487 void
g_mem_set_vtable(GMemVTable * vtable)488 g_mem_set_vtable (GMemVTable *vtable)
489 {
490   g_warning (G_STRLOC ": custom memory allocation vtable not supported");
491 }
492 
493 
494 /**
495  * glib_mem_profiler_table:
496  *
497  * Used to be a #GMemVTable containing profiling variants of the memory
498  * allocation functions, but this variable shouldn't be modified anymore.
499  *
500  * Deprecated: 2.46: Use other memory profiling tools instead
501  */
502 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
503 
504 /**
505  * g_mem_profile:
506  *
507  * GLib used to support some tools for memory profiling, but this
508  * no longer works. There are many other useful tools for memory
509  * profiling these days which can be used instead.
510  *
511  * Deprecated: 2.46: Use other memory profiling tools instead
512  */
513 void
g_mem_profile(void)514 g_mem_profile (void)
515 {
516   g_warning (G_STRLOC ": memory profiling not supported");
517 }
518