1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
23 /* #define ENABLE_MEM_PROFILE */
24 /* #define ENABLE_MEM_CHECK */
27 #define MAX_MEM_AREA 65536L
28 #define MEM_AREA_SIZE 4L
30 #if SIZEOF_VOID_P > SIZEOF_LONG
31 #define MEM_ALIGN SIZEOF_VOID_P
33 #define MEM_ALIGN SIZEOF_LONG
37 typedef struct _GFreeAtom GFreeAtom;
38 typedef struct _GMemArea GMemArea;
39 typedef struct _GRealMemChunk GRealMemChunk;
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
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 */
81 static gulong g_mem_chunk_compute_size (gulong size);
82 static gint g_mem_chunk_area_compare (GMemArea *a,
84 static gint g_mem_chunk_area_search (GMemArea *a,
88 static GRealMemChunk *mem_chunks = NULL;
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 */
100 g_malloc (gulong size)
105 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
107 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
114 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
116 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
118 #ifdef ENABLE_MEM_CHECK
120 #endif /* ENABLE_MEM_CHECK */
123 p = (gpointer) malloc (size);
125 g_error ("could not allocate %ld bytes", size);
128 #ifdef ENABLE_MEM_CHECK
132 p = ((guchar*) p + SIZEOF_LONG);
134 #endif /* ENABLE_MEM_CHECK */
136 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
140 p = ((guchar*) p + SIZEOF_LONG);
143 #ifdef ENABLE_MEM_PROFILE
145 allocations[size-1] += 1;
147 allocations[4095] += 1;
148 allocated_mem += size;
149 #endif /* ENABLE_MEM_PROFILE */
150 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
157 g_malloc0 (gulong size)
162 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
164 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
171 #ifdef ENABLE_MEM_PROFILE
173 #endif /* ENABLE_MEM_PROFILE */
175 #ifdef ENABLE_MEM_CHECK
177 #endif /* ENABLE_MEM_CHECK */
180 p = (gpointer) calloc (size, 1);
182 g_error ("could not allocate %ld bytes", size);
185 #ifdef ENABLE_MEM_CHECK
189 p = ((guchar*) p + SIZEOF_LONG);
191 #endif /* ENABLE_MEM_CHECK */
193 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
197 p = ((guchar*) p + SIZEOF_LONG);
200 #ifdef ENABLE_MEM_PROFILE
202 allocations[size-1] += 1;
204 allocations[4095] += 1;
205 allocated_mem += size;
206 #endif /* ENABLE_MEM_PROFILE */
207 #endif /* ENABLE_MEM_PROFILE */
214 g_realloc (gpointer mem,
219 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
221 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
228 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
230 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
232 #ifdef ENABLE_MEM_CHECK
234 #endif /* ENABLE_MEM_CHECK */
238 p = (gpointer) malloc (size);
241 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
242 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
243 #ifdef ENABLE_MEM_PROFILE
245 #endif /* ENABLE_MEM_PROFILE */
247 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
249 #ifdef ENABLE_MEM_CHECK
250 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
252 g_warning ("trying to realloc freed memory\n");
254 #endif /* ENABLE_MEM_CHECK */
256 p = (gpointer) realloc (mem, size);
260 g_error ("could not reallocate %ld bytes", size);
263 #ifdef ENABLE_MEM_CHECK
267 p = ((guchar*) p + SIZEOF_LONG);
269 #endif /* ENABLE_MEM_CHECK */
271 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
275 p = ((guchar*) p + SIZEOF_LONG);
278 #ifdef ENABLE_MEM_PROFILE
280 allocations[size-1] += 1;
282 allocations[4095] += 1;
283 allocated_mem += size;
284 #endif /* ENABLE_MEM_PROFILE */
285 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
292 g_free (gpointer mem)
296 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
299 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
301 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
302 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
304 #ifdef ENABLE_MEM_PROFILE
306 #endif /* ENABLE_MEM_PROFILE */
308 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
310 #ifdef ENABLE_MEM_CHECK
311 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
313 g_warning ("freeing previously freed memory\n");
317 memset ((guchar*) mem + 8, 0, size);
318 #else /* ENABLE_MEM_CHECK */
320 #endif /* ENABLE_MEM_CHECK */
324 #endif /* ! USE_DMALLOC */
330 #ifdef ENABLE_MEM_PROFILE
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);
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 */
348 g_mem_check (gpointer mem)
350 #ifdef ENABLE_MEM_CHECK
353 t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
356 g_warning ("mem: 0x%08x has been freed: %lu\n", (gulong) mem, *t);
357 #endif /* ENABLE_MEM_CHECK */
361 g_mem_chunk_new (gchar *name,
366 GRealMemChunk *mem_chunk;
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;
381 if (mem_chunk->type == G_ALLOC_AND_FREE)
382 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
384 if (mem_chunk->atom_size % MEM_ALIGN)
385 mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
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;
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);
398 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
399 if (mem_chunk->area_size < mem_chunk->atom_size)
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);
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);
409 mem_chunk->next = mem_chunks;
410 mem_chunk->prev = NULL;
412 mem_chunks->prev = mem_chunk;
413 mem_chunks = mem_chunk;
415 return ((GMemChunk*) mem_chunk);
419 g_mem_chunk_destroy (GMemChunk *mem_chunk)
421 GRealMemChunk *rmem_chunk;
425 g_assert (mem_chunk != NULL);
427 rmem_chunk = (GRealMemChunk*) mem_chunk;
429 mem_areas = rmem_chunk->mem_areas;
432 temp_area = mem_areas;
433 mem_areas = mem_areas->next;
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;
442 if (rmem_chunk == mem_chunks)
443 mem_chunks = mem_chunks->next;
445 if (rmem_chunk->type == G_ALLOC_AND_FREE)
446 g_tree_destroy (rmem_chunk->mem_tree);
452 g_mem_chunk_alloc (GMemChunk *mem_chunk)
454 GRealMemChunk *rmem_chunk;
458 g_assert (mem_chunk != NULL);
460 rmem_chunk = (GRealMemChunk*) mem_chunk;
462 while (rmem_chunk->free_atoms)
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.
469 mem = rmem_chunk->free_atoms;
470 rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
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,
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"
487 * If there is already a "free_mem_area" then we'll just free this mem area.
491 /* Update the "free" memory available in that area */
492 temp_area->free += rmem_chunk->atom_size;
494 if (temp_area->free == rmem_chunk->area_size)
496 if (temp_area == rmem_chunk->mem_area)
497 rmem_chunk->mem_area = NULL;
499 if (rmem_chunk->free_mem_area)
501 rmem_chunk->num_mem_areas -= 1;
504 temp_area->next->prev = 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;
510 if (rmem_chunk->type == G_ALLOC_AND_FREE)
511 g_tree_remove (rmem_chunk->mem_tree, temp_area);
515 rmem_chunk->free_mem_area = temp_area;
517 rmem_chunk->num_marked_areas -= 1;
522 /* Update the number of allocated atoms count.
524 temp_area->allocated += 1;
526 /* The area wasn't marked...return the memory
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.
536 if ((!rmem_chunk->mem_area) ||
537 ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
539 if (rmem_chunk->free_mem_area)
541 rmem_chunk->mem_area = rmem_chunk->free_mem_area;
542 rmem_chunk->free_mem_area = NULL;
546 rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
548 rmem_chunk->area_size);
550 rmem_chunk->num_mem_areas += 1;
551 rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
552 rmem_chunk->mem_area->prev = NULL;
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;
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);
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;
568 /* Get the memory and modify the state variables appropriately.
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;
580 g_mem_chunk_free (GMemChunk *mem_chunk,
583 GRealMemChunk *rmem_chunk;
585 GFreeAtom *free_atom;
587 g_assert (mem_chunk != NULL);
588 g_assert (mem != NULL);
590 rmem_chunk = (GRealMemChunk*) mem_chunk;
592 /* Don't do anything if this is an ALLOC_ONLY chunk
594 if (rmem_chunk->type == G_ALLOC_AND_FREE)
596 /* Place the memory on the "free_atoms" list
598 free_atom = (GFreeAtom*) mem;
599 free_atom->next = rmem_chunk->free_atoms;
600 rmem_chunk->free_atoms = free_atom;
602 temp_area = g_tree_search (rmem_chunk->mem_tree,
603 (GSearchFunc) g_mem_chunk_area_search,
606 temp_area->allocated -= 1;
608 if (temp_area->allocated == 0)
611 rmem_chunk->num_marked_areas += 1;
616 /* This doesn't free the free_area if there is one */
618 g_mem_chunk_clean (GMemChunk *mem_chunk)
620 GRealMemChunk *rmem_chunk;
622 GFreeAtom *prev_free_atom;
623 GFreeAtom *temp_free_atom;
626 g_assert (mem_chunk != NULL);
628 rmem_chunk = (GRealMemChunk*) mem_chunk;
630 if (rmem_chunk->type == G_ALLOC_AND_FREE)
632 prev_free_atom = NULL;
633 temp_free_atom = rmem_chunk->free_atoms;
635 while (temp_free_atom)
637 mem = (gpointer) temp_free_atom;
639 mem_area = g_tree_search (rmem_chunk->mem_tree,
640 (GSearchFunc) g_mem_chunk_area_search,
643 /* If this mem area is marked for destruction then delete the
644 * area and list node and decrement the free mem.
649 prev_free_atom->next = temp_free_atom->next;
651 rmem_chunk->free_atoms = temp_free_atom->next;
652 temp_free_atom = temp_free_atom->next;
654 mem_area->free += rmem_chunk->atom_size;
655 if (mem_area->free == rmem_chunk->area_size)
657 rmem_chunk->num_mem_areas -= 1;
658 rmem_chunk->num_marked_areas -= 1;
661 mem_area->next->prev = 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;
669 if (rmem_chunk->type == G_ALLOC_AND_FREE)
670 g_tree_remove (rmem_chunk->mem_tree, mem_area);
676 prev_free_atom = temp_free_atom;
677 temp_free_atom = temp_free_atom->next;
684 g_mem_chunk_reset (GMemChunk *mem_chunk)
686 GRealMemChunk *rmem_chunk;
690 g_assert (mem_chunk != NULL);
692 rmem_chunk = (GRealMemChunk*) mem_chunk;
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;
701 temp_area = mem_areas;
702 mem_areas = mem_areas->next;
706 rmem_chunk->free_atoms = NULL;
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);
714 g_mem_chunk_print (GMemChunk *mem_chunk)
716 GRealMemChunk *rmem_chunk;
720 g_assert (mem_chunk != NULL);
722 rmem_chunk = (GRealMemChunk*) mem_chunk;
723 mem_areas = rmem_chunk->mem_areas;
728 mem += rmem_chunk->area_size - mem_areas->free;
729 mem_areas = mem_areas->next;
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);
738 g_mem_chunk_info (void)
740 GRealMemChunk *mem_chunk;
744 mem_chunk = mem_chunks;
748 mem_chunk = mem_chunk->next;
751 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks\n", count);
753 mem_chunk = mem_chunks;
756 g_mem_chunk_print ((GMemChunk*) mem_chunk);
757 mem_chunk = mem_chunk->next;
764 GRealMemChunk *mem_chunk;
766 mem_chunk = mem_chunks;
769 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
770 mem_chunk = mem_chunk->next;
776 g_mem_chunk_compute_size (gulong size)
782 while (power_of_2 < size)
785 lower = power_of_2 >> 1;
788 if ((size - lower) < (upper - size))
794 g_mem_chunk_area_compare (GMemArea *a,
797 return (a->mem - b->mem);
801 g_mem_chunk_area_search (GMemArea *a,
806 if (addr < &a->mem[a->index])