usermalloc.txt 1.0 KB

123456789101112131415161718192021222324
  1. User-level malloc
  2. -----------------
  3. The user-level malloc implementation is defined to be simple, not
  4. fast or efficient. It uses a very basic first-fit block algorithm.
  5. There's an 8-byte header which holds the offsets to the previous
  6. and next blocks, a used/free bit, and some magic numbers (for
  7. consistency checking) in the remaining available header bits. It also
  8. allocates in units of 8 bytes to guarantee proper alignment of
  9. doubles. (It also assumes its own headers are aligned on 8-byte
  10. boundaries.)
  11. On malloc(), it searches the entire heap starting at the beginning
  12. for the first block big enough to hold the allocation. If it doesn't
  13. find one, it calls sbrk() to get more memory. If it does find one, it
  14. marks the block in use. It splits the remaining portion of the block
  15. off as a new free block only if said portion is large enough to hold
  16. both a header and some data.
  17. On free(), it marks the block free and then tries to merge it with
  18. the adjacent blocks (both above and below) if they're free.
  19. That's about all there is to it.