malloc.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. /* The allocator divides the heap into blocks of fixed size; large
  12. requests receive one or more whole blocks, and small requests
  13. receive a fragment of a block. Fragment sizes are powers of two,
  14. and all fragments of a block are the same size. When all the
  15. fragments in a block have been freed, the block itself is freed.
  16. */
  17. #define INT_BIT (CHAR_BIT * sizeof (size_t))
  18. #define BLOCKLOG (INT_BIT > 16 ? 12 : 9)
  19. #define BLOCKSIZE (1 << BLOCKLOG)
  20. #define BLOCKIFY(SIZE) (((SIZE) + BLOCKSIZE - 1) / BLOCKSIZE)
  21. /* Determine the amount of memory spanned by the initial heap table
  22. (not an absolute limit). */
  23. #define HEAP (INT_BIT > 16 ? 4194304 : 65536)
  24. /* Number of contiguous free blocks allowed to build up at the end of
  25. memory before they will be returned to the system. */
  26. #define FINAL_FREE_BLOCKS 8
  27. /* Data structure giving per-block information. */
  28. union info {
  29. struct {
  30. size_t type; /* Zero for a large block, or positive
  31. giving the logarithm to the base two
  32. of the fragment size. */
  33. union {
  34. struct {
  35. size_t nfree; /* Free fragments in a fragmented block. */
  36. size_t first; /* First free fragment of the block. */
  37. } frag;
  38. size_t size; /* Size (in blocks) of a large cluster. */
  39. } info;
  40. } busy;
  41. struct {
  42. size_t size; /* Size (in blocks) of a free cluster. */
  43. size_t next; /* Index of next free cluster. */
  44. size_t prev; /* Index of previous free cluster. */
  45. } free;
  46. };
  47. /* Address to block number and vice versa. */
  48. #define BLOCK(A) (((char *) (A) - _heapbase) / BLOCKSIZE + 1)
  49. #define ADDRESS(B) ((void *) (((B) - 1) * BLOCKSIZE + _heapbase))
  50. /* Doubly linked lists of free fragments. */
  51. struct list {
  52. struct list *next;
  53. struct list *prev;
  54. };