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_PROFILE_EXCLUDES_MEM_CHUNKS */
25 /* #define ENABLE_MEM_CHECK */
27 #if defined(ENABLE_MEM_PROFILE) && defined(ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS)
28 #define ENTER_MEM_CHUNK_ROUTINE() allocating_for_mem_chunk++
29 #define LEAVE_MEM_CHUNK_ROUTINE() allocating_for_mem_chunk--
31 #define ENTER_MEM_CHUNK_ROUTINE()
32 #define LEAVE_MEM_CHUNK_ROUTINE()
36 #define MAX_MEM_AREA 65536L
37 #define MEM_AREA_SIZE 4L
39 #if SIZEOF_VOID_P > SIZEOF_LONG
40 #define MEM_ALIGN SIZEOF_VOID_P
42 #define MEM_ALIGN SIZEOF_LONG
46 typedef struct _GFreeAtom GFreeAtom;
47 typedef struct _GMemArea GMemArea;
48 typedef struct _GRealMemChunk GRealMemChunk;
57 GMemArea *next; /* the next mem area */
58 GMemArea *prev; /* the previous mem area */
59 gulong index; /* the current index into the "mem" array */
60 gulong free; /* the number of free bytes in this mem area */
61 gulong allocated; /* the number of atoms allocated from this area */
62 gulong mark; /* is this mem area marked for deletion */
63 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
64 * the actual size of this array is determined by
65 * the mem chunk "area_size". ANSI says that it
66 * must be declared to be the maximum size it
67 * can possibly be (even though the actual size
74 gchar *name; /* name of this MemChunk...used for debugging output */
75 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
76 gint num_mem_areas; /* the number of memory areas */
77 gint num_marked_areas; /* the number of areas marked for deletion */
78 guint atom_size; /* the size of an atom */
79 gulong area_size; /* the size of a memory area */
80 GMemArea *mem_area; /* the current memory area */
81 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
82 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
83 GFreeAtom *free_atoms; /* the free atoms list */
84 GTree *mem_tree; /* tree of mem areas sorted by memory address */
85 GRealMemChunk *next; /* pointer to the next chunk */
86 GRealMemChunk *prev; /* pointer to the previous chunk */
90 static gulong g_mem_chunk_compute_size (gulong size);
91 static gint g_mem_chunk_area_compare (GMemArea *a,
93 static gint g_mem_chunk_area_search (GMemArea *a,
97 static GRealMemChunk *mem_chunks = NULL;
99 #ifdef ENABLE_MEM_PROFILE
100 static gulong allocations[4096] = { 0 };
101 static gulong allocated_mem = 0;
102 static gulong freed_mem = 0;
103 static gint allocating_for_mem_chunk = 0;
104 #endif /* ENABLE_MEM_PROFILE */
110 g_malloc (gulong size)
115 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
117 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
124 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
126 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
128 #ifdef ENABLE_MEM_CHECK
130 #endif /* ENABLE_MEM_CHECK */
133 p = (gpointer) malloc (size);
135 g_error ("could not allocate %ld bytes", size);
138 #ifdef ENABLE_MEM_CHECK
142 p = ((guchar*) p + SIZEOF_LONG);
144 #endif /* ENABLE_MEM_CHECK */
146 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
150 p = ((guchar*) p + SIZEOF_LONG);
153 #ifdef ENABLE_MEM_PROFILE
154 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
155 if(!allocating_for_mem_chunk) {
158 allocations[size-1] += 1;
160 allocations[4095] += 1;
161 allocated_mem += size;
162 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
165 #endif /* ENABLE_MEM_PROFILE */
166 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
173 g_malloc0 (gulong size)
178 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
180 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
187 #ifdef ENABLE_MEM_PROFILE
189 #endif /* ENABLE_MEM_PROFILE */
191 #ifdef ENABLE_MEM_CHECK
193 #endif /* ENABLE_MEM_CHECK */
196 p = (gpointer) calloc (size, 1);
198 g_error ("could not allocate %ld bytes", size);
201 #ifdef ENABLE_MEM_CHECK
205 p = ((guchar*) p + SIZEOF_LONG);
207 #endif /* ENABLE_MEM_CHECK */
209 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
213 p = ((guchar*) p + SIZEOF_LONG);
216 #ifdef ENABLE_MEM_PROFILE
217 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
218 if(!allocating_for_mem_chunk) {
221 allocations[size-1] += 1;
223 allocations[4095] += 1;
224 allocated_mem += size;
225 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
228 #endif /* ENABLE_MEM_PROFILE */
229 #endif /* ENABLE_MEM_PROFILE */
236 g_realloc (gpointer mem,
241 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
243 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
250 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
252 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
254 #ifdef ENABLE_MEM_CHECK
256 #endif /* ENABLE_MEM_CHECK */
260 p = (gpointer) malloc (size);
263 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
264 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
265 #ifdef ENABLE_MEM_PROFILE
267 #endif /* ENABLE_MEM_PROFILE */
269 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
271 #ifdef ENABLE_MEM_CHECK
272 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
274 g_warning ("trying to realloc freed memory\n");
276 #endif /* ENABLE_MEM_CHECK */
278 p = (gpointer) realloc (mem, size);
282 g_error ("could not reallocate %ld bytes", size);
285 #ifdef ENABLE_MEM_CHECK
289 p = ((guchar*) p + SIZEOF_LONG);
291 #endif /* ENABLE_MEM_CHECK */
293 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
297 p = ((guchar*) p + SIZEOF_LONG);
300 #ifdef ENABLE_MEM_PROFILE
301 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
302 if(!allocating_for_mem_chunk) {
305 allocations[size-1] += 1;
307 allocations[4095] += 1;
308 allocated_mem += size;
309 #ifdef ENABLE_MEM_PROFILE_EXCLUDES_MEM_CHUNKS
312 #endif /* ENABLE_MEM_PROFILE */
313 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
320 g_free (gpointer mem)
324 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
327 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
329 #if defined(ENABLE_MEM_PROFILE) || defined(ENABLE_MEM_CHECK)
330 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
332 #ifdef ENABLE_MEM_PROFILE
334 #endif /* ENABLE_MEM_PROFILE */
336 #endif /* ENABLE_MEM_PROFILE || ENABLE_MEM_CHECK */
338 #ifdef ENABLE_MEM_CHECK
339 t = (gulong*) ((guchar*) mem - SIZEOF_LONG);
341 g_warning ("freeing previously freed memory\n");
345 memset ((guchar*) mem + 8, 0, size);
346 #else /* ENABLE_MEM_CHECK */
348 #endif /* ENABLE_MEM_CHECK */
352 #endif /* ! USE_DMALLOC */
358 #ifdef ENABLE_MEM_PROFILE
361 for (i = 0; i < 4095; i++)
362 if (allocations[i] > 0)
363 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
364 "%lu allocations of %d bytes\n", allocations[i], i + 1);
366 if (allocations[4095] > 0)
367 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
368 "%lu allocations of greater than 4095 bytes\n", allocations[4095]);
369 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes allocated\n", allocated_mem);
370 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes freed\n", freed_mem);
371 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%lu bytes in use\n", allocated_mem - freed_mem);
372 #endif /* ENABLE_MEM_PROFILE */
376 g_mem_check (gpointer mem)
378 #ifdef ENABLE_MEM_CHECK
381 t = (gulong*) ((guchar*) mem - SIZEOF_LONG - SIZEOF_LONG);
384 g_warning ("mem: 0x%08x has been freed: %lu\n", (gulong) mem, *t);
385 #endif /* ENABLE_MEM_CHECK */
389 g_mem_chunk_new (gchar *name,
394 GRealMemChunk *mem_chunk;
397 ENTER_MEM_CHUNK_ROUTINE();
399 mem_chunk = g_new (struct _GRealMemChunk, 1);
400 mem_chunk->name = name;
401 mem_chunk->type = type;
402 mem_chunk->num_mem_areas = 0;
403 mem_chunk->num_marked_areas = 0;
404 mem_chunk->mem_area = NULL;
405 mem_chunk->free_mem_area = NULL;
406 mem_chunk->free_atoms = NULL;
407 mem_chunk->mem_tree = NULL;
408 mem_chunk->mem_areas = NULL;
409 mem_chunk->atom_size = atom_size;
411 if (mem_chunk->type == G_ALLOC_AND_FREE)
412 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
414 if (mem_chunk->atom_size % MEM_ALIGN)
415 mem_chunk->atom_size += MEM_ALIGN - (mem_chunk->atom_size % MEM_ALIGN);
417 mem_chunk->area_size = area_size;
418 if (mem_chunk->area_size > MAX_MEM_AREA)
419 mem_chunk->area_size = MAX_MEM_AREA;
420 while (mem_chunk->area_size < mem_chunk->atom_size)
421 mem_chunk->area_size *= 2;
423 rarea_size = mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
424 rarea_size = g_mem_chunk_compute_size (rarea_size);
425 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
428 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
429 if (mem_chunk->area_size < mem_chunk->atom_size)
431 mem_chunk->area_size = (mem_chunk->area_size + sizeof (GMemArea) - MEM_AREA_SIZE) * 2;
432 mem_chunk->area_size -= (sizeof (GMemArea) - MEM_AREA_SIZE);
435 if (mem_chunk->area_size % mem_chunk->atom_size)
436 mem_chunk->area_size += mem_chunk->atom_size - (mem_chunk->area_size % mem_chunk->atom_size);
439 mem_chunk->next = mem_chunks;
440 mem_chunk->prev = NULL;
442 mem_chunks->prev = mem_chunk;
443 mem_chunks = mem_chunk;
445 LEAVE_MEM_CHUNK_ROUTINE();
447 return ((GMemChunk*) mem_chunk);
451 g_mem_chunk_destroy (GMemChunk *mem_chunk)
453 GRealMemChunk *rmem_chunk;
457 g_assert (mem_chunk != NULL);
459 ENTER_MEM_CHUNK_ROUTINE();
461 rmem_chunk = (GRealMemChunk*) mem_chunk;
463 mem_areas = rmem_chunk->mem_areas;
466 temp_area = mem_areas;
467 mem_areas = mem_areas->next;
471 if (rmem_chunk->next)
472 rmem_chunk->next->prev = rmem_chunk->prev;
473 if (rmem_chunk->prev)
474 rmem_chunk->prev->next = rmem_chunk->next;
476 if (rmem_chunk == mem_chunks)
477 mem_chunks = mem_chunks->next;
479 if (rmem_chunk->type == G_ALLOC_AND_FREE)
480 g_tree_destroy (rmem_chunk->mem_tree);
484 LEAVE_MEM_CHUNK_ROUTINE();
488 g_mem_chunk_alloc (GMemChunk *mem_chunk)
490 GRealMemChunk *rmem_chunk;
494 ENTER_MEM_CHUNK_ROUTINE();
496 g_assert (mem_chunk != NULL);
498 rmem_chunk = (GRealMemChunk*) mem_chunk;
500 while (rmem_chunk->free_atoms)
502 /* Get the first piece of memory on the "free_atoms" list.
503 * We can go ahead and destroy the list node we used to keep
504 * track of it with and to update the "free_atoms" list to
505 * point to its next element.
507 mem = rmem_chunk->free_atoms;
508 rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
510 /* Determine which area this piece of memory is allocated from */
511 temp_area = g_tree_search (rmem_chunk->mem_tree,
512 (GSearchFunc) g_mem_chunk_area_search,
515 /* If the area has been marked, then it is being destroyed.
516 * (ie marked to be destroyed).
517 * We check to see if all of the segments on the free list that
518 * reference this area have been removed. This occurs when
519 * the ammount of free memory is less than the allocatable size.
520 * If the chunk should be freed, then we place it in the "free_mem_area".
521 * This is so we make sure not to free the mem area here and then
522 * allocate it again a few lines down.
523 * If we don't allocate a chunk a few lines down then the "free_mem_area"
525 * If there is already a "free_mem_area" then we'll just free this mem area.
529 /* Update the "free" memory available in that area */
530 temp_area->free += rmem_chunk->atom_size;
532 if (temp_area->free == rmem_chunk->area_size)
534 if (temp_area == rmem_chunk->mem_area)
535 rmem_chunk->mem_area = NULL;
537 if (rmem_chunk->free_mem_area)
539 rmem_chunk->num_mem_areas -= 1;
542 temp_area->next->prev = temp_area->prev;
544 temp_area->prev->next = temp_area->next;
545 if (temp_area == rmem_chunk->mem_areas)
546 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
548 if (rmem_chunk->type == G_ALLOC_AND_FREE)
549 g_tree_remove (rmem_chunk->mem_tree, temp_area);
553 rmem_chunk->free_mem_area = temp_area;
555 rmem_chunk->num_marked_areas -= 1;
560 /* Update the number of allocated atoms count.
562 temp_area->allocated += 1;
564 /* The area wasn't marked...return the memory
570 /* If there isn't a current mem area or the current mem area is out of space
571 * then allocate a new mem area. We'll first check and see if we can use
572 * the "free_mem_area". Otherwise we'll just malloc the mem area.
574 if ((!rmem_chunk->mem_area) ||
575 ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
577 if (rmem_chunk->free_mem_area)
579 rmem_chunk->mem_area = rmem_chunk->free_mem_area;
580 rmem_chunk->free_mem_area = NULL;
584 rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
586 rmem_chunk->area_size);
588 rmem_chunk->num_mem_areas += 1;
589 rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
590 rmem_chunk->mem_area->prev = NULL;
592 if (rmem_chunk->mem_areas)
593 rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
594 rmem_chunk->mem_areas = rmem_chunk->mem_area;
596 if (rmem_chunk->type == G_ALLOC_AND_FREE)
597 g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
600 rmem_chunk->mem_area->index = 0;
601 rmem_chunk->mem_area->free = rmem_chunk->area_size;
602 rmem_chunk->mem_area->allocated = 0;
603 rmem_chunk->mem_area->mark = 0;
606 /* Get the memory and modify the state variables appropriately.
608 mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
609 rmem_chunk->mem_area->index += rmem_chunk->atom_size;
610 rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
611 rmem_chunk->mem_area->allocated += 1;
615 LEAVE_MEM_CHUNK_ROUTINE();
621 g_mem_chunk_free (GMemChunk *mem_chunk,
624 GRealMemChunk *rmem_chunk;
626 GFreeAtom *free_atom;
628 g_assert (mem_chunk != NULL);
629 g_assert (mem != NULL);
631 ENTER_MEM_CHUNK_ROUTINE();
633 rmem_chunk = (GRealMemChunk*) mem_chunk;
635 /* Don't do anything if this is an ALLOC_ONLY chunk
637 if (rmem_chunk->type == G_ALLOC_AND_FREE)
639 /* Place the memory on the "free_atoms" list
641 free_atom = (GFreeAtom*) mem;
642 free_atom->next = rmem_chunk->free_atoms;
643 rmem_chunk->free_atoms = free_atom;
645 temp_area = g_tree_search (rmem_chunk->mem_tree,
646 (GSearchFunc) g_mem_chunk_area_search,
649 temp_area->allocated -= 1;
651 if (temp_area->allocated == 0)
654 rmem_chunk->num_marked_areas += 1;
658 LEAVE_MEM_CHUNK_ROUTINE();
661 /* This doesn't free the free_area if there is one */
663 g_mem_chunk_clean (GMemChunk *mem_chunk)
665 GRealMemChunk *rmem_chunk;
667 GFreeAtom *prev_free_atom;
668 GFreeAtom *temp_free_atom;
671 g_assert (mem_chunk != NULL);
673 rmem_chunk = (GRealMemChunk*) mem_chunk;
675 if (rmem_chunk->type == G_ALLOC_AND_FREE)
677 prev_free_atom = NULL;
678 temp_free_atom = rmem_chunk->free_atoms;
680 while (temp_free_atom)
682 mem = (gpointer) temp_free_atom;
684 mem_area = g_tree_search (rmem_chunk->mem_tree,
685 (GSearchFunc) g_mem_chunk_area_search,
688 /* If this mem area is marked for destruction then delete the
689 * area and list node and decrement the free mem.
694 prev_free_atom->next = temp_free_atom->next;
696 rmem_chunk->free_atoms = temp_free_atom->next;
697 temp_free_atom = temp_free_atom->next;
699 mem_area->free += rmem_chunk->atom_size;
700 if (mem_area->free == rmem_chunk->area_size)
702 rmem_chunk->num_mem_areas -= 1;
703 rmem_chunk->num_marked_areas -= 1;
706 mem_area->next->prev = mem_area->prev;
708 mem_area->prev->next = mem_area->next;
709 if (mem_area == rmem_chunk->mem_areas)
710 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
711 if (mem_area == rmem_chunk->mem_area)
712 rmem_chunk->mem_area = NULL;
714 if (rmem_chunk->type == G_ALLOC_AND_FREE)
715 g_tree_remove (rmem_chunk->mem_tree, mem_area);
721 prev_free_atom = temp_free_atom;
722 temp_free_atom = temp_free_atom->next;
729 g_mem_chunk_reset (GMemChunk *mem_chunk)
731 GRealMemChunk *rmem_chunk;
735 g_assert (mem_chunk != NULL);
737 rmem_chunk = (GRealMemChunk*) mem_chunk;
739 mem_areas = rmem_chunk->mem_areas;
740 rmem_chunk->num_mem_areas = 0;
741 rmem_chunk->mem_areas = NULL;
742 rmem_chunk->mem_area = NULL;
746 temp_area = mem_areas;
747 mem_areas = mem_areas->next;
751 rmem_chunk->free_atoms = NULL;
753 if (rmem_chunk->mem_tree)
754 g_tree_destroy (rmem_chunk->mem_tree);
755 rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
759 g_mem_chunk_print (GMemChunk *mem_chunk)
761 GRealMemChunk *rmem_chunk;
765 g_assert (mem_chunk != NULL);
767 rmem_chunk = (GRealMemChunk*) mem_chunk;
768 mem_areas = rmem_chunk->mem_areas;
773 mem += rmem_chunk->area_size - mem_areas->free;
774 mem_areas = mem_areas->next;
777 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
778 "%s: %ld bytes using %d mem areas\n",
779 rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
783 g_mem_chunk_info (void)
785 GRealMemChunk *mem_chunk;
789 mem_chunk = mem_chunks;
793 mem_chunk = mem_chunk->next;
796 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks\n", count);
798 mem_chunk = mem_chunks;
801 g_mem_chunk_print ((GMemChunk*) mem_chunk);
802 mem_chunk = mem_chunk->next;
809 GRealMemChunk *mem_chunk;
811 mem_chunk = mem_chunks;
814 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
815 mem_chunk = mem_chunk->next;
821 g_mem_chunk_compute_size (gulong size)
827 while (power_of_2 < size)
830 lower = power_of_2 >> 1;
833 if ((size - lower) < (upper - size))
839 g_mem_chunk_area_compare (GMemArea *a,
842 return (a->mem - b->mem);
846 g_mem_chunk_area_search (GMemArea *a,
851 if (addr < &a->mem[a->index])