You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

310 lines
12 KiB

  1. /* GLIB - Library of useful routines for C programming
  2. * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
  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. * Modified by the GLib Team and others 1997-2000. See the AUTHORS
  21. * file for a list of people on the GLib Team. See the ChangeLog
  22. * files for a list of changes. These files are distributed with
  23. * GLib at ftp://ftp.gtk.org/pub/gtk/.
  24. */
  25. #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
  26. #error "Only <glib.h> can be included directly."
  27. #endif
  28. #ifndef __G_MEM_H__
  29. #define __G_MEM_H__
  30. #include <glib/gslice.h>
  31. #include <glib/gtypes.h>
  32. G_BEGIN_DECLS
  33. /**
  34. * GMemVTable:
  35. * @malloc: function to use for allocating memory.
  36. * @realloc: function to use for reallocating memory.
  37. * @free: function to use to free memory.
  38. * @calloc: function to use for allocating zero-filled memory.
  39. * @try_malloc: function to use for allocating memory without a default error handler.
  40. * @try_realloc: function to use for reallocating memory without a default error handler.
  41. *
  42. * A set of functions used to perform memory allocation. The same #GMemVTable must
  43. * be used for all allocations in the same program; a call to g_mem_set_vtable(),
  44. * if it exists, should be prior to any use of GLib.
  45. */
  46. typedef struct _GMemVTable GMemVTable;
  47. #if GLIB_SIZEOF_VOID_P > GLIB_SIZEOF_LONG
  48. /**
  49. * G_MEM_ALIGN:
  50. *
  51. * Indicates the number of bytes to which memory will be aligned on the
  52. * current platform.
  53. */
  54. # define G_MEM_ALIGN GLIB_SIZEOF_VOID_P
  55. #else /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
  56. # define G_MEM_ALIGN GLIB_SIZEOF_LONG
  57. #endif /* GLIB_SIZEOF_VOID_P <= GLIB_SIZEOF_LONG */
  58. /* Memory allocation functions
  59. */
  60. void g_free (gpointer mem);
  61. gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  62. gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  63. gpointer g_realloc (gpointer mem,
  64. gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
  65. gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  66. gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1);
  67. gpointer g_try_realloc (gpointer mem,
  68. gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT;
  69. gpointer g_malloc_n (gsize n_blocks,
  70. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  71. gpointer g_malloc0_n (gsize n_blocks,
  72. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  73. gpointer g_realloc_n (gpointer mem,
  74. gsize n_blocks,
  75. gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
  76. gpointer g_try_malloc_n (gsize n_blocks,
  77. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  78. gpointer g_try_malloc0_n (gsize n_blocks,
  79. gsize n_block_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE2(1,2);
  80. gpointer g_try_realloc_n (gpointer mem,
  81. gsize n_blocks,
  82. gsize n_block_bytes) G_GNUC_WARN_UNUSED_RESULT;
  83. /* Optimise: avoid the call to the (slower) _n function if we can
  84. * determine at compile-time that no overflow happens.
  85. */
  86. #if defined (__GNUC__) && (__GNUC__ >= 2) && defined (__OPTIMIZE__)
  87. # define _G_NEW(struct_type, n_structs, func) \
  88. (struct_type *) (__extension__ ({ \
  89. gsize __n = (gsize) (n_structs); \
  90. gsize __s = sizeof (struct_type); \
  91. gpointer __p; \
  92. if (__s == 1) \
  93. __p = g_##func (__n); \
  94. else if (__builtin_constant_p (__n) && \
  95. (__s == 0 || __n <= G_MAXSIZE / __s)) \
  96. __p = g_##func (__n * __s); \
  97. else \
  98. __p = g_##func##_n (__n, __s); \
  99. __p; \
  100. }))
  101. # define _G_RENEW(struct_type, mem, n_structs, func) \
  102. (struct_type *) (__extension__ ({ \
  103. gsize __n = (gsize) (n_structs); \
  104. gsize __s = sizeof (struct_type); \
  105. gpointer __p = (gpointer) (mem); \
  106. if (__s == 1) \
  107. __p = g_##func (__p, __n); \
  108. else if (__builtin_constant_p (__n) && \
  109. (__s == 0 || __n <= G_MAXSIZE / __s)) \
  110. __p = g_##func (__p, __n * __s); \
  111. else \
  112. __p = g_##func##_n (__p, __n, __s); \
  113. __p; \
  114. }))
  115. #else
  116. /* Unoptimised version: always call the _n() function. */
  117. #define _G_NEW(struct_type, n_structs, func) \
  118. ((struct_type *) g_##func##_n ((n_structs), sizeof (struct_type)))
  119. #define _G_RENEW(struct_type, mem, n_structs, func) \
  120. ((struct_type *) g_##func##_n (mem, (n_structs), sizeof (struct_type)))
  121. #endif
  122. /**
  123. * g_new:
  124. * @struct_type: the type of the elements to allocate
  125. * @n_structs: the number of elements to allocate
  126. *
  127. * Allocates @n_structs elements of type @struct_type.
  128. * The returned pointer is cast to a pointer to the given type.
  129. * If @n_structs is 0 it returns %NULL.
  130. * Care is taken to avoid overflow when calculating the size of the allocated block.
  131. *
  132. * Since the returned pointer is already casted to the right type,
  133. * it is normally unnecessary to cast it explicitly, and doing
  134. * so might hide memory allocation errors.
  135. *
  136. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  137. */
  138. #define g_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc)
  139. /**
  140. * g_new0:
  141. * @struct_type: the type of the elements to allocate.
  142. * @n_structs: the number of elements to allocate.
  143. *
  144. * Allocates @n_structs elements of type @struct_type, initialized to 0's.
  145. * The returned pointer is cast to a pointer to the given type.
  146. * If @n_structs is 0 it returns %NULL.
  147. * Care is taken to avoid overflow when calculating the size of the allocated block.
  148. *
  149. * Since the returned pointer is already casted to the right type,
  150. * it is normally unnecessary to cast it explicitly, and doing
  151. * so might hide memory allocation errors.
  152. *
  153. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type.
  154. */
  155. #define g_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, malloc0)
  156. /**
  157. * g_renew:
  158. * @struct_type: the type of the elements to allocate
  159. * @mem: the currently allocated memory
  160. * @n_structs: the number of elements to allocate
  161. *
  162. * Reallocates the memory pointed to by @mem, so that it now has space for
  163. * @n_structs elements of type @struct_type. It returns the new address of
  164. * the memory, which may have been moved.
  165. * Care is taken to avoid overflow when calculating the size of the allocated block.
  166. *
  167. * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
  168. */
  169. #define g_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, realloc)
  170. /**
  171. * g_try_new:
  172. * @struct_type: the type of the elements to allocate
  173. * @n_structs: the number of elements to allocate
  174. *
  175. * Attempts to allocate @n_structs elements of type @struct_type, and returns
  176. * %NULL on failure. Contrast with g_new(), which aborts the program on failure.
  177. * The returned pointer is cast to a pointer to the given type.
  178. * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
  179. *
  180. * Since: 2.8
  181. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  182. */
  183. #define g_try_new(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc)
  184. /**
  185. * g_try_new0:
  186. * @struct_type: the type of the elements to allocate
  187. * @n_structs: the number of elements to allocate
  188. *
  189. * Attempts to allocate @n_structs elements of type @struct_type, initialized
  190. * to 0's, and returns %NULL on failure. Contrast with g_new0(), which aborts
  191. * the program on failure.
  192. * The returned pointer is cast to a pointer to the given type.
  193. * The function returns %NULL when @n_structs is 0 of if an overflow occurs.
  194. *
  195. * Since: 2.8
  196. * Returns: a pointer to the allocated memory, cast to a pointer to @struct_type
  197. */
  198. #define g_try_new0(struct_type, n_structs) _G_NEW (struct_type, n_structs, try_malloc0)
  199. /**
  200. * g_try_renew:
  201. * @struct_type: the type of the elements to allocate
  202. * @mem: the currently allocated memory
  203. * @n_structs: the number of elements to allocate
  204. *
  205. * Attempts to reallocate the memory pointed to by @mem, so that it now has
  206. * space for @n_structs elements of type @struct_type, and returns %NULL on
  207. * failure. Contrast with g_renew(), which aborts the program on failure.
  208. * It returns the new address of the memory, which may have been moved.
  209. * The function returns %NULL if an overflow occurs.
  210. *
  211. * Since: 2.8
  212. * Returns: a pointer to the new allocated memory, cast to a pointer to @struct_type
  213. */
  214. #define g_try_renew(struct_type, mem, n_structs) _G_RENEW (struct_type, mem, n_structs, try_realloc)
  215. /* Memory allocation virtualization for debugging purposes
  216. * g_mem_set_vtable() has to be the very first GLib function called
  217. * if being used
  218. */
  219. struct _GMemVTable {
  220. gpointer (*malloc) (gsize n_bytes);
  221. gpointer (*realloc) (gpointer mem,
  222. gsize n_bytes);
  223. void (*free) (gpointer mem);
  224. /* optional; set to NULL if not used ! */
  225. gpointer (*calloc) (gsize n_blocks,
  226. gsize n_block_bytes);
  227. gpointer (*try_malloc) (gsize n_bytes);
  228. gpointer (*try_realloc) (gpointer mem,
  229. gsize n_bytes);
  230. };
  231. void g_mem_set_vtable (GMemVTable *vtable);
  232. gboolean g_mem_is_system_malloc (void);
  233. GLIB_VAR gboolean g_mem_gc_friendly;
  234. /* Memory profiler and checker, has to be enabled via g_mem_set_vtable()
  235. */
  236. GLIB_VAR GMemVTable *glib_mem_profiler_table;
  237. void g_mem_profile (void);
  238. /* deprecated memchunks and allocators */
  239. #if !defined (G_DISABLE_DEPRECATED) || defined (GTK_COMPILATION) || defined (GDK_COMPILATION)
  240. typedef struct _GAllocator GAllocator;
  241. typedef struct _GMemChunk GMemChunk;
  242. #define g_mem_chunk_create(type, pre_alloc, alloc_type) ( \
  243. g_mem_chunk_new (#type " mem chunks (" #pre_alloc ")", \
  244. sizeof (type), \
  245. sizeof (type) * (pre_alloc), \
  246. (alloc_type)) \
  247. )
  248. #define g_chunk_new(type, chunk) ( \
  249. (type *) g_mem_chunk_alloc (chunk) \
  250. )
  251. #define g_chunk_new0(type, chunk) ( \
  252. (type *) g_mem_chunk_alloc0 (chunk) \
  253. )
  254. #define g_chunk_free(mem, mem_chunk) G_STMT_START { \
  255. g_mem_chunk_free ((mem_chunk), (mem)); \
  256. } G_STMT_END
  257. #define G_ALLOC_ONLY 1
  258. #define G_ALLOC_AND_FREE 2
  259. GMemChunk* g_mem_chunk_new (const gchar *name,
  260. gint atom_size,
  261. gsize area_size,
  262. gint type);
  263. void g_mem_chunk_destroy (GMemChunk *mem_chunk);
  264. gpointer g_mem_chunk_alloc (GMemChunk *mem_chunk);
  265. gpointer g_mem_chunk_alloc0 (GMemChunk *mem_chunk);
  266. void g_mem_chunk_free (GMemChunk *mem_chunk,
  267. gpointer mem);
  268. void g_mem_chunk_clean (GMemChunk *mem_chunk);
  269. void g_mem_chunk_reset (GMemChunk *mem_chunk);
  270. void g_mem_chunk_print (GMemChunk *mem_chunk);
  271. void g_mem_chunk_info (void);
  272. void g_blow_chunks (void);
  273. GAllocator*g_allocator_new (const gchar *name,
  274. guint n_preallocs);
  275. void g_allocator_free (GAllocator *allocator);
  276. #define G_ALLOCATOR_LIST (1)
  277. #define G_ALLOCATOR_SLIST (2)
  278. #define G_ALLOCATOR_NODE (3)
  279. #endif /* G_DISABLE_DEPRECATED */
  280. G_END_DECLS
  281. #endif /* __G_MEM_H__ */