malloc.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Declarations for `malloc' and friends.
  2. Copyright 1990, 1991, 1992, 1993, 1995 Free Software Foundation, Inc.
  3. Written May 1989 by Mike Haertel.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Library General Public License as
  6. published by the Free Software Foundation; either version 2 of the
  7. License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Library General Public License for more details.
  12. You should have received a copy of the GNU Library General Public
  13. License along with this library; see the file COPYING.LIB. If
  14. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  15. Cambridge, MA 02139, USA.
  16. The author may be reached (Email) at the address mike@ai.mit.edu,
  17. or (US mail) as Mike Haertel c/o Free Software Foundation. */
  18. #ifndef _MALLOC_H
  19. #define _MALLOC_H 1
  20. #ifdef _MALLOC_INTERNAL
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #if defined(_LIBC) || defined(STDC_HEADERS) || defined(USG)
  25. #include <string.h>
  26. #else
  27. #ifndef memset
  28. #define memset(s, zero, n) bzero ((s), (n))
  29. #endif
  30. #ifndef memcpy
  31. #define memcpy(d, s, n) bcopy ((s), (d), (n))
  32. #endif
  33. #endif
  34. #if defined (__GNU_LIBRARY__) || (defined (__STDC__) && __STDC__)
  35. #include <limits.h>
  36. #else
  37. #ifndef CHAR_BIT
  38. #define CHAR_BIT 8
  39. #endif
  40. #endif
  41. #ifdef HAVE_UNISTD_H
  42. #include <unistd.h>
  43. #endif
  44. #endif /* _MALLOC_INTERNAL. */
  45. #ifdef __cplusplus
  46. extern "C"
  47. {
  48. #endif
  49. #if defined (__cplusplus) || (defined (__STDC__) && __STDC__)
  50. #undef __P
  51. #define __P(args) args
  52. #undef __ptr_t
  53. #define __ptr_t void *
  54. #else /* Not C++ or ANSI C. */
  55. #undef __P
  56. #define __P(args) ()
  57. #undef const
  58. #define const
  59. #undef __ptr_t
  60. #define __ptr_t char *
  61. #endif /* C++ or ANSI C. */
  62. #if defined (__STDC__) && __STDC__
  63. #include <stddef.h>
  64. #define __malloc_size_t size_t
  65. #define __malloc_ptrdiff_t ptrdiff_t
  66. #else
  67. #define __malloc_size_t unsigned int
  68. #define __malloc_ptrdiff_t int
  69. #endif
  70. #ifndef NULL
  71. #define NULL 0
  72. #endif
  73. /* Allocate SIZE bytes of memory. */
  74. extern __ptr_t malloc __P ((__malloc_size_t __size));
  75. /* Re-allocate the previously allocated block
  76. in __ptr_t, making the new block SIZE bytes long. */
  77. extern __ptr_t realloc __P ((__ptr_t __ptr, __malloc_size_t __size));
  78. /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0. */
  79. extern __ptr_t calloc __P ((__malloc_size_t __nmemb, __malloc_size_t __size));
  80. /* Free a block allocated by `malloc', `realloc' or `calloc'. */
  81. extern void free __P ((__ptr_t __ptr));
  82. /* Allocate SIZE bytes allocated to ALIGNMENT bytes. */
  83. extern __ptr_t memalign __P ((__malloc_size_t __alignment,
  84. __malloc_size_t __size));
  85. /* Allocate SIZE bytes on a page boundary. */
  86. extern __ptr_t valloc __P ((__malloc_size_t __size));
  87. #ifdef _MALLOC_INTERNAL
  88. /* The allocator divides the heap into blocks of fixed size; large
  89. requests receive one or more whole blocks, and small requests
  90. receive a fragment of a block. Fragment sizes are powers of two,
  91. and all fragments of a block are the same size. When all the
  92. fragments in a block have been freed, the block itself is freed. */
  93. #define INT_BIT (CHAR_BIT * sizeof(int))
  94. #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
  95. #define BLOCKSIZE (1 << BLOCKLOG)
  96. #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  97. /* Determine the amount of memory spanned by the initial heap table
  98. (not an absolute limit). */
  99. #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
  100. /* Number of contiguous free blocks allowed to build up at the end of
  101. memory before they will be returned to the system. */
  102. #define FINAL_FREE_BLOCKS 8
  103. /* Data structure giving per-block information. */
  104. typedef union
  105. {
  106. /* Heap information for a busy block. */
  107. struct
  108. {
  109. /* Zero for a large (multiblock) object, or positive giving the
  110. logarithm to the base two of the fragment size. */
  111. int type;
  112. union
  113. {
  114. struct
  115. {
  116. __malloc_size_t nfree; /* Free frags in a fragmented block. */
  117. __malloc_size_t first; /* First free fragment of the block. */
  118. } frag;
  119. /* For a large object, in its first block, this has the number
  120. of blocks in the object. In the other blocks, this has a
  121. negative number which says how far back the first block is. */
  122. __malloc_ptrdiff_t size;
  123. } info;
  124. } busy;
  125. /* Heap information for a free block
  126. (that may be the first of a free cluster). */
  127. struct
  128. {
  129. __malloc_size_t size; /* Size (in blocks) of a free cluster. */
  130. __malloc_size_t next; /* Index of next free cluster. */
  131. __malloc_size_t prev; /* Index of previous free cluster. */
  132. } free;
  133. } malloc_info;
  134. /* Pointer to first block of the heap. */
  135. extern char *_heapbase;
  136. /* Table indexed by block number giving per-block information. */
  137. extern malloc_info *_heapinfo;
  138. /* Address to block number and vice versa. */
  139. #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  140. #define ADDRESS(B) ((__ptr_t) (((B) - 1) * BLOCKSIZE + _heapbase))
  141. /* Current search index for the heap table. */
  142. extern __malloc_size_t _heapindex;
  143. /* Limit of valid info table indices. */
  144. extern __malloc_size_t _heaplimit;
  145. /* Doubly linked lists of free fragments. */
  146. struct list
  147. {
  148. struct list *next;
  149. struct list *prev;
  150. };
  151. /* Free list headers for each fragment size. */
  152. extern struct list _fraghead[];
  153. /* List of blocks allocated with `memalign' (or `valloc'). */
  154. struct alignlist
  155. {
  156. struct alignlist *next;
  157. __ptr_t aligned; /* The address that memaligned returned. */
  158. __ptr_t exact; /* The address that malloc returned. */
  159. };
  160. extern struct alignlist *_aligned_blocks;
  161. /* Instrumentation. */
  162. extern __malloc_size_t _chunks_used;
  163. extern __malloc_size_t _bytes_used;
  164. extern __malloc_size_t _chunks_free;
  165. extern __malloc_size_t _bytes_free;
  166. /* Internal version of `free' used in `morecore' (malloc.c). */
  167. extern void _free_internal __P ((__ptr_t __ptr));
  168. #endif /* _MALLOC_INTERNAL. */
  169. /* Given an address in the middle of a malloc'd object,
  170. return the address of the beginning of the object. */
  171. extern __ptr_t malloc_find_object_address __P ((__ptr_t __ptr));
  172. /* Underlying allocation function; successive calls should
  173. return contiguous pieces of memory. */
  174. extern __ptr_t (*__morecore) __P ((__malloc_ptrdiff_t __size));
  175. /* Default value of `__morecore'. */
  176. extern __ptr_t __default_morecore __P ((__malloc_ptrdiff_t __size));
  177. /* If not NULL, this function is called after each time
  178. `__morecore' is called to increase the data size. */
  179. extern void (*__after_morecore_hook) __P ((void));
  180. /* Nonzero if `malloc' has been called and done its initialization. */
  181. extern int __malloc_initialized;
  182. /* Hooks for debugging versions. */
  183. extern void (*__malloc_initialize_hook) __P ((void));
  184. extern void (*__free_hook) __P ((__ptr_t __ptr));
  185. extern __ptr_t (*__malloc_hook) __P ((__malloc_size_t __size));
  186. extern __ptr_t (*__realloc_hook) __P ((__ptr_t __ptr, __malloc_size_t __size));
  187. extern __ptr_t (*__memalign_hook) __P ((__malloc_size_t __size,
  188. __malloc_size_t __alignment));
  189. /* Return values for `mprobe': these are the kinds of inconsistencies that
  190. `mcheck' enables detection of. */
  191. enum mcheck_status
  192. {
  193. MCHECK_DISABLED = -1, /* Consistency checking is not turned on. */
  194. MCHECK_OK, /* Block is fine. */
  195. MCHECK_FREE, /* Block freed twice. */
  196. MCHECK_HEAD, /* Memory before the block was clobbered. */
  197. MCHECK_TAIL /* Memory after the block was clobbered. */
  198. };
  199. /* Activate a standard collection of debugging hooks. This must be called
  200. before `malloc' is ever called. ABORTFUNC is called with an error code
  201. (see enum above) when an inconsistency is detected. If ABORTFUNC is
  202. null, the standard function prints on stderr and then calls `abort'. */
  203. extern int mcheck __P ((void (*__abortfunc) __P ((enum mcheck_status))));
  204. /* Check for aberrations in a particular malloc'd block. You must have
  205. called `mcheck' already. These are the same checks that `mcheck' does
  206. when you free or reallocate a block. */
  207. extern enum mcheck_status mprobe __P ((__ptr_t __ptr));
  208. /* Activate a standard collection of tracing hooks. */
  209. extern void mtrace __P ((void));
  210. extern void muntrace __P ((void));
  211. /* Statistics available to the user. */
  212. struct mstats
  213. {
  214. __malloc_size_t bytes_total; /* Total size of the heap. */
  215. __malloc_size_t chunks_used; /* Chunks allocated by the user. */
  216. __malloc_size_t bytes_used; /* Byte total of user-allocated chunks. */
  217. __malloc_size_t chunks_free; /* Chunks in the free list. */
  218. __malloc_size_t bytes_free; /* Byte total of chunks in the free list. */
  219. };
  220. /* Pick up the current statistics. */
  221. extern struct mstats mstats __P ((void));
  222. /* Call WARNFUN with a warning message when memory usage is high. */
  223. extern void memory_warnings __P ((__ptr_t __start,
  224. void (*__warnfun) __P ((const char *))));
  225. /* Relocating allocator. */
  226. /* Allocate SIZE bytes, and store the address in *HANDLEPTR. */
  227. extern __ptr_t r_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
  228. /* Free the storage allocated in HANDLEPTR. */
  229. extern void r_alloc_free __P ((__ptr_t *__handleptr));
  230. /* Adjust the block at HANDLEPTR to be SIZE bytes long. */
  231. extern __ptr_t r_re_alloc __P ((__ptr_t *__handleptr, __malloc_size_t __size));
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. #endif /* malloc.h */