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