home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / malloc / malloc.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-20  |  6.8 KB  |  235 lines

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