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 (gulong 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 %lu bytes", G_STRLOC, n_bytes);
142 g_malloc0 (gulong n_bytes)
144 if (G_UNLIKELY (!g_mem_initialized))
145 g_mem_init_nomessage();
146 if (G_LIKELY (n_bytes))
150 mem = glib_mem_vtable.calloc (1, n_bytes);
154 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
161 g_realloc (gpointer mem,
164 if (G_UNLIKELY (!g_mem_initialized))
165 g_mem_init_nomessage();
166 if (G_LIKELY (n_bytes))
168 mem = glib_mem_vtable.realloc (mem, n_bytes);
172 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
176 glib_mem_vtable.free (mem);
182 g_free (gpointer mem)
184 if (G_UNLIKELY (!g_mem_initialized))
185 g_mem_init_nomessage();
187 glib_mem_vtable.free (mem);
191 g_try_malloc (gulong n_bytes)
193 if (G_UNLIKELY (!g_mem_initialized))
194 g_mem_init_nomessage();
195 if (G_LIKELY (n_bytes))
196 return glib_mem_vtable.try_malloc (n_bytes);
202 g_try_malloc0 (gulong n_bytes)
206 mem = g_try_malloc (n_bytes);
209 memset (mem, 0, n_bytes);
215 g_try_realloc (gpointer mem,
218 if (G_UNLIKELY (!g_mem_initialized))
219 g_mem_init_nomessage();
220 if (G_LIKELY (n_bytes))
221 return glib_mem_vtable.try_realloc (mem, n_bytes);
224 glib_mem_vtable.free (mem);
230 fallback_calloc (gsize n_blocks,
233 gsize l = n_blocks * n_block_bytes;
234 gpointer mem = glib_mem_vtable.malloc (l);
242 static gboolean vtable_set = FALSE;
245 * g_mem_is_system_malloc
247 * Checks whether the allocator used by g_malloc() is the system's
248 * malloc implementation. If it returns %TRUE memory allocated with
249 * malloc() can be used interchangeable with memory allocated using g_malloc().
250 * This function is useful for avoiding an extra copy of allocated memory returned
251 * by a non-GLib-based API.
253 * A different allocator can be set using g_mem_set_vtable().
255 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
258 g_mem_is_system_malloc (void)
264 g_mem_set_vtable (GMemVTable *vtable)
268 if (vtable->malloc && vtable->realloc && vtable->free)
270 glib_mem_vtable.malloc = vtable->malloc;
271 glib_mem_vtable.realloc = vtable->realloc;
272 glib_mem_vtable.free = vtable->free;
273 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
274 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
275 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
279 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
282 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
286 /* --- memory profiling and checking --- */
287 #ifdef G_DISABLE_CHECKS
288 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
293 #else /* !G_DISABLE_CHECKS */
300 static guint *profile_data = NULL;
301 static gulong profile_allocs = 0;
302 static gulong profile_zinit = 0;
303 static gulong profile_frees = 0;
304 static GMutex *gmem_profile_mutex = NULL;
305 #ifdef G_ENABLE_DEBUG
306 static volatile gulong g_trap_free_size = 0;
307 static volatile gulong g_trap_realloc_size = 0;
308 static volatile gulong g_trap_malloc_size = 0;
309 #endif /* G_ENABLE_DEBUG */
311 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
314 profiler_log (ProfilerJob job,
318 g_mutex_lock (gmem_profile_mutex);
321 profile_data = standard_calloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8,
322 sizeof (profile_data[0]));
323 if (!profile_data) /* memory system kiddin' me, eh? */
325 g_mutex_unlock (gmem_profile_mutex);
330 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
331 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
332 (job & PROFILER_RELOC) != 0,
335 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
336 (job & PROFILER_RELOC) != 0,
340 if (job & PROFILER_ALLOC)
342 profile_allocs += n_bytes;
343 if (job & PROFILER_ZINIT)
344 profile_zinit += n_bytes;
347 profile_frees += n_bytes;
349 g_mutex_unlock (gmem_profile_mutex);
353 profile_print_locked (guint *local_data,
356 gboolean need_header = TRUE;
359 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
361 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
362 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
363 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
364 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
366 if (!t_malloc && !t_realloc && !t_free && !t_refree)
368 else if (need_header)
371 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
372 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
373 g_print (" | malloc() | free() | realloc() | realloc() | \n");
374 g_print ("===========|============|============|============|============|===========\n");
376 if (i < MEM_PROFILE_TABLE_SIZE)
377 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
378 i, t_malloc, t_free, t_realloc, t_refree,
379 (t_malloc - t_free + t_realloc - t_refree) * i);
380 else if (i >= MEM_PROFILE_TABLE_SIZE)
381 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
382 i, t_malloc, t_free, t_realloc, t_refree);
385 g_print (" --- none ---\n");
391 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
396 if (G_UNLIKELY (!g_mem_initialized))
397 g_mem_init_nomessage();
399 g_mutex_lock (gmem_profile_mutex);
401 local_allocs = profile_allocs;
402 local_zinit = profile_zinit;
403 local_frees = profile_frees;
407 g_mutex_unlock (gmem_profile_mutex);
411 memcpy (local_data, profile_data,
412 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
414 g_mutex_unlock (gmem_profile_mutex);
416 g_print ("GLib Memory statistics (successful operations):\n");
417 profile_print_locked (local_data, TRUE);
418 g_print ("GLib Memory statistics (failing operations):\n");
419 profile_print_locked (local_data, FALSE);
420 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
423 ((gdouble) local_zinit) / local_allocs * 100.0,
425 ((gdouble) local_frees) / local_allocs * 100.0,
426 local_allocs - local_frees);
430 profiler_try_malloc (gsize n_bytes)
434 #ifdef G_ENABLE_DEBUG
435 if (g_trap_malloc_size == n_bytes)
437 #endif /* G_ENABLE_DEBUG */
439 p = standard_malloc (sizeof (gulong) * 2 + n_bytes);
443 p[0] = 0; /* free count */
444 p[1] = n_bytes; /* length */
445 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
449 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
455 profiler_malloc (gsize n_bytes)
457 gpointer mem = profiler_try_malloc (n_bytes);
466 profiler_calloc (gsize n_blocks,
469 gsize l = n_blocks * n_block_bytes;
472 #ifdef G_ENABLE_DEBUG
473 if (g_trap_malloc_size == l)
475 #endif /* G_ENABLE_DEBUG */
477 p = standard_calloc (1, sizeof (gulong) * 2 + l);
481 p[0] = 0; /* free count */
482 p[1] = l; /* length */
483 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
488 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
496 profiler_free (gpointer mem)
501 if (p[0]) /* free count */
503 g_warning ("free(%p): memory has been freed %lu times already", p + 2, p[0]);
504 profiler_log (PROFILER_FREE,
510 #ifdef G_ENABLE_DEBUG
511 if (g_trap_free_size == p[1])
513 #endif /* G_ENABLE_DEBUG */
515 profiler_log (PROFILER_FREE,
518 memset (p + 2, 0xaa, p[1]);
520 /* for all those that miss standard_free (p); in this place, yes,
521 * we do leak all memory when profiling, and that is intentional
522 * to catch double frees. patch submissions are futile.
529 profiler_try_realloc (gpointer mem,
536 #ifdef G_ENABLE_DEBUG
537 if (g_trap_realloc_size == n_bytes)
539 #endif /* G_ENABLE_DEBUG */
541 if (mem && p[0]) /* free count */
543 g_warning ("realloc(%p, %lu): memory has been freed %lu times already", p + 2, (gulong)n_bytes, p[0]);
544 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
550 p = standard_realloc (mem ? p : NULL, sizeof (gulong) * 2 + n_bytes);
555 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
558 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
562 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
569 profiler_realloc (gpointer mem,
572 mem = profiler_try_realloc (mem, n_bytes);
580 static GMemVTable profiler_table = {
586 profiler_try_realloc,
588 GMemVTable *glib_mem_profiler_table = &profiler_table;
590 #endif /* !G_DISABLE_CHECKS */
592 /* --- MemChunks --- */
593 #ifndef G_ALLOC_AND_FREE
594 typedef struct _GAllocator GAllocator;
595 typedef struct _GMemChunk GMemChunk;
596 #define G_ALLOC_ONLY 1
597 #define G_ALLOC_AND_FREE 2
601 guint alloc_size; /* the size of an atom */
605 g_mem_chunk_new (const gchar *name,
610 GMemChunk *mem_chunk;
611 g_return_val_if_fail (atom_size > 0, NULL);
613 mem_chunk = g_slice_new (GMemChunk);
614 mem_chunk->alloc_size = atom_size;
619 g_mem_chunk_destroy (GMemChunk *mem_chunk)
621 g_return_if_fail (mem_chunk != NULL);
623 g_slice_free (GMemChunk, mem_chunk);
627 g_mem_chunk_alloc (GMemChunk *mem_chunk)
629 g_return_val_if_fail (mem_chunk != NULL, NULL);
631 return g_slice_alloc (mem_chunk->alloc_size);
635 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
637 g_return_val_if_fail (mem_chunk != NULL, NULL);
639 return g_slice_alloc0 (mem_chunk->alloc_size);
643 g_mem_chunk_free (GMemChunk *mem_chunk,
646 g_return_if_fail (mem_chunk != NULL);
648 g_slice_free1 (mem_chunk->alloc_size, mem);
651 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
652 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
653 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
654 void g_mem_chunk_info (void) {}
655 void g_blow_chunks (void) {}
658 g_allocator_new (const gchar *name,
661 static struct _GAllocator {
667 GMemChunk *mem_chunk;
670 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
672 /* some (broken) GAllocator uses depend on non-NULL allocators */
673 return (void*) &dummy;
677 g_allocator_free (GAllocator *allocator)
681 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
682 gboolean g_mem_gc_friendly = TRUE;
684 gboolean g_mem_gc_friendly = FALSE;
688 g_mem_init_nomessage (void)
692 const GDebugKey keys[] = {
693 { "gc-friendly", 1 },
696 if (g_mem_initialized)
698 /* don't use g_malloc/g_message here */
699 val = _g_getenv_nomalloc ("G_DEBUG", buffer);
700 flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
701 if (flags & 1) /* gc-friendly */
703 g_mem_gc_friendly = TRUE;
705 g_mem_initialized = TRUE;
709 _g_mem_thread_init_noprivate_nomessage (void)
711 /* we may only create mutexes here, locking/
712 * unlocking a mutex does not yet work.
714 g_mem_init_nomessage();
715 #ifndef G_DISABLE_CHECKS
716 gmem_profile_mutex = g_mutex_new ();
721 #include "galiasdef.c"