CS 208 s21 — Learning Block #22

Table of Contents

1 Quick Checks

Which allocation strategy and requests remove external fragmentation in this heap?1 B3 was the last fulfilled request.

poll-heap.png

  1. best-fit: malloc(50), malloc(50)
  2. first-fit: malloc(50), malloc(30)
  3. next-fit: malloc(30), malloc(50)
  4. next-fit: malloc(50), malloc(30)

What is the relationship between throughput and memory utilization?2

2 Implicit Free List Implementation

  • section 9.9.12 walks through an implementation of an implicit free list
    • beware it is in 32-bit, and your will need to be in 64-bit (generally involves changing constants)
    • pay more attention to the text descriptions than the code
  • use macros or helper functions like these for pointer manipulation
    • make sure you understand why these work/are necessary before you start coding
/* Round up size to the nearest multiple of 16 */
#define ALIGN(size) (((size) + (0xf)) & ~0xf)

#define WSIZE 8
#define DSIZE 16

/* Given block ptr bp, compute address of its header and footer (block pointer points to beginning of payload) */
#define HDRP(bp) ((char *)(bp) - WSIZE)
#define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)

/* Given block ptr bp, compute address of next and previous blocks */
#define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)))
#define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))

3 Summary

  • Allocator Policies
    • All policies offer trade-offs in fragmentation and throughput.
    • Placement policy:
      • First-fit, next-fit, best-fit
    • Splitting policy:
      • Always? Sometimes?
      • Be mindful of minimum block size—never split off a block smaller than the minimum
      • Which side of the split should the allocated block be on?
        • Left? Right? Alternate?
    • Coalescing policy:
      • Immediate vs. deferred

4 Practice

CSPP practice problem 9.7 (p. 852, 854)

Determine minumum block size for the following combinations of alignment requirements and block formats. Assumptions: implicit free list, zero-size payloads are not allowed, and headers and footers are stored in 4-byte words.3

Alignment Allocated block Free block Minimum block size (bytes)
Single word Header and footer Header and footer  
Single word Header, but no footer Header and footer  
Double word Header and footer Header and footer  
Double word Header, but no footer Header and footer  

Footnotes:

1

2. first-fit: malloc(50), malloc(30) will fill in the gaps between allocated blocks, removing external fragmentation. (1) will leave a gap of 30 between B3 and B2, (3) will leave the gap of 50 between B1 and B3, and (4) will leave a gap of 20 between B1 and B3 and a gap of 30 between B3 and B2

2

There's a trade-off between processing a request faster and finding a better choice of block

3
Alignment Allocated block Free block Minimum block size (bytes)
Single word Header and footer Header and footer 12
Single word Header, but no footer Header and footer 8
Double word Header and footer Header and footer 16
Double word Header, but no footer Header and footer 8