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 fallback_calloc (gsize n_blocks,
236 gsize l = n_blocks * n_block_bytes;
237 gpointer mem = glib_mem_vtable.malloc (l);
245 static gboolean vtable_set = FALSE;
248 * g_mem_is_system_malloc
250 * Checks whether the allocator used by g_malloc() is the system's
251 * malloc implementation. If it returns %TRUE memory allocated with
252 * malloc() can be used interchangeable with memory allocated using g_malloc().
253 * This function is useful for avoiding an extra copy of allocated memory returned
254 * by a non-GLib-based API.
256 * A different allocator can be set using g_mem_set_vtable().
258 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
261 g_mem_is_system_malloc (void)
267 g_mem_set_vtable (GMemVTable *vtable)
271 if (vtable->malloc && vtable->realloc && vtable->free)
273 glib_mem_vtable.malloc = vtable->malloc;
274 glib_mem_vtable.realloc = vtable->realloc;
275 glib_mem_vtable.free = vtable->free;
276 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
277 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
278 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
282 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
285 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
289 /* --- memory profiling and checking --- */
290 #ifdef G_DISABLE_CHECKS
291 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
296 #else /* !G_DISABLE_CHECKS */
303 static guint *profile_data = NULL;
304 static gsize profile_allocs = 0;
305 static gsize profile_zinit = 0;
306 static gsize profile_frees = 0;
307 static GMutex *gmem_profile_mutex = NULL;
308 #ifdef G_ENABLE_DEBUG
309 static volatile gsize g_trap_free_size = 0;
310 static volatile gsize g_trap_realloc_size = 0;
311 static volatile gsize g_trap_malloc_size = 0;
312 #endif /* G_ENABLE_DEBUG */
314 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
317 profiler_log (ProfilerJob job,
321 g_mutex_lock (gmem_profile_mutex);
324 profile_data = standard_calloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8,
325 sizeof (profile_data[0]));
326 if (!profile_data) /* memory system kiddin' me, eh? */
328 g_mutex_unlock (gmem_profile_mutex);
333 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
334 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
335 (job & PROFILER_RELOC) != 0,
338 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
339 (job & PROFILER_RELOC) != 0,
343 if (job & PROFILER_ALLOC)
345 profile_allocs += n_bytes;
346 if (job & PROFILER_ZINIT)
347 profile_zinit += n_bytes;
350 profile_frees += n_bytes;
352 g_mutex_unlock (gmem_profile_mutex);
356 profile_print_locked (guint *local_data,
359 gboolean need_header = TRUE;
362 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
364 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
365 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
366 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
367 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
369 if (!t_malloc && !t_realloc && !t_free && !t_refree)
371 else if (need_header)
374 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
375 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
376 g_print (" | malloc() | free() | realloc() | realloc() | \n");
377 g_print ("===========|============|============|============|============|===========\n");
379 if (i < MEM_PROFILE_TABLE_SIZE)
380 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
381 i, t_malloc, t_free, t_realloc, t_refree,
382 (t_malloc - t_free + t_realloc - t_refree) * i);
383 else if (i >= MEM_PROFILE_TABLE_SIZE)
384 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
385 i, t_malloc, t_free, t_realloc, t_refree);
388 g_print (" --- none ---\n");
394 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
399 if (G_UNLIKELY (!g_mem_initialized))
400 g_mem_init_nomessage();
402 g_mutex_lock (gmem_profile_mutex);
404 local_allocs = profile_allocs;
405 local_zinit = profile_zinit;
406 local_frees = profile_frees;
410 g_mutex_unlock (gmem_profile_mutex);
414 memcpy (local_data, profile_data,
415 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
417 g_mutex_unlock (gmem_profile_mutex);
419 g_print ("GLib Memory statistics (successful operations):\n");
420 profile_print_locked (local_data, TRUE);
421 g_print ("GLib Memory statistics (failing operations):\n");
422 profile_print_locked (local_data, FALSE);
423 g_print ("Total bytes: allocated=%"G_GSIZE_FORMAT", "
424 "zero-initialized=%"G_GSIZE_FORMAT" (%.2f%%), "
425 "freed=%"G_GSIZE_FORMAT" (%.2f%%), "
426 "remaining=%"G_GSIZE_FORMAT"\n",
429 ((gdouble) local_zinit) / local_allocs * 100.0,
431 ((gdouble) local_frees) / local_allocs * 100.0,
432 local_allocs - local_frees);
436 profiler_try_malloc (gsize n_bytes)
440 #ifdef G_ENABLE_DEBUG
441 if (g_trap_malloc_size == n_bytes)
443 #endif /* G_ENABLE_DEBUG */
445 p = standard_malloc (sizeof (gsize) * 2 + n_bytes);
449 p[0] = 0; /* free count */
450 p[1] = n_bytes; /* length */
451 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
455 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
461 profiler_malloc (gsize n_bytes)
463 gpointer mem = profiler_try_malloc (n_bytes);
472 profiler_calloc (gsize n_blocks,
475 gsize l = n_blocks * n_block_bytes;
478 #ifdef G_ENABLE_DEBUG
479 if (g_trap_malloc_size == l)
481 #endif /* G_ENABLE_DEBUG */
483 p = standard_calloc (1, sizeof (gsize) * 2 + l);
487 p[0] = 0; /* free count */
488 p[1] = l; /* length */
489 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
494 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
502 profiler_free (gpointer mem)
507 if (p[0]) /* free count */
509 g_warning ("free(%p): memory has been freed %"G_GSIZE_FORMAT" times already",
511 profiler_log (PROFILER_FREE,
517 #ifdef G_ENABLE_DEBUG
518 if (g_trap_free_size == p[1])
520 #endif /* G_ENABLE_DEBUG */
522 profiler_log (PROFILER_FREE,
525 memset (p + 2, 0xaa, p[1]);
527 /* for all those that miss standard_free (p); in this place, yes,
528 * we do leak all memory when profiling, and that is intentional
529 * to catch double frees. patch submissions are futile.
536 profiler_try_realloc (gpointer mem,
543 #ifdef G_ENABLE_DEBUG
544 if (g_trap_realloc_size == n_bytes)
546 #endif /* G_ENABLE_DEBUG */
548 if (mem && p[0]) /* free count */
550 g_warning ("realloc(%p, %"G_GSIZE_FORMAT"): "
551 "memory has been freed %"G_GSIZE_FORMAT" times already",
552 p + 2, (gsize) n_bytes, p[0]);
553 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
559 p = standard_realloc (mem ? p : NULL, sizeof (gsize) * 2 + n_bytes);
564 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
567 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
571 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
578 profiler_realloc (gpointer mem,
581 mem = profiler_try_realloc (mem, n_bytes);
589 static GMemVTable profiler_table = {
595 profiler_try_realloc,
597 GMemVTable *glib_mem_profiler_table = &profiler_table;
599 #endif /* !G_DISABLE_CHECKS */
601 /* --- MemChunks --- */
603 * SECTION: allocators
604 * @title: Memory Allocators
605 * @short_description: deprecated way to allocate chunks of memory for
606 * GList, GSList and GNode
608 * Prior to 2.10, #GAllocator was used as an efficient way to allocate
609 * small pieces of memory for use with the #GList, #GSList and #GNode
610 * data structures. Since 2.10, it has been completely replaced by the
611 * <link linkend="glib-Memory-Slices">slice allocator</link> and
616 * SECTION: memory_chunks
617 * @title: Memory Chunks
618 * @short_description: deprecated way to allocate groups of equal-sized
621 * Memory chunks provide an space-efficient way to allocate equal-sized
622 * pieces of memory, called atoms. However, due to the administrative
623 * overhead (in particular for #G_ALLOC_AND_FREE, and when used from
624 * multiple threads), they are in practise often slower than direct use
625 * of g_malloc(). Therefore, memory chunks have been deprecated in
626 * favor of the <link linkend="glib-Memory-Slices">slice
627 * allocator</link>, which has been added in 2.10. All internal uses of
628 * memory chunks in GLib have been converted to the
629 * <literal>g_slice</literal> API.
631 * There are two types of memory chunks, #G_ALLOC_ONLY, and
632 * #G_ALLOC_AND_FREE. <itemizedlist> <listitem><para> #G_ALLOC_ONLY
633 * chunks only allow allocation of atoms. The atoms can never be freed
634 * individually. The memory chunk can only be free in its entirety.
635 * </para></listitem> <listitem><para> #G_ALLOC_AND_FREE chunks do
636 * allow atoms to be freed individually. The disadvantage of this is
637 * that the memory chunk has to keep track of which atoms have been
638 * freed. This results in more memory being used and a slight
639 * degradation in performance. </para></listitem> </itemizedlist>
641 * To create a memory chunk use g_mem_chunk_new() or the convenience
642 * macro g_mem_chunk_create().
644 * To allocate a new atom use g_mem_chunk_alloc(),
645 * g_mem_chunk_alloc0(), or the convenience macros g_chunk_new() or
648 * To free an atom use g_mem_chunk_free(), or the convenience macro
649 * g_chunk_free(). (Atoms can only be freed if the memory chunk is
650 * created with the type set to #G_ALLOC_AND_FREE.)
652 * To free any blocks of memory which are no longer being used, use
653 * g_mem_chunk_clean(). To clean all memory chunks, use g_blow_chunks().
655 * To reset the memory chunk, freeing all of the atoms, use
656 * g_mem_chunk_reset().
658 * To destroy a memory chunk, use g_mem_chunk_destroy().
660 * To help debug memory chunks, use g_mem_chunk_info() and
661 * g_mem_chunk_print().
664 * <title>Using a #GMemChunk</title>
666 * GMemChunk *mem_chunk;
670 * /<!-- -->* Create a GMemChunk with atoms 50 bytes long, and memory
671 * blocks holding 100 bytes. Note that this means that only 2 atoms
672 * fit into each memory block and so isn't very efficient. *<!-- -->/
673 * mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
674 * /<!-- -->* Now allocate 10000 atoms. *<!-- -->/
675 * for (i = 0; i < 10000; i++)
677 * mem[i] = g_chunk_new (gchar, mem_chunk);
678 * /<!-- -->* Fill in the atom memory with some junk. *<!-- -->/
679 * for (j = 0; j < 50; j++)
682 * /<!-- -->* Now free all of the atoms. Note that since we are going to
683 * destroy the GMemChunk, this wouldn't normally be used. *<!-- -->/
684 * for (i = 0; i < 10000; i++)
686 * g_mem_chunk_free (mem_chunk, mem[i]);
688 * /<!-- -->* We are finished with the GMemChunk, so we destroy it. *<!-- -->/
689 * g_mem_chunk_destroy (mem_chunk);
694 * <title>Using a #GMemChunk with data structures</title>
696 * GMemChunk *array_mem_chunk;
698 * /<!-- -->* Create a GMemChunk to hold GRealArray structures, using
699 * the g_mem_chunk_create(<!-- -->) convenience macro. We want 1024 atoms in each
700 * memory block, and we want to be able to free individual atoms. *<!-- -->/
701 * array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE);
702 * /<!-- -->* Allocate one atom, using the g_chunk_new(<!-- -->) convenience macro. *<!-- -->/
703 * array = g_chunk_new (GRealArray, array_mem_chunk);
704 * /<!-- -->* We can now use array just like a normal pointer to a structure. *<!-- -->/
705 * array->data = NULL;
708 * array->zero_terminated = (zero_terminated ? 1 : 0);
709 * array->clear = (clear ? 1 : 0);
710 * array->elt_size = elt_size;
711 * /<!-- -->* We can free the element, so it can be reused. *<!-- -->/
712 * g_chunk_free (array, array_mem_chunk);
713 * /<!-- -->* We destroy the GMemChunk when we are finished with it. *<!-- -->/
714 * g_mem_chunk_destroy (array_mem_chunk);
719 #ifndef G_ALLOC_AND_FREE
724 * The #GAllocator struct contains private data. and should only be
725 * accessed using the following functions.
727 typedef struct _GAllocator GAllocator;
732 * The #GMemChunk struct is an opaque data structure representing a
733 * memory chunk. It should be accessed only through the use of the
734 * following functions.
736 typedef struct _GMemChunk GMemChunk;
741 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
742 * g_mem_chunk_create() to specify that atoms will never be freed
745 #define G_ALLOC_ONLY 1
750 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
751 * g_mem_chunk_create() to specify that atoms will be freed
754 #define G_ALLOC_AND_FREE 2
758 guint alloc_size; /* the size of an atom */
763 * @name: a string to identify the #GMemChunk. It is not copied so it
764 * should be valid for the lifetime of the #GMemChunk. It is
765 * only used in g_mem_chunk_print(), which is used for debugging.
766 * @atom_size: the size, in bytes, of each element in the #GMemChunk.
767 * @area_size: the size, in bytes, of each block of memory allocated to
769 * @type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used if the
770 * atoms will be freed individually. #G_ALLOC_ONLY should be
771 * used if atoms will never be freed individually.
772 * #G_ALLOC_ONLY is quicker, since it does not need to track
773 * free atoms, but it obviously wastes memory if you no longer
774 * need many of the atoms.
775 * @Returns: the new #GMemChunk.
777 * Creates a new #GMemChunk.
779 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
780 * allocator</link> instead
783 g_mem_chunk_new (const gchar *name,
788 GMemChunk *mem_chunk;
789 g_return_val_if_fail (atom_size > 0, NULL);
791 mem_chunk = g_slice_new (GMemChunk);
792 mem_chunk->alloc_size = atom_size;
797 * g_mem_chunk_destroy:
798 * @mem_chunk: a #GMemChunk.
800 * Frees all of the memory allocated for a #GMemChunk.
802 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
803 * allocator</link> instead
806 g_mem_chunk_destroy (GMemChunk *mem_chunk)
808 g_return_if_fail (mem_chunk != NULL);
810 g_slice_free (GMemChunk, mem_chunk);
815 * @mem_chunk: a #GMemChunk.
816 * @Returns: a pointer to the allocated atom.
818 * Allocates an atom of memory from a #GMemChunk.
820 * Deprecated:2.10: Use g_slice_alloc() instead
823 g_mem_chunk_alloc (GMemChunk *mem_chunk)
825 g_return_val_if_fail (mem_chunk != NULL, NULL);
827 return g_slice_alloc (mem_chunk->alloc_size);
831 * g_mem_chunk_alloc0:
832 * @mem_chunk: a #GMemChunk.
833 * @Returns: a pointer to the allocated atom.
835 * Allocates an atom of memory from a #GMemChunk, setting the memory to
838 * Deprecated:2.10: Use g_slice_alloc0() instead
841 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
843 g_return_val_if_fail (mem_chunk != NULL, NULL);
845 return g_slice_alloc0 (mem_chunk->alloc_size);
850 * @mem_chunk: a #GMemChunk.
851 * @mem: a pointer to the atom to free.
853 * Frees an atom in a #GMemChunk. This should only be called if the
854 * #GMemChunk was created with #G_ALLOC_AND_FREE. Otherwise it will
857 * Deprecated:2.10: Use g_slice_free1() instead
860 g_mem_chunk_free (GMemChunk *mem_chunk,
863 g_return_if_fail (mem_chunk != NULL);
865 g_slice_free1 (mem_chunk->alloc_size, mem);
870 * @mem_chunk: a #GMemChunk.
872 * Frees any blocks in a #GMemChunk which are no longer being used.
874 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
875 * allocator</link> instead
877 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
881 * @mem_chunk: a #GMemChunk.
883 * Resets a GMemChunk to its initial state. It frees all of the
884 * currently allocated blocks of memory.
886 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
887 * allocator</link> instead
889 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
894 * @mem_chunk: a #GMemChunk.
896 * Outputs debugging information for a #GMemChunk. It outputs the name
897 * of the #GMemChunk (set with g_mem_chunk_new()), the number of bytes
898 * used, and the number of blocks of memory allocated.
900 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
901 * allocator</link> instead
903 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
909 * Outputs debugging information for all #GMemChunk objects currently
910 * in use. It outputs the number of #GMemChunk objects currently
911 * allocated, and calls g_mem_chunk_print() to output information on
914 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
915 * allocator</link> instead
917 void g_mem_chunk_info (void) {}
922 * Calls g_mem_chunk_clean() on all #GMemChunk objects.
924 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
925 * allocator</link> instead
927 void g_blow_chunks (void) {}
931 * @type: the type of the #GMemChunk atoms, typically a structure name.
932 * @chunk: a #GMemChunk.
933 * @Returns: a pointer to the allocated atom, cast to a pointer to
936 * A convenience macro to allocate an atom of memory from a #GMemChunk.
937 * It calls g_mem_chunk_alloc0() and casts the returned atom to a
938 * pointer to the given type, avoiding a type cast in the source code.
940 * Deprecated:2.10: Use g_slice_new0() instead
945 * @mem: a pointer to the atom to be freed.
946 * @mem_chunk: a #GMemChunk.
948 * A convenience macro to free an atom of memory from a #GMemChunk. It
949 * simply switches the arguments and calls g_mem_chunk_free() It is
950 * included simply to complement the other convenience macros,
951 * g_chunk_new() and g_chunk_new0().
953 * Deprecated:2.10: Use g_slice_free() instead
958 * @type: the type of the #GMemChunk atoms, typically a structure name.
959 * @chunk: a #GMemChunk.
960 * @Returns: a pointer to the allocated atom, cast to a pointer to
963 * A convenience macro to allocate an atom of memory from a #GMemChunk.
964 * It calls g_mem_chunk_alloc() and casts the returned atom to a
965 * pointer to the given type, avoiding a type cast in the source code.
967 * Deprecated:2.10: Use g_slice_new() instead
971 * g_mem_chunk_create:
972 * @type: the type of the atoms, typically a structure name.
973 * @pre_alloc: the number of atoms to store in each block of memory.
974 * @alloc_type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used
975 * if the atoms will be freed individually. #G_ALLOC_ONLY
976 * should be used if atoms will never be freed
977 * individually. #G_ALLOC_ONLY is quicker, since it does
978 * not need to track free atoms, but it obviously wastes
979 * memory if you no longer need many of the atoms.
980 * @Returns: the new #GMemChunk.
982 * A convenience macro for creating a new #GMemChunk. It calls
983 * g_mem_chunk_new(), using the given type to create the #GMemChunk
984 * name. The atom size is determined using
985 * <function>sizeof()</function>, and the area size is calculated by
986 * multiplying the @pre_alloc parameter with the atom size.
988 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
989 * allocator</link> instead
995 * @name: the name of the #GAllocator. This name is used to set the
996 * name of the #GMemChunk used by the #GAllocator, and is only
997 * used for debugging.
998 * @n_preallocs: the number of elements in each block of memory
999 * allocated. Larger blocks mean less calls to
1000 * g_malloc(), but some memory may be wasted. (GLib uses
1001 * 128 elements per block by default.) The value must be
1002 * between 1 and 65535.
1003 * @Returns: a new #GAllocator.
1005 * Creates a new #GAllocator.
1007 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1008 * allocator</link> instead
1011 g_allocator_new (const gchar *name,
1014 static struct _GAllocator {
1016 guint16 n_preallocs;
1017 guint is_unused : 1;
1020 GMemChunk *mem_chunk;
1023 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
1025 /* some (broken) GAllocator uses depend on non-NULL allocators */
1026 return (void*) &dummy;
1031 * @allocator: a #GAllocator.
1033 * Frees all of the memory allocated by the #GAllocator.
1035 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1036 * allocator</link> instead
1039 g_allocator_free (GAllocator *allocator)
1043 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
1044 gboolean g_mem_gc_friendly = TRUE;
1046 gboolean g_mem_gc_friendly = FALSE;
1050 g_mem_init_nomessage (void)
1054 const GDebugKey keys[] = {
1055 { "gc-friendly", 1 },
1058 if (g_mem_initialized)
1060 /* don't use g_malloc/g_message here */
1061 val = _g_getenv_nomalloc ("G_DEBUG", buffer);
1062 flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1063 if (flags & 1) /* gc-friendly */
1065 g_mem_gc_friendly = TRUE;
1067 g_mem_initialized = TRUE;
1071 _g_mem_thread_init_noprivate_nomessage (void)
1073 /* we may only create mutexes here, locking/
1074 * unlocking a mutex does not yet work.
1076 g_mem_init_nomessage();
1077 #ifndef G_DISABLE_CHECKS
1078 gmem_profile_mutex = g_mutex_new ();
1083 #include "galiasdef.c"