Dynamic Memory Allocation (II) Implementation - PowerPoint PPT Presentation

1 / 35
About This Presentation
Title:

Dynamic Memory Allocation (II) Implementation

Description:

Title: Introduction to Computer Systems Author: Binyu Zang Last modified by: Yi Li Created Date: 1/15/2000 7:54:11 AM Document presentation format – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 36
Provided by: Biny151
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Memory Allocation (II) Implementation


1
Dynamic Memory Allocation(II) Implementation
2
Outline
  • Implementation of a simple allocator
  • Explicit Free List
  • Segregated Free List
  • Suggested reading 9.9

3
Review
1 word
header
a 1 allocated block a 0 free block size
block size payload application data (allocated
blocks only)
Format of allocated and free blocks
boundary tag (footer)
4
Implementing a Simple Allocator
  • 1 int mm_init(void)
  • 2 void mm_malloc(size_t size)
  • 3 void mm_free(void bp)

5
Initialize
  • 1 / private global variables /
  • 2 static void mem_heap / points to first byte
    of the heap /
  • 3 static void mem_brk / points to last byte
    of the heap /
  • 4 static void mem_max_addr / max virtual
    address for the heap /
  • 5

6
Initialize
  • 6 /
  • 7 mem_init - initializes the memory system
    model
  • 8 /
  • 9 void mem_init(void)
  • 10
  • 11 mem_heap (char )Malloc(MAX_HEAP)
  • 12 mem_brk (char )mem_heap
  • 13 mem_max_addr (char ) (mem_heap
    MAX_HEAP)
  • 14
  • 15

7
Initialize
  • 16 /
  • 17 mem_sbrk - simple model of the sbrk
    function. Extends the
  • 18 heap by incr bytes and returns the start
    address of the new
  • 19 area. In this model, the heap cannot be
    shrunk.
  • 20 /

8
Initialize
  • 21 void mem_sbrk(int incr)
  • 22
  • 23 void old_brk mem_brk
  • 24
  • 25 if ( (incr lt 0) ((mem_brk incr) gt
    mem_max_addr))
  • errno ENOMEM
  • fprintf(stderr, ERROR mem_sbrk failed.
    Ran out of memory \n)
  • 28 return (void )-1
  • 29
  • 30 mem_brk incr
  • 31 return old_brk
  • 32

9
Data Structure
10
Macros
  • 1 / Basic constants and macros /
  • 2 define WSIZE 4 / word size (bytes) /
  • 3 define DSIZE 8 / double word size (bytes) /
  • define CHUNKSIZE (1ltlt12) / Extend heap by this
    amount (bytes) /
  • 6 define MAX(x, y) ((x) gt (y)? (x) (y))
  • 7
  • 8 / Pack a size and allocated bit into a word
    /
  • 9 define PACK(size, alloc) ((size) (alloc))
  • 10
  • 11 / Read and write a word at address p /
  • 12 define GET(p) ((unsigned int )(p))
  • 13 define PUT(p, val) ((unsigned int )(p)
    (val))
  • 14

11
Macros
  • 15 / Read the size and allocated fields from
    address p /
  • 16 define GET_SIZE(p) (GET(p) 0x7)
  • 17 define GET_ALLOC(p) (GET(p) 0x1)
  • 18
  • 19 / Given block ptr bp, compute address of its
    header and footer /
  • 20 define HDRP(bp) ((char )(bp) - WSIZE)
  • 21 define FTRP(bp) ((char )(bp)
    GET_SIZE(HDRP(bp)) - DSIZE)
  • 22
  • 23 / Given block ptr bp, compute address of
    next and previous blocks /
  • 24 define NEXT_BLKP(bp) ((char )(bp)
    GET_SIZE(((char )(bp)-WSIZE)))
  • 25 define PREV_BLKP(bp) ((void )(bp) -
    GET_SIZE(((void )(bp) - DSIZE)))

