Home | History | Annotate | Download | only in glib
      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 
     20 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
     21 #error "Only <glib.h> can be included directly."
     22 #endif
     23 
     24 #ifndef __G_SLICE_H__
     25 #define __G_SLICE_H__
     26 
     27 #ifndef __G_MEM_H__
     28 #error Include <glib.h> instead of <gslice.h>
     29 #endif
     30 
     31 #include <glib/gtypes.h>
     32 
     33 G_BEGIN_DECLS
     34 
     35 /* slices - fast allocation/release of small memory blocks
     36  */
     37 gpointer g_slice_alloc          	(gsize	       block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
     38 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_MALLOC G_GNUC_ALLOC_SIZE(1);
     41 void     g_slice_free1          	(gsize         block_size,
     42 					 gpointer      mem_block);
     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 #define  g_slice_new0(type)     ((type*) g_slice_alloc0 (sizeof (type)))
     48 /* MemoryBlockType *
     49  *       g_slice_dup                    (MemoryBlockType,
     50  *	                                 MemoryBlockType *mem_block);
     51  *       g_slice_free                   (MemoryBlockType,
     52  *	                                 MemoryBlockType *mem_block);
     53  *       g_slice_free_chain             (MemoryBlockType,
     54  *                                       MemoryBlockType *first_chain_block,
     55  *                                       memory_block_next_field);
     56  * pseudo prototypes for the macro
     57  * definitions following below.
     58  */
     59 
     60 /* we go through extra hoops to ensure type safety */
     61 #define g_slice_dup(type, mem)                                  \
     62   (1 ? (type*) g_slice_copy (sizeof (type), (mem))              \
     63      : ((void) ((type*) 0 == (mem)), (type*) 0))
     64 #define g_slice_free(type, mem)				do {	\
     65   if (1) g_slice_free1 (sizeof (type), (mem));			\
     66   else   (void) ((type*) 0 == (mem)); 				\
     67 } while (0)
     68 #define g_slice_free_chain(type, mem_chain, next)	do {	\
     69   if (1) g_slice_free_chain_with_offset (sizeof (type),		\
     70                  (mem_chain), G_STRUCT_OFFSET (type, next)); 	\
     71   else   (void) ((type*) 0 == (mem_chain));			\
     72 } while (0)
     73 
     74 
     75 /* --- internal debugging API --- */
     76 typedef enum {
     77   G_SLICE_CONFIG_ALWAYS_MALLOC = 1,
     78   G_SLICE_CONFIG_BYPASS_MAGAZINES,
     79   G_SLICE_CONFIG_WORKING_SET_MSECS,
     80   G_SLICE_CONFIG_COLOR_INCREMENT,
     81   G_SLICE_CONFIG_CHUNK_SIZES,
     82   G_SLICE_CONFIG_CONTENTION_COUNTER
     83 } GSliceConfig;
     84 void     g_slice_set_config	   (GSliceConfig ckey, gint64 value);
     85 gint64   g_slice_get_config	   (GSliceConfig ckey);
     86 gint64*  g_slice_get_config_state  (GSliceConfig ckey, gint64 address, guint *n_values);
     87 
     88 G_END_DECLS
     89 
     90 #endif /* __G_SLICE_H__ */
     91