Add test for g_path_skip_root().
[glib.git] / gmem.c
blobb0909a0dc126a20f1694f77274b76d014bf6e88f
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 Lesser 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 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser 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.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 /*
28 * MT safe
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
35 #include <stdlib.h>
36 #include <string.h>
37 #include "glib.h"
40 /* notes on macros:
41 * having DISABLE_MEM_POOLS defined, disables mem_chunks alltogether, their
42 * allocations are performed through ordinary g_malloc/g_free.
43 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
44 * g_mem_profile().
45 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
46 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
47 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
48 * if ENABLE_GC_FRIENDLY is defined, freed memory should be 0-wiped.
51 #define MEM_PROFILE_TABLE_SIZE 4096
53 #define MEM_AREA_SIZE 4L
55 #ifdef G_DISABLE_CHECKS
56 # define ENTER_MEM_CHUNK_ROUTINE()
57 # define LEAVE_MEM_CHUNK_ROUTINE()
58 # define IN_MEM_CHUNK_ROUTINE() FALSE
59 #else /* !G_DISABLE_CHECKS */
60 static GPrivate* mem_chunk_recursion = NULL;
61 # define MEM_CHUNK_ROUTINE_COUNT() GPOINTER_TO_UINT (g_private_get (mem_chunk_recursion))
62 # define ENTER_MEM_CHUNK_ROUTINE() g_private_set (mem_chunk_recursion, GUINT_TO_POINTER (MEM_CHUNK_ROUTINE_COUNT () + 1))
63 # define LEAVE_MEM_CHUNK_ROUTINE() g_private_set (mem_chunk_recursion, GUINT_TO_POINTER (MEM_CHUNK_ROUTINE_COUNT () - 1))
64 #endif /* !G_DISABLE_CHECKS */
66 #ifndef REALLOC_0_WORKS
67 static gpointer
68 standard_realloc (gpointer mem,
69 gsize n_bytes)
71 if (!mem)
72 return malloc (n_bytes);
73 else
74 return realloc (mem, n_bytes);
76 #endif /* !REALLOC_0_WORKS */
78 #ifdef SANE_MALLOC_PROTOS
79 # define standard_malloc malloc
80 # ifdef REALLOC_0_WORKS
81 # define standard_realloc realloc
82 # endif /* REALLOC_0_WORKS */
83 # define standard_free free
84 # define standard_calloc calloc
85 # define standard_try_malloc malloc
86 # define standard_try_realloc realloc
87 #else /* !SANE_MALLOC_PROTOS */
88 static gpointer
89 standard_malloc (gsize n_bytes)
91 return malloc (n_bytes);
93 # ifdef REALLOC_0_WORKS
94 static gpointer
95 standard_realloc (gpointer mem,
96 gsize n_bytes)
98 return realloc (mem, n_bytes);
100 # endif /* REALLOC_0_WORKS */
101 static void
102 standard_free (gpointer mem)
104 return free (mem);
106 static gpointer
107 standard_calloc (gsize n_blocks,
108 gsize n_bytes)
110 return calloc (n_blocks, n_bytes);
112 #define standard_try_malloc standard_malloc
113 #define standard_try_realloc standard_realloc
114 #endif /* !SANE_MALLOC_PROTOS */
117 /* --- variables --- */
118 static GMemVTable glib_mem_vtable = {
119 standard_malloc,
120 standard_realloc,
121 standard_free,
122 standard_calloc,
123 standard_try_malloc,
124 standard_try_realloc,
128 /* --- functions --- */
129 gpointer
130 g_malloc (gulong n_bytes)
132 if (n_bytes)
134 gpointer mem;
136 mem = glib_mem_vtable.malloc (n_bytes);
137 if (mem)
138 return mem;
140 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
143 return NULL;
146 gpointer
147 g_malloc0 (gulong n_bytes)
149 if (n_bytes)
151 gpointer mem;
153 mem = glib_mem_vtable.calloc (1, n_bytes);
154 if (mem)
155 return mem;
157 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
160 return NULL;
163 gpointer
164 g_realloc (gpointer mem,
165 gulong n_bytes)
167 if (n_bytes)
169 mem = glib_mem_vtable.realloc (mem, n_bytes);
170 if (mem)
171 return mem;
173 g_error ("%s: failed to allocate %lu bytes", G_STRLOC, n_bytes);
176 if (mem)
177 glib_mem_vtable.free (mem);
179 return NULL;
182 void
183 g_free (gpointer mem)
185 if (mem)
186 glib_mem_vtable.free (mem);
189 gpointer
190 g_try_malloc (gulong n_bytes)
192 if (n_bytes)
193 return glib_mem_vtable.try_malloc (n_bytes);
194 else
195 return NULL;
198 gpointer
199 g_try_realloc (gpointer mem,
200 gulong n_bytes)
202 if (n_bytes)
203 return glib_mem_vtable.try_realloc (mem, n_bytes);
205 if (mem)
206 glib_mem_vtable.free (mem);
208 return NULL;
211 static gpointer
212 fallback_calloc (gsize n_blocks,
213 gsize n_block_bytes)
215 gsize l = n_blocks * n_block_bytes;
216 gpointer mem = glib_mem_vtable.malloc (l);
218 if (mem)
219 memset (mem, 0, l);
221 return mem;
224 void
225 g_mem_set_vtable (GMemVTable *vtable)
227 gboolean vtable_set = FALSE;
229 if (!vtable_set)
231 vtable_set |= TRUE;
232 if (vtable->malloc && vtable->realloc && vtable->free)
234 glib_mem_vtable.malloc = vtable->malloc;
235 glib_mem_vtable.realloc = vtable->realloc;
236 glib_mem_vtable.free = vtable->free;
237 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
238 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
239 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
241 else
242 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
244 else
245 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
249 /* --- memory profiling and checking --- */
250 #ifdef G_DISABLE_CHECKS
251 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
252 void
253 g_mem_profile (void)
256 #else /* !G_DISABLE_CHECKS */
257 typedef enum {
258 PROFILER_FREE = 0,
259 PROFILER_ALLOC = 1,
260 PROFILER_RELOC = 2,
261 PROFILER_ZINIT = 4
262 } ProfilerJob;
263 static guint *profile_data = NULL;
264 static gulong profile_allocs = 0;
265 static gulong profile_mc_allocs = 0;
266 static gulong profile_zinit = 0;
267 static gulong profile_frees = 0;
268 static gulong profile_mc_frees = 0;
269 G_LOCK_DEFINE_STATIC (g_profile_mutex);
270 #ifdef G_ENABLE_DEBUG
271 static volatile gulong glib_trap_free_size = 0;
272 static volatile gulong glib_trap_realloc_size = 0;
273 static volatile gulong glib_trap_malloc_size = 0;
274 #endif /* G_ENABLE_DEBUG */
276 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
278 static void
279 profiler_log (ProfilerJob job,
280 gulong n_bytes,
281 gboolean success)
283 G_LOCK (g_profile_mutex);
284 if (!profile_data)
286 profile_data = standard_malloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
287 if (!profile_data) /* memory system kiddin' me, eh? */
289 G_UNLOCK (g_profile_mutex);
290 return;
294 if (MEM_CHUNK_ROUTINE_COUNT () == 0)
296 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
297 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
298 (job & PROFILER_RELOC) != 0,
299 success != 0)] += 1;
300 else
301 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
302 (job & PROFILER_RELOC) != 0,
303 success != 0)] += 1;
304 if (success)
306 if (job & PROFILER_ALLOC)
308 profile_allocs += n_bytes;
309 if (job & PROFILER_ZINIT)
310 profile_zinit += n_bytes;
312 else
313 profile_frees += n_bytes;
316 else if (success)
318 if (job & PROFILER_ALLOC)
319 profile_mc_allocs += n_bytes;
320 else
321 profile_mc_frees += n_bytes;
323 G_UNLOCK (g_profile_mutex);
326 static void
327 profile_print_locked (guint *local_data,
328 gboolean success)
330 gboolean need_header = TRUE;
331 guint i;
333 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
335 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
336 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
337 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
338 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
340 if (!t_malloc && !t_realloc && !t_free && !t_refree)
341 continue;
342 else if (need_header)
344 need_header = FALSE;
345 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
346 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
347 g_print (" | malloc() | free() | realloc() | realloc() | \n");
348 g_print ("===========|============|============|============|============|===========\n");
350 if (i < MEM_PROFILE_TABLE_SIZE)
351 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
352 i, t_malloc, t_free, t_realloc, t_refree,
353 (t_malloc - t_free + t_realloc - t_refree) * i);
354 else if (i >= MEM_PROFILE_TABLE_SIZE)
355 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
356 i, t_malloc, t_free, t_realloc, t_refree);
358 if (need_header)
359 g_print (" --- none ---\n");
362 void
363 g_mem_profile (void)
365 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
366 gulong local_allocs = profile_allocs;
367 gulong local_zinit = profile_zinit;
368 gulong local_frees = profile_frees;
369 gulong local_mc_allocs = profile_mc_allocs;
370 gulong local_mc_frees = profile_mc_frees;
372 G_LOCK (g_profile_mutex);
374 if (!profile_data)
376 G_UNLOCK (g_profile_mutex);
377 return;
380 memcpy (local_data, profile_data, (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
382 g_print ("GLib Memory statistics (successful operations):\n");
383 profile_print_locked (local_data, TRUE);
384 g_print ("GLib Memory statistics (failing operations):\n");
385 profile_print_locked (local_data, FALSE);
386 g_print ("Total bytes: allocated=%lu, zero-initialized=%lu (%.2f%%), freed=%lu (%.2f%%), remaining=%lu\n",
387 local_allocs,
388 local_zinit,
389 ((gdouble) local_zinit) / local_allocs * 100.0,
390 local_frees,
391 ((gdouble) local_frees) / local_allocs * 100.0,
392 local_allocs - local_frees);
393 g_print ("MemChunk bytes: allocated=%lu, freed=%lu (%.2f%%), remaining=%lu\n",
394 local_mc_allocs,
395 local_mc_frees,
396 ((gdouble) local_mc_frees) / local_mc_allocs * 100.0,
397 local_mc_allocs - local_mc_frees);
398 G_UNLOCK (g_profile_mutex);
401 static gpointer
402 profiler_try_malloc (gsize n_bytes)
404 gulong *p;
406 #ifdef G_ENABLE_DEBUG
407 if (glib_trap_malloc_size == n_bytes)
408 G_BREAKPOINT ();
409 #endif /* G_ENABLE_DEBUG */
411 p = standard_malloc (sizeof (gulong) * 2 + n_bytes);
413 if (p)
415 p[0] = 0; /* free count */
416 p[1] = n_bytes; /* length */
417 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
418 p += 2;
420 else
421 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
423 return p;
426 static gpointer
427 profiler_malloc (gsize n_bytes)
429 gpointer mem = profiler_try_malloc (n_bytes);
431 if (!mem)
432 g_mem_profile ();
434 return mem;
437 static gpointer
438 profiler_calloc (gsize n_blocks,
439 gsize n_block_bytes)
441 gsize l = n_blocks * n_block_bytes;
442 gulong *p;
444 #ifdef G_ENABLE_DEBUG
445 if (glib_trap_malloc_size == l)
446 G_BREAKPOINT ();
447 #endif /* G_ENABLE_DEBUG */
449 p = standard_calloc (1, sizeof (gulong) * 2 + l);
451 if (p)
453 p[0] = 0; /* free count */
454 p[1] = l; /* length */
455 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
456 p += 2;
458 else
460 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
461 g_mem_profile ();
464 return p;
467 static void
468 profiler_free (gpointer mem)
470 gulong *p = mem;
472 p -= 2;
473 if (p[0]) /* free count */
475 g_warning ("free(%p): memory has been freed %lu times already", p + 2, p[0]);
476 profiler_log (PROFILER_FREE,
477 p[1], /* length */
478 FALSE);
480 else
482 #ifdef G_ENABLE_DEBUG
483 if (glib_trap_free_size == p[1])
484 G_BREAKPOINT ();
485 #endif /* G_ENABLE_DEBUG */
487 profiler_log (PROFILER_FREE,
488 p[1], /* length */
489 TRUE);
490 memset (p + 2, 0xaa, p[1]);
492 /* for all those that miss standard_free (p); in this place, yes,
493 * we do leak all memory when profiling, and that is intentional
494 * to catch double frees. patch submissions are futile.
497 p[0] += 1;
500 static gpointer
501 profiler_try_realloc (gpointer mem,
502 gsize n_bytes)
504 gulong *p = mem;
506 p -= 2;
508 #ifdef G_ENABLE_DEBUG
509 if (glib_trap_realloc_size == n_bytes)
510 G_BREAKPOINT ();
511 #endif /* G_ENABLE_DEBUG */
513 if (mem && p[0]) /* free count */
515 g_warning ("realloc(%p, %u): memory has been freed %lu times already", p + 2, n_bytes, p[0]);
516 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
518 return NULL;
520 else
522 p = standard_realloc (mem ? p : NULL, sizeof (gulong) * 2 + n_bytes);
524 if (p)
526 if (mem)
527 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
528 p[0] = 0;
529 p[1] = n_bytes;
530 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
531 p += 2;
533 else
534 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
536 return p;
540 static gpointer
541 profiler_realloc (gpointer mem,
542 gsize n_bytes)
544 mem = profiler_try_realloc (mem, n_bytes);
546 if (!mem)
547 g_mem_profile ();
549 return mem;
552 static GMemVTable profiler_table = {
553 profiler_malloc,
554 profiler_realloc,
555 profiler_free,
556 profiler_calloc,
557 profiler_try_malloc,
558 profiler_try_realloc,
560 GMemVTable *glib_mem_profiler_table = &profiler_table;
562 #endif /* !G_DISABLE_CHECKS */
565 /* --- MemChunks --- */
566 typedef struct _GFreeAtom GFreeAtom;
567 typedef struct _GMemArea GMemArea;
568 typedef struct _GRealMemChunk GRealMemChunk;
570 struct _GFreeAtom
572 GFreeAtom *next;
575 struct _GMemArea
577 GMemArea *next; /* the next mem area */
578 GMemArea *prev; /* the previous mem area */
579 gulong index; /* the current index into the "mem" array */
580 gulong free; /* the number of free bytes in this mem area */
581 gulong allocated; /* the number of atoms allocated from this area */
582 gulong mark; /* is this mem area marked for deletion */
583 gchar mem[MEM_AREA_SIZE]; /* the mem array from which atoms get allocated
584 * the actual size of this array is determined by
585 * the mem chunk "area_size". ANSI says that it
586 * must be declared to be the maximum size it
587 * can possibly be (even though the actual size
588 * may be less).
592 struct _GRealMemChunk
594 gchar *name; /* name of this MemChunk...used for debugging output */
595 gint type; /* the type of MemChunk: ALLOC_ONLY or ALLOC_AND_FREE */
596 gint num_mem_areas; /* the number of memory areas */
597 gint num_marked_areas; /* the number of areas marked for deletion */
598 guint atom_size; /* the size of an atom */
599 gulong area_size; /* the size of a memory area */
600 GMemArea *mem_area; /* the current memory area */
601 GMemArea *mem_areas; /* a list of all the mem areas owned by this chunk */
602 GMemArea *free_mem_area; /* the free area...which is about to be destroyed */
603 GFreeAtom *free_atoms; /* the free atoms list */
604 GTree *mem_tree; /* tree of mem areas sorted by memory address */
605 GRealMemChunk *next; /* pointer to the next chunk */
606 GRealMemChunk *prev; /* pointer to the previous chunk */
610 #ifndef DISABLE_MEM_POOLS
611 static gulong g_mem_chunk_compute_size (gulong size,
612 gulong min_size) G_GNUC_CONST;
613 static gint g_mem_chunk_area_compare (GMemArea *a,
614 GMemArea *b);
615 static gint g_mem_chunk_area_search (GMemArea *a,
616 gchar *addr);
618 /* here we can't use StaticMutexes, as they depend upon a working
619 * g_malloc, the same holds true for StaticPrivate
621 static GMutex *mem_chunks_lock = NULL;
622 static GRealMemChunk *mem_chunks = NULL;
624 GMemChunk*
625 g_mem_chunk_new (gchar *name,
626 gint atom_size,
627 gulong area_size,
628 gint type)
630 GRealMemChunk *mem_chunk;
631 gulong rarea_size;
633 g_return_val_if_fail (atom_size > 0, NULL);
634 g_return_val_if_fail (area_size >= atom_size, NULL);
636 ENTER_MEM_CHUNK_ROUTINE ();
638 area_size = (area_size + atom_size - 1) / atom_size;
639 area_size *= atom_size;
641 mem_chunk = g_new (struct _GRealMemChunk, 1);
642 mem_chunk->name = name;
643 mem_chunk->type = type;
644 mem_chunk->num_mem_areas = 0;
645 mem_chunk->num_marked_areas = 0;
646 mem_chunk->mem_area = NULL;
647 mem_chunk->free_mem_area = NULL;
648 mem_chunk->free_atoms = NULL;
649 mem_chunk->mem_tree = NULL;
650 mem_chunk->mem_areas = NULL;
651 mem_chunk->atom_size = atom_size;
653 if (mem_chunk->type == G_ALLOC_AND_FREE)
654 mem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
656 if (mem_chunk->atom_size % G_MEM_ALIGN)
657 mem_chunk->atom_size += G_MEM_ALIGN - (mem_chunk->atom_size % G_MEM_ALIGN);
659 rarea_size = area_size + sizeof (GMemArea) - MEM_AREA_SIZE;
660 rarea_size = g_mem_chunk_compute_size (rarea_size, atom_size + sizeof (GMemArea) - MEM_AREA_SIZE);
661 mem_chunk->area_size = rarea_size - (sizeof (GMemArea) - MEM_AREA_SIZE);
663 g_mutex_lock (mem_chunks_lock);
664 mem_chunk->next = mem_chunks;
665 mem_chunk->prev = NULL;
666 if (mem_chunks)
667 mem_chunks->prev = mem_chunk;
668 mem_chunks = mem_chunk;
669 g_mutex_unlock (mem_chunks_lock);
671 LEAVE_MEM_CHUNK_ROUTINE ();
673 return ((GMemChunk*) mem_chunk);
676 void
677 g_mem_chunk_destroy (GMemChunk *mem_chunk)
679 GRealMemChunk *rmem_chunk;
680 GMemArea *mem_areas;
681 GMemArea *temp_area;
683 g_return_if_fail (mem_chunk != NULL);
685 ENTER_MEM_CHUNK_ROUTINE ();
687 rmem_chunk = (GRealMemChunk*) mem_chunk;
689 mem_areas = rmem_chunk->mem_areas;
690 while (mem_areas)
692 temp_area = mem_areas;
693 mem_areas = mem_areas->next;
694 g_free (temp_area);
697 if (rmem_chunk->next)
698 rmem_chunk->next->prev = rmem_chunk->prev;
699 if (rmem_chunk->prev)
700 rmem_chunk->prev->next = rmem_chunk->next;
702 g_mutex_lock (mem_chunks_lock);
703 if (rmem_chunk == mem_chunks)
704 mem_chunks = mem_chunks->next;
705 g_mutex_unlock (mem_chunks_lock);
707 if (rmem_chunk->type == G_ALLOC_AND_FREE)
708 g_tree_destroy (rmem_chunk->mem_tree);
710 g_free (rmem_chunk);
712 LEAVE_MEM_CHUNK_ROUTINE ();
715 gpointer
716 g_mem_chunk_alloc (GMemChunk *mem_chunk)
718 GRealMemChunk *rmem_chunk;
719 GMemArea *temp_area;
720 gpointer mem;
722 ENTER_MEM_CHUNK_ROUTINE ();
724 g_return_val_if_fail (mem_chunk != NULL, NULL);
726 rmem_chunk = (GRealMemChunk*) mem_chunk;
728 while (rmem_chunk->free_atoms)
730 /* Get the first piece of memory on the "free_atoms" list.
731 * We can go ahead and destroy the list node we used to keep
732 * track of it with and to update the "free_atoms" list to
733 * point to its next element.
735 mem = rmem_chunk->free_atoms;
736 rmem_chunk->free_atoms = rmem_chunk->free_atoms->next;
738 /* Determine which area this piece of memory is allocated from */
739 temp_area = g_tree_search (rmem_chunk->mem_tree,
740 (GCompareFunc) g_mem_chunk_area_search,
741 mem);
743 /* If the area has been marked, then it is being destroyed.
744 * (ie marked to be destroyed).
745 * We check to see if all of the segments on the free list that
746 * reference this area have been removed. This occurs when
747 * the ammount of free memory is less than the allocatable size.
748 * If the chunk should be freed, then we place it in the "free_mem_area".
749 * This is so we make sure not to free the mem area here and then
750 * allocate it again a few lines down.
751 * If we don't allocate a chunk a few lines down then the "free_mem_area"
752 * will be freed.
753 * If there is already a "free_mem_area" then we'll just free this mem area.
755 if (temp_area->mark)
757 /* Update the "free" memory available in that area */
758 temp_area->free += rmem_chunk->atom_size;
760 if (temp_area->free == rmem_chunk->area_size)
762 if (temp_area == rmem_chunk->mem_area)
763 rmem_chunk->mem_area = NULL;
765 if (rmem_chunk->free_mem_area)
767 rmem_chunk->num_mem_areas -= 1;
769 if (temp_area->next)
770 temp_area->next->prev = temp_area->prev;
771 if (temp_area->prev)
772 temp_area->prev->next = temp_area->next;
773 if (temp_area == rmem_chunk->mem_areas)
774 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
776 if (rmem_chunk->type == G_ALLOC_AND_FREE)
777 g_tree_remove (rmem_chunk->mem_tree, temp_area);
778 g_free (temp_area);
780 else
781 rmem_chunk->free_mem_area = temp_area;
783 rmem_chunk->num_marked_areas -= 1;
786 else
788 /* Update the number of allocated atoms count.
790 temp_area->allocated += 1;
792 /* The area wasn't marked...return the memory
794 goto outa_here;
798 /* If there isn't a current mem area or the current mem area is out of space
799 * then allocate a new mem area. We'll first check and see if we can use
800 * the "free_mem_area". Otherwise we'll just malloc the mem area.
802 if ((!rmem_chunk->mem_area) ||
803 ((rmem_chunk->mem_area->index + rmem_chunk->atom_size) > rmem_chunk->area_size))
805 if (rmem_chunk->free_mem_area)
807 rmem_chunk->mem_area = rmem_chunk->free_mem_area;
808 rmem_chunk->free_mem_area = NULL;
810 else
812 #ifdef ENABLE_GC_FRIENDLY
813 rmem_chunk->mem_area = (GMemArea*) g_malloc0 (sizeof (GMemArea) -
814 MEM_AREA_SIZE +
815 rmem_chunk->area_size);
816 #else /* !ENABLE_GC_FRIENDLY */
817 rmem_chunk->mem_area = (GMemArea*) g_malloc (sizeof (GMemArea) -
818 MEM_AREA_SIZE +
819 rmem_chunk->area_size);
820 #endif /* ENABLE_GC_FRIENDLY */
822 rmem_chunk->num_mem_areas += 1;
823 rmem_chunk->mem_area->next = rmem_chunk->mem_areas;
824 rmem_chunk->mem_area->prev = NULL;
826 if (rmem_chunk->mem_areas)
827 rmem_chunk->mem_areas->prev = rmem_chunk->mem_area;
828 rmem_chunk->mem_areas = rmem_chunk->mem_area;
830 if (rmem_chunk->type == G_ALLOC_AND_FREE)
831 g_tree_insert (rmem_chunk->mem_tree, rmem_chunk->mem_area, rmem_chunk->mem_area);
834 rmem_chunk->mem_area->index = 0;
835 rmem_chunk->mem_area->free = rmem_chunk->area_size;
836 rmem_chunk->mem_area->allocated = 0;
837 rmem_chunk->mem_area->mark = 0;
840 /* Get the memory and modify the state variables appropriately.
842 mem = (gpointer) &rmem_chunk->mem_area->mem[rmem_chunk->mem_area->index];
843 rmem_chunk->mem_area->index += rmem_chunk->atom_size;
844 rmem_chunk->mem_area->free -= rmem_chunk->atom_size;
845 rmem_chunk->mem_area->allocated += 1;
847 outa_here:
849 LEAVE_MEM_CHUNK_ROUTINE ();
851 return mem;
854 gpointer
855 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
857 gpointer mem;
859 mem = g_mem_chunk_alloc (mem_chunk);
860 if (mem)
862 GRealMemChunk *rmem_chunk = (GRealMemChunk*) mem_chunk;
864 memset (mem, 0, rmem_chunk->atom_size);
867 return mem;
870 void
871 g_mem_chunk_free (GMemChunk *mem_chunk,
872 gpointer mem)
874 GRealMemChunk *rmem_chunk;
875 GMemArea *temp_area;
876 GFreeAtom *free_atom;
878 g_return_if_fail (mem_chunk != NULL);
879 g_return_if_fail (mem != NULL);
881 ENTER_MEM_CHUNK_ROUTINE ();
883 rmem_chunk = (GRealMemChunk*) mem_chunk;
885 #ifdef ENABLE_GC_FRIENDLY
886 memset (mem, 0, rmem_chunk->atom_size);
887 #endif /* ENABLE_GC_FRIENDLY */
889 /* Don't do anything if this is an ALLOC_ONLY chunk
891 if (rmem_chunk->type == G_ALLOC_AND_FREE)
893 /* Place the memory on the "free_atoms" list
895 free_atom = (GFreeAtom*) mem;
896 free_atom->next = rmem_chunk->free_atoms;
897 rmem_chunk->free_atoms = free_atom;
899 temp_area = g_tree_search (rmem_chunk->mem_tree,
900 (GCompareFunc) g_mem_chunk_area_search,
901 mem);
903 temp_area->allocated -= 1;
905 if (temp_area->allocated == 0)
907 temp_area->mark = 1;
908 rmem_chunk->num_marked_areas += 1;
912 LEAVE_MEM_CHUNK_ROUTINE ();
915 /* This doesn't free the free_area if there is one */
916 void
917 g_mem_chunk_clean (GMemChunk *mem_chunk)
919 GRealMemChunk *rmem_chunk;
920 GMemArea *mem_area;
921 GFreeAtom *prev_free_atom;
922 GFreeAtom *temp_free_atom;
923 gpointer mem;
925 g_return_if_fail (mem_chunk != NULL);
927 ENTER_MEM_CHUNK_ROUTINE ();
929 rmem_chunk = (GRealMemChunk*) mem_chunk;
931 if (rmem_chunk->type == G_ALLOC_AND_FREE)
933 prev_free_atom = NULL;
934 temp_free_atom = rmem_chunk->free_atoms;
936 while (temp_free_atom)
938 mem = (gpointer) temp_free_atom;
940 mem_area = g_tree_search (rmem_chunk->mem_tree,
941 (GCompareFunc) g_mem_chunk_area_search,
942 mem);
944 /* If this mem area is marked for destruction then delete the
945 * area and list node and decrement the free mem.
947 if (mem_area->mark)
949 if (prev_free_atom)
950 prev_free_atom->next = temp_free_atom->next;
951 else
952 rmem_chunk->free_atoms = temp_free_atom->next;
953 temp_free_atom = temp_free_atom->next;
955 mem_area->free += rmem_chunk->atom_size;
956 if (mem_area->free == rmem_chunk->area_size)
958 rmem_chunk->num_mem_areas -= 1;
959 rmem_chunk->num_marked_areas -= 1;
961 if (mem_area->next)
962 mem_area->next->prev = mem_area->prev;
963 if (mem_area->prev)
964 mem_area->prev->next = mem_area->next;
965 if (mem_area == rmem_chunk->mem_areas)
966 rmem_chunk->mem_areas = rmem_chunk->mem_areas->next;
967 if (mem_area == rmem_chunk->mem_area)
968 rmem_chunk->mem_area = NULL;
970 if (rmem_chunk->type == G_ALLOC_AND_FREE)
971 g_tree_remove (rmem_chunk->mem_tree, mem_area);
972 g_free (mem_area);
975 else
977 prev_free_atom = temp_free_atom;
978 temp_free_atom = temp_free_atom->next;
982 LEAVE_MEM_CHUNK_ROUTINE ();
985 void
986 g_mem_chunk_reset (GMemChunk *mem_chunk)
988 GRealMemChunk *rmem_chunk;
989 GMemArea *mem_areas;
990 GMemArea *temp_area;
992 g_return_if_fail (mem_chunk != NULL);
994 ENTER_MEM_CHUNK_ROUTINE ();
996 rmem_chunk = (GRealMemChunk*) mem_chunk;
998 mem_areas = rmem_chunk->mem_areas;
999 rmem_chunk->num_mem_areas = 0;
1000 rmem_chunk->mem_areas = NULL;
1001 rmem_chunk->mem_area = NULL;
1003 while (mem_areas)
1005 temp_area = mem_areas;
1006 mem_areas = mem_areas->next;
1007 g_free (temp_area);
1010 rmem_chunk->free_atoms = NULL;
1012 if (rmem_chunk->mem_tree)
1013 g_tree_destroy (rmem_chunk->mem_tree);
1014 rmem_chunk->mem_tree = g_tree_new ((GCompareFunc) g_mem_chunk_area_compare);
1016 LEAVE_MEM_CHUNK_ROUTINE ();
1019 void
1020 g_mem_chunk_print (GMemChunk *mem_chunk)
1022 GRealMemChunk *rmem_chunk;
1023 GMemArea *mem_areas;
1024 gulong mem;
1026 g_return_if_fail (mem_chunk != NULL);
1028 rmem_chunk = (GRealMemChunk*) mem_chunk;
1029 mem_areas = rmem_chunk->mem_areas;
1030 mem = 0;
1032 while (mem_areas)
1034 mem += rmem_chunk->area_size - mem_areas->free;
1035 mem_areas = mem_areas->next;
1038 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO,
1039 "%s: %ld bytes using %d mem areas",
1040 rmem_chunk->name, mem, rmem_chunk->num_mem_areas);
1043 void
1044 g_mem_chunk_info (void)
1046 GRealMemChunk *mem_chunk;
1047 gint count;
1049 count = 0;
1050 g_mutex_lock (mem_chunks_lock);
1051 mem_chunk = mem_chunks;
1052 while (mem_chunk)
1054 count += 1;
1055 mem_chunk = mem_chunk->next;
1057 g_mutex_unlock (mem_chunks_lock);
1059 g_log (g_log_domain_glib, G_LOG_LEVEL_INFO, "%d mem chunks", count);
1061 g_mutex_lock (mem_chunks_lock);
1062 mem_chunk = mem_chunks;
1063 g_mutex_unlock (mem_chunks_lock);
1065 while (mem_chunk)
1067 g_mem_chunk_print ((GMemChunk*) mem_chunk);
1068 mem_chunk = mem_chunk->next;
1072 void
1073 g_blow_chunks (void)
1075 GRealMemChunk *mem_chunk;
1077 g_mutex_lock (mem_chunks_lock);
1078 mem_chunk = mem_chunks;
1079 g_mutex_unlock (mem_chunks_lock);
1080 while (mem_chunk)
1082 g_mem_chunk_clean ((GMemChunk*) mem_chunk);
1083 mem_chunk = mem_chunk->next;
1087 static gulong
1088 g_mem_chunk_compute_size (gulong size,
1089 gulong min_size)
1091 gulong power_of_2;
1092 gulong lower, upper;
1094 power_of_2 = 16;
1095 while (power_of_2 < size)
1096 power_of_2 <<= 1;
1098 lower = power_of_2 >> 1;
1099 upper = power_of_2;
1101 if (size - lower < upper - size && lower >= min_size)
1102 return lower;
1103 else
1104 return upper;
1107 static gint
1108 g_mem_chunk_area_compare (GMemArea *a,
1109 GMemArea *b)
1111 if (a->mem > b->mem)
1112 return 1;
1113 else if (a->mem < b->mem)
1114 return -1;
1115 return 0;
1118 static gint
1119 g_mem_chunk_area_search (GMemArea *a,
1120 gchar *addr)
1122 if (a->mem <= addr)
1124 if (addr < &a->mem[a->index])
1125 return 0;
1126 return 1;
1128 return -1;
1131 #else /* DISABLE_MEM_POOLS */
1133 typedef struct {
1134 guint alloc_size; /* the size of an atom */
1135 } GMinimalMemChunk;
1137 GMemChunk*
1138 g_mem_chunk_new (gchar *name,
1139 gint atom_size,
1140 gulong area_size,
1141 gint type)
1143 GMinimalMemChunk *mem_chunk;
1145 g_return_val_if_fail (atom_size > 0, NULL);
1147 mem_chunk = g_new (GMinimalMemChunk, 1);
1148 mem_chunk->alloc_size = atom_size;
1150 return ((GMemChunk*) mem_chunk);
1153 void
1154 g_mem_chunk_destroy (GMemChunk *mem_chunk)
1156 g_return_if_fail (mem_chunk != NULL);
1158 g_free (mem_chunk);
1161 gpointer
1162 g_mem_chunk_alloc (GMemChunk *mem_chunk)
1164 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1166 g_return_val_if_fail (mem_chunk != NULL, NULL);
1168 return g_malloc (minimal->alloc_size);
1171 gpointer
1172 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
1174 GMinimalMemChunk *minimal = (GMinimalMemChunk *)mem_chunk;
1176 g_return_val_if_fail (mem_chunk != NULL, NULL);
1178 return g_malloc0 (minimal->alloc_size);
1181 void
1182 g_mem_chunk_free (GMemChunk *mem_chunk,
1183 gpointer mem)
1185 g_return_if_fail (mem_chunk != NULL);
1187 g_free (mem);
1190 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
1191 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
1192 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
1193 void g_mem_chunk_info (void) {}
1194 void g_blow_chunks (void) {}
1196 #endif /* DISABLE_MEM_POOLS */
1199 /* generic allocators
1201 struct _GAllocator /* from gmem.c */
1203 gchar *name;
1204 guint16 n_preallocs;
1205 guint is_unused : 1;
1206 guint type : 4;
1207 GAllocator *last;
1208 GMemChunk *mem_chunk;
1209 gpointer dummy; /* implementation specific */
1212 GAllocator*
1213 g_allocator_new (const gchar *name,
1214 guint n_preallocs)
1216 GAllocator *allocator;
1218 g_return_val_if_fail (name != NULL, NULL);
1220 allocator = g_new0 (GAllocator, 1);
1221 allocator->name = g_strdup (name);
1222 allocator->n_preallocs = CLAMP (n_preallocs, 1, 65535);
1223 allocator->is_unused = TRUE;
1224 allocator->type = 0;
1225 allocator->last = NULL;
1226 allocator->mem_chunk = NULL;
1227 allocator->dummy = NULL;
1229 return allocator;
1232 void
1233 g_allocator_free (GAllocator *allocator)
1235 g_return_if_fail (allocator != NULL);
1236 g_return_if_fail (allocator->is_unused == TRUE);
1238 g_free (allocator->name);
1239 if (allocator->mem_chunk)
1240 g_mem_chunk_destroy (allocator->mem_chunk);
1242 g_free (allocator);
1245 void
1246 g_mem_init (void)
1248 #ifndef DISABLE_MEM_POOLS
1249 mem_chunks_lock = g_mutex_new ();
1250 #endif
1251 #ifndef G_DISABLE_CHECKS
1252 mem_chunk_recursion = g_private_new (NULL);
1253 #endif