12
mm_init()
  • 1 int mm_init(void)
  • 2
  • 3 / create the initial empty heap /
  • 4 if ((heap_listp mem_sbrk(4WSIZE)) (void
    ) -1)
  • 5 return -1
  • 6 PUT(heap_listp, 0) / alignment
    padding /
  • 7 PUT(heap_listp(1WSIZE), PACK(DSIZE, 1))
    / prologue header /
  • 8 PUT(heap_listp(2WSIZE), PACK(DSIZE, 1))
    / prologue footer /
  • 9 PUT(heap_listp(3WSIZE), PACK(0, 1))
    / epilogue header /
  • 10 heap_listp (2WIZE)
  • 11
  • 12 / Extend the empty heap with a free block
    of CHUNKSIZE bytes /
  • 13 if (extend_heap(CHUNKSIZE/WSIZE) NULL)
  • 14 return -1
  • 15 return 0
  • 16

13
mm_init()
  • 1 static void extend_heap(size_t words)
  • 2
  • 3 char bp
  • 4 size_t size
  • 5
  • 6 / Allocate an even number of words to
    maintain alignment /
  • 7 size (words 2) ? (words1) WSIZE
    words WSIZE
  • 8 if ((long)(bp mem_sbrk(size)) -1)
  • 9 return NULL
  • 10
  • 11 / Initialize free block header/footer and
    the epilogue header /
  • 12 PUT(HDRP(bp), PACK(size, 0)) / free
    block header /
  • 13 PUT(FTRP(bp), PACK(size, 0)) / free
    block footer /
  • 14 PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)) / new
    epilogue header /
  • 15
  • 16 / Coalesce if the previous block was free
    /
  • 17 return coalesce(bp)
  • 18

14
mm_free()
  • 1 void mm_free(void bp)
  • 2
  • 3 size_t size GET_SIZE(HDRP(bp))
  • 4
  • 5 PUT(HDRP(bp), PACK(size, 0))
  • 6 PUT(FTRP(bp), PACK(size, 0))
  • 7 coalesce(bp)
  • 8
  • 9

15
mm_free()
  • 10 static void coalesce(void bp)
  • 11
  • 12 size_t prev_alloc GET_ALLOC(FTRP(PREV_BLKP(
    bp)))
  • 13 size_t next_alloc GET_ALLOC(HDRP(NEXT_BLKP(
    bp)))
  • 14 size_t size GET_SIZE(HDRP(bp))
  • 15
  • 16 if (prev_alloc next_alloc) / Case 1 /
  • 17 return bp
  • 18
  • 19
  • 20 else if (prev_alloc !next_alloc) /
    Case 2 /
  • 21 size GET_SIZE(HDRP(NEXT_BLKP(bp)))
  • 22 PUT(HDRP(bp), PACK(size, 0))
  • 23 PUT(FTRP(bp), PACK(size,0))
  • 24 return(bp)
  • 25
  • 26

16
mm_free()
  • 27 else if (!prev_alloc next_alloc) /
    Case 3 /
  • 28 size GET_SIZE(HDRP(PREV_BLKP(bp)))
  • 29 PUT(FTRP(bp), PACK(size, 0))
  • 30 PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0))
  • 31 return(PREV_BLKP(bp))
  • 32
  • 33
  • 34 else / Case 4 /
  • 35 size GET_SIZE(HDRP(PREV_BLKP(bp)))
  • 36 GET_SIZE(FTRP(NEXT_BLKP(bp)))
  • 37 PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0))
  • 38 PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0))
  • 39 return(PREV_BLKP(bp))
  • 40
  • 41

17
mm_malloc()
  • 1 void mm_malloc (size_t size)
  • 2
  • 3 size_t asize / adjusted block size /
  • 4 size_t extendsize / amount to extend heap
    if no fit /
  • 5 char bp
  • 6
  • 7 / Ignore spurious requests /
  • 8 if (size lt 0)
  • 9 return NULL
  • 10
  • 11 / Adjust block size to include overhead and
    alignment reqs. /
  • 12 if (size lt DSIZE)
  • 13 asize 2DSIZE
  • 14 else
  • 15 asize DSIZE ((size (DSIZE)
    (DSIZE-1)) / DSIZE)
  • 16

18
mm_malloc()
  • 17 / Search the free list for a fit /
  • 18 if ((bp find_fit(asize)) ! NULL)
  • 19 place (bp, asize)
  • 20 return bp
  • 21
  • 22
  • 23 / No fit found. Get more memory and place
    the block /
  • 24 extendsize MAX (asize, CHUNKSIZE)
  • 25 if ((bp extend_heap (extendsize/WSIZE))
    NULL)
  • 26 return NULL
  • 27 place (bp, asize)
  • 28 return bp
  • 29

