Build an Allocation, Then End Ownership

1 / 7

Two milestones

  • naive-malloc: every positive request appends a block.
  • free-basic: free marks a block; allocation still always appends.
  • Search and reuse arrive in the next module.
2 / 7

Two pointer contracts

  • request_space returns a header.
  • my_malloc returns the immediately following payload.
  • Store payload capacity; initialize free, magic, and successor before append.
3 / 7

Derive alignment

  • Illustrative H=24, A=8, break=4096.
  • Request 5: header 4096, payload 4120, next break 4125.
  • Request 9: padding 3, header 4128, payload 4152.
  • Use sizeof and _Alignof; logical neighbors need not touch.
4 / 7

Failure has no commit

  • Reject zero and sizes beyond INTPTR_MAX-H-(A-1).
  • Query break; calculate padding; request checked total.
  • (void*)-1 means failure; append only after successful growth.
  • Keep old links and live payloads intact on failure.
5 / 7

Free changes one flag

  • NULL is a no-op before pointer arithmetic.
  • Other inputs must be live payload pointers from this allocator.
  • Preserve capacity and successor; caller ownership ends.
  • A magic marker cannot safely validate an arbitrary address.
6 / 7

Reconstruct

  • H=24, A=8, break=5001: allocate 7 then 8, then free the first.
  • Predict addresses and flags before opening the lesson reveal.
  • Check odd sizes and huge rejections, not only successful aligned requests.
7 / 7
←/→ or click edges to navigate · ? help · N notes · F fullscreen