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/.
39 #include "gbacktrace.h"
40 #include "gtestutils.h"
45 #define MEM_PROFILE_TABLE_SIZE 4096
49 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
51 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
52 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
53 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
54 * g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
57 /* --- prototypes --- */
58 static gboolean g_mem_initialized = FALSE;
59 static void g_mem_init_nomessage (void);
62 /* --- malloc wrappers --- */
63 #ifndef REALLOC_0_WORKS
65 standard_realloc (gpointer mem,
69 return malloc (n_bytes);
71 return realloc (mem, n_bytes);
73 #endif /* !REALLOC_0_WORKS */
75 #ifdef SANE_MALLOC_PROTOS
76 # define standard_malloc malloc
77 # ifdef REALLOC_0_WORKS
78 # define standard_realloc realloc
79 # endif /* REALLOC_0_WORKS */
80 # define standard_free free
81 # define standard_calloc calloc
82 # define standard_try_malloc malloc
83 # define standard_try_realloc realloc
84 #else /* !SANE_MALLOC_PROTOS */
86 standard_malloc (gsize n_bytes)
88 return malloc (n_bytes);
90 # ifdef REALLOC_0_WORKS
92 standard_realloc (gpointer mem,
95 return realloc (mem, n_bytes);
97 # endif /* REALLOC_0_WORKS */
99 standard_free (gpointer mem)
104 standard_calloc (gsize n_blocks,
107 return calloc (n_blocks, n_bytes);
109 #define standard_try_malloc standard_malloc
110 #define standard_try_realloc standard_realloc
111 #endif /* !SANE_MALLOC_PROTOS */
114 /* --- variables --- */
115 static GMemVTable glib_mem_vtable = {
121 standard_try_realloc,
125 /* --- functions --- */
127 g_malloc (gsize n_bytes)
129 if (G_UNLIKELY (!g_mem_initialized))
130 g_mem_init_nomessage();
131 if (G_LIKELY (n_bytes))
135 mem = glib_mem_vtable.malloc (n_bytes);
139 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
147 g_malloc0 (gsize n_bytes)
149 if (G_UNLIKELY (!g_mem_initialized))
150 g_mem_init_nomessage();
151 if (G_LIKELY (n_bytes))
155 mem = glib_mem_vtable.calloc (1, n_bytes);
159 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
167 g_realloc (gpointer mem,
170 if (G_UNLIKELY (!g_mem_initialized))
171 g_mem_init_nomessage();
172 if (G_LIKELY (n_bytes))
174 mem = glib_mem_vtable.realloc (mem, n_bytes);
178 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
183 glib_mem_vtable.free (mem);
189 g_free (gpointer mem)
191 if (G_UNLIKELY (!g_mem_initialized))
192 g_mem_init_nomessage();
194 glib_mem_vtable.free (mem);
198 g_try_malloc (gsize n_bytes)
200 if (G_UNLIKELY (!g_mem_initialized))
201 g_mem_init_nomessage();
202 if (G_LIKELY (n_bytes))
203 return glib_mem_vtable.try_malloc (n_bytes);
209 g_try_malloc0 (gsize n_bytes)
213 mem = g_try_malloc (n_bytes);
216 memset (mem, 0, n_bytes);
222 g_try_realloc (gpointer mem,
225 if (G_UNLIKELY (!g_mem_initialized))
226 g_mem_init_nomessage();
227 if (G_LIKELY (n_bytes))
228 return glib_mem_vtable.try_realloc (mem, n_bytes);
231 glib_mem_vtable.free (mem);
237 #define SIZE_OVERFLOWS(a,b) (G_UNLIKELY ((a) > G_MAXSIZE / (b)))
240 g_malloc_n (gsize n_blocks,
243 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
245 if (G_UNLIKELY (!g_mem_initialized))
246 g_mem_init_nomessage();
248 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
249 G_STRLOC, n_blocks, n_block_bytes);
252 return g_malloc (n_blocks * n_block_bytes);
256 g_malloc0_n (gsize n_blocks,
259 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
261 if (G_UNLIKELY (!g_mem_initialized))
262 g_mem_init_nomessage();
264 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
265 G_STRLOC, n_blocks, n_block_bytes);
268 return g_malloc0 (n_blocks * n_block_bytes);
272 g_realloc_n (gpointer mem,
276 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
278 if (G_UNLIKELY (!g_mem_initialized))
279 g_mem_init_nomessage();
281 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
282 G_STRLOC, n_blocks, n_block_bytes);
285 return g_realloc (mem, n_blocks * n_block_bytes);
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);
299 g_try_malloc0_n (gsize n_blocks,
302 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
305 return g_try_malloc0 (n_blocks * n_block_bytes);
309 g_try_realloc_n (gpointer mem,
313 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
316 return g_try_realloc (mem, n_blocks * n_block_bytes);
322 fallback_calloc (gsize n_blocks,
325 gsize l = n_blocks * n_block_bytes;
326 gpointer mem = glib_mem_vtable.malloc (l);
334 static gboolean vtable_set = FALSE;
337 * g_mem_is_system_malloc
339 * Checks whether the allocator used by g_malloc() is the system's
340 * malloc implementation. If it returns %TRUE memory allocated with
341 * malloc() can be used interchangeable with memory allocated using g_malloc().
342 * This function is useful for avoiding an extra copy of allocated memory returned
343 * by a non-GLib-based API.
345 * A different allocator can be set using g_mem_set_vtable().
347 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
350 g_mem_is_system_malloc (void)
356 g_mem_set_vtable (GMemVTable *vtable)
360 if (vtable->malloc && vtable->realloc && vtable->free)
362 glib_mem_vtable.malloc = vtable->malloc;
363 glib_mem_vtable.realloc = vtable->realloc;
364 glib_mem_vtable.free = vtable->free;
365 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
366 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
367 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
371 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
374 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
378 /* --- memory profiling and checking --- */
379 #ifdef G_DISABLE_CHECKS
380 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
385 #else /* !G_DISABLE_CHECKS */
392 static guint *profile_data = NULL;
393 static gsize profile_allocs = 0;
394 static gsize profile_zinit = 0;
395 static gsize profile_frees = 0;
396 static GMutex *gmem_profile_mutex = NULL;
397 #ifdef G_ENABLE_DEBUG
398 static volatile gsize g_trap_free_size = 0;
399 static volatile gsize g_trap_realloc_size = 0;
400 static volatile gsize g_trap_malloc_size = 0;
401 #endif /* G_ENABLE_DEBUG */
403 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
406 profiler_log (ProfilerJob job,
410 g_mutex_lock (gmem_profile_mutex);
413 profile_data = standard_calloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8,
414 sizeof (profile_data[0]));
415 if (!profile_data) /* memory system kiddin' me, eh? */
417 g_mutex_unlock (gmem_profile_mutex);
422 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
423 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
424 (job & PROFILER_RELOC) != 0,
427 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
428 (job & PROFILER_RELOC) != 0,
432 if (job & PROFILER_ALLOC)
434 profile_allocs += n_bytes;
435 if (job & PROFILER_ZINIT)
436 profile_zinit += n_bytes;
439 profile_frees += n_bytes;
441 g_mutex_unlock (gmem_profile_mutex);
445 profile_print_locked (guint *local_data,
448 gboolean need_header = TRUE;
451 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
453 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
454 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
455 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
456 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
458 if (!t_malloc && !t_realloc && !t_free && !t_refree)
460 else if (need_header)
463 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
464 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
465 g_print (" | malloc() | free() | realloc() | realloc() | \n");
466 g_print ("===========|============|============|============|============|===========\n");
468 if (i < MEM_PROFILE_TABLE_SIZE)
469 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
470 i, t_malloc, t_free, t_realloc, t_refree,
471 (t_malloc - t_free + t_realloc - t_refree) * i);
472 else if (i >= MEM_PROFILE_TABLE_SIZE)
473 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
474 i, t_malloc, t_free, t_realloc, t_refree);
477 g_print (" --- none ---\n");
483 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
488 if (G_UNLIKELY (!g_mem_initialized))
489 g_mem_init_nomessage();
491 g_mutex_lock (gmem_profile_mutex);
493 local_allocs = profile_allocs;
494 local_zinit = profile_zinit;
495 local_frees = profile_frees;
499 g_mutex_unlock (gmem_profile_mutex);
503 memcpy (local_data, profile_data,
504 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
506 g_mutex_unlock (gmem_profile_mutex);
508 g_print ("GLib Memory statistics (successful operations):\n");
509 profile_print_locked (local_data, TRUE);
510 g_print ("GLib Memory statistics (failing operations):\n");
511 profile_print_locked (local_data, FALSE);
512 g_print ("Total bytes: allocated=%"G_GSIZE_FORMAT", "
513 "zero-initialized=%"G_GSIZE_FORMAT" (%.2f%%), "
514 "freed=%"G_GSIZE_FORMAT" (%.2f%%), "
515 "remaining=%"G_GSIZE_FORMAT"\n",
518 ((gdouble) local_zinit) / local_allocs * 100.0,
520 ((gdouble) local_frees) / local_allocs * 100.0,
521 local_allocs - local_frees);
525 profiler_try_malloc (gsize n_bytes)
529 #ifdef G_ENABLE_DEBUG
530 if (g_trap_malloc_size == n_bytes)
532 #endif /* G_ENABLE_DEBUG */
534 p = standard_malloc (sizeof (gsize) * 2 + n_bytes);
538 p[0] = 0; /* free count */
539 p[1] = n_bytes; /* length */
540 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
544 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
550 profiler_malloc (gsize n_bytes)
552 gpointer mem = profiler_try_malloc (n_bytes);
561 profiler_calloc (gsize n_blocks,
564 gsize l = n_blocks * n_block_bytes;
567 #ifdef G_ENABLE_DEBUG
568 if (g_trap_malloc_size == l)
570 #endif /* G_ENABLE_DEBUG */
572 p = standard_calloc (1, sizeof (gsize) * 2 + l);
576 p[0] = 0; /* free count */
577 p[1] = l; /* length */
578 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
583 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
591 profiler_free (gpointer mem)
596 if (p[0]) /* free count */
598 g_warning ("free(%p): memory has been freed %"G_GSIZE_FORMAT" times already",
600 profiler_log (PROFILER_FREE,
606 #ifdef G_ENABLE_DEBUG
607 if (g_trap_free_size == p[1])
609 #endif /* G_ENABLE_DEBUG */
611 profiler_log (PROFILER_FREE,
614 memset (p + 2, 0xaa, p[1]);
616 /* for all those that miss standard_free (p); in this place, yes,
617 * we do leak all memory when profiling, and that is intentional
618 * to catch double frees. patch submissions are futile.
625 profiler_try_realloc (gpointer mem,
632 #ifdef G_ENABLE_DEBUG
633 if (g_trap_realloc_size == n_bytes)
635 #endif /* G_ENABLE_DEBUG */
637 if (mem && p[0]) /* free count */
639 g_warning ("realloc(%p, %"G_GSIZE_FORMAT"): "
640 "memory has been freed %"G_GSIZE_FORMAT" times already",
641 p + 2, (gsize) n_bytes, p[0]);
642 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
648 p = standard_realloc (mem ? p : NULL, sizeof (gsize) * 2 + n_bytes);
653 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
656 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
660 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
667 profiler_realloc (gpointer mem,
670 mem = profiler_try_realloc (mem, n_bytes);
678 static GMemVTable profiler_table = {
684 profiler_try_realloc,
686 GMemVTable *glib_mem_profiler_table = &profiler_table;
688 #endif /* !G_DISABLE_CHECKS */
690 /* --- MemChunks --- */
692 * SECTION: allocators
693 * @title: Memory Allocators
694 * @short_description: deprecated way to allocate chunks of memory for
695 * GList, GSList and GNode
697 * Prior to 2.10, #GAllocator was used as an efficient way to allocate
698 * small pieces of memory for use with the #GList, #GSList and #GNode
699 * data structures. Since 2.10, it has been completely replaced by the
700 * <link linkend="glib-Memory-Slices">slice allocator</link> and
705 * SECTION: memory_chunks
706 * @title: Memory Chunks
707 * @short_description: deprecated way to allocate groups of equal-sized
710 * Memory chunks provide an space-efficient way to allocate equal-sized
711 * pieces of memory, called atoms. However, due to the administrative
712 * overhead (in particular for #G_ALLOC_AND_FREE, and when used from
713 * multiple threads), they are in practise often slower than direct use
714 * of g_malloc(). Therefore, memory chunks have been deprecated in
715 * favor of the <link linkend="glib-Memory-Slices">slice
716 * allocator</link>, which has been added in 2.10. All internal uses of
717 * memory chunks in GLib have been converted to the
718 * <literal>g_slice</literal> API.
720 * There are two types of memory chunks, #G_ALLOC_ONLY, and
721 * #G_ALLOC_AND_FREE. <itemizedlist> <listitem><para> #G_ALLOC_ONLY
722 * chunks only allow allocation of atoms. The atoms can never be freed
723 * individually. The memory chunk can only be free in its entirety.
724 * </para></listitem> <listitem><para> #G_ALLOC_AND_FREE chunks do
725 * allow atoms to be freed individually. The disadvantage of this is
726 * that the memory chunk has to keep track of which atoms have been
727 * freed. This results in more memory being used and a slight
728 * degradation in performance. </para></listitem> </itemizedlist>
730 * To create a memory chunk use g_mem_chunk_new() or the convenience
731 * macro g_mem_chunk_create().
733 * To allocate a new atom use g_mem_chunk_alloc(),
734 * g_mem_chunk_alloc0(), or the convenience macros g_chunk_new() or
737 * To free an atom use g_mem_chunk_free(), or the convenience macro
738 * g_chunk_free(). (Atoms can only be freed if the memory chunk is
739 * created with the type set to #G_ALLOC_AND_FREE.)
741 * To free any blocks of memory which are no longer being used, use
742 * g_mem_chunk_clean(). To clean all memory chunks, use g_blow_chunks().
744 * To reset the memory chunk, freeing all of the atoms, use
745 * g_mem_chunk_reset().
747 * To destroy a memory chunk, use g_mem_chunk_destroy().
749 * To help debug memory chunks, use g_mem_chunk_info() and
750 * g_mem_chunk_print().
753 * <title>Using a #GMemChunk</title>
755 * GMemChunk *mem_chunk;
759 * /<!-- -->* Create a GMemChunk with atoms 50 bytes long, and memory
760 * blocks holding 100 bytes. Note that this means that only 2 atoms
761 * fit into each memory block and so isn't very efficient. *<!-- -->/
762 * mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
763 * /<!-- -->* Now allocate 10000 atoms. *<!-- -->/
764 * for (i = 0; i < 10000; i++)
766 * mem[i] = g_chunk_new (gchar, mem_chunk);
767 * /<!-- -->* Fill in the atom memory with some junk. *<!-- -->/
768 * for (j = 0; j < 50; j++)
771 * /<!-- -->* Now free all of the atoms. Note that since we are going to
772 * destroy the GMemChunk, this wouldn't normally be used. *<!-- -->/
773 * for (i = 0; i < 10000; i++)
775 * g_mem_chunk_free (mem_chunk, mem[i]);
777 * /<!-- -->* We are finished with the GMemChunk, so we destroy it. *<!-- -->/
778 * g_mem_chunk_destroy (mem_chunk);
783 * <title>Using a #GMemChunk with data structures</title>
785 * GMemChunk *array_mem_chunk;
787 * /<!-- -->* Create a GMemChunk to hold GRealArray structures, using
788 * the g_mem_chunk_create(<!-- -->) convenience macro. We want 1024 atoms in each
789 * memory block, and we want to be able to free individual atoms. *<!-- -->/
790 * array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE);
791 * /<!-- -->* Allocate one atom, using the g_chunk_new(<!-- -->) convenience macro. *<!-- -->/
792 * array = g_chunk_new (GRealArray, array_mem_chunk);
793 * /<!-- -->* We can now use array just like a normal pointer to a structure. *<!-- -->/
794 * array->data = NULL;
797 * array->zero_terminated = (zero_terminated ? 1 : 0);
798 * array->clear = (clear ? 1 : 0);
799 * array->elt_size = elt_size;
800 * /<!-- -->* We can free the element, so it can be reused. *<!-- -->/
801 * g_chunk_free (array, array_mem_chunk);
802 * /<!-- -->* We destroy the GMemChunk when we are finished with it. *<!-- -->/
803 * g_mem_chunk_destroy (array_mem_chunk);
808 #ifndef G_ALLOC_AND_FREE
813 * The #GAllocator struct contains private data. and should only be
814 * accessed using the following functions.
816 typedef struct _GAllocator GAllocator;
821 * The #GMemChunk struct is an opaque data structure representing a
822 * memory chunk. It should be accessed only through the use of the
823 * following functions.
825 typedef struct _GMemChunk GMemChunk;
830 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
831 * g_mem_chunk_create() to specify that atoms will never be freed
834 #define G_ALLOC_ONLY 1
839 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
840 * g_mem_chunk_create() to specify that atoms will be freed
843 #define G_ALLOC_AND_FREE 2
847 guint alloc_size; /* the size of an atom */
852 * @name: a string to identify the #GMemChunk. It is not copied so it
853 * should be valid for the lifetime of the #GMemChunk. It is
854 * only used in g_mem_chunk_print(), which is used for debugging.
855 * @atom_size: the size, in bytes, of each element in the #GMemChunk.
856 * @area_size: the size, in bytes, of each block of memory allocated to
858 * @type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used if the
859 * atoms will be freed individually. #G_ALLOC_ONLY should be
860 * used if atoms will never be freed individually.
861 * #G_ALLOC_ONLY is quicker, since it does not need to track
862 * free atoms, but it obviously wastes memory if you no longer
863 * need many of the atoms.
864 * @Returns: the new #GMemChunk.
866 * Creates a new #GMemChunk.
868 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
869 * allocator</link> instead
872 g_mem_chunk_new (const gchar *name,
877 GMemChunk *mem_chunk;
878 g_return_val_if_fail (atom_size > 0, NULL);
880 mem_chunk = g_slice_new (GMemChunk);
881 mem_chunk->alloc_size = atom_size;
886 * g_mem_chunk_destroy:
887 * @mem_chunk: a #GMemChunk.
889 * Frees all of the memory allocated for a #GMemChunk.
891 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
892 * allocator</link> instead
895 g_mem_chunk_destroy (GMemChunk *mem_chunk)
897 g_return_if_fail (mem_chunk != NULL);
899 g_slice_free (GMemChunk, mem_chunk);
904 * @mem_chunk: a #GMemChunk.
905 * @Returns: a pointer to the allocated atom.
907 * Allocates an atom of memory from a #GMemChunk.
909 * Deprecated:2.10: Use g_slice_alloc() instead
912 g_mem_chunk_alloc (GMemChunk *mem_chunk)
914 g_return_val_if_fail (mem_chunk != NULL, NULL);
916 return g_slice_alloc (mem_chunk->alloc_size);
920 * g_mem_chunk_alloc0:
921 * @mem_chunk: a #GMemChunk.
922 * @Returns: a pointer to the allocated atom.
924 * Allocates an atom of memory from a #GMemChunk, setting the memory to
927 * Deprecated:2.10: Use g_slice_alloc0() instead
930 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
932 g_return_val_if_fail (mem_chunk != NULL, NULL);
934 return g_slice_alloc0 (mem_chunk->alloc_size);
939 * @mem_chunk: a #GMemChunk.
940 * @mem: a pointer to the atom to free.
942 * Frees an atom in a #GMemChunk. This should only be called if the
943 * #GMemChunk was created with #G_ALLOC_AND_FREE. Otherwise it will
946 * Deprecated:2.10: Use g_slice_free1() instead
949 g_mem_chunk_free (GMemChunk *mem_chunk,
952 g_return_if_fail (mem_chunk != NULL);
954 g_slice_free1 (mem_chunk->alloc_size, mem);
959 * @mem_chunk: a #GMemChunk.
961 * Frees any blocks in a #GMemChunk which are no longer being used.
963 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
964 * allocator</link> instead
966 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
970 * @mem_chunk: a #GMemChunk.
972 * Resets a GMemChunk to its initial state. It frees all of the
973 * currently allocated blocks of memory.
975 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
976 * allocator</link> instead
978 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
983 * @mem_chunk: a #GMemChunk.
985 * Outputs debugging information for a #GMemChunk. It outputs the name
986 * of the #GMemChunk (set with g_mem_chunk_new()), the number of bytes
987 * used, and the number of blocks of memory allocated.
989 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
990 * allocator</link> instead
992 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
998 * Outputs debugging information for all #GMemChunk objects currently
999 * in use. It outputs the number of #GMemChunk objects currently
1000 * allocated, and calls g_mem_chunk_print() to output information on
1003 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1004 * allocator</link> instead
1006 void g_mem_chunk_info (void) {}
1011 * Calls g_mem_chunk_clean() on all #GMemChunk objects.
1013 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1014 * allocator</link> instead
1016 void g_blow_chunks (void) {}
1020 * @type: the type of the #GMemChunk atoms, typically a structure name.
1021 * @chunk: a #GMemChunk.
1022 * @Returns: a pointer to the allocated atom, cast to a pointer to
1025 * A convenience macro to allocate an atom of memory from a #GMemChunk.
1026 * It calls g_mem_chunk_alloc0() and casts the returned atom to a
1027 * pointer to the given type, avoiding a type cast in the source code.
1029 * Deprecated:2.10: Use g_slice_new0() instead
1034 * @mem: a pointer to the atom to be freed.
1035 * @mem_chunk: a #GMemChunk.
1037 * A convenience macro to free an atom of memory from a #GMemChunk. It
1038 * simply switches the arguments and calls g_mem_chunk_free() It is
1039 * included simply to complement the other convenience macros,
1040 * g_chunk_new() and g_chunk_new0().
1042 * Deprecated:2.10: Use g_slice_free() instead
1047 * @type: the type of the #GMemChunk atoms, typically a structure name.
1048 * @chunk: a #GMemChunk.
1049 * @Returns: a pointer to the allocated atom, cast to a pointer to
1052 * A convenience macro to allocate an atom of memory from a #GMemChunk.
1053 * It calls g_mem_chunk_alloc() and casts the returned atom to a
1054 * pointer to the given type, avoiding a type cast in the source code.
1056 * Deprecated:2.10: Use g_slice_new() instead
1060 * g_mem_chunk_create:
1061 * @type: the type of the atoms, typically a structure name.
1062 * @pre_alloc: the number of atoms to store in each block of memory.
1063 * @alloc_type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used
1064 * if the atoms will be freed individually. #G_ALLOC_ONLY
1065 * should be used if atoms will never be freed
1066 * individually. #G_ALLOC_ONLY is quicker, since it does
1067 * not need to track free atoms, but it obviously wastes
1068 * memory if you no longer need many of the atoms.
1069 * @Returns: the new #GMemChunk.
1071 * A convenience macro for creating a new #GMemChunk. It calls
1072 * g_mem_chunk_new(), using the given type to create the #GMemChunk
1073 * name. The atom size is determined using
1074 * <function>sizeof()</function>, and the area size is calculated by
1075 * multiplying the @pre_alloc parameter with the atom size.
1077 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1078 * allocator</link> instead
1084 * @name: the name of the #GAllocator. This name is used to set the
1085 * name of the #GMemChunk used by the #GAllocator, and is only
1086 * used for debugging.
1087 * @n_preallocs: the number of elements in each block of memory
1088 * allocated. Larger blocks mean less calls to
1089 * g_malloc(), but some memory may be wasted. (GLib uses
1090 * 128 elements per block by default.) The value must be
1091 * between 1 and 65535.
1092 * @Returns: a new #GAllocator.
1094 * Creates a new #GAllocator.
1096 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1097 * allocator</link> instead
1100 g_allocator_new (const gchar *name,
1103 static struct _GAllocator {
1105 guint16 n_preallocs;
1106 guint is_unused : 1;
1109 GMemChunk *mem_chunk;
1112 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
1114 /* some (broken) GAllocator uses depend on non-NULL allocators */
1115 return (void*) &dummy;
1120 * @allocator: a #GAllocator.
1122 * Frees all of the memory allocated by the #GAllocator.
1124 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1125 * allocator</link> instead
1128 g_allocator_free (GAllocator *allocator)
1132 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
1133 gboolean g_mem_gc_friendly = TRUE;
1135 gboolean g_mem_gc_friendly = FALSE;
1139 g_mem_init_nomessage (void)
1143 const GDebugKey keys[] = {
1144 { "gc-friendly", 1 },
1147 if (g_mem_initialized)
1149 /* don't use g_malloc/g_message here */
1150 val = _g_getenv_nomalloc ("G_DEBUG", buffer);
1151 flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1152 if (flags & 1) /* gc-friendly */
1154 g_mem_gc_friendly = TRUE;
1156 g_mem_initialized = TRUE;
1160 _g_mem_thread_init_noprivate_nomessage (void)
1162 /* we may only create mutexes here, locking/
1163 * unlocking a mutex does not yet work.
1165 g_mem_init_nomessage();
1166 #ifndef G_DISABLE_CHECKS
1167 gmem_profile_mutex = g_mutex_new ();
1172 #include "galiasdef.c"