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/.
38 #include "gthreadprivate.h"
41 #define MEM_PROFILE_TABLE_SIZE 4096
45 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
47 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
48 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
49 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
50 * g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
53 /* --- prototypes --- */
54 static gboolean g_mem_initialized = FALSE;
55 static void g_mem_init_nomessage (void);
58 /* --- malloc wrappers --- */
59 #ifndef REALLOC_0_WORKS
61 standard_realloc (gpointer mem,
65 return malloc (n_bytes);
67 return realloc (mem, n_bytes);
69 #endif /* !REALLOC_0_WORKS */
71 #ifdef SANE_MALLOC_PROTOS
72 # define standard_malloc malloc
73 # ifdef REALLOC_0_WORKS
74 # define standard_realloc realloc
75 # endif /* REALLOC_0_WORKS */
76 # define standard_free free
77 # define standard_calloc calloc
78 # define standard_try_malloc malloc
79 # define standard_try_realloc realloc
80 #else /* !SANE_MALLOC_PROTOS */
82 standard_malloc (gsize n_bytes)
84 return malloc (n_bytes);
86 # ifdef REALLOC_0_WORKS
88 standard_realloc (gpointer mem,
91 return realloc (mem, n_bytes);
93 # endif /* REALLOC_0_WORKS */
95 standard_free (gpointer mem)
100 standard_calloc (gsize n_blocks,
103 return calloc (n_blocks, n_bytes);
105 #define standard_try_malloc standard_malloc
106 #define standard_try_realloc standard_realloc
107 #endif /* !SANE_MALLOC_PROTOS */
110 /* --- variables --- */
111 static GMemVTable glib_mem_vtable = {
117 standard_try_realloc,
121 /* --- functions --- */
123 g_malloc (gsize n_bytes)
125 if (G_UNLIKELY (!g_mem_initialized))
126 g_mem_init_nomessage();
127 if (G_LIKELY (n_bytes))
131 mem = glib_mem_vtable.malloc (n_bytes);
135 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
143 g_malloc0 (gsize n_bytes)
145 if (G_UNLIKELY (!g_mem_initialized))
146 g_mem_init_nomessage();
147 if (G_LIKELY (n_bytes))
151 mem = glib_mem_vtable.calloc (1, n_bytes);
155 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
163 g_realloc (gpointer mem,
166 if (G_UNLIKELY (!g_mem_initialized))
167 g_mem_init_nomessage();
168 if (G_LIKELY (n_bytes))
170 mem = glib_mem_vtable.realloc (mem, n_bytes);
174 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
179 glib_mem_vtable.free (mem);
185 g_free (gpointer mem)
187 if (G_UNLIKELY (!g_mem_initialized))
188 g_mem_init_nomessage();
190 glib_mem_vtable.free (mem);
194 g_try_malloc (gsize n_bytes)
196 if (G_UNLIKELY (!g_mem_initialized))
197 g_mem_init_nomessage();
198 if (G_LIKELY (n_bytes))
199 return glib_mem_vtable.try_malloc (n_bytes);
205 g_try_malloc0 (gsize n_bytes)
209 mem = g_try_malloc (n_bytes);
212 memset (mem, 0, n_bytes);
218 g_try_realloc (gpointer mem,
221 if (G_UNLIKELY (!g_mem_initialized))
222 g_mem_init_nomessage();
223 if (G_LIKELY (n_bytes))
224 return glib_mem_vtable.try_realloc (mem, n_bytes);
227 glib_mem_vtable.free (mem);
233 #define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((a) > G_MAXSIZE / (b)))
237 g_malloc_n (gsize n_blocks,
240 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
242 if (G_UNLIKELY (!g_mem_initialized))
243 g_mem_init_nomessage();
245 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
246 G_STRLOC, n_blocks, n_block_bytes);
249 return g_malloc (n_blocks * n_block_bytes);
254 g_malloc0_n (gsize n_blocks,
257 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
259 if (G_UNLIKELY (!g_mem_initialized))
260 g_mem_init_nomessage();
262 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
263 G_STRLOC, n_blocks, n_block_bytes);
266 return g_malloc0 (n_blocks * n_block_bytes);
271 g_realloc_n (gpointer mem,
275 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
277 if (G_UNLIKELY (!g_mem_initialized))
278 g_mem_init_nomessage();
280 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
281 G_STRLOC, n_blocks, n_block_bytes);
284 return g_realloc (mem, n_blocks * n_block_bytes);
287 #undef g_try_malloc_n
289 g_try_malloc_n (gsize n_blocks,
292 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
295 return g_try_malloc (n_blocks * n_block_bytes);
298 #undef g_try_malloc0_n
300 g_try_malloc0_n (gsize n_blocks,
303 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
306 return g_try_malloc0 (n_blocks * n_block_bytes);
309 #undef g_try_realloc_n
311 g_try_realloc_n (gpointer mem,
315 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
318 return g_try_realloc (mem, n_blocks * n_block_bytes);
324 fallback_calloc (gsize n_blocks,
327 gsize l = n_blocks * n_block_bytes;
328 gpointer mem = glib_mem_vtable.malloc (l);
336 static gboolean vtable_set = FALSE;
339 * g_mem_is_system_malloc
341 * Checks whether the allocator used by g_malloc() is the system's
342 * malloc implementation. If it returns %TRUE memory allocated with
343 * malloc() can be used interchangeable with memory allocated using g_malloc().
344 * This function is useful for avoiding an extra copy of allocated memory returned
345 * by a non-GLib-based API.
347 * A different allocator can be set using g_mem_set_vtable().
349 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
352 g_mem_is_system_malloc (void)
358 g_mem_set_vtable (GMemVTable *vtable)
362 if (vtable->malloc && vtable->realloc && vtable->free)
364 glib_mem_vtable.malloc = vtable->malloc;
365 glib_mem_vtable.realloc = vtable->realloc;
366 glib_mem_vtable.free = vtable->free;
367 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
368 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
369 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
373 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
376 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
380 /* --- memory profiling and checking --- */
381 #ifdef G_DISABLE_CHECKS
382 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
387 #else /* !G_DISABLE_CHECKS */
394 static guint *profile_data = NULL;
395 static gsize profile_allocs = 0;
396 static gsize profile_zinit = 0;
397 static gsize profile_frees = 0;
398 static GMutex *gmem_profile_mutex = NULL;
399 #ifdef G_ENABLE_DEBUG
400 static volatile gsize g_trap_free_size = 0;
401 static volatile gsize g_trap_realloc_size = 0;
402 static volatile gsize g_trap_malloc_size = 0;
403 #endif /* G_ENABLE_DEBUG */
405 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
408 profiler_log (ProfilerJob job,
412 g_mutex_lock (gmem_profile_mutex);
415 profile_data = standard_calloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8,
416 sizeof (profile_data[0]));
417 if (!profile_data) /* memory system kiddin' me, eh? */
419 g_mutex_unlock (gmem_profile_mutex);
424 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
425 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
426 (job & PROFILER_RELOC) != 0,
429 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
430 (job & PROFILER_RELOC) != 0,
434 if (job & PROFILER_ALLOC)
436 profile_allocs += n_bytes;
437 if (job & PROFILER_ZINIT)
438 profile_zinit += n_bytes;
441 profile_frees += n_bytes;
443 g_mutex_unlock (gmem_profile_mutex);
447 profile_print_locked (guint *local_data,
450 gboolean need_header = TRUE;
453 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
455 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
456 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
457 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
458 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
460 if (!t_malloc && !t_realloc && !t_free && !t_refree)
462 else if (need_header)
465 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
466 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
467 g_print (" | malloc() | free() | realloc() | realloc() | \n");
468 g_print ("===========|============|============|============|============|===========\n");
470 if (i < MEM_PROFILE_TABLE_SIZE)
471 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
472 i, t_malloc, t_free, t_realloc, t_refree,
473 (t_malloc - t_free + t_realloc - t_refree) * i);
474 else if (i >= MEM_PROFILE_TABLE_SIZE)
475 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
476 i, t_malloc, t_free, t_realloc, t_refree);
479 g_print (" --- none ---\n");
485 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
490 if (G_UNLIKELY (!g_mem_initialized))
491 g_mem_init_nomessage();
493 g_mutex_lock (gmem_profile_mutex);
495 local_allocs = profile_allocs;
496 local_zinit = profile_zinit;
497 local_frees = profile_frees;
501 g_mutex_unlock (gmem_profile_mutex);
505 memcpy (local_data, profile_data,
506 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
508 g_mutex_unlock (gmem_profile_mutex);
510 g_print ("GLib Memory statistics (successful operations):\n");
511 profile_print_locked (local_data, TRUE);
512 g_print ("GLib Memory statistics (failing operations):\n");
513 profile_print_locked (local_data, FALSE);
514 g_print ("Total bytes: allocated=%"G_GSIZE_FORMAT", "
515 "zero-initialized=%"G_GSIZE_FORMAT" (%.2f%%), "
516 "freed=%"G_GSIZE_FORMAT" (%.2f%%), "
517 "remaining=%"G_GSIZE_FORMAT"\n",
520 ((gdouble) local_zinit) / local_allocs * 100.0,
522 ((gdouble) local_frees) / local_allocs * 100.0,
523 local_allocs - local_frees);
527 profiler_try_malloc (gsize n_bytes)
531 #ifdef G_ENABLE_DEBUG
532 if (g_trap_malloc_size == n_bytes)
534 #endif /* G_ENABLE_DEBUG */
536 p = standard_malloc (sizeof (gsize) * 2 + n_bytes);
540 p[0] = 0; /* free count */
541 p[1] = n_bytes; /* length */
542 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
546 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
552 profiler_malloc (gsize n_bytes)
554 gpointer mem = profiler_try_malloc (n_bytes);
563 profiler_calloc (gsize n_blocks,
566 gsize l = n_blocks * n_block_bytes;
569 #ifdef G_ENABLE_DEBUG
570 if (g_trap_malloc_size == l)
572 #endif /* G_ENABLE_DEBUG */
574 p = standard_calloc (1, sizeof (gsize) * 2 + l);
578 p[0] = 0; /* free count */
579 p[1] = l; /* length */
580 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
585 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
593 profiler_free (gpointer mem)
598 if (p[0]) /* free count */
600 g_warning ("free(%p): memory has been freed %"G_GSIZE_FORMAT" times already",
602 profiler_log (PROFILER_FREE,
608 #ifdef G_ENABLE_DEBUG
609 if (g_trap_free_size == p[1])
611 #endif /* G_ENABLE_DEBUG */
613 profiler_log (PROFILER_FREE,
616 memset (p + 2, 0xaa, p[1]);
618 /* for all those that miss standard_free (p); in this place, yes,
619 * we do leak all memory when profiling, and that is intentional
620 * to catch double frees. patch submissions are futile.
627 profiler_try_realloc (gpointer mem,
634 #ifdef G_ENABLE_DEBUG
635 if (g_trap_realloc_size == n_bytes)
637 #endif /* G_ENABLE_DEBUG */
639 if (mem && p[0]) /* free count */
641 g_warning ("realloc(%p, %"G_GSIZE_FORMAT"): "
642 "memory has been freed %"G_GSIZE_FORMAT" times already",
643 p + 2, (gsize) n_bytes, p[0]);
644 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
650 p = standard_realloc (mem ? p : NULL, sizeof (gsize) * 2 + n_bytes);
655 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
658 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
662 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
669 profiler_realloc (gpointer mem,
672 mem = profiler_try_realloc (mem, n_bytes);
680 static GMemVTable profiler_table = {
686 profiler_try_realloc,
688 GMemVTable *glib_mem_profiler_table = &profiler_table;
690 #endif /* !G_DISABLE_CHECKS */
692 /* --- MemChunks --- */
694 * SECTION: allocators
695 * @title: Memory Allocators
696 * @short_description: deprecated way to allocate chunks of memory for
697 * GList, GSList and GNode
699 * Prior to 2.10, #GAllocator was used as an efficient way to allocate
700 * small pieces of memory for use with the #GList, #GSList and #GNode
701 * data structures. Since 2.10, it has been completely replaced by the
702 * <link linkend="glib-Memory-Slices">slice allocator</link> and
707 * SECTION: memory_chunks
708 * @title: Memory Chunks
709 * @short_description: deprecated way to allocate groups of equal-sized
712 * Memory chunks provide an space-efficient way to allocate equal-sized
713 * pieces of memory, called atoms. However, due to the administrative
714 * overhead (in particular for #G_ALLOC_AND_FREE, and when used from
715 * multiple threads), they are in practise often slower than direct use
716 * of g_malloc(). Therefore, memory chunks have been deprecated in
717 * favor of the <link linkend="glib-Memory-Slices">slice
718 * allocator</link>, which has been added in 2.10. All internal uses of
719 * memory chunks in GLib have been converted to the
720 * <literal>g_slice</literal> API.
722 * There are two types of memory chunks, #G_ALLOC_ONLY, and
723 * #G_ALLOC_AND_FREE. <itemizedlist> <listitem><para> #G_ALLOC_ONLY
724 * chunks only allow allocation of atoms. The atoms can never be freed
725 * individually. The memory chunk can only be free in its entirety.
726 * </para></listitem> <listitem><para> #G_ALLOC_AND_FREE chunks do
727 * allow atoms to be freed individually. The disadvantage of this is
728 * that the memory chunk has to keep track of which atoms have been
729 * freed. This results in more memory being used and a slight
730 * degradation in performance. </para></listitem> </itemizedlist>
732 * To create a memory chunk use g_mem_chunk_new() or the convenience
733 * macro g_mem_chunk_create().
735 * To allocate a new atom use g_mem_chunk_alloc(),
736 * g_mem_chunk_alloc0(), or the convenience macros g_chunk_new() or
739 * To free an atom use g_mem_chunk_free(), or the convenience macro
740 * g_chunk_free(). (Atoms can only be freed if the memory chunk is
741 * created with the type set to #G_ALLOC_AND_FREE.)
743 * To free any blocks of memory which are no longer being used, use
744 * g_mem_chunk_clean(). To clean all memory chunks, use g_blow_chunks().
746 * To reset the memory chunk, freeing all of the atoms, use
747 * g_mem_chunk_reset().
749 * To destroy a memory chunk, use g_mem_chunk_destroy().
751 * To help debug memory chunks, use g_mem_chunk_info() and
752 * g_mem_chunk_print().
755 * <title>Using a #GMemChunk</title>
757 * GMemChunk *mem_chunk;
761 * /<!-- -->* Create a GMemChunk with atoms 50 bytes long, and memory
762 * blocks holding 100 bytes. Note that this means that only 2 atoms
763 * fit into each memory block and so isn't very efficient. *<!-- -->/
764 * mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
765 * /<!-- -->* Now allocate 10000 atoms. *<!-- -->/
766 * for (i = 0; i < 10000; i++)
768 * mem[i] = g_chunk_new (gchar, mem_chunk);
769 * /<!-- -->* Fill in the atom memory with some junk. *<!-- -->/
770 * for (j = 0; j < 50; j++)
773 * /<!-- -->* Now free all of the atoms. Note that since we are going to
774 * destroy the GMemChunk, this wouldn't normally be used. *<!-- -->/
775 * for (i = 0; i < 10000; i++)
777 * g_mem_chunk_free (mem_chunk, mem[i]);
779 * /<!-- -->* We are finished with the GMemChunk, so we destroy it. *<!-- -->/
780 * g_mem_chunk_destroy (mem_chunk);
785 * <title>Using a #GMemChunk with data structures</title>
787 * GMemChunk *array_mem_chunk;
789 * /<!-- -->* Create a GMemChunk to hold GRealArray structures, using
790 * the g_mem_chunk_create(<!-- -->) convenience macro. We want 1024 atoms in each
791 * memory block, and we want to be able to free individual atoms. *<!-- -->/
792 * array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE);
793 * /<!-- -->* Allocate one atom, using the g_chunk_new(<!-- -->) convenience macro. *<!-- -->/
794 * array = g_chunk_new (GRealArray, array_mem_chunk);
795 * /<!-- -->* We can now use array just like a normal pointer to a structure. *<!-- -->/
796 * array->data = NULL;
799 * array->zero_terminated = (zero_terminated ? 1 : 0);
800 * array->clear = (clear ? 1 : 0);
801 * array->elt_size = elt_size;
802 * /<!-- -->* We can free the element, so it can be reused. *<!-- -->/
803 * g_chunk_free (array, array_mem_chunk);
804 * /<!-- -->* We destroy the GMemChunk when we are finished with it. *<!-- -->/
805 * g_mem_chunk_destroy (array_mem_chunk);
810 #ifndef G_ALLOC_AND_FREE
815 * The #GAllocator struct contains private data. and should only be
816 * accessed using the following functions.
818 typedef struct _GAllocator GAllocator;
823 * The #GMemChunk struct is an opaque data structure representing a
824 * memory chunk. It should be accessed only through the use of the
825 * following functions.
827 typedef struct _GMemChunk GMemChunk;
832 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
833 * g_mem_chunk_create() to specify that atoms will never be freed
836 #define G_ALLOC_ONLY 1
841 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
842 * g_mem_chunk_create() to specify that atoms will be freed
845 #define G_ALLOC_AND_FREE 2
849 guint alloc_size; /* the size of an atom */
854 * @name: a string to identify the #GMemChunk. It is not copied so it
855 * should be valid for the lifetime of the #GMemChunk. It is
856 * only used in g_mem_chunk_print(), which is used for debugging.
857 * @atom_size: the size, in bytes, of each element in the #GMemChunk.
858 * @area_size: the size, in bytes, of each block of memory allocated to
860 * @type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used if the
861 * atoms will be freed individually. #G_ALLOC_ONLY should be
862 * used if atoms will never be freed individually.
863 * #G_ALLOC_ONLY is quicker, since it does not need to track
864 * free atoms, but it obviously wastes memory if you no longer
865 * need many of the atoms.
866 * @Returns: the new #GMemChunk.
868 * Creates a new #GMemChunk.
870 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
871 * allocator</link> instead
874 g_mem_chunk_new (const gchar *name,
879 GMemChunk *mem_chunk;
880 g_return_val_if_fail (atom_size > 0, NULL);
882 mem_chunk = g_slice_new (GMemChunk);
883 mem_chunk->alloc_size = atom_size;
888 * g_mem_chunk_destroy:
889 * @mem_chunk: a #GMemChunk.
891 * Frees all of the memory allocated for a #GMemChunk.
893 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
894 * allocator</link> instead
897 g_mem_chunk_destroy (GMemChunk *mem_chunk)
899 g_return_if_fail (mem_chunk != NULL);
901 g_slice_free (GMemChunk, mem_chunk);
906 * @mem_chunk: a #GMemChunk.
907 * @Returns: a pointer to the allocated atom.
909 * Allocates an atom of memory from a #GMemChunk.
911 * Deprecated:2.10: Use g_slice_alloc() instead
914 g_mem_chunk_alloc (GMemChunk *mem_chunk)
916 g_return_val_if_fail (mem_chunk != NULL, NULL);
918 return g_slice_alloc (mem_chunk->alloc_size);
922 * g_mem_chunk_alloc0:
923 * @mem_chunk: a #GMemChunk.
924 * @Returns: a pointer to the allocated atom.
926 * Allocates an atom of memory from a #GMemChunk, setting the memory to
929 * Deprecated:2.10: Use g_slice_alloc0() instead
932 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
934 g_return_val_if_fail (mem_chunk != NULL, NULL);
936 return g_slice_alloc0 (mem_chunk->alloc_size);
941 * @mem_chunk: a #GMemChunk.
942 * @mem: a pointer to the atom to free.
944 * Frees an atom in a #GMemChunk. This should only be called if the
945 * #GMemChunk was created with #G_ALLOC_AND_FREE. Otherwise it will
948 * Deprecated:2.10: Use g_slice_free1() instead
951 g_mem_chunk_free (GMemChunk *mem_chunk,
954 g_return_if_fail (mem_chunk != NULL);
956 g_slice_free1 (mem_chunk->alloc_size, mem);
961 * @mem_chunk: a #GMemChunk.
963 * Frees any blocks in a #GMemChunk which are no longer being used.
965 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
966 * allocator</link> instead
968 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
972 * @mem_chunk: a #GMemChunk.
974 * Resets a GMemChunk to its initial state. It frees all of the
975 * currently allocated blocks of memory.
977 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
978 * allocator</link> instead
980 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
985 * @mem_chunk: a #GMemChunk.
987 * Outputs debugging information for a #GMemChunk. It outputs the name
988 * of the #GMemChunk (set with g_mem_chunk_new()), the number of bytes
989 * used, and the number of blocks of memory allocated.
991 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
992 * allocator</link> instead
994 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
1000 * Outputs debugging information for all #GMemChunk objects currently
1001 * in use. It outputs the number of #GMemChunk objects currently
1002 * allocated, and calls g_mem_chunk_print() to output information on
1005 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1006 * allocator</link> instead
1008 void g_mem_chunk_info (void) {}
1013 * Calls g_mem_chunk_clean() on all #GMemChunk objects.
1015 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1016 * allocator</link> instead
1018 void g_blow_chunks (void) {}
1022 * @type: the type of the #GMemChunk atoms, typically a structure name.
1023 * @chunk: a #GMemChunk.
1024 * @Returns: a pointer to the allocated atom, cast to a pointer to
1027 * A convenience macro to allocate an atom of memory from a #GMemChunk.
1028 * It calls g_mem_chunk_alloc0() and casts the returned atom to a
1029 * pointer to the given type, avoiding a type cast in the source code.
1031 * Deprecated:2.10: Use g_slice_new0() instead
1036 * @mem: a pointer to the atom to be freed.
1037 * @mem_chunk: a #GMemChunk.
1039 * A convenience macro to free an atom of memory from a #GMemChunk. It
1040 * simply switches the arguments and calls g_mem_chunk_free() It is
1041 * included simply to complement the other convenience macros,
1042 * g_chunk_new() and g_chunk_new0().
1044 * Deprecated:2.10: Use g_slice_free() instead
1049 * @type: the type of the #GMemChunk atoms, typically a structure name.
1050 * @chunk: a #GMemChunk.
1051 * @Returns: a pointer to the allocated atom, cast to a pointer to
1054 * A convenience macro to allocate an atom of memory from a #GMemChunk.
1055 * It calls g_mem_chunk_alloc() and casts the returned atom to a
1056 * pointer to the given type, avoiding a type cast in the source code.
1058 * Deprecated:2.10: Use g_slice_new() instead
1062 * g_mem_chunk_create:
1063 * @type: the type of the atoms, typically a structure name.
1064 * @pre_alloc: the number of atoms to store in each block of memory.
1065 * @alloc_type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used
1066 * if the atoms will be freed individually. #G_ALLOC_ONLY
1067 * should be used if atoms will never be freed
1068 * individually. #G_ALLOC_ONLY is quicker, since it does
1069 * not need to track free atoms, but it obviously wastes
1070 * memory if you no longer need many of the atoms.
1071 * @Returns: the new #GMemChunk.
1073 * A convenience macro for creating a new #GMemChunk. It calls
1074 * g_mem_chunk_new(), using the given type to create the #GMemChunk
1075 * name. The atom size is determined using
1076 * <function>sizeof()</function>, and the area size is calculated by
1077 * multiplying the @pre_alloc parameter with the atom size.
1079 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1080 * allocator</link> instead
1086 * @name: the name of the #GAllocator. This name is used to set the
1087 * name of the #GMemChunk used by the #GAllocator, and is only
1088 * used for debugging.
1089 * @n_preallocs: the number of elements in each block of memory
1090 * allocated. Larger blocks mean less calls to
1091 * g_malloc(), but some memory may be wasted. (GLib uses
1092 * 128 elements per block by default.) The value must be
1093 * between 1 and 65535.
1094 * @Returns: a new #GAllocator.
1096 * Creates a new #GAllocator.
1098 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1099 * allocator</link> instead
1102 g_allocator_new (const gchar *name,
1105 static struct _GAllocator {
1107 guint16 n_preallocs;
1108 guint is_unused : 1;
1111 GMemChunk *mem_chunk;
1114 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
1116 /* some (broken) GAllocator uses depend on non-NULL allocators */
1117 return (void*) &dummy;
1122 * @allocator: a #GAllocator.
1124 * Frees all of the memory allocated by the #GAllocator.
1126 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1127 * allocator</link> instead
1130 g_allocator_free (GAllocator *allocator)
1134 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
1135 gboolean g_mem_gc_friendly = TRUE;
1137 gboolean g_mem_gc_friendly = FALSE;
1141 g_mem_init_nomessage (void)
1145 const GDebugKey keys[] = {
1146 { "gc-friendly", 1 },
1149 if (g_mem_initialized)
1151 /* don't use g_malloc/g_message here */
1152 val = _g_getenv_nomalloc ("G_DEBUG", buffer);
1153 flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1154 if (flags & 1) /* gc-friendly */
1156 g_mem_gc_friendly = TRUE;
1158 g_mem_initialized = TRUE;
1162 _g_mem_thread_init_noprivate_nomessage (void)
1164 /* we may only create mutexes here, locking/
1165 * unlocking a mutex does not yet work.
1167 g_mem_init_nomessage();
1168 #ifndef G_DISABLE_CHECKS
1169 gmem_profile_mutex = g_mutex_new ();
1174 #include "galiasdef.c"