]> Pileus Git - ~andy/gtk/blob - glib/gmem.c
Initial revision
[~andy/gtk] / 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 Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18 #include <stdlib.h>
19 #include <string.h>
20 #include "glib.h"
21
22
23 /* #define MEM_PROFILE */
24 /* #define 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 MEM_PROFILE
91 static gulong allocations[4096] = { 0 };
92 static gulong allocated_mem = 0;
93 static gulong freed_mem = 0;
94 #endif /* 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(MEM_PROFILE) || defined(MEM_CHECK)
106   gulong *t;
107 #endif /* MEM_PROFILE || MEM_CHECK */
108
109
110   if (size == 0)
111     return NULL;
112
113
114 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
115   size += SIZEOF_LONG;
116 #endif /* MEM_PROFILE || MEM_CHECK */
117
118 #ifdef MEM_CHECK
119   size += SIZEOF_LONG;
120 #endif /* 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 MEM_CHECK
129   size -= SIZEOF_LONG;
130
131   t = p;
132   p = ((guchar*) p + SIZEOF_LONG);
133   *t = 0;
134 #endif /* MEM_CHECK */
135
136 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
137   size -= SIZEOF_LONG;
138
139   t = p;
140   p = ((guchar*) p + SIZEOF_LONG);
141   *t = size;
142
143 #ifdef MEM_PROFILE
144   if (size <= 4095)
145     allocations[size-1] += 1;
146   else
147     allocations[4095] += 1;
148   allocated_mem += size;
149 #endif /* MEM_PROFILE */
150 #endif /* MEM_PROFILE || 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(MEM_PROFILE) || defined(MEM_CHECK)
163   gulong *t;
164 #endif /* MEM_PROFILE || MEM_CHECK */
165
166
167   if (size == 0)
168     return NULL;
169
170
171 #ifdef MEM_PROFILE
172   size += SIZEOF_LONG;
173 #endif /* MEM_PROFILE */
174
175 #ifdef MEM_CHECK
176   size += SIZEOF_LONG;
177 #endif /* 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 MEM_CHECK
186   size -= SIZEOF_LONG;
187
188   t = p;
189   p = ((guchar*) p + SIZEOF_LONG);
190   *t = 0;
191 #endif /* MEM_CHECK */
192
193 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
194   size -= SIZEOF_LONG;
195
196   t = p;
197   p = ((guchar*) p + SIZEOF_LONG);
198   *t = size;
199
200 #ifdef MEM_PROFILE
201   if (size <= 4095)
202     allocations[size-1] += 1;
203   else
204     allocations[4095] += 1;
205   allocated_mem += size;
206 #endif /* MEM_PROFILE */
207 #endif /* 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(MEM_PROFILE) || defined(MEM_CHECK)
220   gulong *t;
221 #endif /* MEM_PROFILE || MEM_CHECK */
222
223
224   if (size == 0)
225     return NULL;
226
227
228 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
229   size += SIZEOF_LONG;
230 #endif /* MEM_PROFILE || MEM_CHECK */
231
232 #ifdef MEM_CHECK
233   size += SIZEOF_LONG;
234 #endif /* MEM_CHECK */
235
236
237   if (!mem)
238     p = (gpointer) malloc (size);
239   else
240     {
241 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
242       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
243 #ifdef MEM_PROFILE
244       freed_mem += *t;
245 #endif /* MEM_PROFILE */
246       mem = t;
247 #endif /* MEM_PROFILE || MEM_CHECK */
248
249 #ifdef 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 /* 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 MEM_CHECK
264   size -= SIZEOF_LONG;
265
266   t = p;
267   p = ((guchar*) p + SIZEOF_LONG);
268   *t = 0;
269 #endif /* MEM_CHECK */
270
271 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
272   size -= SIZEOF_LONG;
273
274   t = p;
275   p = ((guchar*) p + SIZEOF_LONG);
276   *t = size;
277
278 #ifdef MEM_PROFILE
279   if (size <= 4095)
280     allocations[size-1] += 1;
281   else
282     allocations[4095] += 1;
283   allocated_mem += size;
284 #endif /* MEM_PROFILE */
285 #endif /* MEM_PROFILE || MEM_CHECK */
286
287
288   return p;
289 }
290
291 void
292 g_free (gpointer mem)
293 {
294   if (mem)
295     {
296 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
297       gulong *t;
298       gulong size;
299 #endif /* MEM_PROFILE || MEM_CHECK */
300
301 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
302       t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
303       size = *t;
304 #ifdef MEM_PROFILE
305       freed_mem += size;
306 #endif /* MEM_PROFILE */
307       mem = t;
308 #endif /* MEM_PROFILE || MEM_CHECK */
309
310 #ifdef 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 /* MEM_CHECK */
319       free (mem);
320 #endif /* MEM_CHECK */
321     }
322 }
323
324 #endif /* ! USE_DMALLOC */
325
326
327 void
328 g_mem_profile ()
329 {
330 #ifdef 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 /* MEM_PROFILE */
343 }
344
345 void
346 g_mem_check (gpointer mem)
347 {
348 #ifdef 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 /* 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                   rmem_chunk->num_marked_areas -= 1;
501
502                   if (temp_area->next)
503                     temp_area->next->prev = temp_area->prev;
504                   if (temp_area->prev)
505                     temp_area->prev->next = temp_area->next;
506                   if (temp_area == rmem_chunk->mem_areas)
507                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
508                   if (temp_area == rmem_chunk->mem_area)
509                     rmem_chunk->mem_area = NULL;
510
511                   g_free (temp_area);
512                 }
513               else
514                 rmem_chunk->free_mem_area = temp_area;
515             }
516         }
517       else
518         {
519           /* Update the number of allocated atoms count.
520            */
521           temp_area->allocated += 1;
522
523           /* The area wasn't marked...return the memory
524            */
525           goto outa_here;
526         }
527     }
528
529   /* If there isn't a current mem area or the current mem area is out of space
530    *  then allocate a new mem area. We'll first check and see if we can use
531    *  the "free_mem_area". Otherwise we'll just malloc the mem area.
532    */
533   if ((!rmem_chunk->mem_area) ||
534       ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
535     {
536       if (rmem_chunk->free_mem_area)
537         {
538           rmem_chunk->mem_area = rmem_chunk->free_mem_area;
539           rmem_chunk->free_mem_area = NULL;
540         }
541       else
542         {
543           rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
544                                                        MEM_AREA_SIZE +
545                                                        rmem_chunk->area_size);
546
547           rmem_chunk->num_mem_areas += 1;
548           rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
549           rmem_chunk->mem_area->prev = NULL;
550
551           if (rmem_chunk->mem_areas)
552             rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
553           rmem_chunk->mem_areas = rmem_chunk->mem_area;
554
555           if (rmem_chunk->type == G_ALLOC_AND_FREE)
556             g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
557         }
558
559       rmem_chunk->mem_area->index = 0;
560       rmem_chunk->mem_area->free = rmem_chunk->area_size;
561       rmem_chunk->mem_area->allocated = 0;
562       rmem_chunk->mem_area->mark = 0;
563     }
564   else if (rmem_chunk->free_mem_area)
565     {
566       rmem_chunk->num_mem_areas -= 1;
567
568       if (rmem_chunk->free_mem_area->next)
569         rmem_chunk->free_mem_area->next->prev = rmem_chunk->free_mem_area->prev;
570       if (rmem_chunk->free_mem_area->prev)
571         rmem_chunk->free_mem_area->prev->next = rmem_chunk->free_mem_area->next;
572       if (rmem_chunk->free_mem_area == rmem_chunk->mem_areas)
573         rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
574
575       if (rmem_chunk->type == G_ALLOC_AND_FREE)
576         g_tree_remove (rmem_chunk->mem_tree, rmem_chunk->free_mem_area);
577
578       g_free (rmem_chunk->free_mem_area);
579       rmem_chunk->free_mem_area = NULL;
580     }
581
582   /* Get the memory and modify the state variables appropriately.
583    */
584   mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
585   rmem_chunk->mem_area->index += rmem_chunk->atom_size;
586   rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
587   rmem_chunk->mem_area->allocated += 1;
588
589 outa_here:
590   return mem;
591 }
592
593 void
594 g_mem_chunk_free (GMemChunk *mem_chunk,
595                   gpointer   mem)
596 {
597   GRealMemChunk *rmem_chunk;
598   GMemArea *temp_area;
599   GFreeAtom *free_atom;
600
601   g_assert (mem_chunk != NULL);
602   g_assert (mem != NULL);
603
604   rmem_chunk = (GRealMemChunk*) mem_chunk;
605
606   /* Don't do anything if this is an ALLOC_ONLY chunk
607    */
608   if (rmem_chunk->type == G_ALLOC_AND_FREE)
609     {
610       /* Place the memory on the "free_atoms" list
611        */
612       free_atom = (GFreeAtom*) mem;
613       free_atom->next = rmem_chunk->free_atoms;
614       rmem_chunk->free_atoms = free_atom;
615
616       temp_area = g_tree_search (rmem_chunk->mem_tree,
617                                  (GSearchFunc) g_mem_chunk_area_search,
618                                  mem);
619
620       temp_area->allocated -= 1;
621
622       if (temp_area->allocated == 0)
623         {
624           temp_area->mark = 1;
625           rmem_chunk->num_marked_areas += 1;
626
627           g_mem_chunk_clean (mem_chunk);
628         }
629     }
630 }
631
632 void
633 g_mem_chunk_clean (GMemChunk *mem_chunk)
634 {
635   GRealMemChunk *rmem_chunk;
636   GMemArea *mem_area;
637   GFreeAtom *prev_free_atom;
638   GFreeAtom *temp_free_atom;
639   gpointer mem;
640
641   g_assert (mem_chunk != NULL);
642
643   rmem_chunk = (GRealMemChunk*) mem_chunk;
644
645   if (rmem_chunk->type == G_ALLOC_AND_FREE)
646     {
647       prev_free_atom = NULL;
648       temp_free_atom = rmem_chunk->free_atoms;
649
650       while (temp_free_atom)
651         {
652           mem = (gpointer) temp_free_atom;
653
654           mem_area = g_tree_search (rmem_chunk->mem_tree,
655                                     (GSearchFunc) g_mem_chunk_area_search,
656                                     mem);
657
658           /* If this mem area is marked for destruction then delete the
659            *  area and list node and decrement the free mem.
660            */
661           if (mem_area->mark)
662             {
663               if (prev_free_atom)
664                 prev_free_atom->next = temp_free_atom->next;
665               else
666                 rmem_chunk->free_atoms = temp_free_atom->next;
667               temp_free_atom = temp_free_atom->next;
668
669               mem_area->free += rmem_chunk->atom_size;
670               if (mem_area->free == rmem_chunk->area_size)
671                 {
672                   rmem_chunk->num_mem_areas -= 1;
673                   rmem_chunk->num_marked_areas -= 1;
674
675                   if (mem_area->next)
676                     mem_area->next->prev = mem_area->prev;
677                   if (mem_area->prev)
678                     mem_area->prev->next = mem_area->next;
679                   if (mem_area == rmem_chunk->mem_areas)
680                     rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
681                   if (mem_area == rmem_chunk->mem_area)
682                     rmem_chunk->mem_area = NULL;
683
684                   if (rmem_chunk->type == G_ALLOC_AND_FREE)
685                     g_tree_remove (rmem_chunk->mem_tree, mem_area);
686                   g_free (mem_area);
687                 }
688             }
689           else
690             {
691               prev_free_atom = temp_free_atom;
692               temp_free_atom = temp_free_atom->next;
693             }
694         }
695     }
696 }
697
698 void
699 g_mem_chunk_reset (GMemChunk *mem_chunk)
700 {
701   GRealMemChunk *rmem_chunk;
702   GMemArea *mem_areas;
703   GMemArea *temp_area;
704
705   g_assert (mem_chunk != NULL);
706
707   rmem_chunk = (GRealMemChunk*) mem_chunk;
708
709   mem_areas = rmem_chunk->mem_areas;
710   rmem_chunk->num_mem_areas = 0;
711   rmem_chunk->mem_areas = NULL;
712   rmem_chunk->mem_area = NULL;
713
714   while (mem_areas)
715     {
716       temp_area = mem_areas;
717       mem_areas = mem_areas->next;
718       g_free (temp_area);
719     }
720
721   rmem_chunk->free_atoms = NULL;
722
723   if (rmem_chunk->mem_tree)
724     g_tree_destroy (rmem_chunk->mem_tree);
725   rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
726 }
727
728 void
729 g_mem_chunk_print (GMemChunk *mem_chunk)
730 {
731   GRealMemChunk *rmem_chunk;
732   GMemArea *mem_areas;
733   gulong mem;
734
735   g_assert (mem_chunk != NULL);
736
737   rmem_chunk = (GRealMemChunk*) mem_chunk;
738   mem_areas = rmem_chunk->mem_areas;
739   mem = 0;
740
741   while (mem_areas)
742     {
743       mem += rmem_chunk->area_size - mem_areas->free;
744       mem_areas = mem_areas->next;
745     }
746
747   g_print ("%s: %ld bytes using %d mem areas", rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
748 }
749
750 void
751 g_mem_chunk_info ()
752 {
753   GRealMemChunk *mem_chunk;
754   gint count;
755
756   count = 0;
757   mem_chunk = mem_chunks;
758   while (mem_chunk)
759     {
760       count += 1;
761       mem_chunk = mem_chunk->next;
762     }
763
764   g_print ("%d mem chunks", count);
765
766   mem_chunk = mem_chunks;
767   while (mem_chunk)
768     {
769       g_mem_chunk_print ((GMemChunk*) mem_chunk);
770       mem_chunk = mem_chunk->next;
771     }
772 }
773
774 void
775 g_blow_chunks ()
776 {
777   GRealMemChunk *mem_chunk;
778
779   mem_chunk = mem_chunks;
780   while (mem_chunk)
781     {
782       g_mem_chunk_clean ((GMemChunk*) mem_chunk);
783       mem_chunk = mem_chunk->next;
784     }
785 }
786
787
788 static gulong
789 g_mem_chunk_compute_size (gulong size)
790 {
791   gulong power_of_2;
792   gulong lower, upper;
793
794   power_of_2 = 16;
795   while (power_of_2 < size)
796     power_of_2 <<= 1;
797
798   lower = power_of_2 >> 1;
799   upper = power_of_2;
800
801   if ((size - lower) < (upper - size))
802     return lower;
803   return upper;
804 }
805
806 static gint
807 g_mem_chunk_area_compare (GMemArea *a,
808                           GMemArea *b)
809 {
810   return (a->mem - b->mem);
811 }
812
813 static gint
814 g_mem_chunk_area_search (GMemArea *a,
815                          gchar    *addr)
816 {
817   if (a->mem <= addr)
818     {
819       if (addr < &a->mem[a->index])
820         return 0;
821       return 1;
822     }
823   return -1;
824 }