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"
42 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
44 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
45 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
46 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
47 * if ENABLE_GC_FRIENDLY is defined, freed memory should be 0-wiped.
50 #define MEM_PROFILE_TABLE_SIZE 4096
52 #ifndef REALLOC_0_WORKS
54 standard_realloc (gpointer mem,
58 return malloc (n_bytes);
60 return realloc (mem, n_bytes);
62 #endif /* !REALLOC_0_WORKS */
64 #ifdef SANE_MALLOC_PROTOS
65 # define standard_malloc malloc
66 # ifdef REALLOC_0_WORKS
67 # define standard_realloc realloc
68 # endif /* REALLOC_0_WORKS */
69 # define standard_free free
70 # define standard_calloc calloc
71 # define standard_try_malloc malloc
72 # define standard_try_realloc realloc
73 #else /* !SANE_MALLOC_PROTOS */
75 standard_malloc (gsize n_bytes)
77 return malloc (n_bytes);
79 # ifdef REALLOC_0_WORKS
81 standard_realloc (gpointer mem,
84 return realloc (mem, n_bytes);
86 # endif /* REALLOC_0_WORKS */
88 standard_free (gpointer mem)
93 standard_calloc (gsize n_blocks,
96 return calloc (n_blocks, n_bytes);
98 #define standard_try_malloc standard_malloc
99 #define standard_try_realloc standard_realloc
100 #endif /* !SANE_MALLOC_PROTOS */
103 /* --- variables --- */
104 static GMemVTable glib_mem_vtable = {
110 standard_try_realloc,
114 /* --- functions --- */
116 g_malloc (gulong n_bytes)
122 mem = glib_mem_vtable.malloc (n_bytes);
126 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
133 g_malloc0 (gulong n_bytes)
139 mem = glib_mem_vtable.calloc (1, n_bytes);
143 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
150 g_realloc (gpointer mem,
155 mem = glib_mem_vtable.realloc (mem, n_bytes);
159 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
163 glib_mem_vtable.free (mem);
169 g_free (gpointer mem)
172 glib_mem_vtable.free (mem);
176 g_try_malloc (gulong n_bytes)
179 return glib_mem_vtable.try_malloc (n_bytes);
185 g_try_malloc0 (gulong n_bytes)
189 mem = g_try_malloc (n_bytes);
192 memset (mem, 0, n_bytes);
198 g_try_realloc (gpointer mem,
202 return glib_mem_vtable.try_realloc (mem, n_bytes);
205 glib_mem_vtable.free (mem);
211 fallback_calloc (gsize n_blocks,
214 gsize l = n_blocks * n_block_bytes;
215 gpointer mem = glib_mem_vtable.malloc (l);
223 static gboolean vtable_set = FALSE;
226 * g_mem_is_system_malloc
228 * Checks whether the allocator used by g_malloc() is the system's
229 * malloc implementation. If it returns %TRUE memory allocated with
230 * malloc() can be used interchangeable with memory allocated using g_malloc().
231 * This function is useful for avoiding an extra copy of allocated memory returned
232 * by a non-GLib-based API.
234 * A different allocator can be set using g_mem_set_vtable().
236 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
239 g_mem_is_system_malloc (void)
245 g_mem_set_vtable (GMemVTable *vtable)
249 if (vtable->malloc && vtable->realloc && vtable->free)
251 glib_mem_vtable.malloc = vtable->malloc;
252 glib_mem_vtable.realloc = vtable->realloc;
253 glib_mem_vtable.free = vtable->free;
254 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
255 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
256 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
260 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
263 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
267 /* --- memory profiling and checking --- */
268 #ifdef G_DISABLE_CHECKS
269 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
274 #else /* !G_DISABLE_CHECKS */
281 static guint *profile_data = NULL;
282 static gulong profile_allocs = 0;
283 static gulong profile_zinit = 0;
284 static gulong profile_frees = 0;
285 static GMutex *gmem_profile_mutex = NULL;
286 #ifdef G_ENABLE_DEBUG
287 static volatile gulong g_trap_free_size = 0;
288 static volatile gulong g_trap_realloc_size = 0;
289 static volatile gulong g_trap_malloc_size = 0;
290 #endif /* G_ENABLE_DEBUG */
292 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
295 profiler_log (ProfilerJob job,
299 g_mutex_lock (gmem_profile_mutex);
302 profile_data = standard_malloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
303 if (!profile_data) /* memory system kiddin' me, eh? */
305 g_mutex_unlock (gmem_profile_mutex);
310 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
311 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
312 (job & PROFILER_RELOC) != 0,
315 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
316 (job & PROFILER_RELOC) != 0,
320 if (job & PROFILER_ALLOC)
322 profile_allocs += n_bytes;
323 if (job & PROFILER_ZINIT)
324 profile_zinit += n_bytes;
327 profile_frees += n_bytes;
329 g_mutex_unlock (gmem_profile_mutex);
333 profile_print_locked (guint *local_data,
336 gboolean need_header = TRUE;
339 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
341 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
342 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
343 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
344 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
346 if (!t_malloc && !t_realloc && !t_free && !t_refree)
348 else if (need_header)
351 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
352 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
353 g_print (" | malloc() | free() | realloc() | realloc() | \n");
354 g_print ("===========|============|============|============|============|===========\n");
356 if (i < MEM_PROFILE_TABLE_SIZE)
357 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
358 i, t_malloc, t_free, t_realloc, t_refree,
359 (t_malloc - t_free + t_realloc - t_refree) * i);
360 else if (i >= MEM_PROFILE_TABLE_SIZE)
361 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
362 i, t_malloc, t_free, t_realloc, t_refree);
365 g_print (" --- none ---\n");
371 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
376 g_mutex_lock (gmem_profile_mutex);
378 local_allocs = profile_allocs;
379 local_zinit = profile_zinit;
380 local_frees = profile_frees;
384 g_mutex_unlock (gmem_profile_mutex);
388 memcpy (local_data, profile_data,
389 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
391 g_mutex_unlock (gmem_profile_mutex);
393 g_print ("GLib Memory statistics (successful operations):\n");
394 profile_print_locked (local_data, TRUE);
395 g_print ("GLib Memory statistics (failing operations):\n");
396 profile_print_locked (local_data, FALSE);
397 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
400 ((gdouble) local_zinit) / local_allocs * 100.0,
402 ((gdouble) local_frees) / local_allocs * 100.0,
403 local_allocs - local_frees);
407 profiler_try_malloc (gsize n_bytes)
411 #ifdef G_ENABLE_DEBUG
412 if (g_trap_malloc_size == n_bytes)
414 #endif /* G_ENABLE_DEBUG */
416 p = standard_malloc (sizeof (gulong) * 2 + n_bytes);
420 p[0] = 0; /* free count */
421 p[1] = n_bytes; /* length */
422 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
426 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
432 profiler_malloc (gsize n_bytes)
434 gpointer mem = profiler_try_malloc (n_bytes);
443 profiler_calloc (gsize n_blocks,
446 gsize l = n_blocks * n_block_bytes;
449 #ifdef G_ENABLE_DEBUG
450 if (g_trap_malloc_size == l)
452 #endif /* G_ENABLE_DEBUG */
454 p = standard_calloc (1, sizeof (gulong) * 2 + l);
458 p[0] = 0; /* free count */
459 p[1] = l; /* length */
460 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
465 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
473 profiler_free (gpointer mem)
478 if (p[0]) /* free count */
480 g_warning ("free(%p): memory has been freed %lu times already", p + 2, p[0]);
481 profiler_log (PROFILER_FREE,
487 #ifdef G_ENABLE_DEBUG
488 if (g_trap_free_size == p[1])
490 #endif /* G_ENABLE_DEBUG */
492 profiler_log (PROFILER_FREE,
495 memset (p + 2, 0xaa, p[1]);
497 /* for all those that miss standard_free (p); in this place, yes,
498 * we do leak all memory when profiling, and that is intentional
499 * to catch double frees. patch submissions are futile.
506 profiler_try_realloc (gpointer mem,
513 #ifdef G_ENABLE_DEBUG
514 if (g_trap_realloc_size == n_bytes)
516 #endif /* G_ENABLE_DEBUG */
518 if (mem && p[0]) /* free count */
520 g_warning ("realloc(%p, %lu): memory has been freed %lu times already", p + 2, (gulong)n_bytes, p[0]);
521 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
527 p = standard_realloc (mem ? p : NULL, sizeof (gulong) * 2 + n_bytes);
532 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
535 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
539 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
546 profiler_realloc (gpointer mem,
549 mem = profiler_try_realloc (mem, n_bytes);
557 static GMemVTable profiler_table = {
563 profiler_try_realloc,
565 GMemVTable *glib_mem_profiler_table = &profiler_table;
567 #endif /* !G_DISABLE_CHECKS */
569 /* --- MemChunks --- */
570 #ifndef G_ALLOC_AND_FREE
571 typedef struct _GAllocator GAllocator;
572 typedef struct _GMemChunk GMemChunk;
573 #define G_ALLOC_ONLY 1
574 #define G_ALLOC_AND_FREE 2
578 guint alloc_size; /* the size of an atom */
582 g_mem_chunk_new (const gchar *name,
587 GMemChunk *mem_chunk;
588 g_return_val_if_fail (atom_size > 0, NULL);
590 mem_chunk = g_slice_new (GMemChunk);
591 mem_chunk->alloc_size = atom_size;
596 g_mem_chunk_destroy (GMemChunk *mem_chunk)
598 g_return_if_fail (mem_chunk != NULL);
600 g_slice_free (GMemChunk, mem_chunk);
604 g_mem_chunk_alloc (GMemChunk *mem_chunk)
606 g_return_val_if_fail (mem_chunk != NULL, NULL);
608 return g_slice_alloc (mem_chunk->alloc_size);
612 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
614 g_return_val_if_fail (mem_chunk != NULL, NULL);
616 return g_slice_alloc0 (mem_chunk->alloc_size);
620 g_mem_chunk_free (GMemChunk *mem_chunk,
623 g_return_if_fail (mem_chunk != NULL);
625 g_slice_free1 (mem_chunk->alloc_size, mem);
628 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
629 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
630 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
631 void g_mem_chunk_info (void) {}
632 void g_blow_chunks (void) {}
635 g_allocator_new (const gchar *name,
638 static struct _GAllocator {
644 GMemChunk *mem_chunk;
647 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
649 /* some (broken) GAllocator uses depend on non-NULL allocators */
650 return (void*) &dummy;
654 g_allocator_free (GAllocator *allocator)
659 _g_mem_thread_init_noprivate_nomessage (void)
661 /* we may only create mutexes here, locking/unlocking itself
664 #ifndef G_DISABLE_CHECKS
665 gmem_profile_mutex = g_mutex_new ();
670 #include "galiasdef.c"