malloc.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * libc/stdlib/malloc/malloc.h -- small malloc implementation
  3. *
  4. * Copyright (C) 2002 NEC Corporation
  5. * Copyright (C) 2002 Miles Bader <miles@gnu.org>
  6. *
  7. * This file is subject to the terms and conditions of the GNU Lesser
  8. * General Public License. See the file COPYING.LIB in the main
  9. * directory of this archive for more details.
  10. *
  11. * Written by Miles Bader <miles@gnu.org>
  12. */
  13. /* The alignment we guarantee for malloc return values. */
  14. #define MALLOC_ALIGNMENT (sizeof (double))
  15. /* The system pagesize we assume; we really ought to get it with
  16. getpagesize, but gee, how annoying. */
  17. #define MALLOC_PAGE_SIZE 4096
  18. /* The minimum size of block we request from the the system to extend the
  19. heap for small allocations (we may request a bigger block if necessary to
  20. satisfy a particularly big request). */
  21. #define MALLOC_HEAP_EXTEND_SIZE MALLOC_PAGE_SIZE
  22. /* When a heap free-area grows above this size, try to unmap it, releasing
  23. the memory back to the system. */
  24. #define MALLOC_UNMAP_THRESHOLD (8*MALLOC_PAGE_SIZE)
  25. /* When unmapping a free-area, retain this many bytes if it's the only one,
  26. to avoid completely emptying the heap. This is only a heuristic -- the
  27. existance of another free area, even if it's smaller than
  28. MALLOC_MIN_SIZE, will cause us not to reserve anything. */
  29. #define MALLOC_MIN_SIZE (2*MALLOC_PAGE_SIZE)
  30. /* For systems with an MMU, use sbrk to map/unmap memory for the malloc
  31. heap, instead of mmap/munmap. This is a tradeoff -- sbrk is faster than
  32. mmap/munmap, and guarantees contiguous allocation, but is also less
  33. flexible, and causes the heap to only be shrinkable from the end. */
  34. #ifdef __UCLIBC_HAS_MMU__
  35. #define MALLOC_USE_SBRK
  36. #endif
  37. /* Change this to `#if 1' to cause malloc to emit debugging info to stderr. */
  38. #if 0
  39. #include <stdio.h>
  40. #define MALLOC_DEBUG(fmt, args...) fprintf (stderr, fmt , ##args)
  41. #else
  42. #define MALLOC_DEBUG(fmt, args...) (void)0
  43. #endif
  44. /* Return SZ rounded down to POWER_OF_2_SIZE (which must be power of 2). */
  45. #define MALLOC_ROUND_DOWN(sz, power_of_2_size) \
  46. ((sz) & ~(power_of_2_size - 1))
  47. /* Return SZ rounded to POWER_OF_2_SIZE (which must be power of 2). */
  48. #define MALLOC_ROUND_UP(sz, power_of_2_size) \
  49. MALLOC_ROUND_DOWN ((sz) + (power_of_2_size - 1), (power_of_2_size))
  50. /* Return SZ rounded down to a multiple MALLOC_PAGE_SIZE. */
  51. #define MALLOC_ROUND_DOWN_TO_PAGE_SIZE(sz) \
  52. MALLOC_ROUND_DOWN (sz, MALLOC_PAGE_SIZE)
  53. /* Return SZ rounded up to a multiple MALLOC_PAGE_SIZE. */
  54. #define MALLOC_ROUND_UP_TO_PAGE_SIZE(sz) \
  55. MALLOC_ROUND_UP (sz, MALLOC_PAGE_SIZE)
  56. /* The malloc heap. */
  57. extern struct heap __malloc_heap;