19
mm_alloc()
  1. static void find_fit(size_t asize)
  2. void bp
  3. / first fit search /
  4. for (bp heap_listp GET_SIZE(HDRP(bp)) gt
    0 bp NEXT_BLKP(bp) )
  5. if (!GET_ALLOC(HDRP(bp))
    (asizeltGET_SIZE(HDRP(bp))))
  6. return bp
  7. return NULL /no fit /

20
mm_alloc()
  1. static void place(void bp, size_t asize)
  2. size_t csize GET_SIZE(HDRP(bp))
  3. if ( (csize asize) gt (DSIZE OVERHEAD) )
  4. PUT(HDRP(bp), PACK(asize, 1))
  5. PUT(FTRP(bp), PACK(asize, 1))
  6. bp NEXT_BLKP(bp)
  7. PUT(HDRP(bp), PACK(csize-asize, 0)
  8. PUT(FTRP(bp), PACK(csize-asize, 0)
  9. else
  10. PUT(HDRP(bp), PACK(csize, 1)
  11. PUT(FTRP(bp), PACK(csize, 1)

21
Explicit free lists
22
Explicit free lists
  • Explicit list among the free blocks using
    pointers within the free blocks
  • Use data space for link pointers
  • Typically doubly linked
  • Still need boundary tags for coalescing
  • It is important to realize that links are not
    necessarily in the same order as the blocks

23
Explicit free lists
24
Freeing with explicit free lists
  • Where to put the newly freed block in the free
    list
  • LIFO (last-in-first-out) policy
  • insert freed block at the beginning of the free
    list
  • pro simple and constant time
  • con studies suggest fragmentation is worse than
    address ordered.

25
Freeing with explicit free lists
  • Where to put the newly freed block in the free
    list
  • Address-ordered policy
  • insert freed blocks so that free list blocks are
    always in address order
  • i.e. addr(pred) lt addr(curr) lt addr(succ)
  • con requires search
  • pro studies suggest fragmentation is better
    than LIFO

26
Segregated Storage
  • Each size class has its own collection of
    blocks
  • Often have separate collection for every small
    size (2,3,4,)
  • For larger sizes typically have a collection for
    each power of 2

27
Segregated Storage
28
Simple segregated storage
  • Separate heap and free list for each size class
  • No splitting
  • To allocate a block of size n
  • if free list for size n is not empty,
  • allocate first block on list (note, list can be
    implicit or explicit)
  • if free list is empty,
  • get a new page
  • create new free list from all blocks in page
  • allocate first block on list
  • constant time

29
Simple segregated storage
  • To free a block
  • Add to free list
  • Tradeoffs
  • fast, but can fragment badly

30
Segregated fits
  • Array of free lists, each one for some size class

31
Segregated fits
  • To allocate a block of size n
  • search appropriate free list for block of size m
    gt n
  • if an appropriate block is found
  • split block and place fragment on appropriate
    list (optional)
  • if no block is found, try next larger class
  • repeat until block is found
  • if no blocks is found in all classes, try more
    heap memory

32
Segregated fits
  • To free a block
  • coalesce and place on appropriate list (optional)
  • Tradeoffs
  • faster search than sequential fits (i.e., log
    time for power of two size classes)
  • controls fragmentation
  • A simple first-fit approximates a best-fit over
    entire heap
  • coalescing can increase search times
  • deferred coalescing can help

33
Buddy Systems
  • A special case of segregated fits
  • Each size is power of 2
  • Initialize
  • A heap of size 2m

34
Buddy Systems
  • Allocate
  • Roundup to power of 2 such as 2k
  • Find a free block of size 2j (k ? j ? m)
  • Split the block in half until jk
  • Each remaining half block (buddy) is placed on
    the appreciate free list
  • Free
  • Continue coalescing with the free buddies

35
Next
  • Physical and Virtual Addressing
  • Address Spaces
  • VM as a Tool for Caching
  • VM as a Tool for Memory Management
  • VM as a Tool for Memory Protection
  • Suggested reading 9.19.5
Write a Comment
User Comments (0)
About PowerShow.com