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 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 G_LOCK_DEFINE_STATIC (g_profile_mutex
);
270 #ifdef G_ENABLE_DEBUG
271 static volatile gulong glib_trap_free_size
= 0;
272 static volatile gulong glib_trap_realloc_size
= 0;
273 static volatile gulong glib_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_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_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_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])];
366 gulong local_allocs
= profile_allocs
;
367 gulong local_zinit
= profile_zinit
;
368 gulong local_frees
= profile_frees
;
369 gulong local_mc_allocs
= profile_mc_allocs
;
370 gulong local_mc_frees
= profile_mc_frees
;
372 G_LOCK (g_profile_mutex
);
376 G_UNLOCK (g_profile_mutex
);
380 memcpy (local_data
, profile_data
, (MEM_PROFILE_TABLE_SIZE
+ 1) * 8 * sizeof (profile_data
[0]));
382 g_print ("GLib Memory statistics (successful operations):\n");
383 profile_print_locked (local_data
, TRUE
);
384 g_print ("GLib Memory statistics (failing operations):\n");
385 profile_print_locked (local_data
, FALSE
);
386 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
389 ((gdouble
) local_zinit
) / local_allocs
* 100.0,
391 ((gdouble
) local_frees
) / local_allocs
* 100.0,
392 local_allocs
- local_frees
);
393 g_print ("MemChunk bytes: allocated=%lu, freed=%lu (%.2f%%), remaining=%lu\n",
396 ((gdouble
) local_mc_frees
) / local_mc_allocs
* 100.0,
397 local_mc_allocs
- local_mc_frees
);
398 G_UNLOCK (g_profile_mutex
);
402 profiler_try_malloc (gsize n_bytes
)
406 #ifdef G_ENABLE_DEBUG
407 if (glib_trap_malloc_size
== n_bytes
)
409 #endif /* G_ENABLE_DEBUG */
411 p
= standard_malloc (sizeof (gulong
) * 2 + n_bytes
);
415 p
[0] = 0; /* free count */
416 p
[1] = n_bytes
; /* length */
417 profiler_log (PROFILER_ALLOC
, n_bytes
, TRUE
);
421 profiler_log (PROFILER_ALLOC
, n_bytes
, FALSE
);
427 profiler_malloc (gsize n_bytes
)
429 gpointer mem
= profiler_try_malloc (n_bytes
);
438 profiler_calloc (gsize n_blocks
,
441 gsize l
= n_blocks
* n_block_bytes
;
444 #ifdef G_ENABLE_DEBUG
445 if (glib_trap_malloc_size
== l
)
447 #endif /* G_ENABLE_DEBUG */
449 p
= standard_calloc (1, sizeof (gulong
) * 2 + l
);
453 p
[0] = 0; /* free count */
454 p
[1] = l
; /* length */
455 profiler_log (PROFILER_ALLOC
| PROFILER_ZINIT
, l
, TRUE
);
460 profiler_log (PROFILER_ALLOC
| PROFILER_ZINIT
, l
, FALSE
);
468 profiler_free (gpointer mem
)
473 if (p
[0]) /* free count */
475 g_warning ("free(%p): memory has been freed %lu times already", p
+ 2, p
[0]);
476 profiler_log (PROFILER_FREE
,
482 #ifdef G_ENABLE_DEBUG
483 if (glib_trap_free_size
== p
[1])
485 #endif /* G_ENABLE_DEBUG */
487 profiler_log (PROFILER_FREE
,
490 memset (p
+ 2, 0xaa, p
[1]);
492 /* for all those that miss standard_free (p); in this place, yes,
493 * we do leak all memory when profiling, and that is intentional
494 * to catch double frees. patch submissions are futile.
501 profiler_try_realloc (gpointer mem
,
508 #ifdef G_ENABLE_DEBUG
509 if (glib_trap_realloc_size
== n_bytes
)
511 #endif /* G_ENABLE_DEBUG */
513 if (mem
&& p
[0]) /* free count */
515 g_warning ("realloc(%p, %u): memory has been freed %lu times already", p
+ 2, n_bytes
, p
[0]);
516 profiler_log (PROFILER_ALLOC
| PROFILER_RELOC
, n_bytes
, FALSE
);
522 p
= standard_realloc (mem
? p
: NULL
, sizeof (gulong
) * 2 + n_bytes
);
527 profiler_log (PROFILER_FREE
| PROFILER_RELOC
, p
[1], TRUE
);
530 profiler_log (PROFILER_ALLOC
| PROFILER_RELOC
, p
[1], TRUE
);
534 profiler_log (PROFILER_ALLOC
| PROFILER_RELOC
, n_bytes
, FALSE
);
541 profiler_realloc (gpointer mem
,
544 mem
= profiler_try_realloc (mem
, n_bytes
);
552 static GMemVTable profiler_table
= {
558 profiler_try_realloc
,
560 GMemVTable
*glib_mem_profiler_table
= &profiler_table
;
562 #endif /* !G_DISABLE_CHECKS */
565 /* --- MemChunks --- */
566 typedef struct _GFreeAtom GFreeAtom
;
567 typedef struct _GMemArea GMemArea
;
568 typedef struct _GRealMemChunk GRealMemChunk
;
577 GMemArea
*next
; /* the next mem area */
578 GMemArea
*prev
; /* the previous mem area */
579 gulong index
; /* the current index into the "mem" array */
580 gulong free
; /* the number of free bytes in this mem area */
581 gulong allocated
; /* the number of atoms allocated from this area */
582 gulong mark
; /* is this mem area marked for deletion */
583 gchar mem
[MEM_AREA_SIZE
]; /* the mem array from which atoms get allocated
584 * the actual size of this array is determined by
585 * the mem chunk "area_size". ANSI says that it
586 * must be declared to be the maximum size it
587 * can possibly be (even though the actual size
592 struct _GRealMemChunk
594 gchar
*name
; /* name of this MemChunk...used for debugging output */
595 gint type
; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
596 gint num_mem_areas
; /* the number of memory areas */
597 gint num_marked_areas
; /* the number of areas marked for deletion */
598 guint atom_size
; /* the size of an atom */
599 gulong area_size
; /* the size of a memory area */
600 GMemArea
*mem_area
; /* the current memory area */
601 GMemArea
*mem_areas
; /* a list of all the mem areas owned by this chunk */
602 GMemArea
*free_mem_area
; /* the free area...which is about to be destroyed */
603 GFreeAtom
*free_atoms
; /* the free atoms list */
604 GTree
*mem_tree
; /* tree of mem areas sorted by memory address */
605 GRealMemChunk
*next
; /* pointer to the next chunk */
606 GRealMemChunk
*prev
; /* pointer to the previous chunk */
610 #ifndef DISABLE_MEM_POOLS
611 static gulong
g_mem_chunk_compute_size (gulong size
,
612 gulong min_size
) G_GNUC_CONST
;
613 static gint
g_mem_chunk_area_compare (GMemArea
*a
,
615 static gint
g_mem_chunk_area_search (GMemArea
*a
,
618 /* here we can't use StaticMutexes, as they depend upon a working
619 * g_malloc, the same holds true for StaticPrivate
621 static GMutex
*mem_chunks_lock
= NULL
;
622 static GRealMemChunk
*mem_chunks
= NULL
;
625 g_mem_chunk_new (gchar
*name
,
630 GRealMemChunk
*mem_chunk
;
633 g_return_val_if_fail (atom_size
> 0, NULL
);
634 g_return_val_if_fail (area_size
>= atom_size
, NULL
);
636 ENTER_MEM_CHUNK_ROUTINE ();
638 area_size
= (area_size
+ atom_size
- 1) / atom_size
;
639 area_size
*= atom_size
;
641 mem_chunk
= g_new (struct _GRealMemChunk
, 1);
642 mem_chunk
->name
= name
;
643 mem_chunk
->type
= type
;
644 mem_chunk
->num_mem_areas
= 0;
645 mem_chunk
->num_marked_areas
= 0;
646 mem_chunk
->mem_area
= NULL
;
647 mem_chunk
->free_mem_area
= NULL
;
648 mem_chunk
->free_atoms
= NULL
;
649 mem_chunk
->mem_tree
= NULL
;
650 mem_chunk
->mem_areas
= NULL
;
651 mem_chunk
->atom_size
= atom_size
;
653 if (mem_chunk
->type
== G_ALLOC_AND_FREE
)
654 mem_chunk
->mem_tree
= g_tree_new ((GCompareFunc
) g_mem_chunk_area_compare
);
656 if (mem_chunk
->atom_size
% G_MEM_ALIGN
)
657 mem_chunk
->atom_size
+= G_MEM_ALIGN
- (mem_chunk
->atom_size
% G_MEM_ALIGN
);
659 rarea_size
= area_size
+ sizeof (GMemArea
) - MEM_AREA_SIZE
;
660 rarea_size
= g_mem_chunk_compute_size (rarea_size
, atom_size
+ sizeof (GMemArea
) - MEM_AREA_SIZE
);
661 mem_chunk
->area_size
= rarea_size
- (sizeof (GMemArea
) - MEM_AREA_SIZE
);
663 g_mutex_lock (mem_chunks_lock
);
664 mem_chunk
->next
= mem_chunks
;
665 mem_chunk
->prev
= NULL
;
667 mem_chunks
->prev
= mem_chunk
;
668 mem_chunks
= mem_chunk
;
669 g_mutex_unlock (mem_chunks_lock
);
671 LEAVE_MEM_CHUNK_ROUTINE ();
673 return ((GMemChunk
*) mem_chunk
);
677 g_mem_chunk_destroy (GMemChunk
*mem_chunk
)
679 GRealMemChunk
*rmem_chunk
;
683 g_return_if_fail (mem_chunk
!= NULL
);
685 ENTER_MEM_CHUNK_ROUTINE ();
687 rmem_chunk
= (GRealMemChunk
*) mem_chunk
;
689 mem_areas
= rmem_chunk
->mem_areas
;
692 temp_area
= mem_areas
;
693 mem_areas
= mem_areas
->next
;
697 if (rmem_chunk
->next
)
698 rmem_chunk
->next
->prev
= rmem_chunk
->prev
;
699 if (rmem_chunk
->prev
)
700 rmem_chunk
->prev
->next
= rmem_chunk
->next
;
702 g_mutex_lock (mem_chunks_lock
);
703 if (rmem_chunk
== mem_chunks
)
704 mem_chunks
= mem_chunks
->next
;
705 g_mutex_unlock (mem_chunks_lock
);
707 if (rmem_chunk
->type
== G_ALLOC_AND_FREE
)
708 g_tree_destroy (rmem_chunk
->mem_tree
);
712 LEAVE_MEM_CHUNK_ROUTINE ();
716 g_mem_chunk_alloc (GMemChunk
*mem_chunk
)
718 GRealMemChunk
*rmem_chunk
;
722 ENTER_MEM_CHUNK_ROUTINE ();
724 g_return_val_if_fail (mem_chunk
!= NULL
, NULL
);
726 rmem_chunk
= (GRealMemChunk
*) mem_chunk
;
728 while (rmem_chunk
->free_atoms
)
730 /* Get the first piece of memory on the "free_atoms" list.
731 * We can go ahead and destroy the list node we used to keep
732 * track of it with and to update the "free_atoms" list to
733 * point to its next element.
735 mem
= rmem_chunk
->free_atoms
;
736 rmem_chunk
->free_atoms
= rmem_chunk
->free_atoms
->next
;
738 /* Determine which area this piece of memory is allocated from */
739 temp_area
= g_tree_search (rmem_chunk
->mem_tree
,
740 (GCompareFunc
) g_mem_chunk_area_search
,
743 /* If the area has been marked, then it is being destroyed.
744 * (ie marked to be destroyed).
745 * We check to see if all of the segments on the free list that
746 * reference this area have been removed. This occurs when
747 * the ammount of free memory is less than the allocatable size.
748 * If the chunk should be freed, then we place it in the "free_mem_area".
749 * This is so we make sure not to free the mem area here and then
750 * allocate it again a few lines down.
751 * If we don't allocate a chunk a few lines down then the "free_mem_area"
753 * If there is already a "free_mem_area" then we'll just free this mem area.
757 /* Update the "free" memory available in that area */
758 temp_area
->free
+= rmem_chunk
->atom_size
;
760 if (temp_area
->free
== rmem_chunk
->area_size
)
762 if (temp_area
== rmem_chunk
->mem_area
)
763 rmem_chunk
->mem_area
= NULL
;
765 if (rmem_chunk
->free_mem_area
)
767 rmem_chunk
->num_mem_areas
-= 1;
770 temp_area
->next
->prev
= temp_area
->prev
;
772 temp_area
->prev
->next
= temp_area
->next
;
773 if (temp_area
== rmem_chunk
->mem_areas
)
774 rmem_chunk
->mem_areas
= rmem_chunk
->mem_areas
->next
;
776 if (rmem_chunk
->type
== G_ALLOC_AND_FREE
)
777 g_tree_remove (rmem_chunk
->mem_tree
, temp_area
);
781 rmem_chunk
->free_mem_area
= temp_area
;
783 rmem_chunk
->num_marked_areas
-= 1;
788 /* Update the number of allocated atoms count.
790 temp_area
->allocated
+= 1;
792 /* The area wasn't marked...return the memory
798 /* If there isn't a current mem area or the current mem area is out of space
799 * then allocate a new mem area. We'll first check and see if we can use
800 * the "free_mem_area". Otherwise we'll just malloc the mem area.
802 if ((!rmem_chunk
->mem_area
) ||
803 ((rmem_chunk
->mem_area
->index
+ rmem_chunk
->atom_size
) > rmem_chunk
->area_size
))
805 if (rmem_chunk
->free_mem_area
)
807 rmem_chunk
->mem_area
= rmem_chunk
->free_mem_area
;
808 rmem_chunk
->free_mem_area
= NULL
;
812 #ifdef ENABLE_GC_FRIENDLY
813 rmem_chunk
->mem_area
= (GMemArea
*) g_malloc0 (sizeof (GMemArea
) -
815 rmem_chunk
->area_size
);
816 #else /* !ENABLE_GC_FRIENDLY */
817 rmem_chunk
->mem_area
= (GMemArea
*) g_malloc (sizeof (GMemArea
) -
819 rmem_chunk
->area_size
);
820 #endif /* ENABLE_GC_FRIENDLY */
822 rmem_chunk
->num_mem_areas
+= 1;
823 rmem_chunk
->mem_area
->next
= rmem_chunk
->mem_areas
;
824 rmem_chunk
->mem_area
->prev
= NULL
;
826 if (rmem_chunk
->mem_areas
)
827 rmem_chunk
->mem_areas
->prev
= rmem_chunk
->mem_area
;
828 rmem_chunk
->mem_areas
= rmem_chunk
->mem_area
;
830 if (rmem_chunk
->type
== G_ALLOC_AND_FREE
)
831 g_tree_insert (rmem_chunk
->mem_tree
, rmem_chunk
->mem_area
, rmem_chunk
->mem_area
);
834 rmem_chunk
->mem_area
->index
= 0;
835 rmem_chunk
->mem_area
->free
= rmem_chunk
->area_size
;
836 rmem_chunk
->mem_area
->allocated
= 0;
837 rmem_chunk
->mem_area
->mark
= 0;
840 /* Get the memory and modify the state variables appropriately.
842 mem
= (gpointer
) &rmem_chunk
->mem_area
->mem
[rmem_chunk
->mem_area
->index
];
843 rmem_chunk
->mem_area
->index
+= rmem_chunk
->atom_size
;
844 rmem_chunk
->mem_area
->free
-= rmem_chunk
->atom_size
;
845 rmem_chunk
->mem_area
->allocated
+= 1;
849 LEAVE_MEM_CHUNK_ROUTINE ();
855 g_mem_chunk_alloc0 (GMemChunk
*mem_chunk
)
859 mem
= g_mem_chunk_alloc (mem_chunk
);
862 GRealMemChunk
*rmem_chunk
= (GRealMemChunk
*) mem_chunk
;
864 memset (mem
, 0, rmem_chunk
->atom_size
);
871 g_mem_chunk_free (GMemChunk
*mem_chunk
,
874 GRealMemChunk
*rmem_chunk
;
876 GFreeAtom
*free_atom
;
878 g_return_if_fail (mem_chunk
!= NULL
);
879 g_return_if_fail (mem
!= NULL
);
881 ENTER_MEM_CHUNK_ROUTINE ();
883 rmem_chunk
= (GRealMemChunk
*) mem_chunk
;
885 #ifdef ENABLE_GC_FRIENDLY
886 memset (mem
, 0, rmem_chunk
->atom_size
);
887 #endif /* ENABLE_GC_FRIENDLY */
889 /* Don't do anything if this is an ALLOC_ONLY chunk
891 if (rmem_chunk
->type
== G_ALLOC_AND_FREE
)
893 /* Place the memory on the "free_atoms" list
895 free_atom
= (GFreeAtom
*) mem
;
896 free_atom
->next
= rmem_chunk
->free_atoms
;
897 rmem_chunk
->free_atoms
= free_atom
;
899 temp_area
= g_tree_search (rmem_chunk
->mem_tree
,
900 (GCompareFunc
) g_mem_chunk_area_search
,
903 temp_area
->allocated
-= 1;
905 if (temp_area
->allocated
== 0)
908 rmem_chunk
->num_marked_areas
+= 1;
912 LEAVE_MEM_CHUNK_ROUTINE ();
915 /* This doesn't free the free_area if there is one */
917 g_mem_chunk_clean (GMemChunk
*mem_chunk
)
919 GRealMemChunk
*rmem_chunk
;
921 GFreeAtom
*prev_free_atom
;
922 GFreeAtom
*temp_free_atom
;
925 g_return_if_fail (mem_chunk
!= NULL
);
927 ENTER_MEM_CHUNK_ROUTINE ();
929 rmem_chunk
= (GRealMemChunk
*) mem_chunk
;
931 if (rmem_chunk
->type
== G_ALLOC_AND_FREE
)
933 prev_free_atom
= NULL
;
934 temp_free_atom
= rmem_chunk
->free_atoms
;
936 while (temp_free_atom
)
938 mem
= (gpointer
) temp_free_atom
;
940 mem_area
= g_tree_search (rmem_chunk
->mem_tree
,
941 (GCompareFunc
) g_mem_chunk_area_search
,
944 /* If this mem area is marked for destruction then delete the
945 * area and list node and decrement the free mem.
950 prev_free_atom
->next
= temp_free_atom
->next
;
952 rmem_chunk
->free_atoms
= temp_free_atom
->next
;
953 temp_free_atom
= temp_free_atom
->next
;
955 mem_area
->free
+= rmem_chunk
->atom_size
;
956 if (mem_area
->free
== rmem_chunk
->area_size
)
958 rmem_chunk
->num_mem_areas
-= 1;
959 rmem_chunk
->num_marked_areas
-= 1;
962 mem_area
->next
->prev
= mem_area
->prev
;
964 mem_area
->prev
->next
= mem_area
->next
;
965 if (mem_area
== rmem_chunk
->mem_areas
)
966 rmem_chunk
->mem_areas
= rmem_chunk
->mem_areas
->next
;
967 if (mem_area
== rmem_chunk
->mem_area
)
968 rmem_chunk
->mem_area
= NULL
;
970 if (rmem_chunk
->type
== G_ALLOC_AND_FREE
)
971 g_tree_remove (rmem_chunk
->mem_tree
, mem_area
);
977 prev_free_atom
= temp_free_atom
;
978 temp_free_atom
= temp_free_atom
->next
;
982 LEAVE_MEM_CHUNK_ROUTINE ();
986 g_mem_chunk_reset (GMemChunk
*mem_chunk
)
988 GRealMemChunk
*rmem_chunk
;
992 g_return_if_fail (mem_chunk
!= NULL
);
994 ENTER_MEM_CHUNK_ROUTINE ();
996 rmem_chunk
= (GRealMemChunk
*) mem_chunk
;
998 mem_areas
= rmem_chunk
->mem_areas
;
999 rmem_chunk
->num_mem_areas
= 0;
1000 rmem_chunk
->mem_areas
= NULL
;
1001 rmem_chunk
->mem_area
= NULL
;
1005 temp_area
= mem_areas
;
1006 mem_areas
= mem_areas
->next
;
1010 rmem_chunk
->free_atoms
= NULL
;
1012 if (rmem_chunk
->mem_tree
)
1013 g_tree_destroy (rmem_chunk
->mem_tree
);
1014 rmem_chunk
->mem_tree
= g_tree_new ((GCompareFunc
) g_mem_chunk_area_compare
);
1016 LEAVE_MEM_CHUNK_ROUTINE ();
1020 g_mem_chunk_print (GMemChunk
*mem_chunk
)
1022 GRealMemChunk
*rmem_chunk
;
1023 GMemArea
*mem_areas
;
1026 g_return_if_fail (mem_chunk
!= NULL
);
1028 rmem_chunk
= (GRealMemChunk
*) mem_chunk
;
1029 mem_areas
= rmem_chunk
->mem_areas
;
1034 mem
+= rmem_chunk
->area_size
- mem_areas
->free
;
1035 mem_areas
= mem_areas
->next
;
1038 g_log (g_log_domain_glib
, G_LOG_LEVEL_INFO
,
1039 "%s: %ld bytes using %d mem areas",
1040 rmem_chunk
->name
, mem
, rmem_chunk
->num_mem_areas
);
1044 g_mem_chunk_info (void)
1046 GRealMemChunk
*mem_chunk
;
1050 g_mutex_lock (mem_chunks_lock
);
1051 mem_chunk
= mem_chunks
;
1055 mem_chunk
= mem_chunk
->next
;
1057 g_mutex_unlock (mem_chunks_lock
);
1059 g_log (g_log_domain_glib
, G_LOG_LEVEL_INFO
, "%d mem chunks", count
);
1061 g_mutex_lock (mem_chunks_lock
);
1062 mem_chunk
= mem_chunks
;
1063 g_mutex_unlock (mem_chunks_lock
);
1067 g_mem_chunk_print ((GMemChunk
*) mem_chunk
);
1068 mem_chunk
= mem_chunk
->next
;
1073 g_blow_chunks (void)
1075 GRealMemChunk
*mem_chunk
;
1077 g_mutex_lock (mem_chunks_lock
);
1078 mem_chunk
= mem_chunks
;
1079 g_mutex_unlock (mem_chunks_lock
);
1082 g_mem_chunk_clean ((GMemChunk
*) mem_chunk
);
1083 mem_chunk
= mem_chunk
->next
;
1088 g_mem_chunk_compute_size (gulong size
,
1092 gulong lower
, upper
;
1095 while (power_of_2
< size
)
1098 lower
= power_of_2
>> 1;
1101 if (size
- lower
< upper
- size
&& lower
>= min_size
)
1108 g_mem_chunk_area_compare (GMemArea
*a
,
1111 if (a
->mem
> b
->mem
)
1113 else if (a
->mem
< b
->mem
)
1119 g_mem_chunk_area_search (GMemArea
*a
,
1124 if (addr
< &a
->mem
[a
->index
])
1131 #else /* DISABLE_MEM_POOLS */
1134 guint alloc_size
; /* the size of an atom */
1138 g_mem_chunk_new (gchar
*name
,
1143 GMinimalMemChunk
*mem_chunk
;
1145 g_return_val_if_fail (atom_size
> 0, NULL
);
1147 mem_chunk
= g_new (GMinimalMemChunk
, 1);
1148 mem_chunk
->alloc_size
= atom_size
;
1150 return ((GMemChunk
*) mem_chunk
);
1154 g_mem_chunk_destroy (GMemChunk
*mem_chunk
)
1156 g_return_if_fail (mem_chunk
!= NULL
);
1162 g_mem_chunk_alloc (GMemChunk
*mem_chunk
)
1164 GMinimalMemChunk
*minimal
= (GMinimalMemChunk
*)mem_chunk
;
1166 g_return_val_if_fail (mem_chunk
!= NULL
, NULL
);
1168 return g_malloc (minimal
->alloc_size
);
1172 g_mem_chunk_alloc0 (GMemChunk
*mem_chunk
)
1174 GMinimalMemChunk
*minimal
= (GMinimalMemChunk
*)mem_chunk
;
1176 g_return_val_if_fail (mem_chunk
!= NULL
, NULL
);
1178 return g_malloc0 (minimal
->alloc_size
);
1182 g_mem_chunk_free (GMemChunk
*mem_chunk
,
1185 g_return_if_fail (mem_chunk
!= NULL
);
1190 void g_mem_chunk_clean (GMemChunk
*mem_chunk
) {}
1191 void g_mem_chunk_reset (GMemChunk
*mem_chunk
) {}
1192 void g_mem_chunk_print (GMemChunk
*mem_chunk
) {}
1193 void g_mem_chunk_info (void) {}
1194 void g_blow_chunks (void) {}
1196 #endif /* DISABLE_MEM_POOLS */
1199 /* generic allocators
1201 struct _GAllocator
/* from gmem.c */
1204 guint16 n_preallocs
;
1205 guint is_unused
: 1;
1208 GMemChunk
*mem_chunk
;
1209 gpointer dummy
; /* implementation specific */
1213 g_allocator_new (const gchar
*name
,
1216 GAllocator
*allocator
;
1218 g_return_val_if_fail (name
!= NULL
, NULL
);
1220 allocator
= g_new0 (GAllocator
, 1);
1221 allocator
->name
= g_strdup (name
);
1222 allocator
->n_preallocs
= CLAMP (n_preallocs
, 1, 65535);
1223 allocator
->is_unused
= TRUE
;
1224 allocator
->type
= 0;
1225 allocator
->last
= NULL
;
1226 allocator
->mem_chunk
= NULL
;
1227 allocator
->dummy
= NULL
;
1233 g_allocator_free (GAllocator
*allocator
)
1235 g_return_if_fail (allocator
!= NULL
);
1236 g_return_if_fail (allocator
->is_unused
== TRUE
);
1238 g_free (allocator
->name
);
1239 if (allocator
->mem_chunk
)
1240 g_mem_chunk_destroy (allocator
->mem_chunk
);
1248 #ifndef DISABLE_MEM_POOLS
1249 mem_chunks_lock
= g_mutex_new ();
1251 #ifndef G_DISABLE_CHECKS
1252 mem_chunk_recursion
= g_private_new (NULL
);