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