1 /* GLIB sliced memory - fast threaded memory chunk allocator
2 * Copyright (C) 2005 Tim Janik
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
24 #error "Only <glib.h> can be included directly."
27 #include <glib/gtypes.h>
32 /* slices - fast allocation/release of small memory blocks
35 gpointer g_slice_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
37 gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
39 gpointer g_slice_copy (gsize block_size,
40 gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
42 void g_slice_free1 (gsize block_size,
45 void g_slice_free_chain_with_offset (gsize block_size,
48 #define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type)))
50 /* Allow the compiler to inline memset(). Since the size is a constant, this
51 * can significantly improve performance. */
52 #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
53 # define g_slice_new0(type) \
54 (type *) (G_GNUC_EXTENSION ({ \
55 gsize __s = sizeof (type); \
57 __p = g_slice_alloc (__s); \
58 memset (__p, 0, __s); \
62 # define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type)))
66 * g_slice_dup (MemoryBlockType,
67 * MemoryBlockType *mem_block);
68 * g_slice_free (MemoryBlockType,
69 * MemoryBlockType *mem_block);
70 * g_slice_free_chain (MemoryBlockType,
71 * MemoryBlockType *first_chain_block,
72 * memory_block_next_field);
73 * pseudo prototypes for the macro
74 * definitions following below.
77 /* we go through extra hoops to ensure type safety */
78 #define g_slice_dup(type, mem) \
79 (1 ? (type*) g_slice_copy (sizeof (type), (mem)) \
80 : ((void) ((type*) 0 == (mem)), (type*) 0))
81 #define g_slice_free(type, mem) \
83 if (1) g_slice_free1 (sizeof (type), (mem)); \
84 else (void) ((type*) 0 == (mem)); \
86 #define g_slice_free_chain(type, mem_chain, next) \
88 if (1) g_slice_free_chain_with_offset (sizeof (type), \
89 (mem_chain), G_STRUCT_OFFSET (type, next)); \
90 else (void) ((type*) 0 == (mem_chain)); \
93 /* --- internal debugging API --- */
95 G_SLICE_CONFIG_ALWAYS_MALLOC = 1,
96 G_SLICE_CONFIG_BYPASS_MAGAZINES,
97 G_SLICE_CONFIG_WORKING_SET_MSECS,
98 G_SLICE_CONFIG_COLOR_INCREMENT,
99 G_SLICE_CONFIG_CHUNK_SIZES,
100 G_SLICE_CONFIG_CONTENTION_COUNTER
103 GLIB_DEPRECATED_IN_2_34
104 void g_slice_set_config (GSliceConfig ckey, gint64 value);
105 GLIB_DEPRECATED_IN_2_34
106 gint64 g_slice_get_config (GSliceConfig ckey);
107 GLIB_DEPRECATED_IN_2_34
108 gint64* g_slice_get_config_state (GSliceConfig ckey, gint64 address, guint *n_values);
110 #ifdef G_ENABLE_DEBUG
111 GLIB_AVAILABLE_IN_ALL
112 void g_slice_debug_tree_statistics (void);
117 #endif /* __G_SLICE_H__ */