Initial revision
[platform/upstream/glib.git] / glib / 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_print ("%lu allocations of %d bytes\n", allocations[i], i + 1);
336
337   if (allocations[4095] > 0)
338     g_print ("%lu allocations of greater than 4095 bytes\n", allocations[4095]);
339   g_print ("%lu bytes allocated\n", allocated_mem);
340   g_print ("%lu bytes freed\n", freed_mem);
341   g_print ("%lu bytes in use\n", allocated_mem - freed_mem);
342 #endif /* ENABLE_MEM_PROFILE */
343 }
344
345 void
346 g_mem_check (gpointer mem)
347 {
348 #ifdef ENABLE_MEM_CHECK
349   gulong *t;
350
351   t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
352
353   if (*t >= 1)
354     g_warning ("mem: 0x%08x has been freed: %lu\n", (gulong) mem, *t);
355 #endif /* ENABLE_MEM_CHECK */
356 }
357
358 GMemChunk*
359 g_mem_chunk_new (gchar  *name,
360                  gint    atom_size,
361                  gulong  area_size,
362                  gint    type)
363 {
364   GRealMemChunk *mem_chunk;
365   gulong rarea_size;
366
367   mem_chunk = g_new (struct _GRealMemChunk, 1);
368   mem_chunk->name = name;
369   mem_chunk->type = type;
370   mem_chunk->num_mem_areas = 0;
371   mem_chunk->num_marked_areas = 0;
372   mem_chunk->mem_area = NULL;
373   mem_chunk->free_mem_area = NULL;
374   mem_chunk->free_atoms = NULL;
375   mem_chunk->mem_tree = NULL;
376   mem_chunk->mem_areas = NULL;
377   mem_chunk->atom_size = atom_size;
378
379   if (mem_chunk->type == G_ALLOC_AND_FREE)
380     mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
381
382   if (mem_chunk->atom_size % MEM_ALIGN)
383     mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
384
385   mem_chunk->area_size = area_size;
386   if (mem_chunk->area_size > MAX_MEM_AREA)
387     mem_chunk->area_size = MAX_MEM_AREA;
388   while (mem_chunk->area_size < mem_chunk->atom_size)
389     mem_chunk->area_size *= 2;
390
391   rarea_size = mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
392   rarea_size = g_mem_chunk_compute_size (rarea_size);
393   mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
394
395   /*
396   mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
397   if (mem_chunk->area_size < mem_chunk->atom_size)
398     {
399       mem_chunk->area_size = (mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE) * 2;
400       mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
401     }
402
403   if (mem_chunk->area_size % mem_chunk->atom_size)
404     mem_chunk->area_size += mem_chunk->atom_size - (mem_chunk->area_size % mem_chunk->atom_size);
405     */
406
407   mem_chunk->next = mem_chunks;
408   mem_chunk->prev = NULL;
409   if (mem_chunks)
410     mem_chunks->prev = mem_chunk;
411   mem_chunks = mem_chunk;
412
413   return ((GMemChunk*) mem_chunk);
414 }
415
416 void
417 g_mem_chunk_destroy (GMemChunk *mem_chunk)
418 {
419   GRealMemChunk *rmem_chunk;
420   GMemArea *mem_areas;
421   GMemArea *temp_area;
422
423   g_assert (mem_chunk != NULL);
424
425   rmem_chunk = (GRealMemChunk*) mem_chunk;
426
427   mem_areas = rmem_chunk->mem_areas;
428   while (mem_areas)
429     {
430       temp_area = mem_areas;
431       mem_areas = mem_areas->next;
432       g_free (temp_area);
433     }
434
435   if (rmem_chunk->next)
436     rmem_chunk->next->prev = rmem_chunk->prev;
437   if (rmem_chunk->prev)
438     rmem_chunk->prev->next = rmem_chunk->next;
439
440   if (rmem_chunk == mem_chunks)
441     mem_chunks = mem_chunks->next;
442
443   if (rmem_chunk->type == G_ALLOC_AND_FREE)
444     g_tree_destroy (rmem_chunk->mem_tree);
445
446   g_free (rmem_chunk);
447 }
448
449 gpointer
450 g_mem_chunk_alloc (GMemChunk *mem_chunk)
451 {
452   GRealMemChunk *rmem_chunk;
453   GMemArea *temp_area;
454   gpointer mem;
455
456   g_assert (mem_chunk != NULL);
457
458   rmem_chunk = (GRealMemChunk*) mem_chunk;
459
460   while (rmem_chunk->free_atoms)
461     {
462       /* Get the first piece of memory on the "free_atoms" list.
463        * We can go ahead and destroy the list node we used to keep
464        *  track of it with and to update the "free_atoms" list to
465        *  point to its next element.
466        */
467       mem = rmem_chunk->free_atoms;
468       rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
469
470       /* Determine which area this piece of memory is allocated from */
471       temp_area = g_tree_search (rmem_chunk->mem_tree,
472                                  (GSearchFunc) g_mem_chunk_area_search,
473                                  mem);
474
475       /* If the area has been marked, then it is being destroyed.
476        *  (ie marked to be destroyed).
477        * We check to see if all of the segments on the free list that
478        *  reference this area have been removed. This occurs when
479        *  the ammount of free memory is less than the allocatable size.
480        * If the chunk should be freed, then we place it in the "free_mem_area".
481        * This is so we make sure not to free the mem area here and then
482        *  allocate it again a few lines down.
483        * If we don't allocate a chunk a few lines down then the "free_mem_area"
484        *  will be freed.
485        * If there is already a "free_mem_area" then we'll just free this mem area.
486        */
487       if (temp_area->mark)
488         {
489           /* Update the "free" memory available in that area */
490           temp_area->free += rmem_chunk->atom_size;
491
492           if (temp_area->free == rmem_chunk->area_size)
493             {
494               if (temp_area == rmem_chunk->mem_area)
495                 rmem_chunk->mem_area = NULL;
496
497               if (rmem_chunk->free_mem_area)
498                 {
499                   rmem_chunk->num_mem_areas -= 1;
500
501                   if (temp_area->next)
502                     temp_area->next->prev = temp_area->prev;
503                   if (temp_area->prev)
504                     temp_area->prev->next = temp_area->next;
505                   if (temp_area == rmem_chunk->mem_areas)
506                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
507
508                   if (rmem_chunk->type == G_ALLOC_AND_FREE)
509                     g_tree_remove (rmem_chunk->mem_tree, temp_area);
510                   g_free (temp_area);
511                 }
512               else
513                 rmem_chunk->free_mem_area = temp_area;
514               
515               rmem_chunk->num_marked_areas -= 1;
516             }
517         }
518       else
519         {
520           /* Update the number of allocated atoms count.
521            */
522           temp_area->allocated += 1;
523
524           /* The area wasn't marked...return the memory
525            */
526           goto outa_here;
527         }
528     }
529
530   /* If there isn't a current mem area or the current mem area is out of space
531    *  then allocate a new mem area. We'll first check and see if we can use
532    *  the "free_mem_area". Otherwise we'll just malloc the mem area.
533    */
534   if ((!rmem_chunk->mem_area) ||
535       ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
536     {
537       if (rmem_chunk->free_mem_area)
538         {
539           rmem_chunk->mem_area = rmem_chunk->free_mem_area;
540           rmem_chunk->free_mem_area = NULL;
541         }
542       else
543         {
544           rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
545                                                        MEM_AREA_SIZE +
546                                                        rmem_chunk->area_size);
547
548           rmem_chunk->num_mem_areas += 1;
549           rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
550           rmem_chunk->mem_area->prev = NULL;
551
552           if (rmem_chunk->mem_areas)
553             rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
554           rmem_chunk->mem_areas = rmem_chunk->mem_area;
555
556           if (rmem_chunk->type == G_ALLOC_AND_FREE)
557             g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
558         }
559
560       rmem_chunk->mem_area->index = 0;
561       rmem_chunk->mem_area->free = rmem_chunk->area_size;
562       rmem_chunk->mem_area->allocated = 0;
563       rmem_chunk->mem_area->mark = 0;
564     }
565
566   /* Get the memory and modify the state variables appropriately.
567    */
568   mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
569   rmem_chunk->mem_area->index += rmem_chunk->atom_size;
570   rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
571   rmem_chunk->mem_area->allocated += 1;
572
573 outa_here:
574   return mem;
575 }
576
577 void
578 g_mem_chunk_free (GMemChunk *mem_chunk,
579                   gpointer   mem)
580 {
581   GRealMemChunk *rmem_chunk;
582   GMemArea *temp_area;
583   GFreeAtom *free_atom;
584
585   g_assert (mem_chunk != NULL);
586   g_assert (mem != NULL);
587
588   rmem_chunk = (GRealMemChunk*) mem_chunk;
589
590   /* Don't do anything if this is an ALLOC_ONLY chunk
591    */
592   if (rmem_chunk->type == G_ALLOC_AND_FREE)
593     {
594       /* Place the memory on the "free_atoms" list
595        */
596       free_atom = (GFreeAtom*) mem;
597       free_atom->next = rmem_chunk->free_atoms;
598       rmem_chunk->free_atoms = free_atom;
599
600       temp_area = g_tree_search (rmem_chunk->mem_tree,
601                                  (GSearchFunc) g_mem_chunk_area_search,
602                                  mem);
603
604       temp_area->allocated -= 1;
605
606       if (temp_area->allocated == 0)
607         {
608           temp_area->mark = 1;
609           rmem_chunk->num_marked_areas += 1;
610         }
611     }
612 }
613
614 /* This doesn't free the free_area if there is one */
615 void
616 g_mem_chunk_clean (GMemChunk *mem_chunk)
617 {
618   GRealMemChunk *rmem_chunk;
619   GMemArea *mem_area;
620   GFreeAtom *prev_free_atom;
621   GFreeAtom *temp_free_atom;
622   gpointer mem;
623
624   g_assert (mem_chunk != NULL);
625
626   rmem_chunk = (GRealMemChunk*) mem_chunk;
627
628   if (rmem_chunk->type == G_ALLOC_AND_FREE)
629     {
630       prev_free_atom = NULL;
631       temp_free_atom = rmem_chunk->free_atoms;
632
633       while (temp_free_atom)
634         {
635           mem = (gpointer) temp_free_atom;
636
637           mem_area = g_tree_search (rmem_chunk->mem_tree,
638                                     (GSearchFunc) g_mem_chunk_area_search,
639                                     mem);
640
641           /* If this mem area is marked for destruction then delete the
642            *  area and list node and decrement the free mem.
643            */
644           if (mem_area->mark)
645             {
646               if (prev_free_atom)
647                 prev_free_atom->next = temp_free_atom->next;
648               else
649                 rmem_chunk->free_atoms = temp_free_atom->next;
650               temp_free_atom = temp_free_atom->next;
651
652               mem_area->free += rmem_chunk->atom_size;
653               if (mem_area->free == rmem_chunk->area_size)
654                 {
655                   rmem_chunk->num_mem_areas -= 1;
656                   rmem_chunk->num_marked_areas -= 1;
657
658                   if (mem_area->next)
659                     mem_area->next->prev = mem_area->prev;
660                   if (mem_area->prev)
661                     mem_area->prev->next = mem_area->next;
662                   if (mem_area == rmem_chunk->mem_areas)
663                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
664                   if (mem_area == rmem_chunk->mem_area)
665                     rmem_chunk->mem_area = NULL;
666
667                   if (rmem_chunk->type == G_ALLOC_AND_FREE)
668                     g_tree_remove (rmem_chunk->mem_tree, mem_area);
669                   g_free (mem_area);
670                 }
671             }
672           else
673             {
674               prev_free_atom = temp_free_atom;
675               temp_free_atom = temp_free_atom->next;
676             }
677         }
678     }
679 }
680
681 void
682 g_mem_chunk_reset (GMemChunk *mem_chunk)
683 {
684   GRealMemChunk *rmem_chunk;
685   GMemArea *mem_areas;
686   GMemArea *temp_area;
687
688   g_assert (mem_chunk != NULL);
689
690   rmem_chunk = (GRealMemChunk*) mem_chunk;
691
692   mem_areas = rmem_chunk->mem_areas;
693   rmem_chunk->num_mem_areas = 0;
694   rmem_chunk->mem_areas = NULL;
695   rmem_chunk->mem_area = NULL;
696
697   while (mem_areas)
698     {
699       temp_area = mem_areas;
700       mem_areas = mem_areas->next;
701       g_free (temp_area);
702     }
703
704   rmem_chunk->free_atoms = NULL;
705
706   if (rmem_chunk->mem_tree)
707     g_tree_destroy (rmem_chunk->mem_tree);
708   rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
709 }
710
711 void
712 g_mem_chunk_print (GMemChunk *mem_chunk)
713 {
714   GRealMemChunk *rmem_chunk;
715   GMemArea *mem_areas;
716   gulong mem;
717
718   g_assert (mem_chunk != NULL);
719
720   rmem_chunk = (GRealMemChunk*) mem_chunk;
721   mem_areas = rmem_chunk->mem_areas;
722   mem = 0;
723
724   while (mem_areas)
725     {
726       mem += rmem_chunk->area_size - mem_areas->free;
727       mem_areas = mem_areas->next;
728     }
729
730   g_print ("%s: %ld bytes using %d mem areas\n", rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
731 }
732
733 void
734 g_mem_chunk_info (void)
735 {
736   GRealMemChunk *mem_chunk;
737   gint count;
738
739   count = 0;
740   mem_chunk = mem_chunks;
741   while (mem_chunk)
742     {
743       count += 1;
744       mem_chunk = mem_chunk->next;
745     }
746
747   g_print ("%d mem chunks\n", count);
748
749   mem_chunk = mem_chunks;
750   while (mem_chunk)
751     {
752       g_mem_chunk_print ((GMemChunk*) mem_chunk);
753       mem_chunk = mem_chunk->next;
754     }
755 }
756
757 void
758 g_blow_chunks (void)
759 {
760   GRealMemChunk *mem_chunk;
761
762   mem_chunk = mem_chunks;
763   while (mem_chunk)
764     {
765       g_mem_chunk_clean ((GMemChunk*) mem_chunk);
766       mem_chunk = mem_chunk->next;
767     }
768 }
769
770
771 static gulong
772 g_mem_chunk_compute_size (gulong size)
773 {
774   gulong power_of_2;
775   gulong lower, upper;
776
777   power_of_2 = 16;
778   while (power_of_2 < size)
779     power_of_2 <<= 1;
780
781   lower = power_of_2 >> 1;
782   upper = power_of_2;
783
784   if ((size - lower) < (upper - size))
785     return lower;
786   return upper;
787 }
788
789 static gint
790 g_mem_chunk_area_compare (GMemArea *a,
791                           GMemArea *b)
792 {
793   return (a->mem - b->mem);
794 }
795
796 static gint
797 g_mem_chunk_area_search (GMemArea *a,
798                          gchar    *addr)
799 {
800   if (a->mem <= addr)
801     {
802       if (addr < &a->mem[a->index])
803         return 0;
804       return 1;
805     }
806   return -1;
807 }