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/.
41 * having DISABLE_MEM_POOLS defined, disables mem_chunks alltogether, their
42 * allocations are performed through ordinary g_malloc/g_free.
43 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
45 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
46 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
47 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
48 * if ENABLE_GC_FRIENDLY is defined, freed memory should be 0-wiped.
51 #define MEM_PROFILE_TABLE_SIZE 4096
53 #define MEM_AREA_SIZE 4L
55 #ifdef G_DISABLE_CHECKS
56 # define ENTER_MEM_CHUNK_ROUTINE()
57 # define LEAVE_MEM_CHUNK_ROUTINE()
58 # define IN_MEM_CHUNK_ROUTINE() FALSE
59 #else /* !G_DISABLE_CHECKS */
60 static GPrivate* mem_chunk_recursion = NULL;
61 # define MEM_CHUNK_ROUTINE_COUNT() GPOINTER_TO_UINT (g_private_get (mem_chunk_recursion))
62 # define ENTER_MEM_CHUNK_ROUTINE() g_private_set (mem_chunk_recursion, GUINT_TO_POINTER (MEM_CHUNK_ROUTINE_COUNT () + 1))
63 # define LEAVE_MEM_CHUNK_ROUTINE() g_private_set (mem_chunk_recursion, GUINT_TO_POINTER (MEM_CHUNK_ROUTINE_COUNT () - 1))
64 #endif /* !G_DISABLE_CHECKS */
66 #ifndef REALLOC_0_WORKS
68 standard_realloc (gpointer mem,
72 return malloc (n_bytes);
74 return realloc (mem, n_bytes);
76 #endif /* !REALLOC_0_WORKS */
78 #ifdef SANE_MALLOC_PROTOS
79 # define standard_malloc malloc
80 # ifdef REALLOC_0_WORKS
81 # define standard_realloc realloc
82 # endif /* REALLOC_0_WORKS */
83 # define standard_free free
84 # define standard_calloc calloc
85 # define standard_try_malloc malloc
86 # define standard_try_realloc realloc
87 #else /* !SANE_MALLOC_PROTOS */
89 standard_malloc (gsize n_bytes)
91 return malloc (n_bytes);
93 # ifdef REALLOC_0_WORKS
95 standard_realloc (gpointer mem,
98 return realloc (mem, n_bytes);
100 # endif /* REALLOC_0_WORKS */
102 standard_free (gpointer mem)
107 standard_calloc (gsize n_blocks,
110 return calloc (n_blocks, n_bytes);
112 #define standard_try_malloc standard_malloc
113 #define standard_try_realloc standard_realloc
114 #endif /* !SANE_MALLOC_PROTOS */
117 /* --- variables --- */
118 static GMemVTable glib_mem_vtable = {
124 standard_try_realloc,
128 /* --- functions --- */
130 g_malloc (gulong n_bytes)
136 mem = glib_mem_vtable.malloc (n_bytes);
140 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
147 g_malloc0 (gulong n_bytes)
153 mem = glib_mem_vtable.calloc (1, n_bytes);
157 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
164 g_realloc (gpointer mem,
169 mem = glib_mem_vtable.realloc (mem, n_bytes);
173 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
177 glib_mem_vtable.free (mem);
183 g_free (gpointer mem)
186 glib_mem_vtable.free (mem);
190 g_try_malloc (gulong n_bytes)
193 return glib_mem_vtable.try_malloc (n_bytes);
199 g_try_realloc (gpointer mem,
203 return glib_mem_vtable.try_realloc (mem, n_bytes);
206 glib_mem_vtable.free (mem);
212 fallback_calloc (gsize n_blocks,
215 gsize l = n_blocks * n_block_bytes;
216 gpointer mem = glib_mem_vtable.malloc (l);
225 g_mem_set_vtable (GMemVTable *vtable)
227 static gboolean vtable_set = FALSE;
232 if (vtable->malloc && vtable->realloc && vtable->free)
234 glib_mem_vtable.malloc = vtable->malloc;
235 glib_mem_vtable.realloc = vtable->realloc;
236 glib_mem_vtable.free = vtable->free;
237 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
238 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
239 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
242 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
245 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
249 /* --- memory profiling and checking --- */
250 #ifdef G_DISABLE_CHECKS
251 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
256 #else /* !G_DISABLE_CHECKS */
263 static guint *profile_data = NULL;
264 static gulong profile_allocs = 0;
265 static gulong profile_mc_allocs = 0;
266 static gulong profile_zinit = 0;
267 static gulong profile_frees = 0;
268 static gulong profile_mc_frees = 0;
269 static GMutex *g_profile_mutex = NULL;
270 #ifdef G_ENABLE_DEBUG
271 static volatile gulong g_trap_free_size = 0;
272 static volatile gulong g_trap_realloc_size = 0;
273 static volatile gulong g_trap_malloc_size = 0;
274 #endif /* G_ENABLE_DEBUG */
276 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
279 profiler_log (ProfilerJob job,
283 g_mutex_lock (g_profile_mutex);
286 profile_data = standard_malloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
287 if (!profile_data) /* memory system kiddin' me, eh? */
289 g_mutex_unlock (g_profile_mutex);
294 if (MEM_CHUNK_ROUTINE_COUNT () == 0)
296 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
297 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
298 (job & PROFILER_RELOC) != 0,
301 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
302 (job & PROFILER_RELOC) != 0,
306 if (job & PROFILER_ALLOC)
308 profile_allocs += n_bytes;
309 if (job & PROFILER_ZINIT)
310 profile_zinit += n_bytes;
313 profile_frees += n_bytes;
318 if (job & PROFILER_ALLOC)
319 profile_mc_allocs += n_bytes;
321 profile_mc_frees += n_bytes;
323 g_mutex_unlock (g_profile_mutex);
327 profile_print_locked (guint *local_data,
330 gboolean need_header = TRUE;
333 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
335 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
336 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
337 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
338 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
340 if (!t_malloc && !t_realloc && !t_free && !t_refree)
342 else if (need_header)
345 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
346 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
347 g_print (" | malloc() | free() | realloc() | realloc() | \n");
348 g_print ("===========|============|============|============|============|===========\n");
350 if (i < MEM_PROFILE_TABLE_SIZE)
351 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
352 i, t_malloc, t_free, t_realloc, t_refree,
353 (t_malloc - t_free + t_realloc - t_refree) * i);
354 else if (i >= MEM_PROFILE_TABLE_SIZE)
355 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
356 i, t_malloc, t_free, t_realloc, t_refree);
359 g_print (" --- none ---\n");
365 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
369 gulong local_mc_allocs;
370 gulong local_mc_frees;
372 g_mutex_lock (g_profile_mutex);
374 local_allocs = profile_allocs;
375 local_zinit = profile_zinit;
376 local_frees = profile_frees;
377 local_mc_allocs = profile_mc_allocs;
378 local_mc_frees = profile_mc_frees;
382 g_mutex_unlock (g_profile_mutex);
386 memcpy (local_data, profile_data,
387 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
389 g_mutex_unlock (g_profile_mutex);
391 g_print ("GLib Memory statistics (successful operations):\n");
392 profile_print_locked (local_data, TRUE);
393 g_print ("GLib Memory statistics (failing operations):\n");
394 profile_print_locked (local_data, FALSE);
395 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
398 ((gdouble) local_zinit) / local_allocs * 100.0,
400 ((gdouble) local_frees) / local_allocs * 100.0,
401 local_allocs - local_frees);
402 g_print ("MemChunk bytes: allocated=%lu, freed=%lu (%.2f%%), remaining=%lu\n",
405 ((gdouble) local_mc_frees) / local_mc_allocs * 100.0,
406 local_mc_allocs - local_mc_frees);
410 profiler_try_malloc (gsize n_bytes)
414 #ifdef G_ENABLE_DEBUG
415 if (g_trap_malloc_size == n_bytes)
417 #endif /* G_ENABLE_DEBUG */
419 p = standard_malloc (sizeof (gulong) * 2 + n_bytes);
423 p[0] = 0; /* free count */
424 p[1] = n_bytes; /* length */
425 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
429 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
435 profiler_malloc (gsize n_bytes)
437 gpointer mem = profiler_try_malloc (n_bytes);
446 profiler_calloc (gsize n_blocks,
449 gsize l = n_blocks * n_block_bytes;
452 #ifdef G_ENABLE_DEBUG
453 if (g_trap_malloc_size == l)
455 #endif /* G_ENABLE_DEBUG */
457 p = standard_calloc (1, sizeof (gulong) * 2 + l);
461 p[0] = 0; /* free count */
462 p[1] = l; /* length */
463 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
468 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
476 profiler_free (gpointer mem)
481 if (p[0]) /* free count */
483 g_warning ("free(%p): memory has been freed %lu times already", p + 2, p[0]);
484 profiler_log (PROFILER_FREE,
490 #ifdef G_ENABLE_DEBUG
491 if (g_trap_free_size == p[1])
493 #endif /* G_ENABLE_DEBUG */
495 profiler_log (PROFILER_FREE,
498 memset (p + 2, 0xaa, p[1]);
500 /* for all those that miss standard_free (p); in this place, yes,
501 * we do leak all memory when profiling, and that is intentional
502 * to catch double frees. patch submissions are futile.
509 profiler_try_realloc (gpointer mem,
516 #ifdef G_ENABLE_DEBUG
517 if (g_trap_realloc_size == n_bytes)
519 #endif /* G_ENABLE_DEBUG */
521 if (mem && p[0]) /* free count */
523 g_warning ("realloc(%p, %u): memory has been freed %lu times already", p + 2, n_bytes, p[0]);
524 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
530 p = standard_realloc (mem ? p : NULL, sizeof (gulong) * 2 + n_bytes);
535 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
538 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
542 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
549 profiler_realloc (gpointer mem,
552 mem = profiler_try_realloc (mem, n_bytes);
560 static GMemVTable profiler_table = {
566 profiler_try_realloc,
568 GMemVTable *glib_mem_profiler_table = &profiler_table;
570 #endif /* !G_DISABLE_CHECKS */
573 /* --- MemChunks --- */
574 typedef struct _GFreeAtom GFreeAtom;
575 typedef struct _GMemArea GMemArea;
584 GMemArea *next; /* the next mem area */
585 GMemArea *prev; /* the previous mem area */
586 gulong index; /* the current index into the "mem" array */
587 gulong free; /* the number of free bytes in this mem area */
588 gulong allocated; /* the number of atoms allocated from this area */
589 gulong mark; /* is this mem area marked for deletion */
590 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
591 * the actual size of this array is determined by
592 * the mem chunk "area_size". ANSI says that it
593 * must be declared to be the maximum size it
594 * can possibly be (even though the actual size
601 const gchar *name; /* name of this MemChunk...used for debugging output */
602 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
603 gint num_mem_areas; /* the number of memory areas */
604 gint num_marked_areas; /* the number of areas marked for deletion */
605 guint atom_size; /* the size of an atom */
606 gulong area_size; /* the size of a memory area */
607 GMemArea *mem_area; /* the current memory area */
608 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
609 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
610 GFreeAtom *free_atoms; /* the free atoms list */
611 GTree *mem_tree; /* tree of mem areas sorted by memory address */
612 GMemChunk *next; /* pointer to the next chunk */
613 GMemChunk *prev; /* pointer to the previous chunk */
617 #ifndef DISABLE_MEM_POOLS
618 static gulong g_mem_chunk_compute_size (gulong size,
619 gulong min_size) G_GNUC_CONST;
620 static gint g_mem_chunk_area_compare (GMemArea *a,
622 static gint g_mem_chunk_area_search (GMemArea *a,
625 /* here we can't use StaticMutexes, as they depend upon a working
626 * g_malloc, the same holds true for StaticPrivate
628 static GMutex *mem_chunks_lock = NULL;
629 static GMemChunk *mem_chunks = NULL;
632 g_mem_chunk_new (const gchar *name,
637 GMemChunk *mem_chunk;
640 g_return_val_if_fail (atom_size > 0, NULL);
641 g_return_val_if_fail (area_size >= atom_size, NULL);
643 ENTER_MEM_CHUNK_ROUTINE ();
645 area_size = (area_size + atom_size - 1) / atom_size;
646 area_size *= atom_size;
648 mem_chunk = g_new (GMemChunk, 1);
649 mem_chunk->name = name;
650 mem_chunk->type = type;
651 mem_chunk->num_mem_areas = 0;
652 mem_chunk->num_marked_areas = 0;
653 mem_chunk->mem_area = NULL;
654 mem_chunk->free_mem_area = NULL;
655 mem_chunk->free_atoms = NULL;
656 mem_chunk->mem_tree = NULL;
657 mem_chunk->mem_areas = NULL;
658 mem_chunk->atom_size = atom_size;
660 if (mem_chunk->type == G_ALLOC_AND_FREE)
661 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
663 if (mem_chunk->atom_size % G_MEM_ALIGN)
664 mem_chunk->atom_size += G_MEM_ALIGN - (mem_chunk->atom_size % G_MEM_ALIGN);
666 rarea_size = area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
667 rarea_size = g_mem_chunk_compute_size (rarea_size, atom_size + sizeof (GMemArea) - MEM_AREA_SIZE);
668 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
670 g_mutex_lock (mem_chunks_lock);
671 mem_chunk->next = mem_chunks;
672 mem_chunk->prev = NULL;
674 mem_chunks->prev = mem_chunk;
675 mem_chunks = mem_chunk;
676 g_mutex_unlock (mem_chunks_lock);
678 LEAVE_MEM_CHUNK_ROUTINE ();
684 g_mem_chunk_destroy (GMemChunk *mem_chunk)
689 g_return_if_fail (mem_chunk != NULL);
691 ENTER_MEM_CHUNK_ROUTINE ();
693 mem_areas = mem_chunk->mem_areas;
696 temp_area = mem_areas;
697 mem_areas = mem_areas->next;
702 mem_chunk->next->prev = mem_chunk->prev;
704 mem_chunk->prev->next = mem_chunk->next;
706 g_mutex_lock (mem_chunks_lock);
707 if (mem_chunk == mem_chunks)
708 mem_chunks = mem_chunks->next;
709 g_mutex_unlock (mem_chunks_lock);
711 if (mem_chunk->type == G_ALLOC_AND_FREE)
712 g_tree_destroy (mem_chunk->mem_tree);
716 LEAVE_MEM_CHUNK_ROUTINE ();
720 g_mem_chunk_alloc (GMemChunk *mem_chunk)
725 ENTER_MEM_CHUNK_ROUTINE ();
727 g_return_val_if_fail (mem_chunk != NULL, NULL);
729 while (mem_chunk->free_atoms)
731 /* Get the first piece of memory on the "free_atoms" list.
732 * We can go ahead and destroy the list node we used to keep
733 * track of it with and to update the "free_atoms" list to
734 * point to its next element.
736 mem = mem_chunk->free_atoms;
737 mem_chunk->free_atoms = mem_chunk->free_atoms->next;
739 /* Determine which area this piece of memory is allocated from */
740 temp_area = g_tree_search (mem_chunk->mem_tree,
741 (GCompareFunc) g_mem_chunk_area_search,
744 /* If the area has been marked, then it is being destroyed.
745 * (ie marked to be destroyed).
746 * We check to see if all of the segments on the free list that
747 * reference this area have been removed. This occurs when
748 * the ammount of free memory is less than the allocatable size.
749 * If the chunk should be freed, then we place it in the "free_mem_area".
750 * This is so we make sure not to free the mem area here and then
751 * allocate it again a few lines down.
752 * If we don't allocate a chunk a few lines down then the "free_mem_area"
754 * If there is already a "free_mem_area" then we'll just free this mem area.
758 /* Update the "free" memory available in that area */
759 temp_area->free += mem_chunk->atom_size;
761 if (temp_area->free == mem_chunk->area_size)
763 if (temp_area == mem_chunk->mem_area)
764 mem_chunk->mem_area = NULL;
766 if (mem_chunk->free_mem_area)
768 mem_chunk->num_mem_areas -= 1;
771 temp_area->next->prev = temp_area->prev;
773 temp_area->prev->next = temp_area->next;
774 if (temp_area == mem_chunk->mem_areas)
775 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
777 if (mem_chunk->type == G_ALLOC_AND_FREE)
778 g_tree_remove (mem_chunk->mem_tree, temp_area);
782 mem_chunk->free_mem_area = temp_area;
784 mem_chunk->num_marked_areas -= 1;
789 /* Update the number of allocated atoms count.
791 temp_area->allocated += 1;
793 /* The area wasn't marked...return the memory
799 /* If there isn't a current mem area or the current mem area is out of space
800 * then allocate a new mem area. We'll first check and see if we can use
801 * the "free_mem_area". Otherwise we'll just malloc the mem area.
803 if ((!mem_chunk->mem_area) ||
804 ((mem_chunk->mem_area->index + mem_chunk->atom_size) > mem_chunk->area_size))
806 if (mem_chunk->free_mem_area)
808 mem_chunk->mem_area = mem_chunk->free_mem_area;
809 mem_chunk->free_mem_area = NULL;
813 #ifdef ENABLE_GC_FRIENDLY
814 mem_chunk->mem_area = (GMemArea*) g_malloc0 (sizeof (GMemArea) -
816 mem_chunk->area_size);
817 #else /* !ENABLE_GC_FRIENDLY */
818 mem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
820 mem_chunk->area_size);
821 #endif /* ENABLE_GC_FRIENDLY */
823 mem_chunk->num_mem_areas += 1;
824 mem_chunk->mem_area->next = mem_chunk->mem_areas;
825 mem_chunk->mem_area->prev = NULL;
827 if (mem_chunk->mem_areas)
828 mem_chunk->mem_areas->prev = mem_chunk->mem_area;
829 mem_chunk->mem_areas = mem_chunk->mem_area;
831 if (mem_chunk->type == G_ALLOC_AND_FREE)
832 g_tree_insert (mem_chunk->mem_tree, mem_chunk->mem_area, mem_chunk->mem_area);
835 mem_chunk->mem_area->index = 0;
836 mem_chunk->mem_area->free = mem_chunk->area_size;
837 mem_chunk->mem_area->allocated = 0;
838 mem_chunk->mem_area->mark = 0;
841 /* Get the memory and modify the state variables appropriately.
843 mem = (gpointer) &mem_chunk->mem_area->mem[mem_chunk->mem_area->index];
844 mem_chunk->mem_area->index += mem_chunk->atom_size;
845 mem_chunk->mem_area->free -= mem_chunk->atom_size;
846 mem_chunk->mem_area->allocated += 1;
850 LEAVE_MEM_CHUNK_ROUTINE ();
856 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
860 mem = g_mem_chunk_alloc (mem_chunk);
863 memset (mem, 0, mem_chunk->atom_size);
870 g_mem_chunk_free (GMemChunk *mem_chunk,
874 GFreeAtom *free_atom;
876 g_return_if_fail (mem_chunk != NULL);
877 g_return_if_fail (mem != NULL);
879 ENTER_MEM_CHUNK_ROUTINE ();
881 #ifdef ENABLE_GC_FRIENDLY
882 memset (mem, 0, mem_chunk->atom_size);
883 #endif /* ENABLE_GC_FRIENDLY */
885 /* Don't do anything if this is an ALLOC_ONLY chunk
887 if (mem_chunk->type == G_ALLOC_AND_FREE)
889 /* Place the memory on the "free_atoms" list
891 free_atom = (GFreeAtom*) mem;
892 free_atom->next = mem_chunk->free_atoms;
893 mem_chunk->free_atoms = free_atom;
895 temp_area = g_tree_search (mem_chunk->mem_tree,
896 (GCompareFunc) g_mem_chunk_area_search,
899 temp_area->allocated -= 1;
901 if (temp_area->allocated == 0)
904 mem_chunk->num_marked_areas += 1;
908 LEAVE_MEM_CHUNK_ROUTINE ();
911 /* This doesn't free the free_area if there is one */
913 g_mem_chunk_clean (GMemChunk *mem_chunk)
916 GFreeAtom *prev_free_atom;
917 GFreeAtom *temp_free_atom;
920 g_return_if_fail (mem_chunk != NULL);
922 ENTER_MEM_CHUNK_ROUTINE ();
924 if (mem_chunk->type == G_ALLOC_AND_FREE)
926 prev_free_atom = NULL;
927 temp_free_atom = mem_chunk->free_atoms;
929 while (temp_free_atom)
931 mem = (gpointer) temp_free_atom;
933 mem_area = g_tree_search (mem_chunk->mem_tree,
934 (GCompareFunc) g_mem_chunk_area_search,
937 /* If this mem area is marked for destruction then delete the
938 * area and list node and decrement the free mem.
943 prev_free_atom->next = temp_free_atom->next;
945 mem_chunk->free_atoms = temp_free_atom->next;
946 temp_free_atom = temp_free_atom->next;
948 mem_area->free += mem_chunk->atom_size;
949 if (mem_area->free == mem_chunk->area_size)
951 mem_chunk->num_mem_areas -= 1;
952 mem_chunk->num_marked_areas -= 1;
955 mem_area->next->prev = mem_area->prev;
957 mem_area->prev->next = mem_area->next;
958 if (mem_area == mem_chunk->mem_areas)
959 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
960 if (mem_area == mem_chunk->mem_area)
961 mem_chunk->mem_area = NULL;
963 if (mem_chunk->type == G_ALLOC_AND_FREE)
964 g_tree_remove (mem_chunk->mem_tree, mem_area);
970 prev_free_atom = temp_free_atom;
971 temp_free_atom = temp_free_atom->next;
975 LEAVE_MEM_CHUNK_ROUTINE ();
979 g_mem_chunk_reset (GMemChunk *mem_chunk)
984 g_return_if_fail (mem_chunk != NULL);
986 ENTER_MEM_CHUNK_ROUTINE ();
988 mem_areas = mem_chunk->mem_areas;
989 mem_chunk->num_mem_areas = 0;
990 mem_chunk->mem_areas = NULL;
991 mem_chunk->mem_area = NULL;
995 temp_area = mem_areas;
996 mem_areas = mem_areas->next;
1000 mem_chunk->free_atoms = NULL;
1002 if (mem_chunk->mem_tree)
1003 g_tree_destroy (mem_chunk->mem_tree);
1004 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
1006 LEAVE_MEM_CHUNK_ROUTINE ();
1010 g_mem_chunk_print (GMemChunk *mem_chunk)
1012 GMemArea *mem_areas;
1015 g_return_if_fail (mem_chunk != NULL);
1017 mem_areas = mem_chunk->mem_areas;
1022 mem += mem_chunk->area_size - mem_areas->free;
1023 mem_areas = mem_areas->next;
1026 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
1027 "%s: %ld bytes using %d mem areas",
1028 mem_chunk->name, mem, mem_chunk->num_mem_areas);
1032 g_mem_chunk_info (void)
1034 GMemChunk *mem_chunk;
1038 g_mutex_lock (mem_chunks_lock);
1039 mem_chunk = mem_chunks;
1043 mem_chunk = mem_chunk->next;
1045 g_mutex_unlock (mem_chunks_lock);
1047 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks", count);
1049 g_mutex_lock (mem_chunks_lock);
1050 mem_chunk = mem_chunks;
1051 g_mutex_unlock (mem_chunks_lock);
1055 g_mem_chunk_print ((GMemChunk*) mem_chunk);
1056 mem_chunk = mem_chunk->next;
1061 g_blow_chunks (void)
1063 GMemChunk *mem_chunk;
1065 g_mutex_lock (mem_chunks_lock);
1066 mem_chunk = mem_chunks;
1067 g_mutex_unlock (mem_chunks_lock);
1070 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
1071 mem_chunk = mem_chunk->next;
1076 g_mem_chunk_compute_size (gulong size,
1080 gulong lower, upper;
1083 while (power_of_2 < size)
1086 lower = power_of_2 >> 1;
1089 if (size - lower < upper - size && lower >= min_size)
1096 g_mem_chunk_area_compare (GMemArea *a,
1099 if (a->mem > b->mem)
1101 else if (a->mem < b->mem)
1107 g_mem_chunk_area_search (GMemArea *a,
1112 if (addr < &a->mem[a->index])
1119 #else /* DISABLE_MEM_POOLS */
1122 guint alloc_size; /* the size of an atom */
1126 g_mem_chunk_new (const gchar *name,
1131 GMinimalMemChunk *mem_chunk;
1133 g_return_val_if_fail (atom_size > 0, NULL);
1135 mem_chunk = g_new (GMinimalMemChunk, 1);
1136 mem_chunk->alloc_size = atom_size;
1138 return ((GMemChunk*) mem_chunk);
1142 g_mem_chunk_destroy (GMemChunk *mem_chunk)
1144 g_return_if_fail (mem_chunk != NULL);
1150 g_mem_chunk_alloc (GMemChunk *mem_chunk)
1152 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1154 g_return_val_if_fail (mem_chunk != NULL, NULL);
1156 return g_malloc (minimal->alloc_size);
1160 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
1162 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1164 g_return_val_if_fail (mem_chunk != NULL, NULL);
1166 return g_malloc0 (minimal->alloc_size);
1170 g_mem_chunk_free (GMemChunk *mem_chunk,
1173 g_return_if_fail (mem_chunk != NULL);
1178 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
1179 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
1180 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
1181 void g_mem_chunk_info (void) {}
1182 void g_blow_chunks (void) {}
1184 #endif /* DISABLE_MEM_POOLS */
1187 /* generic allocators
1189 struct _GAllocator /* from gmem.c */
1192 guint16 n_preallocs;
1193 guint is_unused : 1;
1196 GMemChunk *mem_chunk;
1197 gpointer dummy; /* implementation specific */
1201 g_allocator_new (const gchar *name,
1204 GAllocator *allocator;
1206 g_return_val_if_fail (name != NULL, NULL);
1208 allocator = g_new0 (GAllocator, 1);
1209 allocator->name = g_strdup (name);
1210 allocator->n_preallocs = CLAMP (n_preallocs, 1, 65535);
1211 allocator->is_unused = TRUE;
1212 allocator->type = 0;
1213 allocator->last = NULL;
1214 allocator->mem_chunk = NULL;
1215 allocator->dummy = NULL;
1221 g_allocator_free (GAllocator *allocator)
1223 g_return_if_fail (allocator != NULL);
1224 g_return_if_fail (allocator->is_unused == TRUE);
1226 g_free (allocator->name);
1227 if (allocator->mem_chunk)
1228 g_mem_chunk_destroy (allocator->mem_chunk);
1236 #ifndef DISABLE_MEM_POOLS
1237 mem_chunks_lock = g_mutex_new ();
1239 #ifndef G_DISABLE_CHECKS
1240 mem_chunk_recursion = g_private_new (NULL);
1241 g_profile_mutex = g_mutex_new ();