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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
39 /* #define ENABLE_MEM_PROFILE */
40 /* #define ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS */
41 /* #define ENABLE_MEM_CHECK */
42 #define MEM_PROFILE_TABLE_SIZE 8192
45 * This library can check for some attempts to do illegal things to
46 * memory (ENABLE_MEM_CHECK), and can do profiling
47 * (ENABLE_MEM_PROFILE). Both features are implemented by storing
48 * words before the start of the memory chunk.
50 * The first, at offset -2*SIZEOF_LONG, is used only if
51 * ENABLE_MEM_CHECK is set, and stores 0 after the memory has been
52 * allocated and 1 when it has been freed. The second, at offset
53 * -SIZEOF_LONG, is used if either flag is set and stores the size of
56 * The MEM_CHECK flag is checked when memory is realloc'd and free'd,
57 * and it can be explicitly checked before using a block by calling
61 #if defined(ENABLE_MEM_PROFILE) && defined(ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS)
62 #define ENTER_MEM_CHUNK_ROUTINE() \
63 g_private_set (allocating_for_mem_chunk, \
64 g_private_get (allocating_for_mem_chunk) + 1)
65 #define LEAVE_MEM_CHUNK_ROUTINE() \
66 g_private_set (allocating_for_mem_chunk, \
67 g_private_get (allocating_for_mem_chunk) - 1)
69 #define ENTER_MEM_CHUNK_ROUTINE()
70 #define LEAVE_MEM_CHUNK_ROUTINE()
74 #define MAX_MEM_AREA 65536L
75 #define MEM_AREA_SIZE 4L
77 #if SIZEOF_VOID_P > SIZEOF_LONG
78 #define MEM_ALIGN SIZEOF_VOID_P
80 #define MEM_ALIGN SIZEOF_LONG
84 typedef struct _GFreeAtom GFreeAtom;
85 typedef struct _GMemArea GMemArea;
86 typedef struct _GRealMemChunk GRealMemChunk;
95 GMemArea *next; /* the next mem area */
96 GMemArea *prev; /* the previous mem area */
97 gulong index; /* the current index into the "mem" array */
98 gulong free; /* the number of free bytes in this mem area */
99 gulong allocated; /* the number of atoms allocated from this area */
100 gulong mark; /* is this mem area marked for deletion */
101 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
102 * the actual size of this array is determined by
103 * the mem chunk "area_size". ANSI says that it
104 * must be declared to be the maximum size it
105 * can possibly be (even though the actual size
110 struct _GRealMemChunk
112 gchar *name; /* name of this MemChunk...used for debugging output */
113 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
114 gint num_mem_areas; /* the number of memory areas */
115 gint num_marked_areas; /* the number of areas marked for deletion */
116 guint atom_size; /* the size of an atom */
117 gulong area_size; /* the size of a memory area */
118 GMemArea *mem_area; /* the current memory area */
119 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
120 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
121 GFreeAtom *free_atoms; /* the free atoms list */
122 GTree *mem_tree; /* tree of mem areas sorted by memory address */
123 GRealMemChunk *next; /* pointer to the next chunk */
124 GRealMemChunk *prev; /* pointer to the previous chunk */
128 static gulong g_mem_chunk_compute_size (gulong size);
129 static gint g_mem_chunk_area_compare (GMemArea *a,
131 static gint g_mem_chunk_area_search (GMemArea *a,
135 /* here we can't use StaticMutexes, as they depend upon a working
136 * g_malloc, the same holds true for StaticPrivate */
137 static GMutex* mem_chunks_lock = NULL;
138 static GRealMemChunk *mem_chunks = NULL;
140 #ifdef ENABLE_MEM_PROFILE
141 static GMutex* mem_profile_lock;
142 static gulong allocations[MEM_PROFILE_TABLE_SIZE] = { 0 };
143 static gulong allocated_mem = 0;
144 static gulong freed_mem = 0;
145 static GPrivate* allocating_for_mem_chunk = NULL;
146 #define IS_IN_MEM_CHUNK_ROUTINE() \
147 GPOINTER_TO_UINT (g_private_get (allocating_for_mem_chunk))
148 #endif /* ENABLE_MEM_PROFILE */
154 g_malloc (gulong size)
159 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
161 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
168 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
170 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
172 #ifdef ENABLE_MEM_CHECK
174 #endif /* ENABLE_MEM_CHECK */
177 p = (gpointer) malloc (size);
179 g_error ("could not allocate %ld bytes", size);
182 #ifdef ENABLE_MEM_CHECK
186 p = ((guchar*) p + SIZEOF_LONG);
188 #endif /* ENABLE_MEM_CHECK */
190 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
194 p = ((guchar*) p + SIZEOF_LONG);
197 #ifdef ENABLE_MEM_PROFILE
198 g_mutex_lock (mem_profile_lock);
199 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
200 if(!IS_IN_MEM_CHUNK_ROUTINE()) {
202 if (size <= MEM_PROFILE_TABLE_SIZE - 1)
203 allocations[size-1] += 1;
205 allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
206 allocated_mem += size;
207 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
210 g_mutex_unlock (mem_profile_lock);
211 #endif /* ENABLE_MEM_PROFILE */
212 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
219 g_malloc0 (gulong size)
224 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
226 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
233 #if defined (ENABLE_MEM_PROFILE) || defined (ENABLE_MEM_CHECK)
235 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
237 #ifdef ENABLE_MEM_CHECK
239 #endif /* ENABLE_MEM_CHECK */
242 p = (gpointer) calloc (size, 1);
244 g_error ("could not allocate %ld bytes", size);
247 #ifdef ENABLE_MEM_CHECK
251 p = ((guchar*) p + SIZEOF_LONG);
253 #endif /* ENABLE_MEM_CHECK */
255 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
259 p = ((guchar*) p + SIZEOF_LONG);
262 # ifdef ENABLE_MEM_PROFILE
263 g_mutex_lock (mem_profile_lock);
264 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
265 if(!IS_IN_MEM_CHUNK_ROUTINE()) {
267 if (size <= (MEM_PROFILE_TABLE_SIZE - 1))
268 allocations[size-1] += 1;
270 allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
271 allocated_mem += size;
272 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
275 g_mutex_unlock (mem_profile_lock);
276 # endif /* ENABLE_MEM_PROFILE */
277 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
284 g_realloc (gpointer mem,
289 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
291 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
302 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
304 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
306 #ifdef ENABLE_MEM_CHECK
308 #endif /* ENABLE_MEM_CHECK */
313 #ifdef REALLOC_0_WORKS
314 p = (gpointer) realloc (NULL, size);
315 #else /* !REALLOC_0_WORKS */
316 p = (gpointer) malloc (size);
317 #endif /* !REALLOC_0_WORKS */
321 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
322 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
323 #ifdef ENABLE_MEM_PROFILE
324 g_mutex_lock (mem_profile_lock);
326 g_mutex_unlock (mem_profile_lock);
327 #endif /* ENABLE_MEM_PROFILE */
329 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
331 #ifdef ENABLE_MEM_CHECK
332 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
334 g_warning ("trying to realloc freed memory\n");
336 #endif /* ENABLE_MEM_CHECK */
338 p = (gpointer) realloc (mem, size);
342 g_error ("could not reallocate %lu bytes", (gulong) size);
345 #ifdef ENABLE_MEM_CHECK
349 p = ((guchar*) p + SIZEOF_LONG);
351 #endif /* ENABLE_MEM_CHECK */
353 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
357 p = ((guchar*) p + SIZEOF_LONG);
360 #ifdef ENABLE_MEM_PROFILE
361 g_mutex_lock (mem_profile_lock);
362 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
363 if(!IS_IN_MEM_CHUNK_ROUTINE()) {
365 if (size <= (MEM_PROFILE_TABLE_SIZE - 1))
366 allocations[size-1] += 1;
368 allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
369 allocated_mem += size;
370 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
373 g_mutex_unlock (mem_profile_lock);
374 #endif /* ENABLE_MEM_PROFILE */
375 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
382 g_free (gpointer mem)
386 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
389 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
391 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
392 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
394 #ifdef ENABLE_MEM_PROFILE
395 g_mutex_lock (mem_profile_lock);
397 g_mutex_unlock (mem_profile_lock);
398 #endif /* ENABLE_MEM_PROFILE */
400 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
402 #ifdef ENABLE_MEM_CHECK
403 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
405 g_warning ("freeing previously freed memory\n");
409 memset ((guchar*) mem + 8, 0, size);
410 #else /* ENABLE_MEM_CHECK */
412 #endif /* ENABLE_MEM_CHECK */
416 #endif /* ! USE_DMALLOC */
422 #ifdef ENABLE_MEM_PROFILE
424 gulong local_allocations[MEM_PROFILE_TABLE_SIZE];
425 gulong local_allocated_mem;
426 gulong local_freed_mem;
428 g_mutex_lock (mem_profile_lock);
429 for (i = 0; i < MEM_PROFILE_TABLE_SIZE; i++)
430 local_allocations[i] = allocations[i];
431 local_allocated_mem = allocated_mem;
432 local_freed_mem = freed_mem;
433 g_mutex_unlock (mem_profile_lock);
435 for (i = 0; i < (MEM_PROFILE_TABLE_SIZE - 1); i++)
436 if (local_allocations[i] > 0)
437 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
438 "%lu allocations of %d bytes", local_allocations[i], i + 1);
440 if (local_allocations[MEM_PROFILE_TABLE_SIZE - 1] > 0)
441 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
442 "%lu allocations of greater than %d bytes",
443 local_allocations[MEM_PROFILE_TABLE_SIZE - 1], MEM_PROFILE_TABLE_SIZE - 1);
444 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes allocated", local_allocated_mem);
445 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes freed", local_freed_mem);
446 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes in use", local_allocated_mem - local_freed_mem);
447 #endif /* ENABLE_MEM_PROFILE */
451 g_mem_check (gpointer mem)
453 #ifdef ENABLE_MEM_CHECK
456 t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
459 g_warning ("mem: 0x%08x has been freed %lu times\n", (gulong) mem, *t);
460 #endif /* ENABLE_MEM_CHECK */
464 g_mem_chunk_new (gchar *name,
469 GRealMemChunk *mem_chunk;
472 ENTER_MEM_CHUNK_ROUTINE();
474 mem_chunk = g_new (struct _GRealMemChunk, 1);
475 mem_chunk->name = name;
476 mem_chunk->type = type;
477 mem_chunk->num_mem_areas = 0;
478 mem_chunk->num_marked_areas = 0;
479 mem_chunk->mem_area = NULL;
480 mem_chunk->free_mem_area = NULL;
481 mem_chunk->free_atoms = NULL;
482 mem_chunk->mem_tree = NULL;
483 mem_chunk->mem_areas = NULL;
484 mem_chunk->atom_size = atom_size;
486 if (mem_chunk->type == G_ALLOC_AND_FREE)
487 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
489 if (mem_chunk->atom_size % MEM_ALIGN)
490 mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
492 mem_chunk->area_size = area_size;
493 if (mem_chunk->area_size > MAX_MEM_AREA)
494 mem_chunk->area_size = MAX_MEM_AREA;
495 while (mem_chunk->area_size < mem_chunk->atom_size)
496 mem_chunk->area_size *= 2;
498 rarea_size = mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
499 rarea_size = g_mem_chunk_compute_size (rarea_size);
500 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
503 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
504 if (mem_chunk->area_size < mem_chunk->atom_size)
506 mem_chunk->area_size = (mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE) * 2;
507 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
510 if (mem_chunk->area_size % mem_chunk->atom_size)
511 mem_chunk->area_size += mem_chunk->atom_size - (mem_chunk->area_size % mem_chunk->atom_size);
514 g_mutex_lock (mem_chunks_lock);
515 mem_chunk->next = mem_chunks;
516 mem_chunk->prev = NULL;
518 mem_chunks->prev = mem_chunk;
519 mem_chunks = mem_chunk;
520 g_mutex_unlock (mem_chunks_lock);
522 LEAVE_MEM_CHUNK_ROUTINE();
524 return ((GMemChunk*) mem_chunk);
528 g_mem_chunk_destroy (GMemChunk *mem_chunk)
530 GRealMemChunk *rmem_chunk;
534 g_return_if_fail (mem_chunk != NULL);
536 ENTER_MEM_CHUNK_ROUTINE();
538 rmem_chunk = (GRealMemChunk*) mem_chunk;
540 mem_areas = rmem_chunk->mem_areas;
543 temp_area = mem_areas;
544 mem_areas = mem_areas->next;
548 if (rmem_chunk->next)
549 rmem_chunk->next->prev = rmem_chunk->prev;
550 if (rmem_chunk->prev)
551 rmem_chunk->prev->next = rmem_chunk->next;
553 g_mutex_lock (mem_chunks_lock);
554 if (rmem_chunk == mem_chunks)
555 mem_chunks = mem_chunks->next;
556 g_mutex_unlock (mem_chunks_lock);
558 if (rmem_chunk->type == G_ALLOC_AND_FREE)
559 g_tree_destroy (rmem_chunk->mem_tree);
563 LEAVE_MEM_CHUNK_ROUTINE();
567 g_mem_chunk_alloc (GMemChunk *mem_chunk)
569 GRealMemChunk *rmem_chunk;
573 ENTER_MEM_CHUNK_ROUTINE();
575 g_return_val_if_fail (mem_chunk != NULL, NULL);
577 rmem_chunk = (GRealMemChunk*) mem_chunk;
579 while (rmem_chunk->free_atoms)
581 /* Get the first piece of memory on the "free_atoms" list.
582 * We can go ahead and destroy the list node we used to keep
583 * track of it with and to update the "free_atoms" list to
584 * point to its next element.
586 mem = rmem_chunk->free_atoms;
587 rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
589 /* Determine which area this piece of memory is allocated from */
590 temp_area = g_tree_search (rmem_chunk->mem_tree,
591 (GSearchFunc) g_mem_chunk_area_search,
594 /* If the area has been marked, then it is being destroyed.
595 * (ie marked to be destroyed).
596 * We check to see if all of the segments on the free list that
597 * reference this area have been removed. This occurs when
598 * the ammount of free memory is less than the allocatable size.
599 * If the chunk should be freed, then we place it in the "free_mem_area".
600 * This is so we make sure not to free the mem area here and then
601 * allocate it again a few lines down.
602 * If we don't allocate a chunk a few lines down then the "free_mem_area"
604 * If there is already a "free_mem_area" then we'll just free this mem area.
608 /* Update the "free" memory available in that area */
609 temp_area->free += rmem_chunk->atom_size;
611 if (temp_area->free == rmem_chunk->area_size)
613 if (temp_area == rmem_chunk->mem_area)
614 rmem_chunk->mem_area = NULL;
616 if (rmem_chunk->free_mem_area)
618 rmem_chunk->num_mem_areas -= 1;
621 temp_area->next->prev = temp_area->prev;
623 temp_area->prev->next = temp_area->next;
624 if (temp_area == rmem_chunk->mem_areas)
625 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
627 if (rmem_chunk->type == G_ALLOC_AND_FREE)
628 g_tree_remove (rmem_chunk->mem_tree, temp_area);
632 rmem_chunk->free_mem_area = temp_area;
634 rmem_chunk->num_marked_areas -= 1;
639 /* Update the number of allocated atoms count.
641 temp_area->allocated += 1;
643 /* The area wasn't marked...return the memory
649 /* If there isn't a current mem area or the current mem area is out of space
650 * then allocate a new mem area. We'll first check and see if we can use
651 * the "free_mem_area". Otherwise we'll just malloc the mem area.
653 if ((!rmem_chunk->mem_area) ||
654 ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
656 if (rmem_chunk->free_mem_area)
658 rmem_chunk->mem_area = rmem_chunk->free_mem_area;
659 rmem_chunk->free_mem_area = NULL;
663 rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
665 rmem_chunk->area_size);
667 rmem_chunk->num_mem_areas += 1;
668 rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
669 rmem_chunk->mem_area->prev = NULL;
671 if (rmem_chunk->mem_areas)
672 rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
673 rmem_chunk->mem_areas = rmem_chunk->mem_area;
675 if (rmem_chunk->type == G_ALLOC_AND_FREE)
676 g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
679 rmem_chunk->mem_area->index = 0;
680 rmem_chunk->mem_area->free = rmem_chunk->area_size;
681 rmem_chunk->mem_area->allocated = 0;
682 rmem_chunk->mem_area->mark = 0;
685 /* Get the memory and modify the state variables appropriately.
687 mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
688 rmem_chunk->mem_area->index += rmem_chunk->atom_size;
689 rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
690 rmem_chunk->mem_area->allocated += 1;
694 LEAVE_MEM_CHUNK_ROUTINE();
700 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
704 mem = g_mem_chunk_alloc (mem_chunk);
707 GRealMemChunk *rmem_chunk = (GRealMemChunk*) mem_chunk;
709 memset (mem, 0, rmem_chunk->atom_size);
716 g_mem_chunk_free (GMemChunk *mem_chunk,
719 GRealMemChunk *rmem_chunk;
721 GFreeAtom *free_atom;
723 g_return_if_fail (mem_chunk != NULL);
724 g_return_if_fail (mem != NULL);
726 ENTER_MEM_CHUNK_ROUTINE();
728 rmem_chunk = (GRealMemChunk*) mem_chunk;
730 /* Don't do anything if this is an ALLOC_ONLY chunk
732 if (rmem_chunk->type == G_ALLOC_AND_FREE)
734 /* Place the memory on the "free_atoms" list
736 free_atom = (GFreeAtom*) mem;
737 free_atom->next = rmem_chunk->free_atoms;
738 rmem_chunk->free_atoms = free_atom;
740 temp_area = g_tree_search (rmem_chunk->mem_tree,
741 (GSearchFunc) g_mem_chunk_area_search,
744 temp_area->allocated -= 1;
746 if (temp_area->allocated == 0)
749 rmem_chunk->num_marked_areas += 1;
753 LEAVE_MEM_CHUNK_ROUTINE();
756 /* This doesn't free the free_area if there is one */
758 g_mem_chunk_clean (GMemChunk *mem_chunk)
760 GRealMemChunk *rmem_chunk;
762 GFreeAtom *prev_free_atom;
763 GFreeAtom *temp_free_atom;
766 g_return_if_fail (mem_chunk != NULL);
768 rmem_chunk = (GRealMemChunk*) mem_chunk;
770 if (rmem_chunk->type == G_ALLOC_AND_FREE)
772 prev_free_atom = NULL;
773 temp_free_atom = rmem_chunk->free_atoms;
775 while (temp_free_atom)
777 mem = (gpointer) temp_free_atom;
779 mem_area = g_tree_search (rmem_chunk->mem_tree,
780 (GSearchFunc) g_mem_chunk_area_search,
783 /* If this mem area is marked for destruction then delete the
784 * area and list node and decrement the free mem.
789 prev_free_atom->next = temp_free_atom->next;
791 rmem_chunk->free_atoms = temp_free_atom->next;
792 temp_free_atom = temp_free_atom->next;
794 mem_area->free += rmem_chunk->atom_size;
795 if (mem_area->free == rmem_chunk->area_size)
797 rmem_chunk->num_mem_areas -= 1;
798 rmem_chunk->num_marked_areas -= 1;
801 mem_area->next->prev = mem_area->prev;
803 mem_area->prev->next = mem_area->next;
804 if (mem_area == rmem_chunk->mem_areas)
805 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
806 if (mem_area == rmem_chunk->mem_area)
807 rmem_chunk->mem_area = NULL;
809 if (rmem_chunk->type == G_ALLOC_AND_FREE)
810 g_tree_remove (rmem_chunk->mem_tree, mem_area);
816 prev_free_atom = temp_free_atom;
817 temp_free_atom = temp_free_atom->next;
824 g_mem_chunk_reset (GMemChunk *mem_chunk)
826 GRealMemChunk *rmem_chunk;
830 g_return_if_fail (mem_chunk != NULL);
832 rmem_chunk = (GRealMemChunk*) mem_chunk;
834 mem_areas = rmem_chunk->mem_areas;
835 rmem_chunk->num_mem_areas = 0;
836 rmem_chunk->mem_areas = NULL;
837 rmem_chunk->mem_area = NULL;
841 temp_area = mem_areas;
842 mem_areas = mem_areas->next;
846 rmem_chunk->free_atoms = NULL;
848 if (rmem_chunk->mem_tree)
849 g_tree_destroy (rmem_chunk->mem_tree);
850 rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
854 g_mem_chunk_print (GMemChunk *mem_chunk)
856 GRealMemChunk *rmem_chunk;
860 g_return_if_fail (mem_chunk != NULL);
862 rmem_chunk = (GRealMemChunk*) mem_chunk;
863 mem_areas = rmem_chunk->mem_areas;
868 mem += rmem_chunk->area_size - mem_areas->free;
869 mem_areas = mem_areas->next;
872 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
873 "%s: %ld bytes using %d mem areas",
874 rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
878 g_mem_chunk_info (void)
880 GRealMemChunk *mem_chunk;
884 g_mutex_lock (mem_chunks_lock);
885 mem_chunk = mem_chunks;
889 mem_chunk = mem_chunk->next;
891 g_mutex_unlock (mem_chunks_lock);
893 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks", count);
895 g_mutex_lock (mem_chunks_lock);
896 mem_chunk = mem_chunks;
897 g_mutex_unlock (mem_chunks_lock);
901 g_mem_chunk_print ((GMemChunk*) mem_chunk);
902 mem_chunk = mem_chunk->next;
909 GRealMemChunk *mem_chunk;
911 g_mutex_lock (mem_chunks_lock);
912 mem_chunk = mem_chunks;
913 g_mutex_unlock (mem_chunks_lock);
916 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
917 mem_chunk = mem_chunk->next;
923 g_mem_chunk_compute_size (gulong size)
929 while (power_of_2 < size)
932 lower = power_of_2 >> 1;
935 if ((size - lower) < (upper - size))
941 g_mem_chunk_area_compare (GMemArea *a,
944 return (a->mem - b->mem);
948 g_mem_chunk_area_search (GMemArea *a,
953 if (addr < &a->mem[a->index])
960 /* generic allocators
962 struct _GAllocator /* from gmem.c */
969 GMemChunk *mem_chunk;
970 gpointer dummy; /* implementation specific */
974 g_allocator_new (const gchar *name,
977 GAllocator *allocator;
979 g_return_val_if_fail (name != NULL, NULL);
981 allocator = g_new0 (GAllocator, 1);
982 allocator->name = g_strdup (name);
983 allocator->n_preallocs = CLAMP (n_preallocs, 1, 65535);
984 allocator->is_unused = TRUE;
986 allocator->last = NULL;
987 allocator->mem_chunk = NULL;
988 allocator->dummy = NULL;
994 g_allocator_free (GAllocator *allocator)
996 g_return_if_fail (allocator != NULL);
997 g_return_if_fail (allocator->is_unused == TRUE);
999 g_free (allocator->name);
1000 if (allocator->mem_chunk)
1001 g_mem_chunk_destroy (allocator->mem_chunk);
1009 mem_chunks_lock = g_mutex_new();
1010 #ifdef ENABLE_MEM_PROFILE
1011 mem_profile_lock = g_mutex_new();
1012 allocating_for_mem_chunk = g_private_new(NULL);