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 DISABLE_MEM_POOLS defined, disables mem_chunks alltogether, their
43 * allocations are performed through ordinary g_malloc/g_free.
44 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
46 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
47 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
48 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
49 * if ENABLE_GC_FRIENDLY is defined, freed memory should be 0-wiped.
52 #define MEM_PROFILE_TABLE_SIZE 4096
54 #define MEM_AREA_SIZE 4L
56 #ifdef G_DISABLE_CHECKS
57 # define ENTER_MEM_CHUNK_ROUTINE()
58 # define LEAVE_MEM_CHUNK_ROUTINE()
59 # define IN_MEM_CHUNK_ROUTINE() FALSE
60 #else /* !G_DISABLE_CHECKS */
61 static GPrivate* mem_chunk_recursion = NULL;
62 # define MEM_CHUNK_ROUTINE_COUNT() GPOINTER_TO_UINT (g_private_get (mem_chunk_recursion))
63 # define ENTER_MEM_CHUNK_ROUTINE() g_private_set (mem_chunk_recursion, GUINT_TO_POINTER (MEM_CHUNK_ROUTINE_COUNT () + 1))
64 # define LEAVE_MEM_CHUNK_ROUTINE() g_private_set (mem_chunk_recursion, GUINT_TO_POINTER (MEM_CHUNK_ROUTINE_COUNT () - 1))
65 #endif /* !G_DISABLE_CHECKS */
67 #ifndef REALLOC_0_WORKS
69 standard_realloc (gpointer mem,
73 return malloc (n_bytes);
75 return realloc (mem, n_bytes);
77 #endif /* !REALLOC_0_WORKS */
79 #ifdef SANE_MALLOC_PROTOS
80 # define standard_malloc malloc
81 # ifdef REALLOC_0_WORKS
82 # define standard_realloc realloc
83 # endif /* REALLOC_0_WORKS */
84 # define standard_free free
85 # define standard_calloc calloc
86 # define standard_try_malloc malloc
87 # define standard_try_realloc realloc
88 #else /* !SANE_MALLOC_PROTOS */
90 standard_malloc (gsize n_bytes)
92 return malloc (n_bytes);
94 # ifdef REALLOC_0_WORKS
96 standard_realloc (gpointer mem,
99 return realloc (mem, n_bytes);
101 # endif /* REALLOC_0_WORKS */
103 standard_free (gpointer mem)
108 standard_calloc (gsize n_blocks,
111 return calloc (n_blocks, n_bytes);
113 #define standard_try_malloc standard_malloc
114 #define standard_try_realloc standard_realloc
115 #endif /* !SANE_MALLOC_PROTOS */
118 /* --- variables --- */
119 static GMemVTable glib_mem_vtable = {
125 standard_try_realloc,
129 /* --- functions --- */
131 g_malloc (gulong n_bytes)
137 mem = glib_mem_vtable.malloc (n_bytes);
141 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
148 g_malloc0 (gulong n_bytes)
154 mem = glib_mem_vtable.calloc (1, n_bytes);
158 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
165 g_realloc (gpointer mem,
170 mem = glib_mem_vtable.realloc (mem, n_bytes);
174 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
178 glib_mem_vtable.free (mem);
184 g_free (gpointer mem)
187 glib_mem_vtable.free (mem);
191 g_try_malloc (gulong n_bytes)
194 return glib_mem_vtable.try_malloc (n_bytes);
200 g_try_malloc0 (gulong n_bytes)
204 mem = g_try_malloc (n_bytes);
207 memset (mem, 0, n_bytes);
213 g_try_realloc (gpointer mem,
217 return glib_mem_vtable.try_realloc (mem, n_bytes);
220 glib_mem_vtable.free (mem);
226 fallback_calloc (gsize n_blocks,
229 gsize l = n_blocks * n_block_bytes;
230 gpointer mem = glib_mem_vtable.malloc (l);
238 static gboolean vtable_set = FALSE;
241 * g_mem_is_system_malloc
243 * Checks whether the allocator used by g_malloc() is the system's
244 * malloc implementation. If it returns %TRUE memory allocated with
245 * malloc() can be used interchangeable with memory allocated using g_malloc().
246 * This function is useful for avoiding an extra copy of allocated memory returned
247 * by a non-GLib-based API.
249 * A different allocator can be set using g_mem_set_vtable().
251 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
254 g_mem_is_system_malloc (void)
260 g_mem_set_vtable (GMemVTable *vtable)
264 if (vtable->malloc && vtable->realloc && vtable->free)
266 glib_mem_vtable.malloc = vtable->malloc;
267 glib_mem_vtable.realloc = vtable->realloc;
268 glib_mem_vtable.free = vtable->free;
269 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
270 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
271 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
275 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
278 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
282 /* --- memory profiling and checking --- */
283 #ifdef G_DISABLE_CHECKS
284 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
289 #else /* !G_DISABLE_CHECKS */
296 static guint *profile_data = NULL;
297 static gulong profile_allocs = 0;
298 static gulong profile_mc_allocs = 0;
299 static gulong profile_zinit = 0;
300 static gulong profile_frees = 0;
301 static gulong profile_mc_frees = 0;
302 static GMutex *g_profile_mutex = NULL;
303 #ifdef G_ENABLE_DEBUG
304 static volatile gulong g_trap_free_size = 0;
305 static volatile gulong g_trap_realloc_size = 0;
306 static volatile gulong g_trap_malloc_size = 0;
307 #endif /* G_ENABLE_DEBUG */
309 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
312 profiler_log (ProfilerJob job,
316 g_mutex_lock (g_profile_mutex);
319 profile_data = standard_malloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
320 if (!profile_data) /* memory system kiddin' me, eh? */
322 g_mutex_unlock (g_profile_mutex);
327 if (MEM_CHUNK_ROUTINE_COUNT () == 0)
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;
351 if (job & PROFILER_ALLOC)
352 profile_mc_allocs += n_bytes;
354 profile_mc_frees += n_bytes;
356 g_mutex_unlock (g_profile_mutex);
360 profile_print_locked (guint *local_data,
363 gboolean need_header = TRUE;
366 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
368 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
369 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
370 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
371 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
373 if (!t_malloc && !t_realloc && !t_free && !t_refree)
375 else if (need_header)
378 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
379 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
380 g_print (" | malloc() | free() | realloc() | realloc() | \n");
381 g_print ("===========|============|============|============|============|===========\n");
383 if (i < MEM_PROFILE_TABLE_SIZE)
384 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
385 i, t_malloc, t_free, t_realloc, t_refree,
386 (t_malloc - t_free + t_realloc - t_refree) * i);
387 else if (i >= MEM_PROFILE_TABLE_SIZE)
388 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
389 i, t_malloc, t_free, t_realloc, t_refree);
392 g_print (" --- none ---\n");
398 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
402 gulong local_mc_allocs;
403 gulong local_mc_frees;
405 g_mutex_lock (g_profile_mutex);
407 local_allocs = profile_allocs;
408 local_zinit = profile_zinit;
409 local_frees = profile_frees;
410 local_mc_allocs = profile_mc_allocs;
411 local_mc_frees = profile_mc_frees;
415 g_mutex_unlock (g_profile_mutex);
419 memcpy (local_data, profile_data,
420 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
422 g_mutex_unlock (g_profile_mutex);
424 g_print ("GLib Memory statistics (successful operations):\n");
425 profile_print_locked (local_data, TRUE);
426 g_print ("GLib Memory statistics (failing operations):\n");
427 profile_print_locked (local_data, FALSE);
428 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
431 ((gdouble) local_zinit) / local_allocs * 100.0,
433 ((gdouble) local_frees) / local_allocs * 100.0,
434 local_allocs - local_frees);
435 g_print ("MemChunk bytes: allocated=%lu, freed=%lu (%.2f%%), remaining=%lu\n",
438 ((gdouble) local_mc_frees) / local_mc_allocs * 100.0,
439 local_mc_allocs - local_mc_frees);
443 profiler_try_malloc (gsize n_bytes)
447 #ifdef G_ENABLE_DEBUG
448 if (g_trap_malloc_size == n_bytes)
450 #endif /* G_ENABLE_DEBUG */
452 p = standard_malloc (sizeof (gulong) * 2 + n_bytes);
456 p[0] = 0; /* free count */
457 p[1] = n_bytes; /* length */
458 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
462 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
468 profiler_malloc (gsize n_bytes)
470 gpointer mem = profiler_try_malloc (n_bytes);
479 profiler_calloc (gsize n_blocks,
482 gsize l = n_blocks * n_block_bytes;
485 #ifdef G_ENABLE_DEBUG
486 if (g_trap_malloc_size == l)
488 #endif /* G_ENABLE_DEBUG */
490 p = standard_calloc (1, sizeof (gulong) * 2 + l);
494 p[0] = 0; /* free count */
495 p[1] = l; /* length */
496 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
501 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
509 profiler_free (gpointer mem)
514 if (p[0]) /* free count */
516 g_warning ("free(%p): memory has been freed %lu times already", p + 2, p[0]);
517 profiler_log (PROFILER_FREE,
523 #ifdef G_ENABLE_DEBUG
524 if (g_trap_free_size == p[1])
526 #endif /* G_ENABLE_DEBUG */
528 profiler_log (PROFILER_FREE,
531 memset (p + 2, 0xaa, p[1]);
533 /* for all those that miss standard_free (p); in this place, yes,
534 * we do leak all memory when profiling, and that is intentional
535 * to catch double frees. patch submissions are futile.
542 profiler_try_realloc (gpointer mem,
549 #ifdef G_ENABLE_DEBUG
550 if (g_trap_realloc_size == n_bytes)
552 #endif /* G_ENABLE_DEBUG */
554 if (mem && p[0]) /* free count */
556 g_warning ("realloc(%p, %lu): memory has been freed %lu times already", p + 2, (gulong)n_bytes, p[0]);
557 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
563 p = standard_realloc (mem ? p : NULL, sizeof (gulong) * 2 + n_bytes);
568 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
571 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
575 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
582 profiler_realloc (gpointer mem,
585 mem = profiler_try_realloc (mem, n_bytes);
593 static GMemVTable profiler_table = {
599 profiler_try_realloc,
601 GMemVTable *glib_mem_profiler_table = &profiler_table;
603 #endif /* !G_DISABLE_CHECKS */
605 /* --- memory slices --- */
607 gpointer dummy, next;
611 g_slice_alloc (guint block_size)
613 return g_malloc (block_size);
617 g_slice_alloc0 (guint block_size)
619 return g_malloc0 (block_size);
623 g_slice_free1 (guint block_size,
631 g_slice_free_chain (guint block_size,
635 MemSlice *slice = mem_chain;
636 g_return_if_fail (next_offset == G_STRUCT_OFFSET (MemSlice, next));
637 g_return_if_fail (block_size >= sizeof (MemSlice));
640 MemSlice *current = slice;
642 g_slice_free1 (block_size, current);
646 /* --- MemChunks --- */
647 #ifndef G_ALLOC_AND_FREE
648 typedef struct _GAllocator GAllocator;
649 typedef struct _GMemChunk GMemChunk;
650 #define G_ALLOC_ONLY 1
651 #define G_ALLOC_AND_FREE 2
654 typedef struct _GFreeAtom GFreeAtom;
655 typedef struct _GMemArea GMemArea;
664 GMemArea *next; /* the next mem area */
665 GMemArea *prev; /* the previous mem area */
666 gulong index; /* the current index into the "mem" array */
667 gulong free; /* the number of free bytes in this mem area */
668 gulong allocated; /* the number of atoms allocated from this area */
669 gulong mark; /* is this mem area marked for deletion */
670 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
671 * the actual size of this array is determined by
672 * the mem chunk "area_size". ANSI says that it
673 * must be declared to be the maximum size it
674 * can possibly be (even though the actual size
681 const gchar *name; /* name of this MemChunk...used for debugging output */
682 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
683 gint num_mem_areas; /* the number of memory areas */
684 gint num_marked_areas; /* the number of areas marked for deletion */
685 guint atom_size; /* the size of an atom */
686 gulong area_size; /* the size of a memory area */
687 GMemArea *mem_area; /* the current memory area */
688 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
689 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
690 GFreeAtom *free_atoms; /* the free atoms list */
691 GTree *mem_tree; /* tree of mem areas sorted by memory address */
692 GMemChunk *next; /* pointer to the next chunk */
693 GMemChunk *prev; /* pointer to the previous chunk */
697 #ifndef DISABLE_MEM_POOLS
698 static gulong g_mem_chunk_compute_size (gulong size,
699 gulong min_size) G_GNUC_CONST;
700 static gint g_mem_chunk_area_compare (GMemArea *a,
702 static gint g_mem_chunk_area_search (GMemArea *a,
705 /* here we can't use StaticMutexes, as they depend upon a working
706 * g_malloc, the same holds true for StaticPrivate
708 static GMutex *mem_chunks_lock = NULL;
709 static GMemChunk *mem_chunks = NULL;
712 g_mem_chunk_new (const gchar *name,
717 GMemChunk *mem_chunk;
720 g_return_val_if_fail (atom_size > 0, NULL);
721 g_return_val_if_fail (area_size >= atom_size, NULL);
723 ENTER_MEM_CHUNK_ROUTINE ();
725 area_size = (area_size + atom_size - 1) / atom_size;
726 area_size *= atom_size;
728 mem_chunk = g_new (GMemChunk, 1);
729 mem_chunk->name = name;
730 mem_chunk->type = type;
731 mem_chunk->num_mem_areas = 0;
732 mem_chunk->num_marked_areas = 0;
733 mem_chunk->mem_area = NULL;
734 mem_chunk->free_mem_area = NULL;
735 mem_chunk->free_atoms = NULL;
736 mem_chunk->mem_tree = NULL;
737 mem_chunk->mem_areas = NULL;
738 mem_chunk->atom_size = atom_size;
740 if (mem_chunk->type == G_ALLOC_AND_FREE)
741 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
743 if (mem_chunk->atom_size % G_MEM_ALIGN)
744 mem_chunk->atom_size += G_MEM_ALIGN - (mem_chunk->atom_size % G_MEM_ALIGN);
746 rarea_size = area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
747 rarea_size = g_mem_chunk_compute_size (rarea_size, atom_size + sizeof (GMemArea) - MEM_AREA_SIZE);
748 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
750 g_mutex_lock (mem_chunks_lock);
751 mem_chunk->next = mem_chunks;
752 mem_chunk->prev = NULL;
754 mem_chunks->prev = mem_chunk;
755 mem_chunks = mem_chunk;
756 g_mutex_unlock (mem_chunks_lock);
758 LEAVE_MEM_CHUNK_ROUTINE ();
764 g_mem_chunk_destroy (GMemChunk *mem_chunk)
769 g_return_if_fail (mem_chunk != NULL);
771 ENTER_MEM_CHUNK_ROUTINE ();
773 mem_areas = mem_chunk->mem_areas;
776 temp_area = mem_areas;
777 mem_areas = mem_areas->next;
781 g_mutex_lock (mem_chunks_lock);
783 mem_chunk->next->prev = mem_chunk->prev;
785 mem_chunk->prev->next = mem_chunk->next;
787 if (mem_chunk == mem_chunks)
788 mem_chunks = mem_chunks->next;
789 g_mutex_unlock (mem_chunks_lock);
791 if (mem_chunk->type == G_ALLOC_AND_FREE)
792 g_tree_destroy (mem_chunk->mem_tree);
796 LEAVE_MEM_CHUNK_ROUTINE ();
800 g_mem_chunk_alloc (GMemChunk *mem_chunk)
805 ENTER_MEM_CHUNK_ROUTINE ();
807 g_return_val_if_fail (mem_chunk != NULL, NULL);
809 while (mem_chunk->free_atoms)
811 /* Get the first piece of memory on the "free_atoms" list.
812 * We can go ahead and destroy the list node we used to keep
813 * track of it with and to update the "free_atoms" list to
814 * point to its next element.
816 mem = mem_chunk->free_atoms;
817 mem_chunk->free_atoms = mem_chunk->free_atoms->next;
819 /* Determine which area this piece of memory is allocated from */
820 temp_area = g_tree_search (mem_chunk->mem_tree,
821 (GCompareFunc) g_mem_chunk_area_search,
824 /* If the area has been marked, then it is being destroyed.
825 * (ie marked to be destroyed).
826 * We check to see if all of the segments on the free list that
827 * reference this area have been removed. This occurs when
828 * the ammount of free memory is less than the allocatable size.
829 * If the chunk should be freed, then we place it in the "free_mem_area".
830 * This is so we make sure not to free the mem area here and then
831 * allocate it again a few lines down.
832 * If we don't allocate a chunk a few lines down then the "free_mem_area"
834 * If there is already a "free_mem_area" then we'll just free this mem area.
838 /* Update the "free" memory available in that area */
839 temp_area->free += mem_chunk->atom_size;
841 if (temp_area->free == mem_chunk->area_size)
843 if (temp_area == mem_chunk->mem_area)
844 mem_chunk->mem_area = NULL;
846 if (mem_chunk->free_mem_area)
848 mem_chunk->num_mem_areas -= 1;
851 temp_area->next->prev = temp_area->prev;
853 temp_area->prev->next = temp_area->next;
854 if (temp_area == mem_chunk->mem_areas)
855 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
857 if (mem_chunk->type == G_ALLOC_AND_FREE)
858 g_tree_remove (mem_chunk->mem_tree, temp_area);
862 mem_chunk->free_mem_area = temp_area;
864 mem_chunk->num_marked_areas -= 1;
869 /* Update the number of allocated atoms count.
871 temp_area->allocated += 1;
873 /* The area wasn't marked...return the memory
879 /* If there isn't a current mem area or the current mem area is out of space
880 * then allocate a new mem area. We'll first check and see if we can use
881 * the "free_mem_area". Otherwise we'll just malloc the mem area.
883 if ((!mem_chunk->mem_area) ||
884 ((mem_chunk->mem_area->index + mem_chunk->atom_size) > mem_chunk->area_size))
886 if (mem_chunk->free_mem_area)
888 mem_chunk->mem_area = mem_chunk->free_mem_area;
889 mem_chunk->free_mem_area = NULL;
893 #ifdef ENABLE_GC_FRIENDLY
894 mem_chunk->mem_area = (GMemArea*) g_malloc0 (sizeof (GMemArea) -
896 mem_chunk->area_size);
897 #else /* !ENABLE_GC_FRIENDLY */
898 mem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
900 mem_chunk->area_size);
901 #endif /* ENABLE_GC_FRIENDLY */
903 mem_chunk->num_mem_areas += 1;
904 mem_chunk->mem_area->next = mem_chunk->mem_areas;
905 mem_chunk->mem_area->prev = NULL;
907 if (mem_chunk->mem_areas)
908 mem_chunk->mem_areas->prev = mem_chunk->mem_area;
909 mem_chunk->mem_areas = mem_chunk->mem_area;
911 if (mem_chunk->type == G_ALLOC_AND_FREE)
912 g_tree_insert (mem_chunk->mem_tree, mem_chunk->mem_area, mem_chunk->mem_area);
915 mem_chunk->mem_area->index = 0;
916 mem_chunk->mem_area->free = mem_chunk->area_size;
917 mem_chunk->mem_area->allocated = 0;
918 mem_chunk->mem_area->mark = 0;
921 /* Get the memory and modify the state variables appropriately.
923 mem = (gpointer) &mem_chunk->mem_area->mem[mem_chunk->mem_area->index];
924 mem_chunk->mem_area->index += mem_chunk->atom_size;
925 mem_chunk->mem_area->free -= mem_chunk->atom_size;
926 mem_chunk->mem_area->allocated += 1;
930 LEAVE_MEM_CHUNK_ROUTINE ();
936 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
940 mem = g_mem_chunk_alloc (mem_chunk);
943 memset (mem, 0, mem_chunk->atom_size);
950 g_mem_chunk_free (GMemChunk *mem_chunk,
954 GFreeAtom *free_atom;
956 g_return_if_fail (mem_chunk != NULL);
957 g_return_if_fail (mem != NULL);
959 ENTER_MEM_CHUNK_ROUTINE ();
961 #ifdef ENABLE_GC_FRIENDLY
962 memset (mem, 0, mem_chunk->atom_size);
963 #endif /* ENABLE_GC_FRIENDLY */
965 /* Don't do anything if this is an ALLOC_ONLY chunk
967 if (mem_chunk->type == G_ALLOC_AND_FREE)
969 /* Place the memory on the "free_atoms" list
971 free_atom = (GFreeAtom*) mem;
972 free_atom->next = mem_chunk->free_atoms;
973 mem_chunk->free_atoms = free_atom;
975 temp_area = g_tree_search (mem_chunk->mem_tree,
976 (GCompareFunc) g_mem_chunk_area_search,
979 temp_area->allocated -= 1;
981 if (temp_area->allocated == 0)
984 mem_chunk->num_marked_areas += 1;
988 LEAVE_MEM_CHUNK_ROUTINE ();
991 /* This doesn't free the free_area if there is one */
993 g_mem_chunk_clean (GMemChunk *mem_chunk)
996 GFreeAtom *prev_free_atom;
997 GFreeAtom *temp_free_atom;
1000 g_return_if_fail (mem_chunk != NULL);
1002 ENTER_MEM_CHUNK_ROUTINE ();
1004 if (mem_chunk->type == G_ALLOC_AND_FREE)
1006 prev_free_atom = NULL;
1007 temp_free_atom = mem_chunk->free_atoms;
1009 while (temp_free_atom)
1011 mem = (gpointer) temp_free_atom;
1013 mem_area = g_tree_search (mem_chunk->mem_tree,
1014 (GCompareFunc) g_mem_chunk_area_search,
1017 /* If this mem area is marked for destruction then delete the
1018 * area and list node and decrement the free mem.
1023 prev_free_atom->next = temp_free_atom->next;
1025 mem_chunk->free_atoms = temp_free_atom->next;
1026 temp_free_atom = temp_free_atom->next;
1028 mem_area->free += mem_chunk->atom_size;
1029 if (mem_area->free == mem_chunk->area_size)
1031 mem_chunk->num_mem_areas -= 1;
1032 mem_chunk->num_marked_areas -= 1;
1035 mem_area->next->prev = mem_area->prev;
1037 mem_area->prev->next = mem_area->next;
1038 if (mem_area == mem_chunk->mem_areas)
1039 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
1040 if (mem_area == mem_chunk->mem_area)
1041 mem_chunk->mem_area = NULL;
1043 if (mem_chunk->type == G_ALLOC_AND_FREE)
1044 g_tree_remove (mem_chunk->mem_tree, mem_area);
1050 prev_free_atom = temp_free_atom;
1051 temp_free_atom = temp_free_atom->next;
1055 LEAVE_MEM_CHUNK_ROUTINE ();
1059 g_mem_chunk_reset (GMemChunk *mem_chunk)
1061 GMemArea *mem_areas;
1062 GMemArea *temp_area;
1064 g_return_if_fail (mem_chunk != NULL);
1066 ENTER_MEM_CHUNK_ROUTINE ();
1068 mem_areas = mem_chunk->mem_areas;
1069 mem_chunk->num_mem_areas = 0;
1070 mem_chunk->mem_areas = NULL;
1071 mem_chunk->mem_area = NULL;
1075 temp_area = mem_areas;
1076 mem_areas = mem_areas->next;
1080 mem_chunk->free_atoms = NULL;
1082 if (mem_chunk->mem_tree)
1084 g_tree_destroy (mem_chunk->mem_tree);
1085 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
1088 LEAVE_MEM_CHUNK_ROUTINE ();
1092 g_mem_chunk_print (GMemChunk *mem_chunk)
1094 GMemArea *mem_areas;
1097 g_return_if_fail (mem_chunk != NULL);
1099 mem_areas = mem_chunk->mem_areas;
1104 mem += mem_chunk->area_size - mem_areas->free;
1105 mem_areas = mem_areas->next;
1108 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO,
1109 "%s: %ld bytes using %d mem areas",
1110 mem_chunk->name, mem, mem_chunk->num_mem_areas);
1114 g_mem_chunk_info (void)
1116 GMemChunk *mem_chunk;
1120 g_mutex_lock (mem_chunks_lock);
1121 mem_chunk = mem_chunks;
1125 mem_chunk = mem_chunk->next;
1127 g_mutex_unlock (mem_chunks_lock);
1129 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "%d mem chunks", count);
1131 g_mutex_lock (mem_chunks_lock);
1132 mem_chunk = mem_chunks;
1133 g_mutex_unlock (mem_chunks_lock);
1137 g_mem_chunk_print ((GMemChunk*) mem_chunk);
1138 mem_chunk = mem_chunk->next;
1143 g_blow_chunks (void)
1145 GMemChunk *mem_chunk;
1147 g_mutex_lock (mem_chunks_lock);
1148 mem_chunk = mem_chunks;
1149 g_mutex_unlock (mem_chunks_lock);
1152 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
1153 mem_chunk = mem_chunk->next;
1158 g_mem_chunk_compute_size (gulong size,
1162 gulong lower, upper;
1165 while (power_of_2 < size)
1168 lower = power_of_2 >> 1;
1171 if (size - lower < upper - size && lower >= min_size)
1178 g_mem_chunk_area_compare (GMemArea *a,
1181 if (a->mem > b->mem)
1183 else if (a->mem < b->mem)
1189 g_mem_chunk_area_search (GMemArea *a,
1194 if (addr < &a->mem[a->index])
1201 #else /* DISABLE_MEM_POOLS */
1204 guint alloc_size; /* the size of an atom */
1208 g_mem_chunk_new (const gchar *name,
1213 GMinimalMemChunk *mem_chunk;
1215 g_return_val_if_fail (atom_size > 0, NULL);
1217 mem_chunk = g_new (GMinimalMemChunk, 1);
1218 mem_chunk->alloc_size = atom_size;
1220 return ((GMemChunk*) mem_chunk);
1224 g_mem_chunk_destroy (GMemChunk *mem_chunk)
1226 g_return_if_fail (mem_chunk != NULL);
1232 g_mem_chunk_alloc (GMemChunk *mem_chunk)
1234 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1236 g_return_val_if_fail (mem_chunk != NULL, NULL);
1238 return g_malloc (minimal->alloc_size);
1242 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
1244 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1246 g_return_val_if_fail (mem_chunk != NULL, NULL);
1248 return g_malloc0 (minimal->alloc_size);
1252 g_mem_chunk_free (GMemChunk *mem_chunk,
1255 g_return_if_fail (mem_chunk != NULL);
1260 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
1261 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
1262 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
1263 void g_mem_chunk_info (void) {}
1264 void g_blow_chunks (void) {}
1266 #endif /* DISABLE_MEM_POOLS */
1270 g_allocator_new (const gchar *name,
1273 static const struct _GAllocator {
1275 guint16 n_preallocs;
1276 guint is_unused : 1;
1279 GMemChunk *mem_chunk;
1282 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
1284 /* some (broken) GAllocator uses depend on non-NULL allocators */
1285 return (GAllocator*) &dummy;
1289 g_allocator_free (GAllocator *allocator)
1294 _g_mem_thread_init (void)
1296 #ifndef DISABLE_MEM_POOLS
1297 mem_chunks_lock = g_mutex_new ();
1299 #ifndef G_DISABLE_CHECKS
1300 g_profile_mutex = g_mutex_new ();
1305 _g_mem_thread_private_init (void)
1307 #ifndef G_DISABLE_CHECKS
1308 g_assert (mem_chunk_recursion == NULL);
1309 mem_chunk_recursion = g_private_new (NULL);
1314 #include "galiasdef.c"