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 "gthreadinit.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_malloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
322 if (!profile_data) /* memory system kiddin' me, eh? */
324 g_mutex_unlock (gmem_profile_mutex);
329 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
330 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
331 (job & PROFILER_RELOC) != 0,
334 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
335 (job & PROFILER_RELOC) != 0,
339 if (job & PROFILER_ALLOC)
341 profile_allocs += n_bytes;
342 if (job & PROFILER_ZINIT)
343 profile_zinit += n_bytes;
346 profile_frees += n_bytes;
348 g_mutex_unlock (gmem_profile_mutex);
352 profile_print_locked (guint *local_data,
355 gboolean need_header = TRUE;
358 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
360 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
361 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
362 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
363 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
365 if (!t_malloc && !t_realloc && !t_free && !t_refree)
367 else if (need_header)
370 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
371 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
372 g_print (" | malloc() | free() | realloc() | realloc() | \n");
373 g_print ("===========|============|============|============|============|===========\n");
375 if (i < MEM_PROFILE_TABLE_SIZE)
376 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
377 i, t_malloc, t_free, t_realloc, t_refree,
378 (t_malloc - t_free + t_realloc - t_refree) * i);
379 else if (i >= MEM_PROFILE_TABLE_SIZE)
380 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
381 i, t_malloc, t_free, t_realloc, t_refree);
384 g_print (" --- none ---\n");
390 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
395 if (G_UNLIKELY (!g_mem_initialized))
396 g_mem_init_nomessage();
398 g_mutex_lock (gmem_profile_mutex);
400 local_allocs = profile_allocs;
401 local_zinit = profile_zinit;
402 local_frees = profile_frees;
406 g_mutex_unlock (gmem_profile_mutex);
410 memcpy (local_data, profile_data,
411 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
413 g_mutex_unlock (gmem_profile_mutex);
415 g_print ("GLib Memory statistics (successful operations):\n");
416 profile_print_locked (local_data, TRUE);
417 g_print ("GLib Memory statistics (failing operations):\n");
418 profile_print_locked (local_data, FALSE);
419 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
422 ((gdouble) local_zinit) / local_allocs * 100.0,
424 ((gdouble) local_frees) / local_allocs * 100.0,
425 local_allocs - local_frees);
429 profiler_try_malloc (gsize n_bytes)
433 #ifdef G_ENABLE_DEBUG
434 if (g_trap_malloc_size == n_bytes)
436 #endif /* G_ENABLE_DEBUG */
438 p = standard_malloc (sizeof (gulong) * 2 + n_bytes);
442 p[0] = 0; /* free count */
443 p[1] = n_bytes; /* length */
444 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
448 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
454 profiler_malloc (gsize n_bytes)
456 gpointer mem = profiler_try_malloc (n_bytes);
465 profiler_calloc (gsize n_blocks,
468 gsize l = n_blocks * n_block_bytes;
471 #ifdef G_ENABLE_DEBUG
472 if (g_trap_malloc_size == l)
474 #endif /* G_ENABLE_DEBUG */
476 p = standard_calloc (1, sizeof (gulong) * 2 + l);
480 p[0] = 0; /* free count */
481 p[1] = l; /* length */
482 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
487 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
495 profiler_free (gpointer mem)
500 if (p[0]) /* free count */
502 g_warning ("free(%p): memory has been freed %lu times already", p + 2, p[0]);
503 profiler_log (PROFILER_FREE,
509 #ifdef G_ENABLE_DEBUG
510 if (g_trap_free_size == p[1])
512 #endif /* G_ENABLE_DEBUG */
514 profiler_log (PROFILER_FREE,
517 memset (p + 2, 0xaa, p[1]);
519 /* for all those that miss standard_free (p); in this place, yes,
520 * we do leak all memory when profiling, and that is intentional
521 * to catch double frees. patch submissions are futile.
528 profiler_try_realloc (gpointer mem,
535 #ifdef G_ENABLE_DEBUG
536 if (g_trap_realloc_size == n_bytes)
538 #endif /* G_ENABLE_DEBUG */
540 if (mem && p[0]) /* free count */
542 g_warning ("realloc(%p, %lu): memory has been freed %lu times already", p + 2, (gulong)n_bytes, p[0]);
543 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
549 p = standard_realloc (mem ? p : NULL, sizeof (gulong) * 2 + n_bytes);
554 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
557 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
561 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
568 profiler_realloc (gpointer mem,
571 mem = profiler_try_realloc (mem, n_bytes);
579 static GMemVTable profiler_table = {
585 profiler_try_realloc,
587 GMemVTable *glib_mem_profiler_table = &profiler_table;
589 #endif /* !G_DISABLE_CHECKS */
591 /* --- MemChunks --- */
592 #ifndef G_ALLOC_AND_FREE
593 typedef struct _GAllocator GAllocator;
594 typedef struct _GMemChunk GMemChunk;
595 #define G_ALLOC_ONLY 1
596 #define G_ALLOC_AND_FREE 2
600 guint alloc_size; /* the size of an atom */
604 g_mem_chunk_new (const gchar *name,
609 GMemChunk *mem_chunk;
610 g_return_val_if_fail (atom_size > 0, NULL);
612 mem_chunk = g_slice_new (GMemChunk);
613 mem_chunk->alloc_size = atom_size;
618 g_mem_chunk_destroy (GMemChunk *mem_chunk)
620 g_return_if_fail (mem_chunk != NULL);
622 g_slice_free (GMemChunk, mem_chunk);
626 g_mem_chunk_alloc (GMemChunk *mem_chunk)
628 g_return_val_if_fail (mem_chunk != NULL, NULL);
630 return g_slice_alloc (mem_chunk->alloc_size);
634 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
636 g_return_val_if_fail (mem_chunk != NULL, NULL);
638 return g_slice_alloc0 (mem_chunk->alloc_size);
642 g_mem_chunk_free (GMemChunk *mem_chunk,
645 g_return_if_fail (mem_chunk != NULL);
647 g_slice_free1 (mem_chunk->alloc_size, mem);
650 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
651 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
652 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
653 void g_mem_chunk_info (void) {}
654 void g_blow_chunks (void) {}
657 g_allocator_new (const gchar *name,
660 static struct _GAllocator {
666 GMemChunk *mem_chunk;
669 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
671 /* some (broken) GAllocator uses depend on non-NULL allocators */
672 return (void*) &dummy;
676 g_allocator_free (GAllocator *allocator)
680 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
681 gboolean g_mem_gc_friendly = TRUE;
683 gboolean g_mem_gc_friendly = FALSE;
687 g_mem_init_nomessage (void)
691 static const GDebugKey keys[] = {
692 { "gc-friendly", 1 },
695 if (g_mem_initialized)
697 /* don't use g_malloc/g_message here */
698 val = _g_getenv_nomalloc ("G_DEBUG", buffer);
699 flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
700 if (flags & 1) /* gc-friendly */
702 g_mem_gc_friendly = TRUE;
704 g_mem_initialized = TRUE;
708 _g_mem_thread_init_noprivate_nomessage (void)
710 /* we may only create mutexes here, locking/
711 * unlocking a mutex does not yet work.
713 g_mem_init_nomessage();
714 #ifndef G_DISABLE_CHECKS
715 gmem_profile_mutex = g_mutex_new ();
720 #include "galiasdef.c"