| 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.1 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, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #ifndef __G_SLICE_H__ |
| 19 | #define __G_SLICE_H__ |
| 20 | |
| 21 | #if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION) |
| 22 | #error "Only <glib.h> can be included directly." |
| 23 | #endif |
| 24 | |
| 25 | #include <glib/gtypes.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | G_BEGIN_DECLS |
| 29 | |
| 30 | /* slices - fast allocation/release of small memory blocks |
| 31 | */ |
| 32 | GLIB_AVAILABLE_IN_ALL |
| 33 | gpointer g_slice_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
| 34 | GLIB_AVAILABLE_IN_ALL |
| 35 | gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); |
| 36 | GLIB_AVAILABLE_IN_ALL |
| 37 | gpointer g_slice_copy (gsize block_size, |
| 38 | gconstpointer mem_block) G_GNUC_ALLOC_SIZE(1); |
| 39 | GLIB_AVAILABLE_IN_ALL |
| 40 | void g_slice_free1 (gsize block_size, |
| 41 | gpointer mem_block); |
| 42 | GLIB_AVAILABLE_IN_ALL |
| 43 | void g_slice_free_chain_with_offset (gsize block_size, |
| 44 | gpointer mem_chain, |
| 45 | gsize next_offset); |
| 46 | #define g_slice_new(type) ((type*) g_slice_alloc (sizeof (type))) |
| 47 | |
| 48 | /* Allow the compiler to inline memset(). Since the size is a constant, this |
| 49 | * can significantly improve performance. */ |
| 50 | #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__) |
| 51 | # define g_slice_new0(type) \ |
| 52 | (type *) (G_GNUC_EXTENSION ({ \ |
| 53 | gsize __s = sizeof (type); \ |
| 54 | gpointer __p; \ |
| 55 | __p = g_slice_alloc (__s); \ |
| 56 | memset (__p, 0, __s); \ |
| 57 | __p; \ |
| 58 | })) |
| 59 | #else |
| 60 | # define g_slice_new0(type) ((type*) g_slice_alloc0 (sizeof (type))) |
| 61 | #endif |
| 62 | |
| 63 | /* MemoryBlockType * |
| 64 | * g_slice_dup (MemoryBlockType, |
| 65 | * MemoryBlockType *mem_block); |
| 66 | * g_slice_free (MemoryBlockType, |
| 67 | * MemoryBlockType *mem_block); |
| 68 | * g_slice_free_chain (MemoryBlockType, |
| 69 | * MemoryBlockType *first_chain_block, |
| 70 | * memory_block_next_field); |
| 71 | * pseudo prototypes for the macro |
| 72 | * definitions following below. |
| 73 | */ |
| 74 | |
| 75 | /* we go through extra hoops to ensure type safety */ |
| 76 | #define g_slice_dup(type, mem) \ |
| 77 | (1 ? (type*) g_slice_copy (sizeof (type), (mem)) \ |
| 78 | : ((void) ((type*) 0 == (mem)), (type*) 0)) |
| 79 | #define g_slice_free(type, mem) \ |
| 80 | G_STMT_START { \ |
| 81 | if (1) g_slice_free1 (sizeof (type), (mem)); \ |
| 82 | else (void) ((type*) 0 == (mem)); \ |
| 83 | } G_STMT_END |
| 84 | #define g_slice_free_chain(type, mem_chain, next) \ |
| 85 | G_STMT_START { \ |
| 86 | if (1) g_slice_free_chain_with_offset (sizeof (type), \ |
| 87 | (mem_chain), G_STRUCT_OFFSET (type, next)); \ |
| 88 | else (void) ((type*) 0 == (mem_chain)); \ |
| 89 | } G_STMT_END |
| 90 | |
| 91 | /* --- internal debugging API --- */ |
| 92 | typedef enum { |
| 93 | G_SLICE_CONFIG_ALWAYS_MALLOC = 1, |
| 94 | G_SLICE_CONFIG_BYPASS_MAGAZINES, |
| 95 | G_SLICE_CONFIG_WORKING_SET_MSECS, |
| 96 | G_SLICE_CONFIG_COLOR_INCREMENT, |
| 97 | G_SLICE_CONFIG_CHUNK_SIZES, |
| 98 | G_SLICE_CONFIG_CONTENTION_COUNTER |
| 99 | } GSliceConfig; |
| 100 | |
| 101 | GLIB_DEPRECATED_IN_2_34 |
| 102 | void g_slice_set_config (GSliceConfig ckey, gint64 value); |
| 103 | GLIB_DEPRECATED_IN_2_34 |
| 104 | gint64 g_slice_get_config (GSliceConfig ckey); |
| 105 | GLIB_DEPRECATED_IN_2_34 |
| 106 | gint64* g_slice_get_config_state (GSliceConfig ckey, gint64 address, guint *n_values); |
| 107 | |
| 108 | #ifdef G_ENABLE_DEBUG |
| 109 | GLIB_AVAILABLE_IN_ALL |
| 110 | void g_slice_debug_tree_statistics (void); |
| 111 | #endif |
| 112 | |
| 113 | G_END_DECLS |
| 114 | |
| 115 | #endif /* __G_SLICE_H__ */ |
| 116 | |