/src/wrappers/glib/library/core/glib_memory_allocation.e

http://github.com/tybor/Liberty · Specman e · 392 lines · 42 code · 91 blank · 259 comment · 1 complexity · 78dfdbea143452c7558b38ea4014f0be MD5 · raw file

  1. indexing
  2. description: "Memory Allocation -- general memory-handling."
  3. library: "GLib 2"
  4. copyright: "(C) 2005 Paolo Redaelli "
  5. license: "LGPL"
  6. note: "[
  7. If any call to allocate memory fails, the application is terminated.
  8. This also means that there is no need to check if the call succeeded.
  9. Every occurrence of gulong n_bytes is wrapped as n_bytes: INTEGER; this shall change when SmartEiffel will provide NATURAL
  10. ]"
  11. deferred class GLIB_MEMORY_ALLOCATION
  12. inherit ANY undefine is_equal, copy end
  13. feature {} -- external features
  14. -- Prev Up Home GLib Reference Manual Next
  15. -- Top | Description
  16. -- Memory Allocation
  17. -- Memory Allocation %G —%@ general memory-handling.
  18. -- Synopsis
  19. -- #include <glib.h>
  20. -- #define g_new (struct_type, n_structs)
  21. -- #define g_new0 (struct_type, n_structs)
  22. -- #define g_renew (struct_type, mem, n_structs)
  23. -- #define g_try_new (struct_type, n_structs)
  24. -- #define g_try_new0 (struct_type, n_structs)
  25. -- #define g_try_renew (struct_type, mem, n_structs)
  26. -- gpointer g_malloc (gulong n_bytes);
  27. -- gpointer g_malloc0 (gulong n_bytes);
  28. -- gpointer g_realloc (gpointer mem,
  29. -- gulong n_bytes);
  30. -- gpointer g_try_malloc (gulong n_bytes);
  31. -- gpointer g_try_malloc0 (gulong n_bytes);
  32. -- gpointer g_try_realloc (gpointer mem,
  33. -- gulong n_bytes);
  34. -- void g_free (gpointer mem);
  35. -- #define g_alloca (size)
  36. -- #define g_newa (struct_type, n_structs)
  37. -- #define g_memmove (dest,src,len)
  38. -- gpointer g_memdup (gconstpointer mem,
  39. -- guint byte_size);
  40. -- GMemVTable;
  41. -- void g_mem_set_vtable (GMemVTable *vtable);
  42. -- gboolean g_mem_is_system_malloc (void);
  43. -- extern GMemVTable *glib_mem_profiler_table;
  44. -- void g_mem_profile (void);
  45. -- Description
  46. -- These functions provide support for allocating and freeing memory.
  47. -- Note
  48. -- If any call to allocate memory fails, the application is terminated. This also means that there is no need to check if the call succeeded.
  49. -- Details
  50. -- g_new ()
  51. -- #define g_new(struct_type, n_structs)
  52. -- Allocates n_structs elements of type struct_type. The returned pointer
  53. -- is cast to a pointer to the given type. If n_structs is 0 it returns
  54. -- NULL.
  55. -- struct_type : the type of the elements to allocate.
  56. -- n_structs : the number of elements to allocate.
  57. -- Returns : a pointer to the allocated memory, cast to a pointer to
  58. -- struct_type.
  59. -- _________________________________________________________________
  60. -- g_new0()
  61. -- #define g_new0(struct_type, n_structs)
  62. -- Allocates n_structs elements of type struct_type, initialized to 0's.
  63. -- The returned pointer is cast to a pointer to the given type. If
  64. -- n_structs is 0 it returns NULL.
  65. -- struct_type : the type of the elements to allocate.
  66. -- n_structs : the number of elements to allocate.
  67. -- Returns : a pointer to the allocated memory, cast to a pointer to
  68. -- struct_type.
  69. -- _________________________________________________________________
  70. -- g_renew()
  71. -- #define g_renew(struct_type, mem, n_structs)
  72. -- Reallocates the memory pointed to by mem, so that it now has space for
  73. -- n_structs elements of type struct_type. It returns the new address of
  74. -- the memory, which may have been moved.
  75. -- struct_type : the type of the elements to allocate.
  76. -- mem : the currently allocated memory.
  77. -- n_structs : the number of elements to allocate.
  78. -- Returns : a pointer to the new allocated memory, cast to a pointer to
  79. -- struct_type.
  80. -- _________________________________________________________________
  81. -- g_try_new()
  82. -- #define g_try_new(struct_type, n_structs)
  83. -- Attempts to allocate n_structs elements of type struct_type, and
  84. -- returns NULL on failure. Contrast with g_new(), which aborts the
  85. -- program on failure. The returned pointer is cast to a pointer to the
  86. -- given type. If n_structs is 0 it returns NULL.
  87. -- struct_type : the type of the elements to allocate.
  88. -- n_structs : the number of elements to allocate.
  89. -- Returns : a pointer to the allocated memory, cast to a pointer to
  90. -- struct_type.
  91. -- Since 2.8
  92. -- _________________________________________________________________
  93. -- g_try_new0()
  94. -- #define g_try_new0(struct_type, n_structs)
  95. -- Attempts to allocate n_structs elements of type struct_type,
  96. -- initialized to 0's, and returns NULL on failure. Contrast with
  97. -- g_new0(), which aborts the program on failure. The returned
  98. -- pointer is cast to a pointer to the given type. If n_counts is 0 it
  99. -- returns NULL.
  100. -- struct_type : the type of the elements to allocate.
  101. -- n_structs : the number of elements to allocate.
  102. -- Returns : a pointer to the allocated memory, cast to a pointer to
  103. -- struct_type.
  104. -- Since 2.8
  105. -- _________________________________________________________________
  106. -- g_try_renew()
  107. -- #define g_try_renew(struct_type, mem, n_structs)
  108. -- Attempts to reallocate the memory pointed to by mem, so that it now
  109. -- has space for n_structs elements of type struct_type, and returns NULL
  110. -- on failure. Contrast with g_renew(), which aborts the program on
  111. -- failure. It returns the new address of the memory, which may have been
  112. -- moved.
  113. -- struct_type : the type of the elements to allocate.
  114. -- mem : the currently allocated memory.
  115. -- n_structs : the number of elements to allocate.
  116. -- Returns : a pointer to the new allocated memory, cast to a pointer to
  117. -- struct_type.
  118. -- Since 2.8
  119. -- _________________________________________________________________
  120. g_malloc (n_bytes: INTEGER): POINTER is
  121. -- Allocates `n_bytes' bytes of memory. If `n_bytes' is 0 it
  122. -- returns NULL. Returns : a pointer to the allocated
  123. -- memory.
  124. obsolete "use g_try_malloc"
  125. external "C use <glib.h>"
  126. end
  127. g_malloc0 (n_bytes: INTEGER): POINTER is
  128. -- Allocates `n_bytes' bytes of memory, initialized to
  129. -- 0's. If n_bytes is 0 it returns NULL. n_bytes : the
  130. -- number of bytes to allocate. Returns : a pointer to the
  131. -- allocated memory.
  132. obsolete "use g_try_malloc0"
  133. external "C use <glib.h>"
  134. end
  135. g_realloc (mem: POINTER; n_bytes: INTEGER): POINTER is
  136. -- Reallocates the memory pointed to by mem, so that it now
  137. -- has space for n_bytes bytes of memory. It returns the new
  138. -- address of the memory, which may have been moved. mem may
  139. -- be NULL, in which case it's considered to have
  140. -- zero-length. n_bytes may be 0, in which case NULL will be
  141. -- returned. mem : the memory to reallocate. n_bytes : new
  142. -- size of the memory in bytes. Returns : the new address of
  143. -- the allocated memory.
  144. obsolete "use g_try_realloc"
  145. external "C use <glib.h>"
  146. end
  147. g_try_malloc (n_bytes: INTEGER): POINTER is
  148. -- Attempts to allocate `n_bytes', and returns NULL on
  149. -- failure. Contrast with g_malloc, which aborts the program
  150. -- on failure. Returns: the allocated memory, or NULL.
  151. external "C use <glib.h>"
  152. end
  153. g_try_malloc0 (n_bytes: INTEGER): POINTER is
  154. -- Attempts to allocate `n_bytes', initialized to 0's, and
  155. -- returns NULL on failure. Contrast with g_malloc0, which
  156. -- aborts the program on failure. Returns : the allocated
  157. -- memory, or NULL.
  158. external "C use <glib.h>"
  159. end
  160. g_try_realloc (mem: POINTER; n_bytes: INTEGER): POINTER is
  161. -- Attempts to realloc mem to a new size, n_bytes, and
  162. -- returns NULL on failure. Contrast with g_realloc(), which
  163. -- aborts the program on failure. If mem is NULL, behaves the
  164. -- same as g_try_malloc(). mem : previously-allocated
  165. -- memory, or NULL. n_bytes : number of bytes to allocate.
  166. -- Returns : the allocated memory, or NULL.
  167. external "C use <glib.h>"
  168. end
  169. g_free (mem: POINTER) is
  170. -- Frees the memory pointed to by `mem'. If mem is NULL it
  171. -- simply returns.
  172. external "C use <glib.h>"
  173. end
  174. -- g_alloca()
  175. -- #define g_alloca(size)
  176. -- Allocates size bytes on the stack; these bytes will be freed when the
  177. -- current stack frame is cleaned up. This macro essentially just wraps
  178. -- the alloca() function present on most UNIX variants. Thus it provides
  179. -- the same advantages and pitfalls as alloca():
  180. -- + alloca() is very fast, as on most systems it's implemented by just
  181. -- adjusting the stack pointer register.
  182. -- + It doesn't cause any memory fragmentation, within its scope,
  183. -- separate alloca() blocks just build up and are released together at
  184. -- function end.
  185. -- - Allocation sizes have to fit into the current stack frame. For
  186. -- instance in a threaded environment on Linux, the per-thread stack size
  187. -- is limited to 2 Megabytes, so be sparse with alloca() uses.
  188. -- - Allocation failure due to insufficient stack space is not indicated
  189. -- with a NULL return like e.g. with malloc(). Instead, most systems
  190. -- probably handle it the same way as out of stack space situations from
  191. -- infinite function recursion, i.e. with a segmentation fault.
  192. -- - Special care has to be taken when mixing alloca() with GNU C
  193. -- variable sized arrays. Stack space allocated with alloca() in the same
  194. -- scope as a variable sized array will be freed together with the
  195. -- variable sized array upon exit of that scope, and not upon exit of the
  196. -- enclosing function scope.
  197. -- size : number of bytes to allocate.
  198. -- Returns : space for size bytes, allocated on the stack
  199. -- _________________________________________________________________
  200. -- g_newa()
  201. -- #define g_newa(struct_type, n_structs)
  202. -- Wraps g_alloca() in a more typesafe manner.
  203. -- struct_type : Type of memory chunks to be allocated
  204. -- n_structs : Number of chunks to be allocated
  205. -- Returns : Pointer to stack space for n_structs chunks of type
  206. -- struct_type
  207. -- _________________________________________________________________
  208. -- g_memmove()
  209. -- #define g_memmove(dest,src,len)
  210. g_memmove(src, dst: POINTER; len:INTEGER) is
  211. -- Copies a block of memory len bytes long, from src to dest. The source
  212. -- and destination areas may overlap.
  213. -- dest : the destination address to copy the bytes to.
  214. -- src : the source address to copy the bytes from.
  215. -- len : the number of bytes to copy.
  216. external "C inline use <string.h>"
  217. alias "memmove"
  218. end
  219. -- In order to use this function, you must include string.h yourself,
  220. -- because this macro will typically simply resolve to memmove() and GLib
  221. -- does not include string.h for you.
  222. -- dest : the destination address to copy the bytes to.
  223. -- src : the source address to copy the bytes from.
  224. -- len : the number of bytes to copy.
  225. -- _________________________________________________________________
  226. -- g_memdup ()
  227. -- gpointer g_memdup (gconstpointer mem,
  228. -- guint byte_size);
  229. -- Allocates byte_size bytes of memory, and copies byte_size bytes into
  230. -- it from mem. If mem is NULL it returns NULL.
  231. -- mem : the memory to copy.
  232. -- byte_size : the number of bytes to copy.
  233. -- Returns : a pointer to the newly-allocated copy of the memory, or NULL
  234. -- if mem is NULL.
  235. -- _________________________________________________________________
  236. -- GMemVTable
  237. -- typedef struct {
  238. -- gpointer (*malloc) (gsize n_bytes);
  239. -- gpointer (*realloc) (gpointer mem,
  240. -- gsize n_bytes);
  241. -- void (*free) (gpointer mem);
  242. -- /* optional; set to NULL if not used ! */
  243. -- gpointer (*calloc) (gsize n_blocks,
  244. -- gsize n_block_bytes);
  245. -- gpointer (*try_malloc) (gsize n_bytes);
  246. -- gpointer (*try_realloc) (gpointer mem,
  247. -- gsize n_bytes);
  248. -- } GMemVTable;
  249. -- A set of functions used to perform memory allocation. The same
  250. -- GMemVTable must be used for all allocations in the same program; a
  251. -- call to g_mem_set_vtable(), if it exists, should be prior to any
  252. -- use of GLib.
  253. -- malloc () function to use for allocating memory.
  254. -- realloc () function to use for reallocating memory.
  255. -- free () function to use to free memory.
  256. -- calloc () function to use for allocating zero-filled memory.
  257. -- try_malloc () function to use for allocating memory without a default
  258. -- error handler.
  259. -- try_realloc () function to use for reallocating memory without a
  260. -- default error handler.
  261. -- _________________________________________________________________
  262. -- g_mem_set_vtable ()
  263. -- void g_mem_set_vtable (GMemVTable *vtable);
  264. -- Sets the GMemVTable to use for memory allocation. You can use this
  265. -- to provide custom memory allocation routines. This function must be
  266. -- called before using any other GLib functions. The vtable only needs to
  267. -- provide malloc(), realloc(), and free() functions; GLib can provide
  268. -- default implementations of the others. The malloc() and realloc()
  269. -- implementations should return NULL on failure, GLib will handle
  270. -- error-checking for you. vtable is copied, so need not persist after
  271. -- this function has been called.
  272. -- vtable : table of memory allocation routines.
  273. -- _________________________________________________________________
  274. -- g_mem_is_system_malloc ()
  275. -- gboolean g_mem_is_system_malloc (void);
  276. -- Checks whether the allocator used by g_malloc() is the system's
  277. -- malloc implementation. If it returns TRUE memory allocated with
  278. -- malloc() can be used interchangeable with memory allocated using
  279. -- g_malloc(). This function is useful for avoiding an extra copy of
  280. -- allocated memory returned by a non-GLib-based API.
  281. -- A different allocator can be set using g_mem_set_vtable().
  282. -- Returns : if TRUE, malloc() and g_malloc() can be mixed.
  283. -- _________________________________________________________________
  284. -- glib_mem_profiler_table
  285. -- extern GMemVTable *glib_mem_profiler_table;
  286. -- A GMemVTable containing profiling variants of the memory
  287. -- allocation functions. Use them together with g_mem_profile() in
  288. -- order to get information about the memory allocation pattern of your
  289. -- program.
  290. -- _________________________________________________________________
  291. -- g_mem_profile ()
  292. -- void g_mem_profile (void);
  293. -- Outputs a summary of memory usage.
  294. -- It outputs the frequency of allocations of different sizes, the total
  295. -- number of bytes which have been allocated, the total number of bytes
  296. -- which have been freed, and the difference between the previous two
  297. -- values, i.e. the number of bytes still in use.
  298. -- Note that this function will not output anything unless you have
  299. -- previously installed the glib_mem_profiler_table with
  300. -- g_mem_set_vtable().
  301. end