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 */
606 /* --- MemChunks --- */
607 typedef struct _GFreeAtom GFreeAtom;
608 typedef struct _GMemArea GMemArea;
617 GMemArea *next; /* the next mem area */
618 GMemArea *prev; /* the previous mem area */
619 gulong index; /* the current index into the "mem" array */
620 gulong free; /* the number of free bytes in this mem area */
621 gulong allocated; /* the number of atoms allocated from this area */
622 gulong mark; /* is this mem area marked for deletion */
623 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
624 * the actual size of this array is determined by
625 * the mem chunk "area_size". ANSI says that it
626 * must be declared to be the maximum size it
627 * can possibly be (even though the actual size
634 const gchar *name; /* name of this MemChunk...used for debugging output */
635 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
636 gint num_mem_areas; /* the number of memory areas */
637 gint num_marked_areas; /* the number of areas marked for deletion */
638 guint atom_size; /* the size of an atom */
639 gulong area_size; /* the size of a memory area */
640 GMemArea *mem_area; /* the current memory area */
641 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
642 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
643 GFreeAtom *free_atoms; /* the free atoms list */
644 GTree *mem_tree; /* tree of mem areas sorted by memory address */
645 GMemChunk *next; /* pointer to the next chunk */
646 GMemChunk *prev; /* pointer to the previous chunk */
650 #ifndef DISABLE_MEM_POOLS
651 static gulong g_mem_chunk_compute_size (gulong size,
652 gulong min_size) G_GNUC_CONST;
653 static gint g_mem_chunk_area_compare (GMemArea *a,
655 static gint g_mem_chunk_area_search (GMemArea *a,
658 /* here we can't use StaticMutexes, as they depend upon a working
659 * g_malloc, the same holds true for StaticPrivate
661 static GMutex *mem_chunks_lock = NULL;
662 static GMemChunk *mem_chunks = NULL;
665 g_mem_chunk_new (const gchar *name,
670 GMemChunk *mem_chunk;
673 g_return_val_if_fail (atom_size > 0, NULL);
674 g_return_val_if_fail (area_size >= atom_size, NULL);
676 ENTER_MEM_CHUNK_ROUTINE ();
678 area_size = (area_size + atom_size - 1) / atom_size;
679 area_size *= atom_size;
681 mem_chunk = g_new (GMemChunk, 1);
682 mem_chunk->name = name;
683 mem_chunk->type = type;
684 mem_chunk->num_mem_areas = 0;
685 mem_chunk->num_marked_areas = 0;
686 mem_chunk->mem_area = NULL;
687 mem_chunk->free_mem_area = NULL;
688 mem_chunk->free_atoms = NULL;
689 mem_chunk->mem_tree = NULL;
690 mem_chunk->mem_areas = NULL;
691 mem_chunk->atom_size = atom_size;
693 if (mem_chunk->type == G_ALLOC_AND_FREE)
694 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
696 if (mem_chunk->atom_size % G_MEM_ALIGN)
697 mem_chunk->atom_size += G_MEM_ALIGN - (mem_chunk->atom_size % G_MEM_ALIGN);
699 rarea_size = area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
700 rarea_size = g_mem_chunk_compute_size (rarea_size, atom_size + sizeof (GMemArea) - MEM_AREA_SIZE);
701 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
703 g_mutex_lock (mem_chunks_lock);
704 mem_chunk->next = mem_chunks;
705 mem_chunk->prev = NULL;
707 mem_chunks->prev = mem_chunk;
708 mem_chunks = mem_chunk;
709 g_mutex_unlock (mem_chunks_lock);
711 LEAVE_MEM_CHUNK_ROUTINE ();
717 g_mem_chunk_destroy (GMemChunk *mem_chunk)
722 g_return_if_fail (mem_chunk != NULL);
724 ENTER_MEM_CHUNK_ROUTINE ();
726 mem_areas = mem_chunk->mem_areas;
729 temp_area = mem_areas;
730 mem_areas = mem_areas->next;
734 g_mutex_lock (mem_chunks_lock);
736 mem_chunk->next->prev = mem_chunk->prev;
738 mem_chunk->prev->next = mem_chunk->next;
740 if (mem_chunk == mem_chunks)
741 mem_chunks = mem_chunks->next;
742 g_mutex_unlock (mem_chunks_lock);
744 if (mem_chunk->type == G_ALLOC_AND_FREE)
745 g_tree_destroy (mem_chunk->mem_tree);
749 LEAVE_MEM_CHUNK_ROUTINE ();
753 g_mem_chunk_alloc (GMemChunk *mem_chunk)
758 ENTER_MEM_CHUNK_ROUTINE ();
760 g_return_val_if_fail (mem_chunk != NULL, NULL);
762 while (mem_chunk->free_atoms)
764 /* Get the first piece of memory on the "free_atoms" list.
765 * We can go ahead and destroy the list node we used to keep
766 * track of it with and to update the "free_atoms" list to
767 * point to its next element.
769 mem = mem_chunk->free_atoms;
770 mem_chunk->free_atoms = mem_chunk->free_atoms->next;
772 /* Determine which area this piece of memory is allocated from */
773 temp_area = g_tree_search (mem_chunk->mem_tree,
774 (GCompareFunc) g_mem_chunk_area_search,
777 /* If the area has been marked, then it is being destroyed.
778 * (ie marked to be destroyed).
779 * We check to see if all of the segments on the free list that
780 * reference this area have been removed. This occurs when
781 * the ammount of free memory is less than the allocatable size.
782 * If the chunk should be freed, then we place it in the "free_mem_area".
783 * This is so we make sure not to free the mem area here and then
784 * allocate it again a few lines down.
785 * If we don't allocate a chunk a few lines down then the "free_mem_area"
787 * If there is already a "free_mem_area" then we'll just free this mem area.
791 /* Update the "free" memory available in that area */
792 temp_area->free += mem_chunk->atom_size;
794 if (temp_area->free == mem_chunk->area_size)
796 if (temp_area == mem_chunk->mem_area)
797 mem_chunk->mem_area = NULL;
799 if (mem_chunk->free_mem_area)
801 mem_chunk->num_mem_areas -= 1;
804 temp_area->next->prev = temp_area->prev;
806 temp_area->prev->next = temp_area->next;
807 if (temp_area == mem_chunk->mem_areas)
808 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
810 if (mem_chunk->type == G_ALLOC_AND_FREE)
811 g_tree_remove (mem_chunk->mem_tree, temp_area);
815 mem_chunk->free_mem_area = temp_area;
817 mem_chunk->num_marked_areas -= 1;
822 /* Update the number of allocated atoms count.
824 temp_area->allocated += 1;
826 /* The area wasn't marked...return the memory
832 /* If there isn't a current mem area or the current mem area is out of space
833 * then allocate a new mem area. We'll first check and see if we can use
834 * the "free_mem_area". Otherwise we'll just malloc the mem area.
836 if ((!mem_chunk->mem_area) ||
837 ((mem_chunk->mem_area->index + mem_chunk->atom_size) > mem_chunk->area_size))
839 if (mem_chunk->free_mem_area)
841 mem_chunk->mem_area = mem_chunk->free_mem_area;
842 mem_chunk->free_mem_area = NULL;
846 #ifdef ENABLE_GC_FRIENDLY
847 mem_chunk->mem_area = (GMemArea*) g_malloc0 (sizeof (GMemArea) -
849 mem_chunk->area_size);
850 #else /* !ENABLE_GC_FRIENDLY */
851 mem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
853 mem_chunk->area_size);
854 #endif /* ENABLE_GC_FRIENDLY */
856 mem_chunk->num_mem_areas += 1;
857 mem_chunk->mem_area->next = mem_chunk->mem_areas;
858 mem_chunk->mem_area->prev = NULL;
860 if (mem_chunk->mem_areas)
861 mem_chunk->mem_areas->prev = mem_chunk->mem_area;
862 mem_chunk->mem_areas = mem_chunk->mem_area;
864 if (mem_chunk->type == G_ALLOC_AND_FREE)
865 g_tree_insert (mem_chunk->mem_tree, mem_chunk->mem_area, mem_chunk->mem_area);
868 mem_chunk->mem_area->index = 0;
869 mem_chunk->mem_area->free = mem_chunk->area_size;
870 mem_chunk->mem_area->allocated = 0;
871 mem_chunk->mem_area->mark = 0;
874 /* Get the memory and modify the state variables appropriately.
876 mem = (gpointer) &mem_chunk->mem_area->mem[mem_chunk->mem_area->index];
877 mem_chunk->mem_area->index += mem_chunk->atom_size;
878 mem_chunk->mem_area->free -= mem_chunk->atom_size;
879 mem_chunk->mem_area->allocated += 1;
883 LEAVE_MEM_CHUNK_ROUTINE ();
889 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
893 mem = g_mem_chunk_alloc (mem_chunk);
896 memset (mem, 0, mem_chunk->atom_size);
903 g_mem_chunk_free (GMemChunk *mem_chunk,
907 GFreeAtom *free_atom;
909 g_return_if_fail (mem_chunk != NULL);
910 g_return_if_fail (mem != NULL);
912 ENTER_MEM_CHUNK_ROUTINE ();
914 #ifdef ENABLE_GC_FRIENDLY
915 memset (mem, 0, mem_chunk->atom_size);
916 #endif /* ENABLE_GC_FRIENDLY */
918 /* Don't do anything if this is an ALLOC_ONLY chunk
920 if (mem_chunk->type == G_ALLOC_AND_FREE)
922 /* Place the memory on the "free_atoms" list
924 free_atom = (GFreeAtom*) mem;
925 free_atom->next = mem_chunk->free_atoms;
926 mem_chunk->free_atoms = free_atom;
928 temp_area = g_tree_search (mem_chunk->mem_tree,
929 (GCompareFunc) g_mem_chunk_area_search,
932 temp_area->allocated -= 1;
934 if (temp_area->allocated == 0)
937 mem_chunk->num_marked_areas += 1;
941 LEAVE_MEM_CHUNK_ROUTINE ();
944 /* This doesn't free the free_area if there is one */
946 g_mem_chunk_clean (GMemChunk *mem_chunk)
949 GFreeAtom *prev_free_atom;
950 GFreeAtom *temp_free_atom;
953 g_return_if_fail (mem_chunk != NULL);
955 ENTER_MEM_CHUNK_ROUTINE ();
957 if (mem_chunk->type == G_ALLOC_AND_FREE)
959 prev_free_atom = NULL;
960 temp_free_atom = mem_chunk->free_atoms;
962 while (temp_free_atom)
964 mem = (gpointer) temp_free_atom;
966 mem_area = g_tree_search (mem_chunk->mem_tree,
967 (GCompareFunc) g_mem_chunk_area_search,
970 /* If this mem area is marked for destruction then delete the
971 * area and list node and decrement the free mem.
976 prev_free_atom->next = temp_free_atom->next;
978 mem_chunk->free_atoms = temp_free_atom->next;
979 temp_free_atom = temp_free_atom->next;
981 mem_area->free += mem_chunk->atom_size;
982 if (mem_area->free == mem_chunk->area_size)
984 mem_chunk->num_mem_areas -= 1;
985 mem_chunk->num_marked_areas -= 1;
988 mem_area->next->prev = mem_area->prev;
990 mem_area->prev->next = mem_area->next;
991 if (mem_area == mem_chunk->mem_areas)
992 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
993 if (mem_area == mem_chunk->mem_area)
994 mem_chunk->mem_area = NULL;
996 if (mem_chunk->type == G_ALLOC_AND_FREE)
997 g_tree_remove (mem_chunk->mem_tree, mem_area);
1003 prev_free_atom = temp_free_atom;
1004 temp_free_atom = temp_free_atom->next;
1008 LEAVE_MEM_CHUNK_ROUTINE ();
1012 g_mem_chunk_reset (GMemChunk *mem_chunk)
1014 GMemArea *mem_areas;
1015 GMemArea *temp_area;
1017 g_return_if_fail (mem_chunk != NULL);
1019 ENTER_MEM_CHUNK_ROUTINE ();
1021 mem_areas = mem_chunk->mem_areas;
1022 mem_chunk->num_mem_areas = 0;
1023 mem_chunk->mem_areas = NULL;
1024 mem_chunk->mem_area = NULL;
1028 temp_area = mem_areas;
1029 mem_areas = mem_areas->next;
1033 mem_chunk->free_atoms = NULL;
1035 if (mem_chunk->mem_tree)
1037 g_tree_destroy (mem_chunk->mem_tree);
1038 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
1041 LEAVE_MEM_CHUNK_ROUTINE ();
1045 g_mem_chunk_print (GMemChunk *mem_chunk)
1047 GMemArea *mem_areas;
1050 g_return_if_fail (mem_chunk != NULL);
1052 mem_areas = mem_chunk->mem_areas;
1057 mem += mem_chunk->area_size - mem_areas->free;
1058 mem_areas = mem_areas->next;
1061 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO,
1062 "%s: %ld bytes using %d mem areas",
1063 mem_chunk->name, mem, mem_chunk->num_mem_areas);
1067 g_mem_chunk_info (void)
1069 GMemChunk *mem_chunk;
1073 g_mutex_lock (mem_chunks_lock);
1074 mem_chunk = mem_chunks;
1078 mem_chunk = mem_chunk->next;
1080 g_mutex_unlock (mem_chunks_lock);
1082 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "%d mem chunks", count);
1084 g_mutex_lock (mem_chunks_lock);
1085 mem_chunk = mem_chunks;
1086 g_mutex_unlock (mem_chunks_lock);
1090 g_mem_chunk_print ((GMemChunk*) mem_chunk);
1091 mem_chunk = mem_chunk->next;
1096 g_blow_chunks (void)
1098 GMemChunk *mem_chunk;
1100 g_mutex_lock (mem_chunks_lock);
1101 mem_chunk = mem_chunks;
1102 g_mutex_unlock (mem_chunks_lock);
1105 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
1106 mem_chunk = mem_chunk->next;
1111 g_mem_chunk_compute_size (gulong size,
1115 gulong lower, upper;
1118 while (power_of_2 < size)
1121 lower = power_of_2 >> 1;
1124 if (size - lower < upper - size && lower >= min_size)
1131 g_mem_chunk_area_compare (GMemArea *a,
1134 if (a->mem > b->mem)
1136 else if (a->mem < b->mem)
1142 g_mem_chunk_area_search (GMemArea *a,
1147 if (addr < &a->mem[a->index])
1154 #else /* DISABLE_MEM_POOLS */
1157 guint alloc_size; /* the size of an atom */
1161 g_mem_chunk_new (const gchar *name,
1166 GMinimalMemChunk *mem_chunk;
1168 g_return_val_if_fail (atom_size > 0, NULL);
1170 mem_chunk = g_new (GMinimalMemChunk, 1);
1171 mem_chunk->alloc_size = atom_size;
1173 return ((GMemChunk*) mem_chunk);
1177 g_mem_chunk_destroy (GMemChunk *mem_chunk)
1179 g_return_if_fail (mem_chunk != NULL);
1185 g_mem_chunk_alloc (GMemChunk *mem_chunk)
1187 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1189 g_return_val_if_fail (mem_chunk != NULL, NULL);
1191 return g_malloc (minimal->alloc_size);
1195 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
1197 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1199 g_return_val_if_fail (mem_chunk != NULL, NULL);
1201 return g_malloc0 (minimal->alloc_size);
1205 g_mem_chunk_free (GMemChunk *mem_chunk,
1208 g_return_if_fail (mem_chunk != NULL);
1213 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
1214 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
1215 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
1216 void g_mem_chunk_info (void) {}
1217 void g_blow_chunks (void) {}
1219 #endif /* DISABLE_MEM_POOLS */
1222 /* generic allocators
1224 struct _GAllocator /* from gmem.c */
1227 guint16 n_preallocs;
1228 guint is_unused : 1;
1231 GMemChunk *mem_chunk;
1232 gpointer dummy; /* implementation specific */
1236 g_allocator_new (const gchar *name,
1239 GAllocator *allocator;
1241 g_return_val_if_fail (name != NULL, NULL);
1243 allocator = g_new0 (GAllocator, 1);
1244 allocator->name = g_strdup (name);
1245 allocator->n_preallocs = CLAMP (n_preallocs, 1, 65535);
1246 allocator->is_unused = TRUE;
1247 allocator->type = 0;
1248 allocator->last = NULL;
1249 allocator->mem_chunk = NULL;
1250 allocator->dummy = NULL;
1256 g_allocator_free (GAllocator *allocator)
1258 g_return_if_fail (allocator != NULL);
1259 g_return_if_fail (allocator->is_unused == TRUE);
1261 g_free (allocator->name);
1262 if (allocator->mem_chunk)
1263 g_mem_chunk_destroy (allocator->mem_chunk);
1269 _g_mem_thread_init (void)
1271 #ifndef DISABLE_MEM_POOLS
1272 mem_chunks_lock = g_mutex_new ();
1274 #ifndef G_DISABLE_CHECKS
1275 g_profile_mutex = g_mutex_new ();
1280 _g_mem_thread_private_init (void)
1282 #ifndef G_DISABLE_CHECKS
1283 g_assert (mem_chunk_recursion == NULL);
1284 mem_chunk_recursion = g_private_new (NULL);
1289 #include "galiasdef.c"