malloc.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* malloc.h - declarations for the allocator.
  2. Copyright (c) 1989, 1993 Michael J. Haertel
  3. You may redistribute this library under the terms of the
  4. GNU Library General Public License (version 2 or any later
  5. version) as published by the Free Software Foundation.
  6. THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED
  7. WARRANTY. IN PARTICULAR, THE AUTHOR MAKES NO REPRESENTATION OR
  8. WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY OF THIS
  9. SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */
  10. #include <sys/cdefs.h>
  11. /* Underlying allocation function; successive calls should return
  12. contiguous pieces of memory. */
  13. extern void *(*__morecore)(long);
  14. /* Default value of previous. */
  15. extern void *__default_morecore_init(long);
  16. extern void *__default_morecore(long);
  17. /* The allocator divides the heap into blocks of fixed size; large
  18. requests receive one or more whole blocks, and small requests
  19. receive a fragment of a block. Fragment sizes are powers of two,
  20. and all fragments of a block are the same size. When all the
  21. fragments in a block have been freed, the block itself is freed.
  22. WARNING: BLOCKSIZE must be set greater than or equal to the
  23. machine's page size for valloc() to work correctly. The default
  24. definition here is 4096 bytes. */
  25. #define INT_BIT (CHAR_BIT * sizeof (int))
  26. #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
  27. #define BLOCKSIZE (1 << BLOCKLOG)
  28. #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  29. /* Determine the amount of memory spanned by the initial heap table
  30. (not an absolute limit). */
  31. #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
  32. /* Number of contiguous free blocks allowed to build up at the end of
  33. memory before they will be returned to the system. */
  34. #define FINAL_FREE_BLOCKS 8
  35. /* Data structure giving per-block information. */
  36. union info {
  37. struct {
  38. int type; /* Zero for a large block, or positive
  39. giving the logarithm to the base two
  40. of the fragment size. */
  41. union {
  42. struct {
  43. int nfree; /* Free fragments in a fragmented block. */
  44. int first; /* First free fragment of the block. */
  45. } frag;
  46. int size; /* Size (in blocks) of a large cluster. */
  47. } info;
  48. } busy;
  49. struct {
  50. int size; /* Size (in blocks) of a free cluster. */
  51. int next; /* Index of next free cluster. */
  52. int prev; /* Index of previous free cluster. */
  53. } free;
  54. };
  55. /* Pointer to first block of the heap. */
  56. extern char *_heapbase;
  57. /* Table indexed by block number giving per-block information. */
  58. extern union info *_heapinfo;
  59. /* Address to block number and vice versa. */
  60. #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  61. #define ADDRESS(B) ((void *) (((B) - 1) * BLOCKSIZE + _heapbase))
  62. /* Current search index for the heap table. */
  63. extern int _heapindex;
  64. /* Limit of valid info table indices. */
  65. extern int _heaplimit;
  66. /* Doubly linked lists of free fragments. */
  67. struct list {
  68. struct list *next;
  69. struct list *prev;
  70. };
  71. /* Count of blocks for each fragment size. */
  72. extern int _fragblocks[];
  73. /* Free list headers for each fragment size. */
  74. extern struct list _fraghead[];
  75. /* List of blocks allocated with `memalign' (or `valloc'). */
  76. struct alignlist
  77. {
  78. struct alignlist *next;
  79. __ptr_t aligned; /* The address that memaligned returned. */
  80. __ptr_t exact; /* The address that malloc returned. */
  81. };
  82. extern struct alignlist *_aligned_blocks;
  83. extern void _free_internal __P ((__ptr_t __ptr));
  84. extern void free (void *);
  85. extern void * malloc (size_t);
  86. extern void * calloc (size_t, size_t);
  87. extern void * valloc (size_t);
  88. extern void * memalign (size_t, size_t);
  89. extern void * realloc (void *, size_t);