williamr@2: /* GLIB - Library of useful routines for C programming williamr@2: * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald williamr@2: * Portions copyright (c) 2006 Nokia Corporation. All rights reserved. williamr@2: * williamr@2: * This library is free software; you can redistribute it and/or williamr@2: * modify it under the terms of the GNU Lesser General Public williamr@2: * License as published by the Free Software Foundation; either williamr@2: * version 2 of the License, or (at your option) any later version. williamr@2: * williamr@2: * This library is distributed in the hope that it will be useful, williamr@2: * but WITHOUT ANY WARRANTY; without even the implied warranty of williamr@2: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU williamr@2: * Lesser General Public License for more details. williamr@2: * williamr@2: * You should have received a copy of the GNU Lesser General Public williamr@2: * License along with this library; if not, write to the williamr@2: * Free Software Foundation, Inc., 59 Temple Place - Suite 330, williamr@2: * Boston, MA 02111-1307, USA. williamr@2: */ williamr@2: williamr@2: /* williamr@2: * Modified by the GLib Team and others 1997-2000. See the AUTHORS williamr@2: * file for a list of people on the GLib Team. See the ChangeLog williamr@2: * files for a list of changes. These files are distributed with williamr@2: * GLib at ftp://ftp.gtk.org/pub/gtk/. williamr@2: */ williamr@2: williamr@2: #ifndef __G_MEM_H__ williamr@2: #define __G_MEM_H__ williamr@2: williamr@2: #include <_ansi.h> williamr@2: #include williamr@2: williamr@2: G_BEGIN_DECLS williamr@2: williamr@2: typedef struct _GMemVTable GMemVTable; williamr@2: williamr@2: williamr@2: #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG williamr@2: # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P williamr@2: #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */ williamr@2: # define G_MEM_ALIGN GLIB_SIZEOF_LONG williamr@2: #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */ williamr@2: williamr@2: williamr@2: /* Memory allocation functions williamr@2: */ williamr@2: IMPORT_C gpointer g_malloc (gulong n_bytes) G_GNUC_MALLOC; williamr@2: IMPORT_C gpointer g_malloc0 (gulong n_bytes) G_GNUC_MALLOC; williamr@2: IMPORT_C gpointer g_realloc (gpointer mem, williamr@2: gulong n_bytes) G_GNUC_WARN_UNUSED_RESULT; williamr@2: IMPORT_C void g_free (gpointer mem); williamr@2: IMPORT_C gpointer g_try_malloc (gulong n_bytes) G_GNUC_MALLOC; williamr@2: IMPORT_C gpointer g_try_malloc0 (gulong n_bytes) G_GNUC_MALLOC; williamr@2: IMPORT_C gpointer g_try_realloc (gpointer mem, williamr@2: gulong n_bytes) G_GNUC_WARN_UNUSED_RESULT; williamr@2: williamr@2: williamr@2: /* Convenience memory allocators williamr@2: */ williamr@2: #define g_new(struct_type, n_structs) \ williamr@2: ((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) williamr@2: #define g_new0(struct_type, n_structs) \ williamr@2: ((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) williamr@2: #define g_renew(struct_type, mem, n_structs) \ williamr@2: ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) williamr@2: williamr@2: #define g_try_new(struct_type, n_structs) \ williamr@2: ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) williamr@2: #define g_try_new0(struct_type, n_structs) \ williamr@2: ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) williamr@2: #define g_try_renew(struct_type, mem, n_structs) \ williamr@2: ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) williamr@2: williamr@2: williamr@2: /* Memory allocation virtualization for debugging purposes williamr@2: * g_mem_set_vtable() has to be the very first GLib function called williamr@2: * if being used williamr@2: */ williamr@2: struct _GMemVTable williamr@2: { williamr@2: gpointer (*malloc) (gsize n_bytes); williamr@2: gpointer (*realloc) (gpointer mem, williamr@2: gsize n_bytes); williamr@2: void (*free) (gpointer mem); williamr@2: /* optional; set to NULL if not used ! */ williamr@2: gpointer (*calloc) (gsize n_blocks, williamr@2: gsize n_block_bytes); williamr@2: gpointer (*try_malloc) (gsize n_bytes); williamr@2: gpointer (*try_realloc) (gpointer mem, williamr@2: gsize n_bytes); williamr@2: }; williamr@2: IMPORT_C void g_mem_set_vtable (GMemVTable *vtable); williamr@2: IMPORT_C gboolean g_mem_is_system_malloc (void); williamr@2: williamr@2: #ifdef __SYMBIAN32__ williamr@2: IMPORT_C gboolean * _g_mem_gc_friendly(); williamr@2: #endif /* __SYMBIAN32__ */ williamr@2: GLIB_VAR gboolean g_mem_gc_friendly; williamr@2: williamr@2: /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()*/ williamr@2: #ifdef __SYMBIAN32__ williamr@2: IMPORT_C GMemVTable ** _glib_mem_profiler_table(); williamr@2: #endif /* __SYMBIAN32__ */ williamr@2: GLIB_VAR GMemVTable *glib_mem_profiler_table; williamr@2: williamr@2: IMPORT_C void g_mem_profile (void); williamr@2: williamr@2: williamr@2: /* deprecated memchunks and allocators */ williamr@2: #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION) williamr@2: typedef struct _GAllocator GAllocator; williamr@2: typedef struct _GMemChunk GMemChunk; williamr@2: #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \ williamr@2: g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \ williamr@2: sizeof (type), \ williamr@2: sizeof (type) * (pre_alloc), \ williamr@2: (alloc_type)) \ williamr@2: ) williamr@2: #define g_chunk_new(type, chunk) ( \ williamr@2: (type *) g_mem_chunk_alloc (chunk) \ williamr@2: ) williamr@2: #define g_chunk_new0(type, chunk) ( \ williamr@2: (type *) g_mem_chunk_alloc0 (chunk) \ williamr@2: ) williamr@2: #define g_chunk_free(mem, mem_chunk) G_STMT_START { \ williamr@2: g_mem_chunk_free ((mem_chunk), (mem)); \ williamr@2: } G_STMT_END williamr@2: #define G_ALLOC_ONLY 1 williamr@2: #define G_ALLOC_AND_FREE 2 williamr@2: IMPORT_C GMemChunk* g_mem_chunk_new (const gchar *name, williamr@2: gint atom_size, williamr@2: gulong area_size, williamr@2: gint type); williamr@2: IMPORT_C void g_mem_chunk_destroy (GMemChunk *mem_chunk); williamr@2: IMPORT_C gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk); williamr@2: IMPORT_C gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk); williamr@2: IMPORT_C void g_mem_chunk_free (GMemChunk *mem_chunk, williamr@2: gpointer mem); williamr@2: IMPORT_C void g_mem_chunk_clean (GMemChunk *mem_chunk); williamr@2: IMPORT_C void g_mem_chunk_reset (GMemChunk *mem_chunk); williamr@2: IMPORT_C void g_mem_chunk_print (GMemChunk *mem_chunk); williamr@2: IMPORT_C void g_mem_chunk_info (void); williamr@2: IMPORT_C void g_blow_chunks (void); williamr@2: IMPORT_C GAllocator* g_allocator_new (const gchar *name, williamr@2: guint n_preallocs); williamr@2: IMPORT_C void g_allocator_free (GAllocator *allocator); williamr@2: #define G_ALLOCATOR_LIST (1) williamr@2: #define G_ALLOCATOR_SLIST (2) williamr@2: #define G_ALLOCATOR_NODE (3) williamr@2: #endif /* G_DISABLE_DEPRECATED */ williamr@2: williamr@2: G_END_DECLS williamr@2: williamr@2: #endif /* __G_MEM_H__ */