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.
32 /* #define ENABLE_MEM_PROFILE */
33 /* #define ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS */
34 /* #define ENABLE_MEM_CHECK */
35 #define MEM_PROFILE_TABLE_SIZE 8192
38 * This library can check for some attempts to do illegal things to
39 * memory (ENABLE_MEM_CHECK), and can do profiling
40 * (ENABLE_MEM_PROFILE). Both features are implemented by storing
41 * words before the start of the memory chunk.
43 * The first, at offset -2*SIZEOF_LONG, is used only if
44 * ENABLE_MEM_CHECK is set, and stores 0 after the memory has been
45 * allocated and 1 when it has been freed. The second, at offset
46 * -SIZEOF_LONG, is used if either flag is set and stores the size of
49 * The MEM_CHECK flag is checked when memory is realloc'd and free'd,
50 * and it can be explicitly checked before using a block by calling
54 #if defined(ENABLE_MEM_PROFILE) && defined(ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS)
55 #define ENTER_MEM_CHUNK_ROUTINE() \
56 g_static_set (allocating_for_mem_chunk, \
57 g_static_get (allocating_for_mem_chunk) + 1)
58 #define ENTER_MEM_CHUNK_ROUTINE() \
59 g_static_set (allocating_for_mem_chunk, \
60 g_static_get (allocating_for_mem_chunk) - 1)
62 #define ENTER_MEM_CHUNK_ROUTINE()
63 #define LEAVE_MEM_CHUNK_ROUTINE()
67 #define MAX_MEM_AREA 65536L
68 #define MEM_AREA_SIZE 4L
70 #if SIZEOF_VOID_P > SIZEOF_LONG
71 #define MEM_ALIGN SIZEOF_VOID_P
73 #define MEM_ALIGN SIZEOF_LONG
77 typedef struct _GFreeAtom GFreeAtom;
78 typedef struct _GMemArea GMemArea;
79 typedef struct _GRealMemChunk GRealMemChunk;
88 GMemArea *next; /* the next mem area */
89 GMemArea *prev; /* the previous mem area */
90 gulong index; /* the current index into the "mem" array */
91 gulong free; /* the number of free bytes in this mem area */
92 gulong allocated; /* the number of atoms allocated from this area */
93 gulong mark; /* is this mem area marked for deletion */
94 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
95 * the actual size of this array is determined by
96 * the mem chunk "area_size". ANSI says that it
97 * must be declared to be the maximum size it
98 * can possibly be (even though the actual size
103 struct _GRealMemChunk
105 gchar *name; /* name of this MemChunk...used for debugging output */
106 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
107 gint num_mem_areas; /* the number of memory areas */
108 gint num_marked_areas; /* the number of areas marked for deletion */
109 guint atom_size; /* the size of an atom */
110 gulong area_size; /* the size of a memory area */
111 GMemArea *mem_area; /* the current memory area */
112 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
113 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
114 GFreeAtom *free_atoms; /* the free atoms list */
115 GTree *mem_tree; /* tree of mem areas sorted by memory address */
116 GRealMemChunk *next; /* pointer to the next chunk */
117 GRealMemChunk *prev; /* pointer to the previous chunk */
121 static gulong g_mem_chunk_compute_size (gulong size);
122 static gint g_mem_chunk_area_compare (GMemArea *a,
124 static gint g_mem_chunk_area_search (GMemArea *a,
128 /* here we can't use StaticMutexes, as they depend upon a working
129 * g_malloc, the same holds true for StaticPrivate */
130 static GMutex* mem_chunks_lock = NULL;
131 static GRealMemChunk *mem_chunks = NULL;
133 #ifdef ENABLE_MEM_PROFILE
134 static GMutex* mem_profile_lock;
135 static gulong allocations[MEM_PROFILE_TABLE_SIZE] = { 0 };
136 static gulong allocated_mem = 0;
137 static gulong freed_mem = 0;
138 static GPrivate* allocating_for_mem_chunk = NULL;
139 #define IS_IN_MEM_CHUNK_ROUTINE() \
140 GPOINTER_TO_UINT (g_static_get (allocating_for_mem_chunk))
141 #endif /* ENABLE_MEM_PROFILE */
147 g_malloc (gulong size)
152 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
154 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
161 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
163 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
165 #ifdef ENABLE_MEM_CHECK
167 #endif /* ENABLE_MEM_CHECK */
170 p = (gpointer) malloc (size);
172 g_error ("could not allocate %ld bytes", size);
175 #ifdef ENABLE_MEM_CHECK
179 p = ((guchar*) p + SIZEOF_LONG);
181 #endif /* ENABLE_MEM_CHECK */
183 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
187 p = ((guchar*) p + SIZEOF_LONG);
190 #ifdef ENABLE_MEM_PROFILE
191 g_mutex_lock (mem_profile_lock);
192 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
193 if(!IS_IN_MEM_CHUNK_ROUTINE()) {
195 if (size <= MEM_PROFILE_TABLE_SIZE - 1)
196 allocations[size-1] += 1;
198 allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
199 allocated_mem += size;
200 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
203 g_mutex_unlock (mem_profile_lock);
204 #endif /* ENABLE_MEM_PROFILE */
205 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
212 g_malloc0 (gulong size)
217 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
219 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
226 #if defined (ENABLE_MEM_PROFILE) || defined (ENABLE_MEM_CHECK)
228 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
230 #ifdef ENABLE_MEM_CHECK
232 #endif /* ENABLE_MEM_CHECK */
235 p = (gpointer) calloc (size, 1);
237 g_error ("could not allocate %ld bytes", size);
240 #ifdef ENABLE_MEM_CHECK
244 p = ((guchar*) p + SIZEOF_LONG);
246 #endif /* ENABLE_MEM_CHECK */
248 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
252 p = ((guchar*) p + SIZEOF_LONG);
255 # ifdef ENABLE_MEM_PROFILE
256 g_mutex_lock (mem_profile_lock);
257 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
258 if(!IS_IN_MEM_CHUNK_ROUTINE()) {
260 if (size <= (MEM_PROFILE_TABLE_SIZE - 1))
261 allocations[size-1] += 1;
263 allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
264 allocated_mem += size;
265 # ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
268 g_mutex_unlock (mem_profile_lock);
269 # endif /* ENABLE_MEM_PROFILE */
270 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
277 g_realloc (gpointer mem,
282 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
284 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
291 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
293 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
295 #ifdef ENABLE_MEM_CHECK
297 #endif /* ENABLE_MEM_CHECK */
301 p = (gpointer) malloc (size);
304 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
305 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
306 #ifdef ENABLE_MEM_PROFILE
307 g_mutex_lock (mem_profile);
309 g_mutex_unlock (mem_profile);
310 #endif /* ENABLE_MEM_PROFILE */
312 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
314 #ifdef ENABLE_MEM_CHECK
315 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
317 g_warning ("trying to realloc freed memory\n");
319 #endif /* ENABLE_MEM_CHECK */
321 p = (gpointer) realloc (mem, size);
325 g_error ("could not reallocate %lu bytes", (gulong) size);
328 #ifdef ENABLE_MEM_CHECK
332 p = ((guchar*) p + SIZEOF_LONG);
334 #endif /* ENABLE_MEM_CHECK */
336 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
340 p = ((guchar*) p + SIZEOF_LONG);
343 #ifdef ENABLE_MEM_PROFILE
344 g_mutex_lock (mem_profile_lock);
345 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
346 if(!IS_IN_MEM_CHUNK_ROUTINE()) {
348 if (size <= (MEM_PROFILE_TABLE_SIZE - 1))
349 allocations[size-1] += 1;
351 allocations[MEM_PROFILE_TABLE_SIZE - 1] += 1;
352 allocated_mem += size;
353 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
356 g_mutex_unlock (mem_profile_lock);
357 #endif /* ENABLE_MEM_PROFILE */
358 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
365 g_free (gpointer mem)
369 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
372 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
374 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
375 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
377 #ifdef ENABLE_MEM_PROFILE
378 g_mutex_lock (mem_profile);
380 g_mutex_unlock (mem_profile);
381 #endif /* ENABLE_MEM_PROFILE */
383 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
385 #ifdef ENABLE_MEM_CHECK
386 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
388 g_warning ("freeing previously freed memory\n");
392 memset ((guchar*) mem + 8, 0, size);
393 #else /* ENABLE_MEM_CHECK */
395 #endif /* ENABLE_MEM_CHECK */
399 #endif /* ! USE_DMALLOC */
405 #ifdef ENABLE_MEM_PROFILE
407 gulong local_allocations[MEM_PROFILE_TABLE_SIZE];
408 gulong local_allocated_mem;
409 gulong local_freed_mem;
411 g_mutex_lock (mem_profile);
412 for (i = 0; i < (MEM_PROFILE_TABLE_SIZE - 1); i++)
413 local_allocations[i] = allocations[i];
414 local_allocated_mem = allocated_mem;
415 local_freed_mem = freed_mem;
416 g_mutex_unlock (mem_profile);
418 for (i = 0; i < (MEM_PROFILE_TABLE_SIZE - 1); i++)
419 if (local_allocations[i] > 0)
420 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
421 "%lu allocations of %d bytes\n", local_allocations[i], i + 1);
423 if (local_allocations[MEM_PROFILE_TABLE_SIZE - 1] > 0)
424 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
425 "%lu allocations of greater than %d bytes\n",
426 local_allocations[MEM_PROFILE_TABLE_SIZE - 1], MEM_PROFILE_TABLE_SIZE - 1);
427 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes allocated\n", local_allocated_mem);
428 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes freed\n", local_freed_mem);
429 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes in use\n", local_allocated_mem - local_freed_mem);
430 #endif /* ENABLE_MEM_PROFILE */
434 g_mem_check (gpointer mem)
436 #ifdef ENABLE_MEM_CHECK
439 t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
442 g_warning ("mem: 0x%08x has been freed %lu times\n", (gulong) mem, *t);
443 #endif /* ENABLE_MEM_CHECK */
447 g_mem_chunk_new (gchar *name,
452 GRealMemChunk *mem_chunk;
455 ENTER_MEM_CHUNK_ROUTINE();
457 mem_chunk = g_new (struct _GRealMemChunk, 1);
458 mem_chunk->name = name;
459 mem_chunk->type = type;
460 mem_chunk->num_mem_areas = 0;
461 mem_chunk->num_marked_areas = 0;
462 mem_chunk->mem_area = NULL;
463 mem_chunk->free_mem_area = NULL;
464 mem_chunk->free_atoms = NULL;
465 mem_chunk->mem_tree = NULL;
466 mem_chunk->mem_areas = NULL;
467 mem_chunk->atom_size = atom_size;
469 if (mem_chunk->type == G_ALLOC_AND_FREE)
470 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
472 if (mem_chunk->atom_size % MEM_ALIGN)
473 mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
475 mem_chunk->area_size = area_size;
476 if (mem_chunk->area_size > MAX_MEM_AREA)
477 mem_chunk->area_size = MAX_MEM_AREA;
478 while (mem_chunk->area_size < mem_chunk->atom_size)
479 mem_chunk->area_size *= 2;
481 rarea_size = mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
482 rarea_size = g_mem_chunk_compute_size (rarea_size);
483 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
486 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
487 if (mem_chunk->area_size < mem_chunk->atom_size)
489 mem_chunk->area_size = (mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE) * 2;
490 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
493 if (mem_chunk->area_size % mem_chunk->atom_size)
494 mem_chunk->area_size += mem_chunk->atom_size - (mem_chunk->area_size % mem_chunk->atom_size);
497 g_mutex_lock (mem_chunks_lock);
498 mem_chunk->next = mem_chunks;
499 mem_chunk->prev = NULL;
501 mem_chunks->prev = mem_chunk;
502 mem_chunks = mem_chunk;
503 g_mutex_unlock (mem_chunks_lock);
505 LEAVE_MEM_CHUNK_ROUTINE();
507 return ((GMemChunk*) mem_chunk);
511 g_mem_chunk_destroy (GMemChunk *mem_chunk)
513 GRealMemChunk *rmem_chunk;
517 g_assert (mem_chunk != NULL);
519 ENTER_MEM_CHUNK_ROUTINE();
521 rmem_chunk = (GRealMemChunk*) mem_chunk;
523 mem_areas = rmem_chunk->mem_areas;
526 temp_area = mem_areas;
527 mem_areas = mem_areas->next;
531 if (rmem_chunk->next)
532 rmem_chunk->next->prev = rmem_chunk->prev;
533 if (rmem_chunk->prev)
534 rmem_chunk->prev->next = rmem_chunk->next;
536 g_mutex_lock (mem_chunks_lock);
537 if (rmem_chunk == mem_chunks)
538 mem_chunks = mem_chunks->next;
539 g_mutex_unlock (mem_chunks_lock);
541 if (rmem_chunk->type == G_ALLOC_AND_FREE)
542 g_tree_destroy (rmem_chunk->mem_tree);
546 LEAVE_MEM_CHUNK_ROUTINE();
550 g_mem_chunk_alloc (GMemChunk *mem_chunk)
552 GRealMemChunk *rmem_chunk;
556 ENTER_MEM_CHUNK_ROUTINE();
558 g_assert (mem_chunk != NULL);
560 rmem_chunk = (GRealMemChunk*) mem_chunk;
562 while (rmem_chunk->free_atoms)
564 /* Get the first piece of memory on the "free_atoms" list.
565 * We can go ahead and destroy the list node we used to keep
566 * track of it with and to update the "free_atoms" list to
567 * point to its next element.
569 mem = rmem_chunk->free_atoms;
570 rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
572 /* Determine which area this piece of memory is allocated from */
573 temp_area = g_tree_search (rmem_chunk->mem_tree,
574 (GSearchFunc) g_mem_chunk_area_search,
577 /* If the area has been marked, then it is being destroyed.
578 * (ie marked to be destroyed).
579 * We check to see if all of the segments on the free list that
580 * reference this area have been removed. This occurs when
581 * the ammount of free memory is less than the allocatable size.
582 * If the chunk should be freed, then we place it in the "free_mem_area".
583 * This is so we make sure not to free the mem area here and then
584 * allocate it again a few lines down.
585 * If we don't allocate a chunk a few lines down then the "free_mem_area"
587 * If there is already a "free_mem_area" then we'll just free this mem area.
591 /* Update the "free" memory available in that area */
592 temp_area->free += rmem_chunk->atom_size;
594 if (temp_area->free == rmem_chunk->area_size)
596 if (temp_area == rmem_chunk->mem_area)
597 rmem_chunk->mem_area = NULL;
599 if (rmem_chunk->free_mem_area)
601 rmem_chunk->num_mem_areas -= 1;
604 temp_area->next->prev = temp_area->prev;
606 temp_area->prev->next = temp_area->next;
607 if (temp_area == rmem_chunk->mem_areas)
608 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
610 if (rmem_chunk->type == G_ALLOC_AND_FREE)
611 g_tree_remove (rmem_chunk->mem_tree, temp_area);
615 rmem_chunk->free_mem_area = temp_area;
617 rmem_chunk->num_marked_areas -= 1;
622 /* Update the number of allocated atoms count.
624 temp_area->allocated += 1;
626 /* The area wasn't marked...return the memory
632 /* If there isn't a current mem area or the current mem area is out of space
633 * then allocate a new mem area. We'll first check and see if we can use
634 * the "free_mem_area". Otherwise we'll just malloc the mem area.
636 if ((!rmem_chunk->mem_area) ||
637 ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
639 if (rmem_chunk->free_mem_area)
641 rmem_chunk->mem_area = rmem_chunk->free_mem_area;
642 rmem_chunk->free_mem_area = NULL;
646 rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
648 rmem_chunk->area_size);
650 rmem_chunk->num_mem_areas += 1;
651 rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
652 rmem_chunk->mem_area->prev = NULL;
654 if (rmem_chunk->mem_areas)
655 rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
656 rmem_chunk->mem_areas = rmem_chunk->mem_area;
658 if (rmem_chunk->type == G_ALLOC_AND_FREE)
659 g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
662 rmem_chunk->mem_area->index = 0;
663 rmem_chunk->mem_area->free = rmem_chunk->area_size;
664 rmem_chunk->mem_area->allocated = 0;
665 rmem_chunk->mem_area->mark = 0;
668 /* Get the memory and modify the state variables appropriately.
670 mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
671 rmem_chunk->mem_area->index += rmem_chunk->atom_size;
672 rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
673 rmem_chunk->mem_area->allocated += 1;
677 LEAVE_MEM_CHUNK_ROUTINE();
683 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
687 mem = g_mem_chunk_alloc (mem_chunk);
690 GRealMemChunk *rmem_chunk = (GRealMemChunk*) mem_chunk;
692 memset (mem, 0, rmem_chunk->atom_size);
699 g_mem_chunk_free (GMemChunk *mem_chunk,
702 GRealMemChunk *rmem_chunk;
704 GFreeAtom *free_atom;
706 g_assert (mem_chunk != NULL);
707 g_assert (mem != NULL);
709 ENTER_MEM_CHUNK_ROUTINE();
711 rmem_chunk = (GRealMemChunk*) mem_chunk;
713 /* Don't do anything if this is an ALLOC_ONLY chunk
715 if (rmem_chunk->type == G_ALLOC_AND_FREE)
717 /* Place the memory on the "free_atoms" list
719 free_atom = (GFreeAtom*) mem;
720 free_atom->next = rmem_chunk->free_atoms;
721 rmem_chunk->free_atoms = free_atom;
723 temp_area = g_tree_search (rmem_chunk->mem_tree,
724 (GSearchFunc) g_mem_chunk_area_search,
727 temp_area->allocated -= 1;
729 if (temp_area->allocated == 0)
732 rmem_chunk->num_marked_areas += 1;
736 LEAVE_MEM_CHUNK_ROUTINE();
739 /* This doesn't free the free_area if there is one */
741 g_mem_chunk_clean (GMemChunk *mem_chunk)
743 GRealMemChunk *rmem_chunk;
745 GFreeAtom *prev_free_atom;
746 GFreeAtom *temp_free_atom;
749 g_assert (mem_chunk != NULL);
751 rmem_chunk = (GRealMemChunk*) mem_chunk;
753 if (rmem_chunk->type == G_ALLOC_AND_FREE)
755 prev_free_atom = NULL;
756 temp_free_atom = rmem_chunk->free_atoms;
758 while (temp_free_atom)
760 mem = (gpointer) temp_free_atom;
762 mem_area = g_tree_search (rmem_chunk->mem_tree,
763 (GSearchFunc) g_mem_chunk_area_search,
766 /* If this mem area is marked for destruction then delete the
767 * area and list node and decrement the free mem.
772 prev_free_atom->next = temp_free_atom->next;
774 rmem_chunk->free_atoms = temp_free_atom->next;
775 temp_free_atom = temp_free_atom->next;
777 mem_area->free += rmem_chunk->atom_size;
778 if (mem_area->free == rmem_chunk->area_size)
780 rmem_chunk->num_mem_areas -= 1;
781 rmem_chunk->num_marked_areas -= 1;
784 mem_area->next->prev = mem_area->prev;
786 mem_area->prev->next = mem_area->next;
787 if (mem_area == rmem_chunk->mem_areas)
788 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
789 if (mem_area == rmem_chunk->mem_area)
790 rmem_chunk->mem_area = NULL;
792 if (rmem_chunk->type == G_ALLOC_AND_FREE)
793 g_tree_remove (rmem_chunk->mem_tree, mem_area);
799 prev_free_atom = temp_free_atom;
800 temp_free_atom = temp_free_atom->next;
807 g_mem_chunk_reset (GMemChunk *mem_chunk)
809 GRealMemChunk *rmem_chunk;
813 g_assert (mem_chunk != NULL);
815 rmem_chunk = (GRealMemChunk*) mem_chunk;
817 mem_areas = rmem_chunk->mem_areas;
818 rmem_chunk->num_mem_areas = 0;
819 rmem_chunk->mem_areas = NULL;
820 rmem_chunk->mem_area = NULL;
824 temp_area = mem_areas;
825 mem_areas = mem_areas->next;
829 rmem_chunk->free_atoms = NULL;
831 if (rmem_chunk->mem_tree)
832 g_tree_destroy (rmem_chunk->mem_tree);
833 rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
837 g_mem_chunk_print (GMemChunk *mem_chunk)
839 GRealMemChunk *rmem_chunk;
843 g_assert (mem_chunk != NULL);
845 rmem_chunk = (GRealMemChunk*) mem_chunk;
846 mem_areas = rmem_chunk->mem_areas;
851 mem += rmem_chunk->area_size - mem_areas->free;
852 mem_areas = mem_areas->next;
855 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
856 "%s: %ld bytes using %d mem areas\n",
857 rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
861 g_mem_chunk_info (void)
863 GRealMemChunk *mem_chunk;
867 g_mutex_lock (mem_chunks_lock);
868 mem_chunk = mem_chunks;
872 mem_chunk = mem_chunk->next;
874 g_mutex_unlock (mem_chunks_lock);
876 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks\n", count);
878 g_mutex_lock (mem_chunks_lock);
879 mem_chunk = mem_chunks;
880 g_mutex_unlock (mem_chunks_lock);
884 g_mem_chunk_print ((GMemChunk*) mem_chunk);
885 mem_chunk = mem_chunk->next;
892 GRealMemChunk *mem_chunk;
894 g_mutex_lock (mem_chunks_lock);
895 mem_chunk = mem_chunks;
896 g_mutex_unlock (mem_chunks_lock);
899 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
900 mem_chunk = mem_chunk->next;
906 g_mem_chunk_compute_size (gulong size)
912 while (power_of_2 < size)
915 lower = power_of_2 >> 1;
918 if ((size - lower) < (upper - size))
924 g_mem_chunk_area_compare (GMemArea *a,
927 return (a->mem - b->mem);
931 g_mem_chunk_area_search (GMemArea *a,
936 if (addr < &a->mem[a->index])
943 /* generic allocators
945 struct _GAllocator /* from gmem.c */
952 GMemChunk *mem_chunk;
953 gpointer dummy; /* implementation specific */
957 g_allocator_new (const gchar *name,
960 GAllocator *allocator;
962 g_return_val_if_fail (name != NULL, NULL);
964 allocator = g_new0 (GAllocator, 1);
965 allocator->name = g_strdup (name);
966 allocator->n_preallocs = CLAMP (n_preallocs, 1, 65535);
967 allocator->is_unused = TRUE;
969 allocator->last = NULL;
970 allocator->mem_chunk = NULL;
971 allocator->dummy = NULL;
977 g_allocator_free (GAllocator *allocator)
979 g_return_if_fail (allocator != NULL);
980 g_return_if_fail (allocator->is_unused == TRUE);
982 g_free (allocator->name);
983 if (allocator->mem_chunk)
984 g_mem_chunk_destroy (allocator->mem_chunk);
992 mem_chunks_lock = g_mutex_new();
993 #ifdef ENABLE_MEM_PROFILE
994 mem_profile_lock = g_mutex_new();
995 allocating_for_mem_chunk = g_private_new(NULL);