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"
42 #define MEM_PROFILE_TABLE_SIZE 4096
46 * having G_DISABLE_CHECKS defined disables use of glib_mem_profiler_table and
48 * REALLOC_0_WORKS is defined if g_realloc (NULL, x) works.
49 * SANE_MALLOC_PROTOS is defined if the systems malloc() and friends functions
50 * match the corresponding GLib prototypes, keep configure.in and gmem.h in sync here.
51 * g_mem_gc_friendly is TRUE, freed memory should be 0-wiped.
54 /* --- prototypes --- */
55 static gboolean g_mem_initialized = FALSE;
56 static void g_mem_init_nomessage (void);
59 /* --- malloc wrappers --- */
60 #ifndef REALLOC_0_WORKS
62 standard_realloc (gpointer mem,
66 return malloc (n_bytes);
68 return realloc (mem, n_bytes);
70 #endif /* !REALLOC_0_WORKS */
72 #ifdef SANE_MALLOC_PROTOS
73 # define standard_malloc malloc
74 # ifdef REALLOC_0_WORKS
75 # define standard_realloc realloc
76 # endif /* REALLOC_0_WORKS */
77 # define standard_free free
78 # define standard_calloc calloc
79 # define standard_try_malloc malloc
80 # define standard_try_realloc realloc
81 #else /* !SANE_MALLOC_PROTOS */
83 standard_malloc (gsize n_bytes)
85 return malloc (n_bytes);
87 # ifdef REALLOC_0_WORKS
89 standard_realloc (gpointer mem,
92 return realloc (mem, n_bytes);
94 # endif /* REALLOC_0_WORKS */
96 standard_free (gpointer mem)
101 standard_calloc (gsize n_blocks,
104 return calloc (n_blocks, n_bytes);
106 #define standard_try_malloc standard_malloc
107 #define standard_try_realloc standard_realloc
108 #endif /* !SANE_MALLOC_PROTOS */
111 /* --- variables --- */
112 static GMemVTable glib_mem_vtable = {
118 standard_try_realloc,
122 /* --- functions --- */
124 g_malloc (gsize n_bytes)
126 if (G_UNLIKELY (!g_mem_initialized))
127 g_mem_init_nomessage();
128 if (G_LIKELY (n_bytes))
132 mem = glib_mem_vtable.malloc (n_bytes);
136 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
144 g_malloc0 (gsize n_bytes)
146 if (G_UNLIKELY (!g_mem_initialized))
147 g_mem_init_nomessage();
148 if (G_LIKELY (n_bytes))
152 mem = glib_mem_vtable.calloc (1, n_bytes);
156 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
164 g_realloc (gpointer mem,
167 if (G_UNLIKELY (!g_mem_initialized))
168 g_mem_init_nomessage();
169 if (G_LIKELY (n_bytes))
171 mem = glib_mem_vtable.realloc (mem, n_bytes);
175 g_error ("%s: failed to allocate %"G_GSIZE_FORMAT" bytes",
180 glib_mem_vtable.free (mem);
186 g_free (gpointer mem)
188 if (G_UNLIKELY (!g_mem_initialized))
189 g_mem_init_nomessage();
191 glib_mem_vtable.free (mem);
195 g_try_malloc (gsize n_bytes)
197 if (G_UNLIKELY (!g_mem_initialized))
198 g_mem_init_nomessage();
199 if (G_LIKELY (n_bytes))
200 return glib_mem_vtable.try_malloc (n_bytes);
206 g_try_malloc0 (gsize n_bytes)
210 mem = g_try_malloc (n_bytes);
213 memset (mem, 0, n_bytes);
219 g_try_realloc (gpointer mem,
222 if (G_UNLIKELY (!g_mem_initialized))
223 g_mem_init_nomessage();
224 if (G_LIKELY (n_bytes))
225 return glib_mem_vtable.try_realloc (mem, n_bytes);
228 glib_mem_vtable.free (mem);
234 #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);
253 g_malloc0_n (gsize n_blocks,
256 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
258 if (G_UNLIKELY (!g_mem_initialized))
259 g_mem_init_nomessage();
261 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
262 G_STRLOC, n_blocks, n_block_bytes);
265 return g_malloc0 (n_blocks * n_block_bytes);
269 g_realloc_n (gpointer mem,
273 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
275 if (G_UNLIKELY (!g_mem_initialized))
276 g_mem_init_nomessage();
278 g_error ("%s: overflow allocating %"G_GSIZE_FORMAT"*%"G_GSIZE_FORMAT" bytes",
279 G_STRLOC, n_blocks, n_block_bytes);
282 return g_realloc (mem, n_blocks * n_block_bytes);
286 g_try_malloc_n (gsize n_blocks,
289 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
292 return g_try_malloc (n_blocks * n_block_bytes);
296 g_try_malloc0_n (gsize n_blocks,
299 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
302 return g_try_malloc0 (n_blocks * n_block_bytes);
306 g_try_realloc_n (gpointer mem,
310 if (SIZE_OVERFLOWS (n_blocks, n_block_bytes))
313 return g_try_realloc (mem, n_blocks * n_block_bytes);
319 fallback_calloc (gsize n_blocks,
322 gsize l = n_blocks * n_block_bytes;
323 gpointer mem = glib_mem_vtable.malloc (l);
331 static gboolean vtable_set = FALSE;
334 * g_mem_is_system_malloc
336 * Checks whether the allocator used by g_malloc() is the system's
337 * malloc implementation. If it returns %TRUE memory allocated with
338 * malloc() can be used interchangeable with memory allocated using g_malloc().
339 * This function is useful for avoiding an extra copy of allocated memory returned
340 * by a non-GLib-based API.
342 * A different allocator can be set using g_mem_set_vtable().
344 * Return value: if %TRUE, malloc() and g_malloc() can be mixed.
347 g_mem_is_system_malloc (void)
353 g_mem_set_vtable (GMemVTable *vtable)
357 if (vtable->malloc && vtable->realloc && vtable->free)
359 glib_mem_vtable.malloc = vtable->malloc;
360 glib_mem_vtable.realloc = vtable->realloc;
361 glib_mem_vtable.free = vtable->free;
362 glib_mem_vtable.calloc = vtable->calloc ? vtable->calloc : fallback_calloc;
363 glib_mem_vtable.try_malloc = vtable->try_malloc ? vtable->try_malloc : glib_mem_vtable.malloc;
364 glib_mem_vtable.try_realloc = vtable->try_realloc ? vtable->try_realloc : glib_mem_vtable.realloc;
368 g_warning (G_STRLOC ": memory allocation vtable lacks one of malloc(), realloc() or free()");
371 g_warning (G_STRLOC ": memory allocation vtable can only be set once at startup");
375 /* --- memory profiling and checking --- */
376 #ifdef G_DISABLE_CHECKS
377 GMemVTable *glib_mem_profiler_table = &glib_mem_vtable;
382 #else /* !G_DISABLE_CHECKS */
389 static guint *profile_data = NULL;
390 static gsize profile_allocs = 0;
391 static gsize profile_zinit = 0;
392 static gsize profile_frees = 0;
393 static GMutex *gmem_profile_mutex = NULL;
394 #ifdef G_ENABLE_DEBUG
395 static volatile gsize g_trap_free_size = 0;
396 static volatile gsize g_trap_realloc_size = 0;
397 static volatile gsize g_trap_malloc_size = 0;
398 #endif /* G_ENABLE_DEBUG */
400 #define PROFILE_TABLE(f1,f2,f3) ( ( ((f3) << 2) | ((f2) << 1) | (f1) ) * (MEM_PROFILE_TABLE_SIZE + 1))
403 profiler_log (ProfilerJob job,
407 g_mutex_lock (gmem_profile_mutex);
410 profile_data = standard_calloc ((MEM_PROFILE_TABLE_SIZE + 1) * 8,
411 sizeof (profile_data[0]));
412 if (!profile_data) /* memory system kiddin' me, eh? */
414 g_mutex_unlock (gmem_profile_mutex);
419 if (n_bytes < MEM_PROFILE_TABLE_SIZE)
420 profile_data[n_bytes + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
421 (job & PROFILER_RELOC) != 0,
424 profile_data[MEM_PROFILE_TABLE_SIZE + PROFILE_TABLE ((job & PROFILER_ALLOC) != 0,
425 (job & PROFILER_RELOC) != 0,
429 if (job & PROFILER_ALLOC)
431 profile_allocs += n_bytes;
432 if (job & PROFILER_ZINIT)
433 profile_zinit += n_bytes;
436 profile_frees += n_bytes;
438 g_mutex_unlock (gmem_profile_mutex);
442 profile_print_locked (guint *local_data,
445 gboolean need_header = TRUE;
448 for (i = 0; i <= MEM_PROFILE_TABLE_SIZE; i++)
450 glong t_malloc = local_data[i + PROFILE_TABLE (1, 0, success)];
451 glong t_realloc = local_data[i + PROFILE_TABLE (1, 1, success)];
452 glong t_free = local_data[i + PROFILE_TABLE (0, 0, success)];
453 glong t_refree = local_data[i + PROFILE_TABLE (0, 1, success)];
455 if (!t_malloc && !t_realloc && !t_free && !t_refree)
457 else if (need_header)
460 g_print (" blocks of | allocated | freed | allocated | freed | n_bytes \n");
461 g_print (" n_bytes | n_times by | n_times by | n_times by | n_times by | remaining \n");
462 g_print (" | malloc() | free() | realloc() | realloc() | \n");
463 g_print ("===========|============|============|============|============|===========\n");
465 if (i < MEM_PROFILE_TABLE_SIZE)
466 g_print ("%10u | %10ld | %10ld | %10ld | %10ld |%+11ld\n",
467 i, t_malloc, t_free, t_realloc, t_refree,
468 (t_malloc - t_free + t_realloc - t_refree) * i);
469 else if (i >= MEM_PROFILE_TABLE_SIZE)
470 g_print (" >%6u | %10ld | %10ld | %10ld | %10ld | ***\n",
471 i, t_malloc, t_free, t_realloc, t_refree);
474 g_print (" --- none ---\n");
480 guint local_data[(MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0])];
485 if (G_UNLIKELY (!g_mem_initialized))
486 g_mem_init_nomessage();
488 g_mutex_lock (gmem_profile_mutex);
490 local_allocs = profile_allocs;
491 local_zinit = profile_zinit;
492 local_frees = profile_frees;
496 g_mutex_unlock (gmem_profile_mutex);
500 memcpy (local_data, profile_data,
501 (MEM_PROFILE_TABLE_SIZE + 1) * 8 * sizeof (profile_data[0]));
503 g_mutex_unlock (gmem_profile_mutex);
505 g_print ("GLib Memory statistics (successful operations):\n");
506 profile_print_locked (local_data, TRUE);
507 g_print ("GLib Memory statistics (failing operations):\n");
508 profile_print_locked (local_data, FALSE);
509 g_print ("Total bytes: allocated=%"G_GSIZE_FORMAT", "
510 "zero-initialized=%"G_GSIZE_FORMAT" (%.2f%%), "
511 "freed=%"G_GSIZE_FORMAT" (%.2f%%), "
512 "remaining=%"G_GSIZE_FORMAT"\n",
515 ((gdouble) local_zinit) / local_allocs * 100.0,
517 ((gdouble) local_frees) / local_allocs * 100.0,
518 local_allocs - local_frees);
522 profiler_try_malloc (gsize n_bytes)
526 #ifdef G_ENABLE_DEBUG
527 if (g_trap_malloc_size == n_bytes)
529 #endif /* G_ENABLE_DEBUG */
531 p = standard_malloc (sizeof (gsize) * 2 + n_bytes);
535 p[0] = 0; /* free count */
536 p[1] = n_bytes; /* length */
537 profiler_log (PROFILER_ALLOC, n_bytes, TRUE);
541 profiler_log (PROFILER_ALLOC, n_bytes, FALSE);
547 profiler_malloc (gsize n_bytes)
549 gpointer mem = profiler_try_malloc (n_bytes);
558 profiler_calloc (gsize n_blocks,
561 gsize l = n_blocks * n_block_bytes;
564 #ifdef G_ENABLE_DEBUG
565 if (g_trap_malloc_size == l)
567 #endif /* G_ENABLE_DEBUG */
569 p = standard_calloc (1, sizeof (gsize) * 2 + l);
573 p[0] = 0; /* free count */
574 p[1] = l; /* length */
575 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, TRUE);
580 profiler_log (PROFILER_ALLOC | PROFILER_ZINIT, l, FALSE);
588 profiler_free (gpointer mem)
593 if (p[0]) /* free count */
595 g_warning ("free(%p): memory has been freed %"G_GSIZE_FORMAT" times already",
597 profiler_log (PROFILER_FREE,
603 #ifdef G_ENABLE_DEBUG
604 if (g_trap_free_size == p[1])
606 #endif /* G_ENABLE_DEBUG */
608 profiler_log (PROFILER_FREE,
611 memset (p + 2, 0xaa, p[1]);
613 /* for all those that miss standard_free (p); in this place, yes,
614 * we do leak all memory when profiling, and that is intentional
615 * to catch double frees. patch submissions are futile.
622 profiler_try_realloc (gpointer mem,
629 #ifdef G_ENABLE_DEBUG
630 if (g_trap_realloc_size == n_bytes)
632 #endif /* G_ENABLE_DEBUG */
634 if (mem && p[0]) /* free count */
636 g_warning ("realloc(%p, %"G_GSIZE_FORMAT"): "
637 "memory has been freed %"G_GSIZE_FORMAT" times already",
638 p + 2, (gsize) n_bytes, p[0]);
639 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
645 p = standard_realloc (mem ? p : NULL, sizeof (gsize) * 2 + n_bytes);
650 profiler_log (PROFILER_FREE | PROFILER_RELOC, p[1], TRUE);
653 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, p[1], TRUE);
657 profiler_log (PROFILER_ALLOC | PROFILER_RELOC, n_bytes, FALSE);
664 profiler_realloc (gpointer mem,
667 mem = profiler_try_realloc (mem, n_bytes);
675 static GMemVTable profiler_table = {
681 profiler_try_realloc,
683 GMemVTable *glib_mem_profiler_table = &profiler_table;
685 #endif /* !G_DISABLE_CHECKS */
687 /* --- MemChunks --- */
689 * SECTION: allocators
690 * @title: Memory Allocators
691 * @short_description: deprecated way to allocate chunks of memory for
692 * GList, GSList and GNode
694 * Prior to 2.10, #GAllocator was used as an efficient way to allocate
695 * small pieces of memory for use with the #GList, #GSList and #GNode
696 * data structures. Since 2.10, it has been completely replaced by the
697 * <link linkend="glib-Memory-Slices">slice allocator</link> and
702 * SECTION: memory_chunks
703 * @title: Memory Chunks
704 * @short_description: deprecated way to allocate groups of equal-sized
707 * Memory chunks provide an space-efficient way to allocate equal-sized
708 * pieces of memory, called atoms. However, due to the administrative
709 * overhead (in particular for #G_ALLOC_AND_FREE, and when used from
710 * multiple threads), they are in practise often slower than direct use
711 * of g_malloc(). Therefore, memory chunks have been deprecated in
712 * favor of the <link linkend="glib-Memory-Slices">slice
713 * allocator</link>, which has been added in 2.10. All internal uses of
714 * memory chunks in GLib have been converted to the
715 * <literal>g_slice</literal> API.
717 * There are two types of memory chunks, #G_ALLOC_ONLY, and
718 * #G_ALLOC_AND_FREE. <itemizedlist> <listitem><para> #G_ALLOC_ONLY
719 * chunks only allow allocation of atoms. The atoms can never be freed
720 * individually. The memory chunk can only be free in its entirety.
721 * </para></listitem> <listitem><para> #G_ALLOC_AND_FREE chunks do
722 * allow atoms to be freed individually. The disadvantage of this is
723 * that the memory chunk has to keep track of which atoms have been
724 * freed. This results in more memory being used and a slight
725 * degradation in performance. </para></listitem> </itemizedlist>
727 * To create a memory chunk use g_mem_chunk_new() or the convenience
728 * macro g_mem_chunk_create().
730 * To allocate a new atom use g_mem_chunk_alloc(),
731 * g_mem_chunk_alloc0(), or the convenience macros g_chunk_new() or
734 * To free an atom use g_mem_chunk_free(), or the convenience macro
735 * g_chunk_free(). (Atoms can only be freed if the memory chunk is
736 * created with the type set to #G_ALLOC_AND_FREE.)
738 * To free any blocks of memory which are no longer being used, use
739 * g_mem_chunk_clean(). To clean all memory chunks, use g_blow_chunks().
741 * To reset the memory chunk, freeing all of the atoms, use
742 * g_mem_chunk_reset().
744 * To destroy a memory chunk, use g_mem_chunk_destroy().
746 * To help debug memory chunks, use g_mem_chunk_info() and
747 * g_mem_chunk_print().
750 * <title>Using a #GMemChunk</title>
752 * GMemChunk *mem_chunk;
756 * /<!-- -->* Create a GMemChunk with atoms 50 bytes long, and memory
757 * blocks holding 100 bytes. Note that this means that only 2 atoms
758 * fit into each memory block and so isn't very efficient. *<!-- -->/
759 * mem_chunk = g_mem_chunk_new ("test mem chunk", 50, 100, G_ALLOC_AND_FREE);
760 * /<!-- -->* Now allocate 10000 atoms. *<!-- -->/
761 * for (i = 0; i < 10000; i++)
763 * mem[i] = g_chunk_new (gchar, mem_chunk);
764 * /<!-- -->* Fill in the atom memory with some junk. *<!-- -->/
765 * for (j = 0; j < 50; j++)
768 * /<!-- -->* Now free all of the atoms. Note that since we are going to
769 * destroy the GMemChunk, this wouldn't normally be used. *<!-- -->/
770 * for (i = 0; i < 10000; i++)
772 * g_mem_chunk_free (mem_chunk, mem[i]);
774 * /<!-- -->* We are finished with the GMemChunk, so we destroy it. *<!-- -->/
775 * g_mem_chunk_destroy (mem_chunk);
780 * <title>Using a #GMemChunk with data structures</title>
782 * GMemChunk *array_mem_chunk;
784 * /<!-- -->* Create a GMemChunk to hold GRealArray structures, using
785 * the g_mem_chunk_create(<!-- -->) convenience macro. We want 1024 atoms in each
786 * memory block, and we want to be able to free individual atoms. *<!-- -->/
787 * array_mem_chunk = g_mem_chunk_create (GRealArray, 1024, G_ALLOC_AND_FREE);
788 * /<!-- -->* Allocate one atom, using the g_chunk_new(<!-- -->) convenience macro. *<!-- -->/
789 * array = g_chunk_new (GRealArray, array_mem_chunk);
790 * /<!-- -->* We can now use array just like a normal pointer to a structure. *<!-- -->/
791 * array->data = NULL;
794 * array->zero_terminated = (zero_terminated ? 1 : 0);
795 * array->clear = (clear ? 1 : 0);
796 * array->elt_size = elt_size;
797 * /<!-- -->* We can free the element, so it can be reused. *<!-- -->/
798 * g_chunk_free (array, array_mem_chunk);
799 * /<!-- -->* We destroy the GMemChunk when we are finished with it. *<!-- -->/
800 * g_mem_chunk_destroy (array_mem_chunk);
805 #ifndef G_ALLOC_AND_FREE
810 * The #GAllocator struct contains private data. and should only be
811 * accessed using the following functions.
813 typedef struct _GAllocator GAllocator;
818 * The #GMemChunk struct is an opaque data structure representing a
819 * memory chunk. It should be accessed only through the use of the
820 * following functions.
822 typedef struct _GMemChunk GMemChunk;
827 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
828 * g_mem_chunk_create() to specify that atoms will never be freed
831 #define G_ALLOC_ONLY 1
836 * Specifies the type of a #GMemChunk. Used in g_mem_chunk_new() and
837 * g_mem_chunk_create() to specify that atoms will be freed
840 #define G_ALLOC_AND_FREE 2
844 guint alloc_size; /* the size of an atom */
849 * @name: a string to identify the #GMemChunk. It is not copied so it
850 * should be valid for the lifetime of the #GMemChunk. It is
851 * only used in g_mem_chunk_print(), which is used for debugging.
852 * @atom_size: the size, in bytes, of each element in the #GMemChunk.
853 * @area_size: the size, in bytes, of each block of memory allocated to
855 * @type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used if the
856 * atoms will be freed individually. #G_ALLOC_ONLY should be
857 * used if atoms will never be freed individually.
858 * #G_ALLOC_ONLY is quicker, since it does not need to track
859 * free atoms, but it obviously wastes memory if you no longer
860 * need many of the atoms.
861 * @Returns: the new #GMemChunk.
863 * Creates a new #GMemChunk.
865 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
866 * allocator</link> instead
869 g_mem_chunk_new (const gchar *name,
874 GMemChunk *mem_chunk;
875 g_return_val_if_fail (atom_size > 0, NULL);
877 mem_chunk = g_slice_new (GMemChunk);
878 mem_chunk->alloc_size = atom_size;
883 * g_mem_chunk_destroy:
884 * @mem_chunk: a #GMemChunk.
886 * Frees all of the memory allocated for a #GMemChunk.
888 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
889 * allocator</link> instead
892 g_mem_chunk_destroy (GMemChunk *mem_chunk)
894 g_return_if_fail (mem_chunk != NULL);
896 g_slice_free (GMemChunk, mem_chunk);
901 * @mem_chunk: a #GMemChunk.
902 * @Returns: a pointer to the allocated atom.
904 * Allocates an atom of memory from a #GMemChunk.
906 * Deprecated:2.10: Use g_slice_alloc() instead
909 g_mem_chunk_alloc (GMemChunk *mem_chunk)
911 g_return_val_if_fail (mem_chunk != NULL, NULL);
913 return g_slice_alloc (mem_chunk->alloc_size);
917 * g_mem_chunk_alloc0:
918 * @mem_chunk: a #GMemChunk.
919 * @Returns: a pointer to the allocated atom.
921 * Allocates an atom of memory from a #GMemChunk, setting the memory to
924 * Deprecated:2.10: Use g_slice_alloc0() instead
927 g_mem_chunk_alloc0 (GMemChunk *mem_chunk)
929 g_return_val_if_fail (mem_chunk != NULL, NULL);
931 return g_slice_alloc0 (mem_chunk->alloc_size);
936 * @mem_chunk: a #GMemChunk.
937 * @mem: a pointer to the atom to free.
939 * Frees an atom in a #GMemChunk. This should only be called if the
940 * #GMemChunk was created with #G_ALLOC_AND_FREE. Otherwise it will
943 * Deprecated:2.10: Use g_slice_free1() instead
946 g_mem_chunk_free (GMemChunk *mem_chunk,
949 g_return_if_fail (mem_chunk != NULL);
951 g_slice_free1 (mem_chunk->alloc_size, mem);
956 * @mem_chunk: a #GMemChunk.
958 * Frees any blocks in a #GMemChunk which are no longer being used.
960 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
961 * allocator</link> instead
963 void g_mem_chunk_clean (GMemChunk *mem_chunk) {}
967 * @mem_chunk: a #GMemChunk.
969 * Resets a GMemChunk to its initial state. It frees all of the
970 * currently allocated blocks of memory.
972 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
973 * allocator</link> instead
975 void g_mem_chunk_reset (GMemChunk *mem_chunk) {}
980 * @mem_chunk: a #GMemChunk.
982 * Outputs debugging information for a #GMemChunk. It outputs the name
983 * of the #GMemChunk (set with g_mem_chunk_new()), the number of bytes
984 * used, and the number of blocks of memory allocated.
986 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
987 * allocator</link> instead
989 void g_mem_chunk_print (GMemChunk *mem_chunk) {}
995 * Outputs debugging information for all #GMemChunk objects currently
996 * in use. It outputs the number of #GMemChunk objects currently
997 * allocated, and calls g_mem_chunk_print() to output information on
1000 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1001 * allocator</link> instead
1003 void g_mem_chunk_info (void) {}
1008 * Calls g_mem_chunk_clean() on all #GMemChunk objects.
1010 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1011 * allocator</link> instead
1013 void g_blow_chunks (void) {}
1017 * @type: the type of the #GMemChunk atoms, typically a structure name.
1018 * @chunk: a #GMemChunk.
1019 * @Returns: a pointer to the allocated atom, cast to a pointer to
1022 * A convenience macro to allocate an atom of memory from a #GMemChunk.
1023 * It calls g_mem_chunk_alloc0() and casts the returned atom to a
1024 * pointer to the given type, avoiding a type cast in the source code.
1026 * Deprecated:2.10: Use g_slice_new0() instead
1031 * @mem: a pointer to the atom to be freed.
1032 * @mem_chunk: a #GMemChunk.
1034 * A convenience macro to free an atom of memory from a #GMemChunk. It
1035 * simply switches the arguments and calls g_mem_chunk_free() It is
1036 * included simply to complement the other convenience macros,
1037 * g_chunk_new() and g_chunk_new0().
1039 * Deprecated:2.10: Use g_slice_free() instead
1044 * @type: the type of the #GMemChunk atoms, typically a structure name.
1045 * @chunk: a #GMemChunk.
1046 * @Returns: a pointer to the allocated atom, cast to a pointer to
1049 * A convenience macro to allocate an atom of memory from a #GMemChunk.
1050 * It calls g_mem_chunk_alloc() and casts the returned atom to a
1051 * pointer to the given type, avoiding a type cast in the source code.
1053 * Deprecated:2.10: Use g_slice_new() instead
1057 * g_mem_chunk_create:
1058 * @type: the type of the atoms, typically a structure name.
1059 * @pre_alloc: the number of atoms to store in each block of memory.
1060 * @alloc_type: the type of the #GMemChunk. #G_ALLOC_AND_FREE is used
1061 * if the atoms will be freed individually. #G_ALLOC_ONLY
1062 * should be used if atoms will never be freed
1063 * individually. #G_ALLOC_ONLY is quicker, since it does
1064 * not need to track free atoms, but it obviously wastes
1065 * memory if you no longer need many of the atoms.
1066 * @Returns: the new #GMemChunk.
1068 * A convenience macro for creating a new #GMemChunk. It calls
1069 * g_mem_chunk_new(), using the given type to create the #GMemChunk
1070 * name. The atom size is determined using
1071 * <function>sizeof()</function>, and the area size is calculated by
1072 * multiplying the @pre_alloc parameter with the atom size.
1074 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1075 * allocator</link> instead
1081 * @name: the name of the #GAllocator. This name is used to set the
1082 * name of the #GMemChunk used by the #GAllocator, and is only
1083 * used for debugging.
1084 * @n_preallocs: the number of elements in each block of memory
1085 * allocated. Larger blocks mean less calls to
1086 * g_malloc(), but some memory may be wasted. (GLib uses
1087 * 128 elements per block by default.) The value must be
1088 * between 1 and 65535.
1089 * @Returns: a new #GAllocator.
1091 * Creates a new #GAllocator.
1093 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1094 * allocator</link> instead
1097 g_allocator_new (const gchar *name,
1100 static struct _GAllocator {
1102 guint16 n_preallocs;
1103 guint is_unused : 1;
1106 GMemChunk *mem_chunk;
1109 "GAllocator is deprecated", 1, TRUE, 0, NULL, NULL, NULL,
1111 /* some (broken) GAllocator uses depend on non-NULL allocators */
1112 return (void*) &dummy;
1117 * @allocator: a #GAllocator.
1119 * Frees all of the memory allocated by the #GAllocator.
1121 * Deprecated:2.10: Use the <link linkend="glib-Memory-Slices">slice
1122 * allocator</link> instead
1125 g_allocator_free (GAllocator *allocator)
1129 #ifdef ENABLE_GC_FRIENDLY_DEFAULT
1130 gboolean g_mem_gc_friendly = TRUE;
1132 gboolean g_mem_gc_friendly = FALSE;
1136 g_mem_init_nomessage (void)
1140 const GDebugKey keys[] = {
1141 { "gc-friendly", 1 },
1144 if (g_mem_initialized)
1146 /* don't use g_malloc/g_message here */
1147 val = _g_getenv_nomalloc ("G_DEBUG", buffer);
1148 flags = !val ? 0 : g_parse_debug_string (val, keys, G_N_ELEMENTS (keys));
1149 if (flags & 1) /* gc-friendly */
1151 g_mem_gc_friendly = TRUE;
1153 g_mem_initialized = TRUE;
1157 _g_mem_thread_init_noprivate_nomessage (void)
1159 /* we may only create mutexes here, locking/
1160 * unlocking a mutex does not yet work.
1162 g_mem_init_nomessage();
1163 #ifndef G_DISABLE_CHECKS
1164 gmem_profile_mutex = g_mutex_new ();
1169 #include "galiasdef.c"