23 - Dynamic Memory Allocation 1 Outline...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Dynamic Memory Allocation
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2 Outline Implementation of a simple allocator Suggested reading: 10.9
Background image of page 2
3 Implementation issues How do we know how much memory to free just given a  pointer? How do we keep track of the free blocks? p1 = malloc(1) p0 free(p0)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4 Implementation issues What do we do with the extra space when allocating a  structure that is smaller than the free block it is placed in? How do we pick a block to use for allocation  many might fit? How do we reinsert freed block?
Background image of page 4
5 Knowing how much to free Standard method keep the length of a structure in the word preceding the structure This word is often called the header field or header requires an extra word for every allocated structure
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6 Knowing how much to free free(p0) p0 = malloc(4) p0 Block size data 5
Background image of page 6
7 Implicit list Need to identify whether each block is free or allocated Can use extra bit Bit can be put in the same word as the size if block sizes are  always multiples of 8 (mask out low order bit when reading size).
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
8 Implicit list size 1 word Format of allocated and free blocks payload a = 1: allocated block a = 0: free block size: block size payload: application data (allocated blocks only) a optional padding 0 0 4 4 2 6 p
Background image of page 8
9 Finding a free block First fit: Search list from beginning, choose first free block that fits Can take linear time in total number of blocks (allocated and free) In practice it can cause “splinters” at beginning of list
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
10 Finding a free block Next fit : Like first-fit, but search list from location of end of previous  search Research suggests that fragmentation is worse  Best fit: Search the list, choose the free block with the closest size that fits Keeps fragments small --- usually helps fragmentation Will typically run slower than first-fit
Background image of page 10
11 Allocating in a free block Allocating in a free block - splitting Since allocated space might be smaller than free space, we might  want to split the block 4 4 2 6 p 4 2 4 2 4
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
12 Freeing a block Simplest implementation: Only need to clear allocated flag
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 36

23 - Dynamic Memory Allocation 1 Outline...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online