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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /* #define MEM_PROFILE */
24 /* #define 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;
91 static gulong allocations[4096] = { 0 };
92 static gulong allocated_mem = 0;
93 static gulong freed_mem = 0;
94 #endif /* MEM_PROFILE */
100 g_malloc (gulong size)
105 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
107 #endif /* MEM_PROFILE || MEM_CHECK */
114 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
116 #endif /* MEM_PROFILE || MEM_CHECK */
120 #endif /* MEM_CHECK */
123 p = (gpointer) malloc (size);
125 g_error ("could not allocate %ld bytes", size);
132 p = ((guchar*) p + SIZEOF_LONG);
134 #endif /* MEM_CHECK */
136 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
140 p = ((guchar*) p + SIZEOF_LONG);
145 allocations[size-1] += 1;
147 allocations[4095] += 1;
148 allocated_mem += size;
149 #endif /* MEM_PROFILE */
150 #endif /* MEM_PROFILE || MEM_CHECK */
157 g_malloc0 (gulong size)
162 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
164 #endif /* MEM_PROFILE || MEM_CHECK */
173 #endif /* MEM_PROFILE */
177 #endif /* MEM_CHECK */
180 p = (gpointer) calloc (size, 1);
182 g_error ("could not allocate %ld bytes", size);
189 p = ((guchar*) p + SIZEOF_LONG);
191 #endif /* MEM_CHECK */
193 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
197 p = ((guchar*) p + SIZEOF_LONG);
202 allocations[size-1] += 1;
204 allocations[4095] += 1;
205 allocated_mem += size;
206 #endif /* MEM_PROFILE */
207 #endif /* MEM_PROFILE */
214 g_realloc (gpointer mem,
219 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
221 #endif /* MEM_PROFILE || MEM_CHECK */
228 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
230 #endif /* MEM_PROFILE || MEM_CHECK */
234 #endif /* MEM_CHECK */
238 p = (gpointer) malloc (size);
241 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
242 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
245 #endif /* MEM_PROFILE */
247 #endif /* MEM_PROFILE || MEM_CHECK */
250 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
252 g_warning ("trying to realloc freed memory\n");
254 #endif /* MEM_CHECK */
256 p = (gpointer) realloc (mem, size);
260 g_error ("could not reallocate %ld bytes", size);
267 p = ((guchar*) p + SIZEOF_LONG);
269 #endif /* MEM_CHECK */
271 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
275 p = ((guchar*) p + SIZEOF_LONG);
280 allocations[size-1] += 1;
282 allocations[4095] += 1;
283 allocated_mem += size;
284 #endif /* MEM_PROFILE */
285 #endif /* MEM_PROFILE || MEM_CHECK */
292 g_free (gpointer mem)
296 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
299 #endif /* MEM_PROFILE || MEM_CHECK */
301 #if defined(MEM_PROFILE) || defined(MEM_CHECK)
302 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
306 #endif /* MEM_PROFILE */
308 #endif /* MEM_PROFILE || 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 /* MEM_CHECK */
320 #endif /* MEM_CHECK */
324 #endif /* ! USE_DMALLOC */
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);
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 */
346 g_mem_check (gpointer mem)
351 t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
354 g_warning ("mem: 0x%08x has been freed: %lu\n", (gulong) mem, *t);
355 #endif /* MEM_CHECK */
359 g_mem_chunk_new (gchar *name,
364 GRealMemChunk *mem_chunk;
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;
379 if (mem_chunk->type == G_ALLOC_AND_FREE)
380 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
382 if (mem_chunk->atom_size % MEM_ALIGN)
383 mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
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;
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);
396 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
397 if (mem_chunk->area_size < mem_chunk->atom_size)
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);
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);
407 mem_chunk->next = mem_chunks;
408 mem_chunk->prev = NULL;
410 mem_chunks->prev = mem_chunk;
411 mem_chunks = mem_chunk;
413 return ((GMemChunk*) mem_chunk);
417 g_mem_chunk_destroy (GMemChunk *mem_chunk)
419 GRealMemChunk *rmem_chunk;
423 g_assert (mem_chunk != NULL);
425 rmem_chunk = (GRealMemChunk*) mem_chunk;
427 mem_areas = rmem_chunk->mem_areas;
430 temp_area = mem_areas;
431 mem_areas = mem_areas->next;
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;
440 if (rmem_chunk == mem_chunks)
441 mem_chunks = mem_chunks->next;
443 if (rmem_chunk->type == G_ALLOC_AND_FREE)
444 g_tree_destroy (rmem_chunk->mem_tree);
450 g_mem_chunk_alloc (GMemChunk *mem_chunk)
452 GRealMemChunk *rmem_chunk;
456 g_assert (mem_chunk != NULL);
458 rmem_chunk = (GRealMemChunk*) mem_chunk;
460 while (rmem_chunk->free_atoms)
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.
467 mem = rmem_chunk->free_atoms;
468 rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
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,
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"
485 * If there is already a "free_mem_area" then we'll just free this mem area.
489 /* Update the "free" memory available in that area */
490 temp_area->free += rmem_chunk->atom_size;
492 if (temp_area->free == rmem_chunk->area_size)
494 if (temp_area == rmem_chunk->mem_area)
495 rmem_chunk->mem_area = NULL;
497 if (rmem_chunk->free_mem_area)
499 rmem_chunk->num_mem_areas -= 1;
500 rmem_chunk->num_marked_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;
508 if (temp_area == rmem_chunk->mem_area)
509 rmem_chunk->mem_area = NULL;
514 rmem_chunk->free_mem_area = temp_area;
519 /* Update the number of allocated atoms count.
521 temp_area->allocated += 1;
523 /* The area wasn't marked...return the memory
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.
533 if ((!rmem_chunk->mem_area) ||
534 ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
536 if (rmem_chunk->free_mem_area)
538 rmem_chunk->mem_area = rmem_chunk->free_mem_area;
539 rmem_chunk->free_mem_area = NULL;
543 rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
545 rmem_chunk->area_size);
547 rmem_chunk->num_mem_areas += 1;
548 rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
549 rmem_chunk->mem_area->prev = NULL;
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;
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);
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;
564 else if (rmem_chunk->free_mem_area)
566 rmem_chunk->num_mem_areas -= 1;
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;
575 if (rmem_chunk->type == G_ALLOC_AND_FREE)
576 g_tree_remove (rmem_chunk->mem_tree, rmem_chunk->free_mem_area);
578 g_free (rmem_chunk->free_mem_area);
579 rmem_chunk->free_mem_area = NULL;
582 /* Get the memory and modify the state variables appropriately.
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;
594 g_mem_chunk_free (GMemChunk *mem_chunk,
597 GRealMemChunk *rmem_chunk;
599 GFreeAtom *free_atom;
601 g_assert (mem_chunk != NULL);
602 g_assert (mem != NULL);
604 rmem_chunk = (GRealMemChunk*) mem_chunk;
606 /* Don't do anything if this is an ALLOC_ONLY chunk
608 if (rmem_chunk->type == G_ALLOC_AND_FREE)
610 /* Place the memory on the "free_atoms" list
612 free_atom = (GFreeAtom*) mem;
613 free_atom->next = rmem_chunk->free_atoms;
614 rmem_chunk->free_atoms = free_atom;
616 temp_area = g_tree_search (rmem_chunk->mem_tree,
617 (GSearchFunc) g_mem_chunk_area_search,
620 temp_area->allocated -= 1;
622 if (temp_area->allocated == 0)
625 rmem_chunk->num_marked_areas += 1;
627 g_mem_chunk_clean (mem_chunk);
633 g_mem_chunk_clean (GMemChunk *mem_chunk)
635 GRealMemChunk *rmem_chunk;
637 GFreeAtom *prev_free_atom;
638 GFreeAtom *temp_free_atom;
641 g_assert (mem_chunk != NULL);
643 rmem_chunk = (GRealMemChunk*) mem_chunk;
645 if (rmem_chunk->type == G_ALLOC_AND_FREE)
647 prev_free_atom = NULL;
648 temp_free_atom = rmem_chunk->free_atoms;
650 while (temp_free_atom)
652 mem = (gpointer) temp_free_atom;
654 mem_area = g_tree_search (rmem_chunk->mem_tree,
655 (GSearchFunc) g_mem_chunk_area_search,
658 /* If this mem area is marked for destruction then delete the
659 * area and list node and decrement the free mem.
664 prev_free_atom->next = temp_free_atom->next;
666 rmem_chunk->free_atoms = temp_free_atom->next;
667 temp_free_atom = temp_free_atom->next;
669 mem_area->free += rmem_chunk->atom_size;
670 if (mem_area->free == rmem_chunk->area_size)
672 rmem_chunk->num_mem_areas -= 1;
673 rmem_chunk->num_marked_areas -= 1;
676 mem_area->next->prev = 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;
684 if (rmem_chunk->type == G_ALLOC_AND_FREE)
685 g_tree_remove (rmem_chunk->mem_tree, mem_area);
691 prev_free_atom = temp_free_atom;
692 temp_free_atom = temp_free_atom->next;
699 g_mem_chunk_reset (GMemChunk *mem_chunk)
701 GRealMemChunk *rmem_chunk;
705 g_assert (mem_chunk != NULL);
707 rmem_chunk = (GRealMemChunk*) mem_chunk;
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;
716 temp_area = mem_areas;
717 mem_areas = mem_areas->next;
721 rmem_chunk->free_atoms = NULL;
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);
729 g_mem_chunk_print (GMemChunk *mem_chunk)
731 GRealMemChunk *rmem_chunk;
735 g_assert (mem_chunk != NULL);
737 rmem_chunk = (GRealMemChunk*) mem_chunk;
738 mem_areas = rmem_chunk->mem_areas;
743 mem += rmem_chunk->area_size - mem_areas->free;
744 mem_areas = mem_areas->next;
747 g_print ("%s: %ld bytes using %d mem areas", rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
753 GRealMemChunk *mem_chunk;
757 mem_chunk = mem_chunks;
761 mem_chunk = mem_chunk->next;
764 g_print ("%d mem chunks", count);
766 mem_chunk = mem_chunks;
769 g_mem_chunk_print ((GMemChunk*) mem_chunk);
770 mem_chunk = mem_chunk->next;
777 GRealMemChunk *mem_chunk;
779 mem_chunk = mem_chunks;
782 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
783 mem_chunk = mem_chunk->next;
789 g_mem_chunk_compute_size (gulong size)
795 while (power_of_2 < size)
798 lower = power_of_2 >> 1;
801 if ((size - lower) < (upper - size))
807 g_mem_chunk_area_compare (GMemArea *a,
810 return (a->mem - b->mem);
814 g_mem_chunk_area_search (GMemArea *a,
819 if (addr < &a->mem[a->index])