1/* GLIB sliced memory - fast threaded memory chunk allocator
2 * Copyright (C) 2005 Tim Janik
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#ifndef __G_SLICE_H__
21#define __G_SLICE_H__
22
23#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
24#error "Only <glib.h> can be included directly."
25#endif
26
27#include <glib/gtypes.h>
28#include <string.h>
29
30G_BEGIN_DECLS
31
32/* slices - fast allocation/release of small memory blocks
33 */
34GLIB_AVAILABLE_IN_ALL
35gpointer g_slice_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
36GLIB_AVAILABLE_IN_ALL
37gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
38GLIB_AVAILABLE_IN_ALL
39gpointer g_slice_copy (gsize block_size,
40 gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1);
41GLIB_AVAILABLE_IN_ALL
42void g_slice_free1 (gsize block_size,
43 gpointer mem_block);
44GLIB_AVAILABLE_IN_ALL
45void g_slice_free_chain_with_offset (gsize block_size,
46 gpointer mem_chain,
47 gsize next_offset);
48#define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type)))
49
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); \
56 gpointer __p; \
57 __p = g_slice_alloc (__s); \
58 memset (__p, 0, __s); \
59 __p; \
60 }))
61#else
62# define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type)))
63#endif
64
65/* MemoryBlockType *
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.
75 */
76
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) \
82G_STMT_START { \
83 if (1) g_slice_free1 (sizeof (type), (mem)); \
84 else (void) ((type*) 0 == (mem)); \
85} G_STMT_END
86#define g_slice_free_chain(type, mem_chain, next) \
87G_STMT_START { \
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)); \
91} G_STMT_END
92
93/* --- internal debugging API --- */
94typedef enum {
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
101} GSliceConfig;
102
103GLIB_DEPRECATED_IN_2_34
104void g_slice_set_config (GSliceConfig ckey, gint64 value);
105GLIB_DEPRECATED_IN_2_34
106gint64 g_slice_get_config (GSliceConfig ckey);
107GLIB_DEPRECATED_IN_2_34
108gint64* g_slice_get_config_state (GSliceConfig ckey, gint64 address, guint *n_values);
109
110#ifndef __GI_SCANNER__
111#ifdef G_ENABLE_DEBUG
112GLIB_AVAILABLE_IN_ALL
113void g_slice_debug_tree_statistics (void);
114#endif
115#endif
116
117G_END_DECLS
118
119#endif /* __G_SLICE_H__ */
120