malloc.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #define MIN(x,y) ({ \
  12. const typeof(x) _x = (x); \
  13. const typeof(y) _y = (y); \
  14. (void) (&_x == &_y); \
  15. _x < _y ? _x : _y; })
  16. /* The allocator divides the heap into blocks of fixed size; large
  17. requests receive one or more whole blocks, and small requests
  18. receive a fragment of a block. Fragment sizes are powers of two,
  19. and all fragments of a block are the same size. When all the
  20. fragments in a block have been freed, the block itself is freed.
  21. */
  22. #define INT_BIT (CHAR_BIT * sizeof (size_t))
  23. #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
  24. #define BLOCKSIZE (1 << BLOCKLOG)
  25. #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  26. /* Determine the amount of memory spanned by the initial heap table
  27. (not an absolute limit). */
  28. #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
  29. /* Number of contiguous free blocks allowed to build up at the end of
  30. memory before they will be returned to the system. */
  31. #define FINAL_FREE_BLOCKS 8
  32. /* Data structure giving per-block information. */
  33. union info {
  34. struct {
  35. size_t type; /* Zero for a large block, or positive
  36. giving the logarithm to the base two
  37. of the fragment size. */
  38. union {
  39. struct {
  40. size_t nfree; /* Free fragments in a fragmented block. */
  41. size_t first; /* First free fragment of the block. */
  42. } frag;
  43. size_t size; /* Size (in blocks) of a large cluster. */
  44. } info;
  45. } busy;
  46. struct {
  47. size_t size; /* Size (in blocks) of a free cluster. */
  48. size_t next; /* Index of next free cluster. */
  49. size_t prev; /* Index of previous free cluster. */
  50. } free;
  51. };
  52. /* Address to block number and vice versa. */
  53. #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  54. #define ADDRESS(B) ((void *) (((B) - 1) * BLOCKSIZE + _heapbase))
  55. /* Doubly linked lists of free fragments. */
  56. struct list {
  57. struct list *next;
  58. struct list *prev;
  59. };
  60. /* List of blocks allocated with memalign or valloc */
  61. struct alignlist
  62. {
  63. struct alignlist *next;
  64. __ptr_t aligned; /* The address that memaligned returned. */
  65. __ptr_t exact; /* The address that malloc returned. */
  66. };
  67. extern struct alignlist *_aligned_blocks;
  68. extern char *_heapbase;
  69. extern union info *_heapinfo;
  70. extern size_t _heapindex;
  71. extern size_t _heaplimit;
  72. extern void *__malloc_unlocked (size_t size);
  73. extern void __free_unlocked(void *ptr);