Docsity

C Malloc Implementation - Systems Programming | CS 351, Study notes of Computer Science

Partial preview of the text

Download C Malloc Implementation - Systems Programming | CS 351 and more Study notes Computer Science in PDF only on Docsity!

CS351 : Saelee

C malloc

implementation

void *malloc(size_t size); void *realloc(void *ptr, size_t size); void free(void *ptr); C Standard Library

basic issues:

  • tracking free space
  • tracking allocated blocks
  • storing block metadata

Heap start brk logical view

heap records (blocks) must be self-describing

  1. storing block metadata

malloc(10) metadata payload 10 bytes pointer returned to application

block size

need to distinguish allocated vs. free blocks

allocated bit — use least significant bit of header word

malloc( 11 ) 0x0000000F (^) payload 4 bytes 11 bytes ??

require block size alignment to powers of 2

  1. tracking allocated blocks

not really necessary!