1 gmem.h |
1 /* GLIB - Library of useful routines for C programming |
|
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald |
|
3 * Portions copyright (c) 2006 Nokia Corporation. All rights reserved. |
|
4 * |
|
5 * This library is free software; you can redistribute it and/or |
|
6 * modify it under the terms of the GNU Lesser General Public |
|
7 * License as published by the Free Software Foundation; either |
|
8 * version 2 of the License, or (at your option) any later version. |
|
9 * |
|
10 * This library is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 * Lesser General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU Lesser General Public |
|
16 * License along with this library; if not, write to the |
|
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
18 * Boston, MA 02111-1307, USA. |
|
19 */ |
|
20 |
|
21 /* |
|
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS |
|
23 * file for a list of people on the GLib Team. See the ChangeLog |
|
24 * files for a list of changes. These files are distributed with |
|
25 * GLib at ftp://ftp.gtk.org/pub/gtk/. |
|
26 */ |
|
27 |
|
28 #ifndef __G_MEM_H__ |
|
29 #define __G_MEM_H__ |
|
30 |
|
31 #include <_ansi.h> |
|
32 #include <glib/gtypes.h> |
|
33 |
|
34 G_BEGIN_DECLS |
|
35 |
|
36 typedef struct _GMemVTable GMemVTable; |
|
37 |
|
38 |
|
39 #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG |
|
40 # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P |
|
41 #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */ |
|
42 # define G_MEM_ALIGN GLIB_SIZEOF_LONG |
|
43 #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */ |
|
44 |
|
45 |
|
46 /* Memory allocation functions |
|
47 */ |
|
48 IMPORT_C gpointer g_malloc (gulong n_bytes) G_GNUC_MALLOC; |
|
49 IMPORT_C gpointer g_malloc0 (gulong n_bytes) G_GNUC_MALLOC; |
|
50 IMPORT_C gpointer g_realloc (gpointer mem, |
|
51 gulong n_bytes) G_GNUC_WARN_UNUSED_RESULT; |
|
52 IMPORT_C void g_free (gpointer mem); |
|
53 IMPORT_C gpointer g_try_malloc (gulong n_bytes) G_GNUC_MALLOC; |
|
54 IMPORT_C gpointer g_try_malloc0 (gulong n_bytes) G_GNUC_MALLOC; |
|
55 IMPORT_C gpointer g_try_realloc (gpointer mem, |
|
56 gulong n_bytes) G_GNUC_WARN_UNUSED_RESULT; |
|
57 |
|
58 |
|
59 /* Convenience memory allocators |
|
60 */ |
|
61 #define g_new(struct_type, n_structs) \ |
|
62 ((struct_type *) g_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) |
|
63 #define g_new0(struct_type, n_structs) \ |
|
64 ((struct_type *) g_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) |
|
65 #define g_renew(struct_type, mem, n_structs) \ |
|
66 ((struct_type *) g_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) |
|
67 |
|
68 #define g_try_new(struct_type, n_structs) \ |
|
69 ((struct_type *) g_try_malloc (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) |
|
70 #define g_try_new0(struct_type, n_structs) \ |
|
71 ((struct_type *) g_try_malloc0 (((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) |
|
72 #define g_try_renew(struct_type, mem, n_structs) \ |
|
73 ((struct_type *) g_try_realloc ((mem), ((gsize) sizeof (struct_type)) * ((gsize) (n_structs)))) |
|
74 |
|
75 |
|
76 /* Memory allocation virtualization for debugging purposes |
|
77 * g_mem_set_vtable() has to be the very first GLib function called |
|
78 * if being used |
|
79 */ |
|
80 struct _GMemVTable |
|
81 { |
|
82 gpointer (*malloc) (gsize n_bytes); |
|
83 gpointer (*realloc) (gpointer mem, |
|
84 gsize n_bytes); |
|
85 void (*free) (gpointer mem); |
|
86 /* optional; set to NULL if not used ! */ |
|
87 gpointer (*calloc) (gsize n_blocks, |
|
88 gsize n_block_bytes); |
|
89 gpointer (*try_malloc) (gsize n_bytes); |
|
90 gpointer (*try_realloc) (gpointer mem, |
|
91 gsize n_bytes); |
|
92 }; |
|
93 IMPORT_C void g_mem_set_vtable (GMemVTable *vtable); |
|
94 IMPORT_C gboolean g_mem_is_system_malloc (void); |
|
95 |
|
96 #ifdef __SYMBIAN32__ |
|
97 IMPORT_C gboolean * _g_mem_gc_friendly(); |
|
98 #endif /* __SYMBIAN32__ */ |
|
99 GLIB_VAR gboolean g_mem_gc_friendly; |
|
100 |
|
101 /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()*/ |
|
102 #ifdef __SYMBIAN32__ |
|
103 IMPORT_C GMemVTable ** _glib_mem_profiler_table(); |
|
104 #endif /* __SYMBIAN32__ */ |
|
105 GLIB_VAR GMemVTable *glib_mem_profiler_table; |
|
106 |
|
107 IMPORT_C void g_mem_profile (void); |
|
108 |
|
109 |
|
110 /* deprecated memchunks and allocators */ |
|
111 #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION) |
|
112 typedef struct _GAllocator GAllocator; |
|
113 typedef struct _GMemChunk GMemChunk; |
|
114 #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \ |
|
115 g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \ |
|
116 sizeof (type), \ |
|
117 sizeof (type) * (pre_alloc), \ |
|
118 (alloc_type)) \ |
|
119 ) |
|
120 #define g_chunk_new(type, chunk) ( \ |
|
121 (type *) g_mem_chunk_alloc (chunk) \ |
|
122 ) |
|
123 #define g_chunk_new0(type, chunk) ( \ |
|
124 (type *) g_mem_chunk_alloc0 (chunk) \ |
|
125 ) |
|
126 #define g_chunk_free(mem, mem_chunk) G_STMT_START { \ |
|
127 g_mem_chunk_free ((mem_chunk), (mem)); \ |
|
128 } G_STMT_END |
|
129 #define G_ALLOC_ONLY 1 |
|
130 #define G_ALLOC_AND_FREE 2 |
|
131 IMPORT_C GMemChunk* g_mem_chunk_new (const gchar *name, |
|
132 gint atom_size, |
|
133 gulong area_size, |
|
134 gint type); |
|
135 IMPORT_C void g_mem_chunk_destroy (GMemChunk *mem_chunk); |
|
136 IMPORT_C gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk); |
|
137 IMPORT_C gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk); |
|
138 IMPORT_C void g_mem_chunk_free (GMemChunk *mem_chunk, |
|
139 gpointer mem); |
|
140 IMPORT_C void g_mem_chunk_clean (GMemChunk *mem_chunk); |
|
141 IMPORT_C void g_mem_chunk_reset (GMemChunk *mem_chunk); |
|
142 IMPORT_C void g_mem_chunk_print (GMemChunk *mem_chunk); |
|
143 IMPORT_C void g_mem_chunk_info (void); |
|
144 IMPORT_C void g_blow_chunks (void); |
|
145 IMPORT_C GAllocator* g_allocator_new (const gchar *name, |
|
146 guint n_preallocs); |
|
147 IMPORT_C void g_allocator_free (GAllocator *allocator); |
|
148 #define G_ALLOCATOR_LIST (1) |
|
149 #define G_ALLOCATOR_SLIST (2) |
|
150 #define G_ALLOCATOR_NODE (3) |
|
151 #endif /* G_DISABLE_DEPRECATED */ |
|
152 |
|
153 G_END_DECLS |
|
154 |
|
155 #endif /* __G_MEM_H__ */ |