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
49#ifndef __GI_SCANNER__
50/* Private helper to give the allocation size for a struct — we want to
51 * guarantee non-empty allocations, so it returns a minimum of 1B. This is
52 * necessary because some compilers do allow empty (zero size) structs. */
53#define g_slice_alloc_size(type) (sizeof (type) > 0 ? sizeof (type) : 1)
54#endif
55
56#define g_slice_new(type) ((type*) g_slice_alloc (g_slice_alloc_size (type)))
57
58/* Allow the compiler to inline memset(). Since the size is a constant, this
59 * can significantly improve performance. */
60#if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
61# define g_slice_new0(type) \
62 (type *) (G_GNUC_EXTENSION ({ \
63 gsize __s = g_slice_alloc_size (type); \
64 gpointer __p; \
65 __p = g_slice_alloc (__s); \
66 memset (__p, 0, __s); \
67 __p; \
68 }))
69#else
70# define g_slice_new0(type) ((type*) g_slice_alloc0 (g_slice_alloc_size (type)))
71#endif
72
73/* MemoryBlockType *
74 * g_slice_dup (MemoryBlockType,
75 * MemoryBlockType *mem_block);
76 * g_slice_free (MemoryBlockType,
77 * MemoryBlockType *mem_block);
78 * g_slice_free_chain (MemoryBlockType,
79 * MemoryBlockType *first_chain_block,
80 * memory_block_next_field);
81 * pseudo prototypes for the macro
82 * definitions following below.
83 */
84
85/* we go through extra hoops to ensure type safety */
86#define g_slice_dup(type, mem) \
87 (1 ? (type*) g_slice_copy (g_slice_alloc_size (type), (mem)) \
88 : ((void) ((type*) 0 == (mem)), (type*) 0))
89#define g_slice_free(type, mem) \
90G_STMT_START { \
91 if (1) g_slice_free1 (sizeof (type), (mem)); \
92 else (void) ((type*) 0 == (mem)); \
93} G_STMT_END
94#define g_slice_free_chain(type, mem_chain, next) \
95G_STMT_START { \
96 if (1) g_slice_free_chain_with_offset (sizeof (type), \
97 (mem_chain), G_STRUCT_OFFSET (type, next)); \
98 else (void) ((type*) 0 == (mem_chain)); \
99} G_STMT_END
100
101/* --- internal debugging API --- */
102typedef enum {
103 G_SLICE_CONFIG_ALWAYS_MALLOC = 1,
104 G_SLICE_CONFIG_BYPASS_MAGAZINES,
105 G_SLICE_CONFIG_WORKING_SET_MSECS,
106 G_SLICE_CONFIG_COLOR_INCREMENT,
107 G_SLICE_CONFIG_CHUNK_SIZES,
108 G_SLICE_CONFIG_CONTENTION_COUNTER
109} GSliceConfig;
110
111GLIB_DEPRECATED_IN_2_34
112void g_slice_set_config (GSliceConfig ckey, gint64 value);
113GLIB_DEPRECATED_IN_2_34
114gint64 g_slice_get_config (GSliceConfig ckey);
115GLIB_DEPRECATED_IN_2_34
116gint64* g_slice_get_config_state (GSliceConfig ckey, gint64 address, guint *n_values);
117
118#ifndef __GI_SCANNER__
119#ifdef G_ENABLE_DEBUG
120GLIB_AVAILABLE_IN_ALL
121void g_slice_debug_tree_statistics (void);
122#endif
123#endif
124
125G_END_DECLS
126
127#endif /* __G_SLICE_H__ */
128