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.
24 /* #define MEM_PROFILE */
25 /* #define MEM_CHECK */
28 #define MAX_MEM_AREA 65536L
29 #define MEM_AREA_SIZE 4L
31 #if SIZEOF_VOID_P > SIZEOF_LONG
32 #define MEM_ALIGN SIZEOF_VOID_P
34 #define MEM_ALIGN SIZEOF_LONG
38 typedef struct _GFreeAtom GFreeAtom;
39 typedef struct _GMemArea GMemArea;
40 typedef struct _GRealMemChunk GRealMemChunk;
49 GMemArea *next; /* the next mem area */
50 GMemArea *prev; /* the previous mem area */
51 gulong index; /* the current index into the "mem" array */
52 gulong free; /* the number of free bytes in this mem area */
53 gulong allocated; /* the number of atoms allocated from this area */
54 gulong mark; /* is this mem area marked for deletion */
55 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
56 * the actual size of this array is determined by
57 * the mem chunk "area_size". ANSI says that it
58 * must be declared to be the maximum size it
59 * can possibly be (even though the actual size
66 gchar *name; /* name of this MemChunk...used for debugging output */
67 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
68 gint num_mem_areas; /* the number of memory areas */
69 gint num_marked_areas; /* the number of areas marked for deletion */
70 guint atom_size; /* the size of an atom */
71 gulong area_size; /* the size of a memory area */
72 GMemArea *mem_area; /* the current memory area */
73 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
74 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
75 GFreeAtom *free_atoms; /* the free atoms list */
76 GTree *mem_tree; /* tree of mem areas sorted by memory address */
77 GRealMemChunk *next; /* pointer to the next chunk */
78 GRealMemChunk *prev; /* pointer to the previous chunk */
82 static gulong g_mem_chunk_compute_size (gulong size);
83 static gint g_mem_chunk_area_compare (GMemArea *a,
85 static gint g_mem_chunk_area_search (GMemArea *a,
89 static GRealMemChunk *mem_chunks = NULL;
92 static gulong allocations[4096] = { 0 };
93 static gulong allocated_mem = 0;
94 static gulong freed_mem = 0;
95 #endif /* MEM_PROFILE */
101 g_malloc (gulong size)
106 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
108 #endif /* MEM_PROFILE || MEM_CHECK */
115 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
117 #endif /* MEM_PROFILE || MEM_CHECK */
121 #endif /* MEM_CHECK */
124 p = (gpointer) malloc (size);
126 g_error ("could not allocate %ld bytes", size);
133 p = ((guchar*) p + SIZEOF_LONG);
135 #endif /* MEM_CHECK */
137 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
141 p = ((guchar*) p + SIZEOF_LONG);
146 allocations[size-1] += 1;
148 allocations[4095] += 1;
149 allocated_mem += size;
150 #endif /* MEM_PROFILE */
151 #endif /* MEM_PROFILE || MEM_CHECK */
158 g_malloc0 (gulong size)
163 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
165 #endif /* MEM_PROFILE || MEM_CHECK */
174 #endif /* MEM_PROFILE */
178 #endif /* MEM_CHECK */
181 p = (gpointer) calloc (size, 1);
183 g_error ("could not allocate %ld bytes", size);
190 p = ((guchar*) p + SIZEOF_LONG);
192 #endif /* MEM_CHECK */
194 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
198 p = ((guchar*) p + SIZEOF_LONG);
203 allocations[size-1] += 1;
205 allocations[4095] += 1;
206 allocated_mem += size;
207 #endif /* MEM_PROFILE */
208 #endif /* MEM_PROFILE */
215 g_realloc (gpointer mem,
220 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
222 #endif /* MEM_PROFILE || MEM_CHECK */
229 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
231 #endif /* MEM_PROFILE || MEM_CHECK */
235 #endif /* MEM_CHECK */
239 p = (gpointer) malloc (size);
242 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
243 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
246 #endif /* MEM_PROFILE */
248 #endif /* MEM_PROFILE || MEM_CHECK */
251 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
253 g_warning ("trying to realloc freed memory\n");
255 #endif /* MEM_CHECK */
257 p = (gpointer) realloc (mem, size);
261 g_error ("could not reallocate %ld bytes", size);
268 p = ((guchar*) p + SIZEOF_LONG);
270 #endif /* MEM_CHECK */
272 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
276 p = ((guchar*) p + SIZEOF_LONG);
281 allocations[size-1] += 1;
283 allocations[4095] += 1;
284 allocated_mem += size;
285 #endif /* MEM_PROFILE */
286 #endif /* MEM_PROFILE || MEM_CHECK */
293 g_free (gpointer mem)
297 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
300 #endif /* MEM_PROFILE || MEM_CHECK */
302 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
303 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
307 #endif /* MEM_PROFILE */
309 #endif /* MEM_PROFILE || MEM_CHECK */
312 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
314 g_warning ("freeing previously freed memory\n");
318 memset ((guchar*) mem + 8, 0, size);
319 #else /* MEM_CHECK */
321 #endif /* MEM_CHECK */
325 #endif /* ! USE_DMALLOC */
334 for (i = 0; i < 4095; i++)
335 if (allocations[i] > 0)
336 g_print ("%lu allocations of %d bytes\n", allocations[i], i + 1);
338 if (allocations[4095] > 0)
339 g_print ("%lu allocations of greater than 4095 bytes\n", allocations[4095]);
340 g_print ("%lu bytes allocated\n", allocated_mem);
341 g_print ("%lu bytes freed\n", freed_mem);
342 g_print ("%lu bytes in use\n", allocated_mem - freed_mem);
343 #endif /* MEM_PROFILE */
347 g_mem_check (gpointer mem)
352 t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
355 g_warning ("mem: 0x%08x has been freed: %lu\n", (gulong) mem, *t);
356 #endif /* MEM_CHECK */
360 g_mem_chunk_new (gchar *name,
365 GRealMemChunk *mem_chunk;
368 mem_chunk = g_new (struct _GRealMemChunk, 1);
369 mem_chunk->name = name;
370 mem_chunk->type = type;
371 mem_chunk->num_mem_areas = 0;
372 mem_chunk->num_marked_areas = 0;
373 mem_chunk->mem_area = NULL;
374 mem_chunk->free_mem_area = NULL;
375 mem_chunk->free_atoms = NULL;
376 mem_chunk->mem_tree = NULL;
377 mem_chunk->mem_areas = NULL;
378 mem_chunk->atom_size = atom_size;
380 if (mem_chunk->type == G_ALLOC_AND_FREE)
381 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
383 if (mem_chunk->atom_size % MEM_ALIGN)
384 mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
386 mem_chunk->area_size = area_size;
387 if (mem_chunk->area_size > MAX_MEM_AREA)
388 mem_chunk->area_size = MAX_MEM_AREA;
389 while (mem_chunk->area_size < mem_chunk->atom_size)
390 mem_chunk->area_size *= 2;
392 rarea_size = mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
393 rarea_size = g_mem_chunk_compute_size (rarea_size);
394 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
397 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
398 if (mem_chunk->area_size < mem_chunk->atom_size)
400 mem_chunk->area_size = (mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE) * 2;
401 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
404 if (mem_chunk->area_size % mem_chunk->atom_size)
405 mem_chunk->area_size += mem_chunk->atom_size - (mem_chunk->area_size % mem_chunk->atom_size);
408 mem_chunk->next = mem_chunks;
409 mem_chunk->prev = NULL;
411 mem_chunks->prev = mem_chunk;
412 mem_chunks = mem_chunk;
414 return ((GMemChunk*) mem_chunk);
418 g_mem_chunk_destroy (GMemChunk *mem_chunk)
420 GRealMemChunk *rmem_chunk;
424 g_assert (mem_chunk != NULL);
426 rmem_chunk = (GRealMemChunk*) mem_chunk;
428 mem_areas = rmem_chunk->mem_areas;
431 temp_area = mem_areas;
432 mem_areas = mem_areas->next;
436 if (rmem_chunk->next)
437 rmem_chunk->next->prev = rmem_chunk->prev;
438 if (rmem_chunk->prev)
439 rmem_chunk->prev->next = rmem_chunk->next;
441 if (rmem_chunk == mem_chunks)
442 mem_chunks = mem_chunks->next;
444 if (rmem_chunk->type == G_ALLOC_AND_FREE)
445 g_tree_destroy (rmem_chunk->mem_tree);
451 g_mem_chunk_alloc (GMemChunk *mem_chunk)
453 GRealMemChunk *rmem_chunk;
457 g_assert (mem_chunk != NULL);
459 rmem_chunk = (GRealMemChunk*) mem_chunk;
461 while (rmem_chunk->free_atoms)
463 /* Get the first piece of memory on the "free_atoms" list.
464 * We can go ahead and destroy the list node we used to keep
465 * track of it with and to update the "free_atoms" list to
466 * point to its next element.
468 mem = rmem_chunk->free_atoms;
469 rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
471 /* Determine which area this piece of memory is allocated from */
472 temp_area = g_tree_search (rmem_chunk->mem_tree,
473 (GSearchFunc) g_mem_chunk_area_search,
476 /* If the area has been marked, then it is being destroyed.
477 * (ie marked to be destroyed).
478 * We check to see if all of the segments on the free list that
479 * reference this area have been removed. This occurs when
480 * the ammount of free memory is less than the allocatable size.
481 * If the chunk should be freed, then we place it in the "free_mem_area".
482 * This is so we make sure not to free the mem area here and then
483 * allocate it again a few lines down.
484 * If we don't allocate a chunk a few lines down then the "free_mem_area"
486 * If there is already a "free_mem_area" then we'll just free this mem area.
490 /* Update the "free" memory available in that area */
491 temp_area->free += rmem_chunk->atom_size;
493 if (temp_area->free == rmem_chunk->area_size)
495 if (temp_area == rmem_chunk->mem_area)
496 rmem_chunk->mem_area = NULL;
498 if (rmem_chunk->free_mem_area)
500 rmem_chunk->num_mem_areas -= 1;
503 temp_area->next->prev = 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;
509 if (rmem_chunk->type == G_ALLOC_AND_FREE)
510 g_tree_remove (rmem_chunk->mem_tree, temp_area);
514 rmem_chunk->free_mem_area = temp_area;
516 rmem_chunk->num_marked_areas -= 1;
521 /* Update the number of allocated atoms count.
523 temp_area->allocated += 1;
525 /* The area wasn't marked...return the memory
531 /* If there isn't a current mem area or the current mem area is out of space
532 * then allocate a new mem area. We'll first check and see if we can use
533 * the "free_mem_area". Otherwise we'll just malloc the mem area.
535 if ((!rmem_chunk->mem_area) ||
536 ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
538 if (rmem_chunk->free_mem_area)
540 rmem_chunk->mem_area = rmem_chunk->free_mem_area;
541 rmem_chunk->free_mem_area = NULL;
545 rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
547 rmem_chunk->area_size);
549 rmem_chunk->num_mem_areas += 1;
550 rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
551 rmem_chunk->mem_area->prev = NULL;
553 if (rmem_chunk->mem_areas)
554 rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
555 rmem_chunk->mem_areas = rmem_chunk->mem_area;
557 if (rmem_chunk->type == G_ALLOC_AND_FREE)
558 g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
561 rmem_chunk->mem_area->index = 0;
562 rmem_chunk->mem_area->free = rmem_chunk->area_size;
563 rmem_chunk->mem_area->allocated = 0;
564 rmem_chunk->mem_area->mark = 0;
567 /* Get the memory and modify the state variables appropriately.
569 mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
570 rmem_chunk->mem_area->index += rmem_chunk->atom_size;
571 rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
572 rmem_chunk->mem_area->allocated += 1;
579 g_mem_chunk_free (GMemChunk *mem_chunk,
582 GRealMemChunk *rmem_chunk;
584 GFreeAtom *free_atom;
586 g_assert (mem_chunk != NULL);
587 g_assert (mem != NULL);
589 rmem_chunk = (GRealMemChunk*) mem_chunk;
591 /* Don't do anything if this is an ALLOC_ONLY chunk
593 if (rmem_chunk->type == G_ALLOC_AND_FREE)
595 /* Place the memory on the "free_atoms" list
597 free_atom = (GFreeAtom*) mem;
598 free_atom->next = rmem_chunk->free_atoms;
599 rmem_chunk->free_atoms = free_atom;
601 temp_area = g_tree_search (rmem_chunk->mem_tree,
602 (GSearchFunc) g_mem_chunk_area_search,
605 temp_area->allocated -= 1;
607 if (temp_area->allocated == 0)
610 rmem_chunk->num_marked_areas += 1;
615 /* This doesn't free the free_area if there is one */
617 g_mem_chunk_clean (GMemChunk *mem_chunk)
619 GRealMemChunk *rmem_chunk;
621 GFreeAtom *prev_free_atom;
622 GFreeAtom *temp_free_atom;
625 g_assert (mem_chunk != NULL);
627 rmem_chunk = (GRealMemChunk*) mem_chunk;
629 if (rmem_chunk->type == G_ALLOC_AND_FREE)
631 prev_free_atom = NULL;
632 temp_free_atom = rmem_chunk->free_atoms;
634 while (temp_free_atom)
636 mem = (gpointer) temp_free_atom;
638 mem_area = g_tree_search (rmem_chunk->mem_tree,
639 (GSearchFunc) g_mem_chunk_area_search,
642 /* If this mem area is marked for destruction then delete the
643 * area and list node and decrement the free mem.
648 prev_free_atom->next = temp_free_atom->next;
650 rmem_chunk->free_atoms = temp_free_atom->next;
651 temp_free_atom = temp_free_atom->next;
653 mem_area->free += rmem_chunk->atom_size;
654 if (mem_area->free == rmem_chunk->area_size)
656 rmem_chunk->num_mem_areas -= 1;
657 rmem_chunk->num_marked_areas -= 1;
660 mem_area->next->prev = mem_area->prev;
662 mem_area->prev->next = mem_area->next;
663 if (mem_area == rmem_chunk->mem_areas)
664 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
665 if (mem_area == rmem_chunk->mem_area)
666 rmem_chunk->mem_area = NULL;
668 if (rmem_chunk->type == G_ALLOC_AND_FREE)
669 g_tree_remove (rmem_chunk->mem_tree, mem_area);
675 prev_free_atom = temp_free_atom;
676 temp_free_atom = temp_free_atom->next;
683 g_mem_chunk_reset (GMemChunk *mem_chunk)
685 GRealMemChunk *rmem_chunk;
689 g_assert (mem_chunk != NULL);
691 rmem_chunk = (GRealMemChunk*) mem_chunk;
693 mem_areas = rmem_chunk->mem_areas;
694 rmem_chunk->num_mem_areas = 0;
695 rmem_chunk->mem_areas = NULL;
696 rmem_chunk->mem_area = NULL;
700 temp_area = mem_areas;
701 mem_areas = mem_areas->next;
705 rmem_chunk->free_atoms = NULL;
707 if (rmem_chunk->mem_tree)
708 g_tree_destroy (rmem_chunk->mem_tree);
709 rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
713 g_mem_chunk_print (GMemChunk *mem_chunk)
715 GRealMemChunk *rmem_chunk;
719 g_assert (mem_chunk != NULL);
721 rmem_chunk = (GRealMemChunk*) mem_chunk;
722 mem_areas = rmem_chunk->mem_areas;
727 mem += rmem_chunk->area_size - mem_areas->free;
728 mem_areas = mem_areas->next;
731 g_print ("%s: %ld bytes using %d mem areas", rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
735 g_mem_chunk_info (void)
737 GRealMemChunk *mem_chunk;
741 mem_chunk = mem_chunks;
745 mem_chunk = mem_chunk->next;
748 g_print ("%d mem chunks", count);
750 mem_chunk = mem_chunks;
753 g_mem_chunk_print ((GMemChunk*) mem_chunk);
754 mem_chunk = mem_chunk->next;
761 GRealMemChunk *mem_chunk;
763 mem_chunk = mem_chunks;
766 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
767 mem_chunk = mem_chunk->next;
773 g_mem_chunk_compute_size (gulong size)
779 while (power_of_2 < size)
782 lower = power_of_2 >> 1;
785 if ((size - lower) < (upper - size))
791 g_mem_chunk_area_compare (GMemArea *a,
794 return (a->mem - b->mem);
798 g_mem_chunk_area_search (GMemArea *a,
803 if (addr < &a->mem[a->index])