cd3b5aadbff2eb03f7111041a37577d42e763e00
[platform/upstream/glib.git] / glib / gslice.h
1 /* GLIB sliced memory - fast threaded memory chunk allocator
2  * Copyright (C) 2005 Tim Janik
3  *
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.
8  *
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.
13  *
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.
18  */
19 #ifndef __G_SLICE_H__
20 #define __G_SLICE_H__
21
22 #ifndef __G_MEM_H__
23 #error Include <glib.h> instead of <gslice.h>
24 #endif
25
26 #include <glib/gtypes.h>
27
28 G_BEGIN_DECLS
29
30 /* slices - fast allocation/release of small memory blocks
31  */
32 gpointer g_slice_alloc                  (gsize         block_size) G_GNUC_MALLOC;
33 gpointer g_slice_alloc0                 (gsize         block_size) G_GNUC_MALLOC;
34 gpointer g_slice_copy                   (gsize         block_size,
35                                          gconstpointer mem_block) G_GNUC_MALLOC;
36 void     g_slice_free1                  (gsize         block_size,
37                                          gpointer      mem_block);
38 void     g_slice_free_chain_with_offset (gsize         block_size,
39                                          gpointer      mem_chain,
40                                          gsize         next_offset);
41 #define  g_slice_new(type)      ((type*) g_slice_alloc (sizeof (type)))
42 #define  g_slice_new0(type)     ((type*) g_slice_alloc0 (sizeof (type)))
43 /* MemoryBlockType *
44  *       g_slice_dup                    (MemoryBlockType,
45  *                                       MemoryBlockType *mem_block);
46  *       g_slice_free                   (MemoryBlockType,
47  *                                       MemoryBlockType *mem_block);
48  *       g_slice_free_chain             (MemoryBlockType,
49  *                                       MemoryBlockType *first_chain_block,
50  *                                       memory_block_next_field);
51  * pseudo prototypes for the macro
52  * definitions following below.
53  */
54
55 /* we go through extra hoops to ensure type safety */
56 #define g_slice_dup(type, mem)                                  \
57   (1 ? g_slice_copy (sizeof (type), (mem)) : (type*) ((type*) 0 == (mem)))
58 #define g_slice_free(type, mem)                         do {    \
59   if (1) g_slice_free1 (sizeof (type), (mem));                  \
60   else   (void) ((type*) 0 == (mem));                           \
61 } while (0)
62 #define g_slice_free_chain(type, mem_chain, next)       do {    \
63   if (1) g_slice_free_chain_with_offset (sizeof (type),         \
64                  (mem_chain), G_STRUCT_OFFSET (type, next));    \
65   else   (void) ((type*) 0 == (mem_chain));                     \
66 } while (0)
67
68
69 /* --- internal debugging API --- */
70 typedef enum {
71   G_SLICE_CONFIG_ALWAYS_MALLOC = 1,
72   G_SLICE_CONFIG_BYPASS_MAGAZINES,
73   G_SLICE_CONFIG_WORKING_SET_MSECS,
74   G_SLICE_CONFIG_COLOR_INCREMENT,
75   G_SLICE_CONFIG_CHUNK_SIZES,
76   G_SLICE_CONFIG_CONTENTION_COUNTER
77 } GSliceConfig;
78 void     g_slice_set_config        (GSliceConfig ckey, gint64 value);
79 gint64   g_slice_get_config        (GSliceConfig ckey);
80 gint64*  g_slice_get_config_state  (GSliceConfig ckey, gint64 address, guint *n_values);
81
82 G_END_DECLS
83
84 #endif /* __G_SLICE_H__ */