new function g_log_set_always_fatal() to set an additional fatal_mask for
[platform/upstream/glib.git] / gmem.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19 #include <stdlib.h>
20 #include <string.h>
21 #include "glib.h"
22
23 /* #define ENABLE_MEM_PROFILE */
24 /* #define ENABLE_MEM_CHECK */
25
26
27 #define MAX_MEM_AREA  65536L
28 #define MEM_AREA_SIZE 4L
29
30 #if SIZEOF_VOID_P > SIZEOF_LONG
31 #define MEM_ALIGN     SIZEOF_VOID_P
32 #else
33 #define MEM_ALIGN     SIZEOF_LONG
34 #endif
35
36
37 typedef struct _GFreeAtom      GFreeAtom;
38 typedef struct _GMemArea       GMemArea;
39 typedef struct _GRealMemChunk  GRealMemChunk;
40
41 struct _GFreeAtom
42 {
43   GFreeAtom *next;
44 };
45
46 struct _GMemArea
47 {
48   GMemArea *next;            /* the next mem area */
49   GMemArea *prev;            /* the previous mem area */
50   gulong index;              /* the current index into the "mem" array */
51   gulong free;               /* the number of free bytes in this mem area */
52   gulong allocated;          /* the number of atoms allocated from this area */
53   gulong mark;               /* is this mem area marked for deletion */
54   gchar mem[MEM_AREA_SIZE];  /* the mem array from which atoms get allocated
55                               * the actual size of this array is determined by
56                               *  the mem chunk "area_size". ANSI says that it
57                               *  must be declared to be the maximum size it
58                               *  can possibly be (even though the actual size
59                               *  may be less).
60                               */
61 };
62
63 struct _GRealMemChunk
64 {
65   gchar *name;               /* name of this MemChunk...used for debugging output */
66   gint type;                 /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
67   gint num_mem_areas;        /* the number of memory areas */
68   gint num_marked_areas;     /* the number of areas marked for deletion */
69   guint atom_size;           /* the size of an atom */
70   gulong area_size;          /* the size of a memory area */
71   GMemArea *mem_area;        /* the current memory area */
72   GMemArea *mem_areas;       /* a list of all the mem areas owned by this chunk */
73   GMemArea *free_mem_area;   /* the free area...which is about to be destroyed */
74   GFreeAtom *free_atoms;     /* the free atoms list */
75   GTree *mem_tree;           /* tree of mem areas sorted by memory address */
76   GRealMemChunk *next;       /* pointer to the next chunk */
77   GRealMemChunk *prev;       /* pointer to the previous chunk */
78 };
79
80
81 static gulong g_mem_chunk_compute_size (gulong    size);
82 static gint   g_mem_chunk_area_compare (GMemArea *a,
83                                         GMemArea *b);
84 static gint   g_mem_chunk_area_search  (GMemArea *a,
85                                         gchar    *addr);
86
87
88 static GRealMemChunk *mem_chunks = NULL;
89
90 #ifdef ENABLE_MEM_PROFILE
91 static gulong allocations[4096] = { 0 };
92 static gulong allocated_mem = 0;
93 static gulong freed_mem = 0;
94 #endif /* ENABLE_MEM_PROFILE */
95
96
97 #ifndef USE_DMALLOC
98
99 gpointer
100 g_malloc (gulong size)
101 {
102   gpointer p;
103   
104   
105 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
106   gulong *t;
107 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
108   
109   
110   if (size == 0)
111     return NULL;
112   
113   
114 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
115   size += SIZEOF_LONG;
116 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
117   
118 #ifdef ENABLE_MEM_CHECK
119   size += SIZEOF_LONG;
120 #endif /* ENABLE_MEM_CHECK */
121   
122   
123   p = (gpointer) malloc (size);
124   if (!p)
125     g_error ("could not allocate %ld bytes", size);
126   
127   
128 #ifdef ENABLE_MEM_CHECK
129   size -= SIZEOF_LONG;
130   
131   t = p;
132   p = ((guchar*) p + SIZEOF_LONG);
133   *t = 0;
134 #endif /* ENABLE_MEM_CHECK */
135   
136 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
137   size -= SIZEOF_LONG;
138   
139   t = p;
140   p = ((guchar*) p + SIZEOF_LONG);
141   *t = size;
142   
143 #ifdef ENABLE_MEM_PROFILE
144   if (size <= 4095)
145     allocations[size-1] += 1;
146   else
147     allocations[4095] += 1;
148   allocated_mem += size;
149 #endif /* ENABLE_MEM_PROFILE */
150 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
151   
152   
153   return p;
154 }
155
156 gpointer
157 g_malloc0 (gulong size)
158 {
159   gpointer p;
160   
161   
162 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
163   gulong *t;
164 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
165   
166   
167   if (size == 0)
168     return NULL;
169   
170   
171 #ifdef ENABLE_MEM_PROFILE
172   size += SIZEOF_LONG;
173 #endif /* ENABLE_MEM_PROFILE */
174   
175 #ifdef ENABLE_MEM_CHECK
176   size += SIZEOF_LONG;
177 #endif /* ENABLE_MEM_CHECK */
178   
179   
180   p = (gpointer) calloc (size, 1);
181   if (!p)
182     g_error ("could not allocate %ld bytes", size);
183   
184   
185 #ifdef ENABLE_MEM_CHECK
186   size -= SIZEOF_LONG;
187   
188   t = p;
189   p = ((guchar*) p + SIZEOF_LONG);
190   *t = 0;
191 #endif /* ENABLE_MEM_CHECK */
192   
193 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
194   size -= SIZEOF_LONG;
195   
196   t = p;
197   p = ((guchar*) p + SIZEOF_LONG);
198   *t = size;
199   
200 #ifdef ENABLE_MEM_PROFILE
201   if (size <= 4095)
202     allocations[size-1] += 1;
203   else
204     allocations[4095] += 1;
205   allocated_mem += size;
206 #endif /* ENABLE_MEM_PROFILE */
207 #endif /* ENABLE_MEM_PROFILE */
208   
209   
210   return p;
211 }
212
213 gpointer
214 g_realloc (gpointer mem,
215            gulong   size)
216 {
217   gpointer p;
218   
219 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
220   gulong *t;
221 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
222   
223   
224   if (size == 0)
225     return NULL;
226   
227   
228 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
229   size += SIZEOF_LONG;
230 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
231   
232 #ifdef ENABLE_MEM_CHECK
233   size += SIZEOF_LONG;
234 #endif /* ENABLE_MEM_CHECK */
235   
236   
237   if (!mem)
238     p = (gpointer) malloc (size);
239   else
240     {
241 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
242       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
243 #ifdef ENABLE_MEM_PROFILE
244       freed_mem += *t;
245 #endif /* ENABLE_MEM_PROFILE */
246       mem = t;
247 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
248       
249 #ifdef ENABLE_MEM_CHECK
250       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
251       if (*t >= 1)
252         g_warning ("trying to realloc freed memory\n");
253       mem = t;
254 #endif /* ENABLE_MEM_CHECK */
255       
256       p = (gpointer) realloc (mem, size);
257     }
258   
259   if (!p)
260     g_error ("could not reallocate %ld bytes", size);
261   
262   
263 #ifdef ENABLE_MEM_CHECK
264   size -= SIZEOF_LONG;
265   
266   t = p;
267   p = ((guchar*) p + SIZEOF_LONG);
268   *t = 0;
269 #endif /* ENABLE_MEM_CHECK */
270   
271 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
272   size -= SIZEOF_LONG;
273   
274   t = p;
275   p = ((guchar*) p + SIZEOF_LONG);
276   *t = size;
277   
278 #ifdef ENABLE_MEM_PROFILE
279   if (size <= 4095)
280     allocations[size-1] += 1;
281   else
282     allocations[4095] += 1;
283   allocated_mem += size;
284 #endif /* ENABLE_MEM_PROFILE */
285 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
286   
287   
288   return p;
289 }
290
291 void
292 g_free (gpointer mem)
293 {
294   if (mem)
295     {
296 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
297       gulong *t;
298       gulong size;
299 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
300       
301 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
302       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
303       size = *t;
304 #ifdef ENABLE_MEM_PROFILE
305       freed_mem += size;
306 #endif /* ENABLE_MEM_PROFILE */
307       mem = t;
308 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
309       
310 #ifdef ENABLE_MEM_CHECK
311       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
312       if (*t >= 1)
313         g_warning ("freeing previously freed memory\n");
314       *t += 1;
315       mem = t;
316       
317       memset ((guchar*) mem + 8, 0, size);
318 #else /* ENABLE_MEM_CHECK */
319       free (mem);
320 #endif /* ENABLE_MEM_CHECK */
321     }
322 }
323
324 #endif /* ! USE_DMALLOC */
325
326
327 void
328 g_mem_profile (void)
329 {
330 #ifdef ENABLE_MEM_PROFILE
331   gint i;
332   
333   for (i = 0; i < 4095; i++)
334     if (allocations[i] > 0)
335       g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
336              "%lu allocations of %d bytes\n", allocations[i], i + 1);
337   
338   if (allocations[4095] > 0)
339     g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
340            "%lu allocations of greater than 4095 bytes\n", allocations[4095]);
341   g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes allocated\n", allocated_mem);
342   g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes freed\n", freed_mem);
343   g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes in use\n", allocated_mem - freed_mem);
344 #endif /* ENABLE_MEM_PROFILE */
345 }
346
347 void
348 g_mem_check (gpointer mem)
349 {
350 #ifdef ENABLE_MEM_CHECK
351   gulong *t;
352   
353   t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
354   
355   if (*t >= 1)
356     g_warning ("mem: 0x%08x has been freed: %lu\n", (gulong) mem, *t);
357 #endif /* ENABLE_MEM_CHECK */
358 }
359
360 GMemChunk*
361 g_mem_chunk_new (gchar  *name,
362                  gint    atom_size,
363                  gulong  area_size,
364                  gint    type)
365 {
366   GRealMemChunk *mem_chunk;
367   gulong rarea_size;
368   
369   mem_chunk = g_new (struct _GRealMemChunk, 1);
370   mem_chunk->name = name;
371   mem_chunk->type = type;
372   mem_chunk->num_mem_areas = 0;
373   mem_chunk->num_marked_areas = 0;
374   mem_chunk->mem_area = NULL;
375   mem_chunk->free_mem_area = NULL;
376   mem_chunk->free_atoms = NULL;
377   mem_chunk->mem_tree = NULL;
378   mem_chunk->mem_areas = NULL;
379   mem_chunk->atom_size = atom_size;
380   
381   if (mem_chunk->type == G_ALLOC_AND_FREE)
382     mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
383   
384   if (mem_chunk->atom_size % MEM_ALIGN)
385     mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
386   
387   mem_chunk->area_size = area_size;
388   if (mem_chunk->area_size > MAX_MEM_AREA)
389     mem_chunk->area_size = MAX_MEM_AREA;
390   while (mem_chunk->area_size < mem_chunk->atom_size)
391     mem_chunk->area_size *= 2;
392   
393   rarea_size = mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
394   rarea_size = g_mem_chunk_compute_size (rarea_size);
395   mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
396   
397   /*
398     mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
399     if (mem_chunk->area_size < mem_chunk->atom_size)
400     {
401     mem_chunk->area_size = (mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE) * 2;
402     mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
403     }
404     
405     if (mem_chunk->area_size % mem_chunk->atom_size)
406     mem_chunk->area_size += mem_chunk->atom_size - (mem_chunk->area_size % mem_chunk->atom_size);
407   */
408   
409   mem_chunk->next = mem_chunks;
410   mem_chunk->prev = NULL;
411   if (mem_chunks)
412     mem_chunks->prev = mem_chunk;
413   mem_chunks = mem_chunk;
414   
415   return ((GMemChunk*) mem_chunk);
416 }
417
418 void
419 g_mem_chunk_destroy (GMemChunk *mem_chunk)
420 {
421   GRealMemChunk *rmem_chunk;
422   GMemArea *mem_areas;
423   GMemArea *temp_area;
424   
425   g_assert (mem_chunk != NULL);
426   
427   rmem_chunk = (GRealMemChunk*) mem_chunk;
428   
429   mem_areas = rmem_chunk->mem_areas;
430   while (mem_areas)
431     {
432       temp_area = mem_areas;
433       mem_areas = mem_areas->next;
434       g_free (temp_area);
435     }
436   
437   if (rmem_chunk->next)
438     rmem_chunk->next->prev = rmem_chunk->prev;
439   if (rmem_chunk->prev)
440     rmem_chunk->prev->next = rmem_chunk->next;
441   
442   if (rmem_chunk == mem_chunks)
443     mem_chunks = mem_chunks->next;
444   
445   if (rmem_chunk->type == G_ALLOC_AND_FREE)
446     g_tree_destroy (rmem_chunk->mem_tree);
447   
448   g_free (rmem_chunk);
449 }
450
451 gpointer
452 g_mem_chunk_alloc (GMemChunk *mem_chunk)
453 {
454   GRealMemChunk *rmem_chunk;
455   GMemArea *temp_area;
456   gpointer mem;
457   
458   g_assert (mem_chunk != NULL);
459   
460   rmem_chunk = (GRealMemChunk*) mem_chunk;
461   
462   while (rmem_chunk->free_atoms)
463     {
464       /* Get the first piece of memory on the "free_atoms" list.
465        * We can go ahead and destroy the list node we used to keep
466        *  track of it with and to update the "free_atoms" list to
467        *  point to its next element.
468        */
469       mem = rmem_chunk->free_atoms;
470       rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
471       
472       /* Determine which area this piece of memory is allocated from */
473       temp_area = g_tree_search (rmem_chunk->mem_tree,
474                                  (GSearchFunc) g_mem_chunk_area_search,
475                                  mem);
476       
477       /* If the area has been marked, then it is being destroyed.
478        *  (ie marked to be destroyed).
479        * We check to see if all of the segments on the free list that
480        *  reference this area have been removed. This occurs when
481        *  the ammount of free memory is less than the allocatable size.
482        * If the chunk should be freed, then we place it in the "free_mem_area".
483        * This is so we make sure not to free the mem area here and then
484        *  allocate it again a few lines down.
485        * If we don't allocate a chunk a few lines down then the "free_mem_area"
486        *  will be freed.
487        * If there is already a "free_mem_area" then we'll just free this mem area.
488        */
489       if (temp_area->mark)
490         {
491           /* Update the "free" memory available in that area */
492           temp_area->free += rmem_chunk->atom_size;
493           
494           if (temp_area->free == rmem_chunk->area_size)
495             {
496               if (temp_area == rmem_chunk->mem_area)
497                 rmem_chunk->mem_area = NULL;
498               
499               if (rmem_chunk->free_mem_area)
500                 {
501                   rmem_chunk->num_mem_areas -= 1;
502                   
503                   if (temp_area->next)
504                     temp_area->next->prev = temp_area->prev;
505                   if (temp_area->prev)
506                     temp_area->prev->next = temp_area->next;
507                   if (temp_area == rmem_chunk->mem_areas)
508                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
509                   
510                   if (rmem_chunk->type == G_ALLOC_AND_FREE)
511                     g_tree_remove (rmem_chunk->mem_tree, temp_area);
512                   g_free (temp_area);
513                 }
514               else
515                 rmem_chunk->free_mem_area = temp_area;
516               
517               rmem_chunk->num_marked_areas -= 1;
518             }
519         }
520       else
521         {
522           /* Update the number of allocated atoms count.
523            */
524           temp_area->allocated += 1;
525           
526           /* The area wasn't marked...return the memory
527            */
528           goto outa_here;
529         }
530     }
531   
532   /* If there isn't a current mem area or the current mem area is out of space
533    *  then allocate a new mem area. We'll first check and see if we can use
534    *  the "free_mem_area". Otherwise we'll just malloc the mem area.
535    */
536   if ((!rmem_chunk->mem_area) ||
537       ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
538     {
539       if (rmem_chunk->free_mem_area)
540         {
541           rmem_chunk->mem_area = rmem_chunk->free_mem_area;
542           rmem_chunk->free_mem_area = NULL;
543         }
544       else
545         {
546           rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
547                                                        MEM_AREA_SIZE +
548                                                        rmem_chunk->area_size);
549           
550           rmem_chunk->num_mem_areas += 1;
551           rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
552           rmem_chunk->mem_area->prev = NULL;
553           
554           if (rmem_chunk->mem_areas)
555             rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
556           rmem_chunk->mem_areas = rmem_chunk->mem_area;
557           
558           if (rmem_chunk->type == G_ALLOC_AND_FREE)
559             g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
560         }
561       
562       rmem_chunk->mem_area->index = 0;
563       rmem_chunk->mem_area->free = rmem_chunk->area_size;
564       rmem_chunk->mem_area->allocated = 0;
565       rmem_chunk->mem_area->mark = 0;
566     }
567   
568   /* Get the memory and modify the state variables appropriately.
569    */
570   mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
571   rmem_chunk->mem_area->index += rmem_chunk->atom_size;
572   rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
573   rmem_chunk->mem_area->allocated += 1;
574   
575  outa_here:
576   return mem;
577 }
578
579 void
580 g_mem_chunk_free (GMemChunk *mem_chunk,
581                   gpointer   mem)
582 {
583   GRealMemChunk *rmem_chunk;
584   GMemArea *temp_area;
585   GFreeAtom *free_atom;
586   
587   g_assert (mem_chunk != NULL);
588   g_assert (mem != NULL);
589   
590   rmem_chunk = (GRealMemChunk*) mem_chunk;
591   
592   /* Don't do anything if this is an ALLOC_ONLY chunk
593    */
594   if (rmem_chunk->type == G_ALLOC_AND_FREE)
595     {
596       /* Place the memory on the "free_atoms" list
597        */
598       free_atom = (GFreeAtom*) mem;
599       free_atom->next = rmem_chunk->free_atoms;
600       rmem_chunk->free_atoms = free_atom;
601       
602       temp_area = g_tree_search (rmem_chunk->mem_tree,
603                                  (GSearchFunc) g_mem_chunk_area_search,
604                                  mem);
605       
606       temp_area->allocated -= 1;
607       
608       if (temp_area->allocated == 0)
609         {
610           temp_area->mark = 1;
611           rmem_chunk->num_marked_areas += 1;
612         }
613     }
614 }
615
616 /* This doesn't free the free_area if there is one */
617 void
618 g_mem_chunk_clean (GMemChunk *mem_chunk)
619 {
620   GRealMemChunk *rmem_chunk;
621   GMemArea *mem_area;
622   GFreeAtom *prev_free_atom;
623   GFreeAtom *temp_free_atom;
624   gpointer mem;
625   
626   g_assert (mem_chunk != NULL);
627   
628   rmem_chunk = (GRealMemChunk*) mem_chunk;
629   
630   if (rmem_chunk->type == G_ALLOC_AND_FREE)
631     {
632       prev_free_atom = NULL;
633       temp_free_atom = rmem_chunk->free_atoms;
634       
635       while (temp_free_atom)
636         {
637           mem = (gpointer) temp_free_atom;
638           
639           mem_area = g_tree_search (rmem_chunk->mem_tree,
640                                     (GSearchFunc) g_mem_chunk_area_search,
641                                     mem);
642           
643           /* If this mem area is marked for destruction then delete the
644            *  area and list node and decrement the free mem.
645            */
646           if (mem_area->mark)
647             {
648               if (prev_free_atom)
649                 prev_free_atom->next = temp_free_atom->next;
650               else
651                 rmem_chunk->free_atoms = temp_free_atom->next;
652               temp_free_atom = temp_free_atom->next;
653               
654               mem_area->free += rmem_chunk->atom_size;
655               if (mem_area->free == rmem_chunk->area_size)
656                 {
657                   rmem_chunk->num_mem_areas -= 1;
658                   rmem_chunk->num_marked_areas -= 1;
659                   
660                   if (mem_area->next)
661                     mem_area->next->prev = mem_area->prev;
662                   if (mem_area->prev)
663                     mem_area->prev->next = mem_area->next;
664                   if (mem_area == rmem_chunk->mem_areas)
665                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
666                   if (mem_area == rmem_chunk->mem_area)
667                     rmem_chunk->mem_area = NULL;
668                   
669                   if (rmem_chunk->type == G_ALLOC_AND_FREE)
670                     g_tree_remove (rmem_chunk->mem_tree, mem_area);
671                   g_free (mem_area);
672                 }
673             }
674           else
675             {
676               prev_free_atom = temp_free_atom;
677               temp_free_atom = temp_free_atom->next;
678             }
679         }
680     }
681 }
682
683 void
684 g_mem_chunk_reset (GMemChunk *mem_chunk)
685 {
686   GRealMemChunk *rmem_chunk;
687   GMemArea *mem_areas;
688   GMemArea *temp_area;
689   
690   g_assert (mem_chunk != NULL);
691   
692   rmem_chunk = (GRealMemChunk*) mem_chunk;
693   
694   mem_areas = rmem_chunk->mem_areas;
695   rmem_chunk->num_mem_areas = 0;
696   rmem_chunk->mem_areas = NULL;
697   rmem_chunk->mem_area = NULL;
698   
699   while (mem_areas)
700     {
701       temp_area = mem_areas;
702       mem_areas = mem_areas->next;
703       g_free (temp_area);
704     }
705   
706   rmem_chunk->free_atoms = NULL;
707   
708   if (rmem_chunk->mem_tree)
709     g_tree_destroy (rmem_chunk->mem_tree);
710   rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
711 }
712
713 void
714 g_mem_chunk_print (GMemChunk *mem_chunk)
715 {
716   GRealMemChunk *rmem_chunk;
717   GMemArea *mem_areas;
718   gulong mem;
719   
720   g_assert (mem_chunk != NULL);
721   
722   rmem_chunk = (GRealMemChunk*) mem_chunk;
723   mem_areas = rmem_chunk->mem_areas;
724   mem = 0;
725   
726   while (mem_areas)
727     {
728       mem += rmem_chunk->area_size - mem_areas->free;
729       mem_areas = mem_areas->next;
730     }
731   
732   g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
733          "%s: %ld bytes using %d mem areas\n",
734          rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
735 }
736
737 void
738 g_mem_chunk_info (void)
739 {
740   GRealMemChunk *mem_chunk;
741   gint count;
742   
743   count = 0;
744   mem_chunk = mem_chunks;
745   while (mem_chunk)
746     {
747       count += 1;
748       mem_chunk = mem_chunk->next;
749     }
750   
751   g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks\n", count);
752   
753   mem_chunk = mem_chunks;
754   while (mem_chunk)
755     {
756       g_mem_chunk_print ((GMemChunk*) mem_chunk);
757       mem_chunk = mem_chunk->next;
758     }
759 }
760
761 void
762 g_blow_chunks (void)
763 {
764   GRealMemChunk *mem_chunk;
765   
766   mem_chunk = mem_chunks;
767   while (mem_chunk)
768     {
769       g_mem_chunk_clean ((GMemChunk*) mem_chunk);
770       mem_chunk = mem_chunk->next;
771     }
772 }
773
774
775 static gulong
776 g_mem_chunk_compute_size (gulong size)
777 {
778   gulong power_of_2;
779   gulong lower, upper;
780   
781   power_of_2 = 16;
782   while (power_of_2 < size)
783     power_of_2 <<= 1;
784   
785   lower = power_of_2 >> 1;
786   upper = power_of_2;
787   
788   if ((size - lower) < (upper - size))
789     return lower;
790   return upper;
791 }
792
793 static gint
794 g_mem_chunk_area_compare (GMemArea *a,
795                           GMemArea *b)
796 {
797   return (a->mem - b->mem);
798 }
799
800 static gint
801 g_mem_chunk_area_search (GMemArea *a,
802                          gchar    *addr)
803 {
804   if (a->mem <= addr)
805     {
806       if (addr < &a->mem[a->index])
807         return 0;
808       return 1;
809     }
810   return -1;
811 }