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/.
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_realloc (gpointer mem,
204 return glib_mem_vtable.try_realloc (mem, n_bytes);
207 glib_mem_vtable.free (mem);
213 fallback_calloc (gsize n_blocks,
216 gsize l = n_blocks * n_block_bytes;
217 gpointer mem = glib_mem_vtable.malloc (l);
225 static gboolean vtable_set = FALSE;
228 * g_mem_is_system_malloc
230 * Checks whether the allocator used by g_malloc() is the system's
231 * malloc implementation. If it returns %TRUE memory allocated with
232 * <function>malloc()</function> can be used interchangeable with
233 * memory allocated using g_malloc(). This function is useful for
234 * avoiding an extra copy of allocated memory returned by a
235 * non-GLib-based API.
237 * A different allocator can be set using g_mem_set_vtable().
239 * Return value: if %TRUE, <function>malloc()</function> and g_malloc() can be mixed.
242 g_mem_is_system_malloc (void)
248 g_mem_set_vtable (GMemVTable *vtable)
253 if (vtable->malloc && vtable->realloc && vtable->free)
255 glib_mem_vtable.malloc = vtable->malloc;
256 glib_mem_vtable.realloc = vtable->realloc;
257 glib_mem_vtable.free = vtable->free;
258 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
259 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
260 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
263 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
266 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
270 /* --- memory profiling and checking --- */
271 #ifdef G_DISABLE_CHECKS
272 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
277 #else /* !G_DISABLE_CHECKS */
284 static guint *profile_data = NULL;
285 static gulong profile_allocs = 0;
286 static gulong profile_mc_allocs = 0;
287 static gulong profile_zinit = 0;
288 static gulong profile_frees = 0;
289 static gulong profile_mc_frees = 0;
290 static GMutex *g_profile_mutex = NULL;
291 #ifdef G_ENABLE_DEBUG
292 static volatile gulong g_trap_free_size = 0;
293 static volatile gulong g_trap_realloc_size = 0;
294 static volatile gulong g_trap_malloc_size = 0;
295 #endif /* G_ENABLE_DEBUG */
297 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
300 profiler_log (ProfilerJob job,
304 g_mutex_lock (g_profile_mutex);
307 profile_data = standard_malloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
308 if (!profile_data) /* memory system kiddin' me, eh? */
310 g_mutex_unlock (g_profile_mutex);
315 if (MEM_CHUNK_ROUTINE_COUNT () == 0)
317 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
318 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
319 (job & PROFILER_RELOC) != 0,
322 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
323 (job & PROFILER_RELOC) != 0,
327 if (job & PROFILER_ALLOC)
329 profile_allocs += n_bytes;
330 if (job & PROFILER_ZINIT)
331 profile_zinit += n_bytes;
334 profile_frees += n_bytes;
339 if (job & PROFILER_ALLOC)
340 profile_mc_allocs += n_bytes;
342 profile_mc_frees += n_bytes;
344 g_mutex_unlock (g_profile_mutex);
348 profile_print_locked (guint *local_data,
351 gboolean need_header = TRUE;
354 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
356 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
357 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
358 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
359 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
361 if (!t_malloc && !t_realloc && !t_free && !t_refree)
363 else if (need_header)
366 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
367 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
368 g_print (" | malloc() | free() | realloc() | realloc() | \n");
369 g_print ("===========|============|============|============|============|===========\n");
371 if (i < MEM_PROFILE_TABLE_SIZE)
372 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
373 i, t_malloc, t_free, t_realloc, t_refree,
374 (t_malloc - t_free + t_realloc - t_refree) * i);
375 else if (i >= MEM_PROFILE_TABLE_SIZE)
376 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
377 i, t_malloc, t_free, t_realloc, t_refree);
380 g_print (" --- none ---\n");
386 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
390 gulong local_mc_allocs;
391 gulong local_mc_frees;
393 g_mutex_lock (g_profile_mutex);
395 local_allocs = profile_allocs;
396 local_zinit = profile_zinit;
397 local_frees = profile_frees;
398 local_mc_allocs = profile_mc_allocs;
399 local_mc_frees = profile_mc_frees;
403 g_mutex_unlock (g_profile_mutex);
407 memcpy (local_data, profile_data,
408 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
410 g_mutex_unlock (g_profile_mutex);
412 g_print ("GLib Memory statistics (successful operations):\n");
413 profile_print_locked (local_data, TRUE);
414 g_print ("GLib Memory statistics (failing operations):\n");
415 profile_print_locked (local_data, FALSE);
416 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
419 ((gdouble) local_zinit) / local_allocs * 100.0,
421 ((gdouble) local_frees) / local_allocs * 100.0,
422 local_allocs - local_frees);
423 g_print ("MemChunk bytes: allocated=%lu, freed=%lu (%.2f%%), remaining=%lu\n",
426 ((gdouble) local_mc_frees) / local_mc_allocs * 100.0,
427 local_mc_allocs - local_mc_frees);
431 profiler_try_malloc (gsize n_bytes)
435 #ifdef G_ENABLE_DEBUG
436 if (g_trap_malloc_size == n_bytes)
438 #endif /* G_ENABLE_DEBUG */
440 p = standard_malloc (sizeof (gulong) * 2 + n_bytes);
444 p[0] = 0; /* free count */
445 p[1] = n_bytes; /* length */
446 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
450 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
456 profiler_malloc (gsize n_bytes)
458 gpointer mem = profiler_try_malloc (n_bytes);
467 profiler_calloc (gsize n_blocks,
470 gsize l = n_blocks * n_block_bytes;
473 #ifdef G_ENABLE_DEBUG
474 if (g_trap_malloc_size == l)
476 #endif /* G_ENABLE_DEBUG */
478 p = standard_calloc (1, sizeof (gulong) * 2 + l);
482 p[0] = 0; /* free count */
483 p[1] = l; /* length */
484 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
489 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
497 profiler_free (gpointer mem)
502 if (p[0]) /* free count */
504 g_warning ("free(%p): memory has been freed %lu times already", p + 2, p[0]);
505 profiler_log (PROFILER_FREE,
511 #ifdef G_ENABLE_DEBUG
512 if (g_trap_free_size == p[1])
514 #endif /* G_ENABLE_DEBUG */
516 profiler_log (PROFILER_FREE,
519 memset (p + 2, 0xaa, p[1]);
521 /* for all those that miss standard_free (p); in this place, yes,
522 * we do leak all memory when profiling, and that is intentional
523 * to catch double frees. patch submissions are futile.
530 profiler_try_realloc (gpointer mem,
537 #ifdef G_ENABLE_DEBUG
538 if (g_trap_realloc_size == n_bytes)
540 #endif /* G_ENABLE_DEBUG */
542 if (mem && p[0]) /* free count */
544 g_warning ("realloc(%p, %u): memory has been freed %lu times already", p + 2, n_bytes, p[0]);
545 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
551 p = standard_realloc (mem ? p : NULL, sizeof (gulong) * 2 + n_bytes);
556 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
559 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
563 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
570 profiler_realloc (gpointer mem,
573 mem = profiler_try_realloc (mem, n_bytes);
581 static GMemVTable profiler_table = {
587 profiler_try_realloc,
589 GMemVTable *glib_mem_profiler_table = &profiler_table;
591 #endif /* !G_DISABLE_CHECKS */
594 /* --- MemChunks --- */
595 typedef struct _GFreeAtom GFreeAtom;
596 typedef struct _GMemArea GMemArea;
605 GMemArea *next; /* the next mem area */
606 GMemArea *prev; /* the previous mem area */
607 gulong index; /* the current index into the "mem" array */
608 gulong free; /* the number of free bytes in this mem area */
609 gulong allocated; /* the number of atoms allocated from this area */
610 gulong mark; /* is this mem area marked for deletion */
611 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
612 * the actual size of this array is determined by
613 * the mem chunk "area_size". ANSI says that it
614 * must be declared to be the maximum size it
615 * can possibly be (even though the actual size
622 const gchar *name; /* name of this MemChunk...used for debugging output */
623 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
624 gint num_mem_areas; /* the number of memory areas */
625 gint num_marked_areas; /* the number of areas marked for deletion */
626 guint atom_size; /* the size of an atom */
627 gulong area_size; /* the size of a memory area */
628 GMemArea *mem_area; /* the current memory area */
629 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
630 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
631 GFreeAtom *free_atoms; /* the free atoms list */
632 GTree *mem_tree; /* tree of mem areas sorted by memory address */
633 GMemChunk *next; /* pointer to the next chunk */
634 GMemChunk *prev; /* pointer to the previous chunk */
638 #ifndef DISABLE_MEM_POOLS
639 static gulong g_mem_chunk_compute_size (gulong size,
640 gulong min_size) G_GNUC_CONST;
641 static gint g_mem_chunk_area_compare (GMemArea *a,
643 static gint g_mem_chunk_area_search (GMemArea *a,
646 /* here we can't use StaticMutexes, as they depend upon a working
647 * g_malloc, the same holds true for StaticPrivate
649 static GMutex *mem_chunks_lock = NULL;
650 static GMemChunk *mem_chunks = NULL;
653 g_mem_chunk_new (const gchar *name,
658 GMemChunk *mem_chunk;
661 g_return_val_if_fail (atom_size > 0, NULL);
662 g_return_val_if_fail (area_size >= atom_size, NULL);
664 ENTER_MEM_CHUNK_ROUTINE ();
666 area_size = (area_size + atom_size - 1) / atom_size;
667 area_size *= atom_size;
669 mem_chunk = g_new (GMemChunk, 1);
670 mem_chunk->name = name;
671 mem_chunk->type = type;
672 mem_chunk->num_mem_areas = 0;
673 mem_chunk->num_marked_areas = 0;
674 mem_chunk->mem_area = NULL;
675 mem_chunk->free_mem_area = NULL;
676 mem_chunk->free_atoms = NULL;
677 mem_chunk->mem_tree = NULL;
678 mem_chunk->mem_areas = NULL;
679 mem_chunk->atom_size = atom_size;
681 if (mem_chunk->type == G_ALLOC_AND_FREE)
682 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
684 if (mem_chunk->atom_size % G_MEM_ALIGN)
685 mem_chunk->atom_size += G_MEM_ALIGN - (mem_chunk->atom_size % G_MEM_ALIGN);
687 rarea_size = area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
688 rarea_size = g_mem_chunk_compute_size (rarea_size, atom_size + sizeof (GMemArea) - MEM_AREA_SIZE);
689 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
691 g_mutex_lock (mem_chunks_lock);
692 mem_chunk->next = mem_chunks;
693 mem_chunk->prev = NULL;
695 mem_chunks->prev = mem_chunk;
696 mem_chunks = mem_chunk;
697 g_mutex_unlock (mem_chunks_lock);
699 LEAVE_MEM_CHUNK_ROUTINE ();
705 g_mem_chunk_destroy (GMemChunk *mem_chunk)
710 g_return_if_fail (mem_chunk != NULL);
712 ENTER_MEM_CHUNK_ROUTINE ();
714 mem_areas = mem_chunk->mem_areas;
717 temp_area = mem_areas;
718 mem_areas = mem_areas->next;
723 mem_chunk->next->prev = mem_chunk->prev;
725 mem_chunk->prev->next = mem_chunk->next;
727 g_mutex_lock (mem_chunks_lock);
728 if (mem_chunk == mem_chunks)
729 mem_chunks = mem_chunks->next;
730 g_mutex_unlock (mem_chunks_lock);
732 if (mem_chunk->type == G_ALLOC_AND_FREE)
733 g_tree_destroy (mem_chunk->mem_tree);
737 LEAVE_MEM_CHUNK_ROUTINE ();
741 g_mem_chunk_alloc (GMemChunk *mem_chunk)
746 ENTER_MEM_CHUNK_ROUTINE ();
748 g_return_val_if_fail (mem_chunk != NULL, NULL);
750 while (mem_chunk->free_atoms)
752 /* Get the first piece of memory on the "free_atoms" list.
753 * We can go ahead and destroy the list node we used to keep
754 * track of it with and to update the "free_atoms" list to
755 * point to its next element.
757 mem = mem_chunk->free_atoms;
758 mem_chunk->free_atoms = mem_chunk->free_atoms->next;
760 /* Determine which area this piece of memory is allocated from */
761 temp_area = g_tree_search (mem_chunk->mem_tree,
762 (GCompareFunc) g_mem_chunk_area_search,
765 /* If the area has been marked, then it is being destroyed.
766 * (ie marked to be destroyed).
767 * We check to see if all of the segments on the free list that
768 * reference this area have been removed. This occurs when
769 * the ammount of free memory is less than the allocatable size.
770 * If the chunk should be freed, then we place it in the "free_mem_area".
771 * This is so we make sure not to free the mem area here and then
772 * allocate it again a few lines down.
773 * If we don't allocate a chunk a few lines down then the "free_mem_area"
775 * If there is already a "free_mem_area" then we'll just free this mem area.
779 /* Update the "free" memory available in that area */
780 temp_area->free += mem_chunk->atom_size;
782 if (temp_area->free == mem_chunk->area_size)
784 if (temp_area == mem_chunk->mem_area)
785 mem_chunk->mem_area = NULL;
787 if (mem_chunk->free_mem_area)
789 mem_chunk->num_mem_areas -= 1;
792 temp_area->next->prev = temp_area->prev;
794 temp_area->prev->next = temp_area->next;
795 if (temp_area == mem_chunk->mem_areas)
796 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
798 if (mem_chunk->type == G_ALLOC_AND_FREE)
799 g_tree_remove (mem_chunk->mem_tree, temp_area);
803 mem_chunk->free_mem_area = temp_area;
805 mem_chunk->num_marked_areas -= 1;
810 /* Update the number of allocated atoms count.
812 temp_area->allocated += 1;
814 /* The area wasn't marked...return the memory
820 /* If there isn't a current mem area or the current mem area is out of space
821 * then allocate a new mem area. We'll first check and see if we can use
822 * the "free_mem_area". Otherwise we'll just malloc the mem area.
824 if ((!mem_chunk->mem_area) ||
825 ((mem_chunk->mem_area->index + mem_chunk->atom_size) > mem_chunk->area_size))
827 if (mem_chunk->free_mem_area)
829 mem_chunk->mem_area = mem_chunk->free_mem_area;
830 mem_chunk->free_mem_area = NULL;
834 #ifdef ENABLE_GC_FRIENDLY
835 mem_chunk->mem_area = (GMemArea*) g_malloc0 (sizeof (GMemArea) -
837 mem_chunk->area_size);
838 #else /* !ENABLE_GC_FRIENDLY */
839 mem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
841 mem_chunk->area_size);
842 #endif /* ENABLE_GC_FRIENDLY */
844 mem_chunk->num_mem_areas += 1;
845 mem_chunk->mem_area->next = mem_chunk->mem_areas;
846 mem_chunk->mem_area->prev = NULL;
848 if (mem_chunk->mem_areas)
849 mem_chunk->mem_areas->prev = mem_chunk->mem_area;
850 mem_chunk->mem_areas = mem_chunk->mem_area;
852 if (mem_chunk->type == G_ALLOC_AND_FREE)
853 g_tree_insert (mem_chunk->mem_tree, mem_chunk->mem_area, mem_chunk->mem_area);
856 mem_chunk->mem_area->index = 0;
857 mem_chunk->mem_area->free = mem_chunk->area_size;
858 mem_chunk->mem_area->allocated = 0;
859 mem_chunk->mem_area->mark = 0;
862 /* Get the memory and modify the state variables appropriately.
864 mem = (gpointer) &mem_chunk->mem_area->mem[mem_chunk->mem_area->index];
865 mem_chunk->mem_area->index += mem_chunk->atom_size;
866 mem_chunk->mem_area->free -= mem_chunk->atom_size;
867 mem_chunk->mem_area->allocated += 1;
871 LEAVE_MEM_CHUNK_ROUTINE ();
877 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
881 mem = g_mem_chunk_alloc (mem_chunk);
884 memset (mem, 0, mem_chunk->atom_size);
891 g_mem_chunk_free (GMemChunk *mem_chunk,
895 GFreeAtom *free_atom;
897 g_return_if_fail (mem_chunk != NULL);
898 g_return_if_fail (mem != NULL);
900 ENTER_MEM_CHUNK_ROUTINE ();
902 #ifdef ENABLE_GC_FRIENDLY
903 memset (mem, 0, mem_chunk->atom_size);
904 #endif /* ENABLE_GC_FRIENDLY */
906 /* Don't do anything if this is an ALLOC_ONLY chunk
908 if (mem_chunk->type == G_ALLOC_AND_FREE)
910 /* Place the memory on the "free_atoms" list
912 free_atom = (GFreeAtom*) mem;
913 free_atom->next = mem_chunk->free_atoms;
914 mem_chunk->free_atoms = free_atom;
916 temp_area = g_tree_search (mem_chunk->mem_tree,
917 (GCompareFunc) g_mem_chunk_area_search,
920 temp_area->allocated -= 1;
922 if (temp_area->allocated == 0)
925 mem_chunk->num_marked_areas += 1;
929 LEAVE_MEM_CHUNK_ROUTINE ();
932 /* This doesn't free the free_area if there is one */
934 g_mem_chunk_clean (GMemChunk *mem_chunk)
937 GFreeAtom *prev_free_atom;
938 GFreeAtom *temp_free_atom;
941 g_return_if_fail (mem_chunk != NULL);
943 ENTER_MEM_CHUNK_ROUTINE ();
945 if (mem_chunk->type == G_ALLOC_AND_FREE)
947 prev_free_atom = NULL;
948 temp_free_atom = mem_chunk->free_atoms;
950 while (temp_free_atom)
952 mem = (gpointer) temp_free_atom;
954 mem_area = g_tree_search (mem_chunk->mem_tree,
955 (GCompareFunc) g_mem_chunk_area_search,
958 /* If this mem area is marked for destruction then delete the
959 * area and list node and decrement the free mem.
964 prev_free_atom->next = temp_free_atom->next;
966 mem_chunk->free_atoms = temp_free_atom->next;
967 temp_free_atom = temp_free_atom->next;
969 mem_area->free += mem_chunk->atom_size;
970 if (mem_area->free == mem_chunk->area_size)
972 mem_chunk->num_mem_areas -= 1;
973 mem_chunk->num_marked_areas -= 1;
976 mem_area->next->prev = mem_area->prev;
978 mem_area->prev->next = mem_area->next;
979 if (mem_area == mem_chunk->mem_areas)
980 mem_chunk->mem_areas = mem_chunk->mem_areas->next;
981 if (mem_area == mem_chunk->mem_area)
982 mem_chunk->mem_area = NULL;
984 if (mem_chunk->type == G_ALLOC_AND_FREE)
985 g_tree_remove (mem_chunk->mem_tree, mem_area);
991 prev_free_atom = temp_free_atom;
992 temp_free_atom = temp_free_atom->next;
996 LEAVE_MEM_CHUNK_ROUTINE ();
1000 g_mem_chunk_reset (GMemChunk *mem_chunk)
1002 GMemArea *mem_areas;
1003 GMemArea *temp_area;
1005 g_return_if_fail (mem_chunk != NULL);
1007 ENTER_MEM_CHUNK_ROUTINE ();
1009 mem_areas = mem_chunk->mem_areas;
1010 mem_chunk->num_mem_areas = 0;
1011 mem_chunk->mem_areas = NULL;
1012 mem_chunk->mem_area = NULL;
1016 temp_area = mem_areas;
1017 mem_areas = mem_areas->next;
1021 mem_chunk->free_atoms = NULL;
1023 if (mem_chunk->mem_tree)
1025 g_tree_destroy (mem_chunk->mem_tree);
1026 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
1029 LEAVE_MEM_CHUNK_ROUTINE ();
1033 g_mem_chunk_print (GMemChunk *mem_chunk)
1035 GMemArea *mem_areas;
1038 g_return_if_fail (mem_chunk != NULL);
1040 mem_areas = mem_chunk->mem_areas;
1045 mem += mem_chunk->area_size - mem_areas->free;
1046 mem_areas = mem_areas->next;
1049 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO,
1050 "%s: %ld bytes using %d mem areas",
1051 mem_chunk->name, mem, mem_chunk->num_mem_areas);
1055 g_mem_chunk_info (void)
1057 GMemChunk *mem_chunk;
1061 g_mutex_lock (mem_chunks_lock);
1062 mem_chunk = mem_chunks;
1066 mem_chunk = mem_chunk->next;
1068 g_mutex_unlock (mem_chunks_lock);
1070 g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "%d mem chunks", count);
1072 g_mutex_lock (mem_chunks_lock);
1073 mem_chunk = mem_chunks;
1074 g_mutex_unlock (mem_chunks_lock);
1078 g_mem_chunk_print ((GMemChunk*) mem_chunk);
1079 mem_chunk = mem_chunk->next;
1084 g_blow_chunks (void)
1086 GMemChunk *mem_chunk;
1088 g_mutex_lock (mem_chunks_lock);
1089 mem_chunk = mem_chunks;
1090 g_mutex_unlock (mem_chunks_lock);
1093 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
1094 mem_chunk = mem_chunk->next;
1099 g_mem_chunk_compute_size (gulong size,
1103 gulong lower, upper;
1106 while (power_of_2 < size)
1109 lower = power_of_2 >> 1;
1112 if (size - lower < upper - size && lower >= min_size)
1119 g_mem_chunk_area_compare (GMemArea *a,
1122 if (a->mem > b->mem)
1124 else if (a->mem < b->mem)
1130 g_mem_chunk_area_search (GMemArea *a,
1135 if (addr < &a->mem[a->index])
1142 #else /* DISABLE_MEM_POOLS */
1145 guint alloc_size; /* the size of an atom */
1149 g_mem_chunk_new (const gchar *name,
1154 GMinimalMemChunk *mem_chunk;
1156 g_return_val_if_fail (atom_size > 0, NULL);
1158 mem_chunk = g_new (GMinimalMemChunk, 1);
1159 mem_chunk->alloc_size = atom_size;
1161 return ((GMemChunk*) mem_chunk);
1165 g_mem_chunk_destroy (GMemChunk *mem_chunk)
1167 g_return_if_fail (mem_chunk != NULL);
1173 g_mem_chunk_alloc (GMemChunk *mem_chunk)
1175 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1177 g_return_val_if_fail (mem_chunk != NULL, NULL);
1179 return g_malloc (minimal->alloc_size);
1183 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
1185 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1187 g_return_val_if_fail (mem_chunk != NULL, NULL);
1189 return g_malloc0 (minimal->alloc_size);
1193 g_mem_chunk_free (GMemChunk *mem_chunk,
1196 g_return_if_fail (mem_chunk != NULL);
1201 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
1202 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
1203 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
1204 void g_mem_chunk_info (void) {}
1205 void g_blow_chunks (void) {}
1207 #endif /* DISABLE_MEM_POOLS */
1210 /* generic allocators
1212 struct _GAllocator /* from gmem.c */
1215 guint16 n_preallocs;
1216 guint is_unused : 1;
1219 GMemChunk *mem_chunk;
1220 gpointer dummy; /* implementation specific */
1224 g_allocator_new (const gchar *name,
1227 GAllocator *allocator;
1229 g_return_val_if_fail (name != NULL, NULL);
1231 allocator = g_new0 (GAllocator, 1);
1232 allocator->name = g_strdup (name);
1233 allocator->n_preallocs = CLAMP (n_preallocs, 1, 65535);
1234 allocator->is_unused = TRUE;
1235 allocator->type = 0;
1236 allocator->last = NULL;
1237 allocator->mem_chunk = NULL;
1238 allocator->dummy = NULL;
1244 g_allocator_free (GAllocator *allocator)
1246 g_return_if_fail (allocator != NULL);
1247 g_return_if_fail (allocator->is_unused == TRUE);
1249 g_free (allocator->name);
1250 if (allocator->mem_chunk)
1251 g_mem_chunk_destroy (allocator->mem_chunk);
1259 #ifndef DISABLE_MEM_POOLS
1260 mem_chunks_lock = g_mutex_new ();
1262 #ifndef G_DISABLE_CHECKS
1263 mem_chunk_recursion = g_private_new (NULL);
1264 g_profile_mutex = g_mutex_new ();