1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
38 #include "gthreadprivate.h"
41 #define MEM_PROFILE_TABLE_SIZE 4096
45 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
47 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
48 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
49 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
50 * g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
53 /* --- prototypes --- */
54 static gboolean g_mem_initialized
= FALSE
;
55 static void g_mem_init_nomessage (void);
58 /* --- malloc wrappers --- */
59 #ifndef REALLOC_0_WORKS
61 standard_realloc (gpointer mem
,
65 return malloc (n_bytes
);
67 return realloc (mem
, n_bytes
);
69 #endif /* !REALLOC_0_WORKS */
71 #ifdef SANE_MALLOC_PROTOS
72 # define standard_malloc malloc
73 # ifdef REALLOC_0_WORKS
74 # define standard_realloc realloc
75 # endif /* REALLOC_0_WORKS */
76 # define standard_free free
77 # define standard_calloc calloc
78 # define standard_try_malloc malloc
79 # define standard_try_realloc realloc
80 #else /* !SANE_MALLOC_PROTOS */
82 standard_malloc (gsize n_bytes
)
84 return malloc (n_bytes
);
86 # ifdef REALLOC_0_WORKS
88 standard_realloc (gpointer mem
,
91 return realloc (mem
, n_bytes
);
93 # endif /* REALLOC_0_WORKS */
95 standard_free (gpointer mem
)
100 standard_calloc (gsize n_blocks
,
103 return calloc (n_blocks
, n_bytes
);
105 #define standard_try_malloc standard_malloc
106 #define standard_try_realloc standard_realloc
107 #endif /* !SANE_MALLOC_PROTOS */
110 /* --- variables --- */
111 static GMemVTable glib_mem_vtable
= {
117 standard_try_realloc
,
121 /* --- functions --- */
123 g_malloc (gsize n_bytes
)
125 if (G_UNLIKELY (!g_mem_initialized
))
126 g_mem_init_nomessage();
127 if (G_LIKELY (n_bytes
))
131 mem
= glib_mem_vtable
.malloc (n_bytes
);
135 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
143 g_malloc0 (gsize n_bytes
)
145 if (G_UNLIKELY (!g_mem_initialized
))
146 g_mem_init_nomessage();
147 if (G_LIKELY (n_bytes
))
151 mem
= glib_mem_vtable
.calloc (1, n_bytes
);
155 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
163 g_realloc (gpointer mem
,
166 if (G_UNLIKELY (!g_mem_initialized
))
167 g_mem_init_nomessage();
168 if (G_LIKELY (n_bytes
))
170 mem
= glib_mem_vtable
.realloc (mem
, n_bytes
);
174 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT
" bytes",
179 glib_mem_vtable
.free (mem
);
185 g_free (gpointer mem
)
187 if (G_UNLIKELY (!g_mem_initialized
))
188 g_mem_init_nomessage();
190 glib_mem_vtable
.free (mem
);
194 g_try_malloc (gsize n_bytes
)
196 if (G_UNLIKELY (!g_mem_initialized
))
197 g_mem_init_nomessage();
198 if (G_LIKELY (n_bytes
))
199 return glib_mem_vtable
.try_malloc (n_bytes
);
205 g_try_malloc0 (gsize n_bytes
)
209 mem
= g_try_malloc (n_bytes
);
212 memset (mem
, 0, n_bytes
);
218 g_try_realloc (gpointer mem
,
221 if (G_UNLIKELY (!g_mem_initialized
))
222 g_mem_init_nomessage();
223 if (G_LIKELY (n_bytes
))
224 return glib_mem_vtable
.try_realloc (mem
, n_bytes
);
227 glib_mem_vtable
.free (mem
);
233 fallback_calloc (gsize n_blocks
,
236 gsize l
= n_blocks
* n_block_bytes
;
237 gpointer mem
= glib_mem_vtable
.malloc (l
);
245 static gboolean vtable_set
= FALSE
;
248 * g_mem_is_system_malloc
250 * Checks whether the allocator used by g_malloc() is the system's
251 * malloc implementation. If it returns %TRUE memory allocated with
252 * malloc() can be used interchangeable with memory allocated using g_malloc().
253 * This function is useful for avoiding an extra copy of allocated memory returned
254 * by a non-GLib-based API.
256 * A different allocator can be set using g_mem_set_vtable().
258 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
261 g_mem_is_system_malloc (void)
267 g_mem_set_vtable (GMemVTable
*vtable
)
271 if (vtable
->malloc
&& vtable
->realloc
&& vtable
->free
)
273 glib_mem_vtable
.malloc
= vtable
->malloc
;
274 glib_mem_vtable
.realloc
= vtable
->realloc
;
275 glib_mem_vtable
.free
= vtable
->free
;
276 glib_mem_vtable
.calloc
= vtable
->calloc
? vtable
->calloc
: fallback_calloc
;
277 glib_mem_vtable
.try_malloc
= vtable
->try_malloc
? vtable
->try_malloc
: glib_mem_vtable
.malloc
;
278 glib_mem_vtable
.try_realloc
= vtable
->try_realloc
? vtable
->try_realloc
: glib_mem_vtable
.realloc
;
282 g_warning (G_STRLOC
": memory allocation vtable lacks one of malloc(), realloc() or free()");
285 g_warning (G_STRLOC
": memory allocation vtable can only be set once at startup");
289 /* --- memory profiling and checking --- */
290 #ifdef G_DISABLE_CHECKS
291 GMemVTable
*glib_mem_profiler_table
= &glib_mem_vtable
;
296 #else /* !G_DISABLE_CHECKS */
303 static guint
*profile_data
= NULL
;
304 static gsize profile_allocs
= 0;
305 static gsize profile_zinit
= 0;
306 static gsize profile_frees
= 0;
307 static GMutex
*gmem_profile_mutex
= NULL
;
308 #ifdef G_ENABLE_DEBUG
309 static volatile gsize g_trap_free_size
= 0;
310 static volatile gsize g_trap_realloc_size
= 0;
311 static volatile gsize g_trap_malloc_size
= 0;
312 #endif /* G_ENABLE_DEBUG */
314 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
317 profiler_log (ProfilerJob job
,
321 g_mutex_lock (gmem_profile_mutex
);
324 profile_data
= standard_calloc ((MEM_PROFILE_TABLE_SIZE
+ 1) * 8,
325 sizeof (profile_data
[0]));
326 if (!profile_data
) /* memory system kiddin' me, eh? */
328 g_mutex_unlock (gmem_profile_mutex
);
333 if (n_bytes
< MEM_PROFILE_TABLE_SIZE
)
334 profile_data
[n_bytes
+ PROFILE_TABLE ((job
& PROFILER_ALLOC
) != 0,
335 (job
& PROFILER_RELOC
) != 0,
338 profile_data
[MEM_PROFILE_TABLE_SIZE
+ PROFILE_TABLE ((job
& PROFILER_ALLOC
) != 0,
339 (job
& PROFILER_RELOC
) != 0,
343 if (job
& PROFILER_ALLOC
)
345 profile_allocs
+= n_bytes
;
346 if (job
& PROFILER_ZINIT
)
347 profile_zinit
+= n_bytes
;
350 profile_frees
+= n_bytes
;
352 g_mutex_unlock (gmem_profile_mutex
);
356 profile_print_locked (guint
*local_data
,
359 gboolean need_header
= TRUE
;
362 for (i
= 0; i
<= MEM_PROFILE_TABLE_SIZE
; i
++)
364 glong t_malloc
= local_data
[i
+ PROFILE_TABLE (1, 0, success
)];
365 glong t_realloc
= local_data
[i
+ PROFILE_TABLE (1, 1, success
)];
366 glong t_free
= local_data
[i
+ PROFILE_TABLE (0, 0, success
)];
367 glong t_refree
= local_data
[i
+ PROFILE_TABLE (0, 1, success
)];
369 if (!t_malloc
&& !t_realloc
&& !t_free
&& !t_refree
)
371 else if (need_header
)
374 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
375 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
376 g_print (" | malloc() | free() | realloc() | realloc() | \n");
377 g_print ("===========|============|============|============|============|===========\n");
379 if (i
< MEM_PROFILE_TABLE_SIZE
)
380 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
381 i
, t_malloc
, t_free
, t_realloc
, t_refree
,
382 (t_malloc
- t_free
+ t_realloc
- t_refree
) * i
);
383 else if (i
>= MEM_PROFILE_TABLE_SIZE
)
384 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
385 i
, t_malloc
, t_free
, t_realloc
, t_refree
);
388 g_print (" --- none ---\n");
394 guint local_data
[(MEM_PROFILE_TABLE_SIZE
+ 1) * 8 * sizeof (profile_data
[0])];
399 if (G_UNLIKELY (!g_mem_initialized
))
400 g_mem_init_nomessage();
402 g_mutex_lock (gmem_profile_mutex
);
404 local_allocs
= profile_allocs
;
405 local_zinit
= profile_zinit
;
406 local_frees
= profile_frees
;
410 g_mutex_unlock (gmem_profile_mutex
);
414 memcpy (local_data
, profile_data
,
415 (MEM_PROFILE_TABLE_SIZE
+ 1) * 8 * sizeof (profile_data
[0]));
417 g_mutex_unlock (gmem_profile_mutex
);
419 g_print ("GLib Memory statistics (successful operations):\n");
420 profile_print_locked (local_data
, TRUE
);
421 g_print ("GLib Memory statistics (failing operations):\n");
422 profile_print_locked (local_data
, FALSE
);
423 g_print ("Total bytes: allocated=%"G_GSIZE_FORMAT
", "
424 "zero-initialized=%"G_GSIZE_FORMAT
" (%.2f%%), "
425 "freed=%"G_GSIZE_FORMAT
" (%.2f%%), "
426 "remaining=%"G_GSIZE_FORMAT
"\n",
429 ((gdouble
) local_zinit
) / local_allocs
* 100.0,
431 ((gdouble
) local_frees
) / local_allocs
* 100.0,
432 local_allocs
- local_frees
);
436 profiler_try_malloc (gsize n_bytes
)
440 #ifdef G_ENABLE_DEBUG
441 if (g_trap_malloc_size
== n_bytes
)
443 #endif /* G_ENABLE_DEBUG */
445 p
= standard_malloc (sizeof (gsize
) * 2 + n_bytes
);
449 p
[0] = 0; /* free count */
450 p
[1] = n_bytes
; /* length */
451 profiler_log (PROFILER_ALLOC
, n_bytes
, TRUE
);
455 profiler_log (PROFILER_ALLOC
, n_bytes
, FALSE
);
461 profiler_malloc (gsize n_bytes
)
463 gpointer mem
= profiler_try_malloc (n_bytes
);
472 profiler_calloc (gsize n_blocks
,
475 gsize l
= n_blocks
* n_block_bytes
;
478 #ifdef G_ENABLE_DEBUG
479 if (g_trap_malloc_size
== l
)
481 #endif /* G_ENABLE_DEBUG */
483 p
= standard_calloc (1, sizeof (gsize
) * 2 + l
);
487 p
[0] = 0; /* free count */
488 p
[1] = l
; /* length */
489 profiler_log (PROFILER_ALLOC
| PROFILER_ZINIT
, l
, TRUE
);
494 profiler_log (PROFILER_ALLOC
| PROFILER_ZINIT
, l
, FALSE
);
502 profiler_free (gpointer mem
)
507 if (p
[0]) /* free count */
509 g_warning ("free(%p): memory has been freed %"G_GSIZE_FORMAT
" times already",
511 profiler_log (PROFILER_FREE
,
517 #ifdef G_ENABLE_DEBUG
518 if (g_trap_free_size
== p
[1])
520 #endif /* G_ENABLE_DEBUG */
522 profiler_log (PROFILER_FREE
,
525 memset (p
+ 2, 0xaa, p
[1]);
527 /* for all those that miss standard_free (p); in this place, yes,
528 * we do leak all memory when profiling, and that is intentional
529 * to catch double frees. patch submissions are futile.
536 profiler_try_realloc (gpointer mem
,
543 #ifdef G_ENABLE_DEBUG
544 if (g_trap_realloc_size
== n_bytes
)
546 #endif /* G_ENABLE_DEBUG */
548 if (mem
&& p
[0]) /* free count */
550 g_warning ("realloc(%p, %"G_GSIZE_FORMAT
"): "
551 "memory has been freed %"G_GSIZE_FORMAT
" times already",
552 p
+ 2, (gsize
) n_bytes
, p
[0]);
553 profiler_log (PROFILER_ALLOC
| PROFILER_RELOC
, n_bytes
, FALSE
);
559 p
= standard_realloc (mem
? p
: NULL
, sizeof (gsize
) * 2 + n_bytes
);
564 profiler_log (PROFILER_FREE
| PROFILER_RELOC
, p
[1], TRUE
);
567 profiler_log (PROFILER_ALLOC
| PROFILER_RELOC
, p
[1], TRUE
);
571 profiler_log (PROFILER_ALLOC
| PROFILER_RELOC
, n_bytes
, FALSE
);
578 profiler_realloc (gpointer mem
,
581 mem
= profiler_try_realloc (mem
, n_bytes
);
589 static GMemVTable profiler_table
= {
595 profiler_try_realloc
,
597 GMemVTable
*glib_mem_profiler_table
= &profiler_table
;
599 #endif /* !G_DISABLE_CHECKS */
601 /* --- MemChunks --- */
602 #ifndef G_ALLOC_AND_FREE
603 typedef struct _GAllocator GAllocator
;
604 typedef struct _GMemChunk GMemChunk
;
605 #define G_ALLOC_ONLY 1
606 #define G_ALLOC_AND_FREE 2
610 guint alloc_size
; /* the size of an atom */
614 g_mem_chunk_new (const gchar
*name
,
619 GMemChunk
*mem_chunk
;
620 g_return_val_if_fail (atom_size
> 0, NULL
);
622 mem_chunk
= g_slice_new (GMemChunk
);
623 mem_chunk
->alloc_size
= atom_size
;
628 g_mem_chunk_destroy (GMemChunk
*mem_chunk
)
630 g_return_if_fail (mem_chunk
!= NULL
);
632 g_slice_free (GMemChunk
, mem_chunk
);
636 g_mem_chunk_alloc (GMemChunk
*mem_chunk
)
638 g_return_val_if_fail (mem_chunk
!= NULL
, NULL
);
640 return g_slice_alloc (mem_chunk
->alloc_size
);
644 g_mem_chunk_alloc0 (GMemChunk
*mem_chunk
)
646 g_return_val_if_fail (mem_chunk
!= NULL
, NULL
);
648 return g_slice_alloc0 (mem_chunk
->alloc_size
);
652 g_mem_chunk_free (GMemChunk
*mem_chunk
,
655 g_return_if_fail (mem_chunk
!= NULL
);
657 g_slice_free1 (mem_chunk
->alloc_size
, mem
);
660 void g_mem_chunk_clean (GMemChunk
*mem_chunk
) {}
661 void g_mem_chunk_reset (GMemChunk
*mem_chunk
) {}
662 void g_mem_chunk_print (GMemChunk
*mem_chunk
) {}
663 void g_mem_chunk_info (void) {}
664 void g_blow_chunks (void) {}
667 g_allocator_new (const gchar
*name
,
670 static struct _GAllocator
{
676 GMemChunk
*mem_chunk
;
679 "GAllocator is deprecated", 1, TRUE
, 0, NULL
, NULL
, NULL
,
681 /* some (broken) GAllocator uses depend on non-NULL allocators */
682 return (void*) &dummy
;
686 g_allocator_free (GAllocator
*allocator
)
690 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
691 gboolean g_mem_gc_friendly
= TRUE
;
693 gboolean g_mem_gc_friendly
= FALSE
;
697 g_mem_init_nomessage (void)
701 const GDebugKey keys
[] = {
702 { "gc-friendly", 1 },
705 if (g_mem_initialized
)
707 /* don't use g_malloc/g_message here */
708 val
= _g_getenv_nomalloc ("G_DEBUG", buffer
);
709 flags
= !val
? 0 : g_parse_debug_string (val
, keys
, G_N_ELEMENTS (keys
));
710 if (flags
& 1) /* gc-friendly */
712 g_mem_gc_friendly
= TRUE
;
714 g_mem_initialized
= TRUE
;
718 _g_mem_thread_init_noprivate_nomessage (void)
720 /* we may only create mutexes here, locking/
721 * unlocking a mutex does not yet work.
723 g_mem_init_nomessage();
724 #ifndef G_DISABLE_CHECKS
725 gmem_profile_mutex
= g_mutex_new ();
730 #include "galiasdef.c"