CS 33. Storage Allocation. CS33 Intro to Computer Systems XXVII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Similar documents
Dynamic Memory Allocation

Dynamic Memory Allocation: Basic Concepts

Dynamic Memory Allocation: Basic Concepts

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Introduction to Computer Systems /18 243, fall th Lecture, Oct. 22 th

Dynamic Memory Allocation. Basic Concepts. Computer Organization 4/3/2012. CSC252 - Spring The malloc Package. Kai Shen

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Allocation. Gerson Robboy Portland State University. class20.ppt

Dynamic Memory Allocation I Nov 5, 2002

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Allocation I

Dynamic Memory Allocation

Process s Address Space. Dynamic Memory. Backing the Heap. Dynamic memory allocation 3/29/2013. When a process starts the heap is empty

CSCI-UA /2. Computer Systems Organization Lecture 19: Dynamic Memory Allocation: Basics

CS 153 Design of Operating Systems

Binghamton University Dynamic Memory Alloca/on

Memory Allocation. Copyright : University of Illinois CS 241 Staff 1

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Foundations of Computer Systems

Foundations of Computer Systems

Dynamic Memory Allocation

Dynamic Memory Allocation

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

ANITA S SUPER AWESOME RECITATION SLIDES

Internal Fragmentation

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation: Advanced Concepts

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation. Zhaoguo Wang

Dynamic Memory Allocation: Advanced Concepts

Today. Dynamic Memory Allocation: Advanced Concepts. Explicit Free Lists. Keeping Track of Free Blocks. Allocating From Explicit Free Lists

Dynamic Memory Allocation: Advanced Concepts

Dynamic Memory Alloca/on: Advanced Concepts

Carnegie Mellon. Malloc Boot Camp. Stan, Nikhil, Kim

Dynamic Memory Allocation II October 22, 2008

Recitation #11 Malloc Lab. November 7th, 2017

CS 153 Design of Operating Systems

Recitation #12 Malloc Lab - Part 2. November 14th, 2017

Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Management

Princeton University Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

Princeton University. Computer Science 217: Introduction to Programming Systems. Dynamic Memory Management

10.1. CS356 Unit 10. Memory Allocation & Heap Management

Memory management. Johan Montelius KTH

CS61C : Machine Structures

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

CSE 351, Spring 2010 Lab 7: Writing a Dynamic Storage Allocator Due: Thursday May 27, 11:59PM

Optimizing Dynamic Memory Management

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Review! Lecture 5 C Memory Management !

Dynamic Memory Allocation

CS61C : Machine Structures

CS201: Lab #4 Writing a Dynamic Storage Allocator

Lectures 13 & 14. memory management

Engine Support System. asyrani.com

Secure Software Programming and Vulnerability Analysis

Dynamic Memory Management

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

ECE 598 Advanced Operating Systems Lecture 10

A.Arpaci-Dusseau. Mapping from logical address space to physical address space. CS 537:Operating Systems lecture12.fm.2

CS 536 Introduction to Programming Languages and Compilers Charles N. Fischer Lecture 11

Memory Management. To do. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

CS61C : Machine Structures

Memory Management. Didactic Module 14 Programming Languages - EEL670 1

Memory Management. Chapter Fourteen Modern Programming Languages, 2nd ed. 1

CS61C : Machine Structures

CSCI0330 Intro Computer Systems Doeppner. Project Malloc. Due: 11/28/18. 1 Introduction 1. 2 Assignment Specification Support Routines 4

CSE 361S Intro to Systems Software Final Project

Memory Management. q Basic memory management q Swapping q Kernel memory allocation q Next Time: Virtual memory

Spring 2016, Malloc Lab: Writing Dynamic Memory Allocator

HW 3: Malloc CS 162. Due: Monday, March 28, 2016

CS 213, Fall 2002 Malloc Lab: Writing a Debugging Dynamic Storage Allocator Assigned: Fri Nov. 1, Due: Tuesday Nov. 19, 11:59PM

Memory Allocation III

ECE 598 Advanced Operating Systems Lecture 12

ECE454, Fall 2014 Homework3: Dynamic Memory Allocation Assigned: Oct 9th, Due: Nov 6th, 11:59PM

CS61C : Machine Structures

CS61C : Machine Structures

In Java we have the keyword null, which is the value of an uninitialized reference type

Requirements, Partitioning, paging, and segmentation

CSE351 Spring 2010 Final Exam (9 June 2010)

1 Introduction. 2 Logistics. 3 Hand Out Instructions

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

My malloc: mylloc and mhysa. Johan Montelius HT2016

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

CSE351 Spring 2010 Final Exam (9 June 2010)

Short Notes of CS201

Heap Management. Heap Allocation

CS201 - Introduction to Programming Glossary By

CS 105 Malloc Lab: Writing a Dynamic Storage Allocator See Web page for due date

Lecture 8 Dynamic Memory Allocation

COSC Software Engineering. Lectures 14 and 15: The Heap and Dynamic Memory Allocation

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Dynamic Memory: Alignment and Fragmentation

Class Information ANNOUCEMENTS

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Assignment 5. CS/ECE 354 Spring 2016 DUE: April 22nd (Friday) at 9 am

CS 2461: Computer Architecture I: Heap: Dynamic Memory Allocation Data Structures. Recap. Variables and Structures

CS 241 Honors Memory

Transcription:

CS 33 Storage Allocation CS33 Intro to Computer Systems XXVII 1 Copyright 2017 Thomas W. Doeppner. All rights reserved.

The Unix Address Space stack dynamic bss program break data text CS33 Intro to Computer Systems XXVII 2 Copyright 2017 Thomas W. Doeppner. All rights reserved.

sbrk System Call void *sbrk(intptr_t increment) moves the program break by an amount equal to increment returns the previous program break intptr_t is typedef d to be a long CS33 Intro to Computer Systems XXVII 3 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Managing Dynamic Storage Strategy get a chunk of memory from the OS using sbrk» create pool of available storage, aka the heap malloc, calloc, realloc, and free use this storage if possible» they manage the heap if not possible, get more storage from OS» heap is made larger (by calling sbrk) Important note: when process terminates, all storage is given back to the system» all memory-related sins are forgotten! CS33 Intro to Computer Systems XXVII 4 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Dynamic Memory Allocation Allocator maintains heap as collection of variable sized blocks, which are either allocated or free Types of allocators explicit allocator: application allocates and frees space» e.g., malloc and free in C implicit allocator: application allocates, but does not free space» e.g. garbage collection in Java, ML, and Racket CS33 Intro to Computer Systems XXVII 5 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Assumptions Made in This Lecture Memory is word addressed (each word can hold a pointer) Allocated block (4 words) Free block (3 words) Free word Allocated word CS33 Intro to Computer Systems XXVII 6 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Allocation Example p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(2) CS33 Intro to Computer Systems XXVII 7 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Constraints Applications can issue arbitrary sequence of malloc and free requests free request must be to a malloc d block Allocators can t control number or size of allocated blocks must respond immediately to malloc requests» i.e., can t reorder or buffer requests must allocate blocks from free memory» i.e., can only place allocated blocks in free memory must align blocks so they satisfy all alignment requirements» 8-byte alignment for GNU malloc (libc malloc) on Linux can manipulate and modify only free memory can t move the allocated blocks once they are malloc d» i.e., compaction is not allowed CS33 Intro to Computer Systems XXVII 8 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Internal Fragmentation For a given block, internal fragmentation occurs if payload is smaller than block size Block Internal fragmentation Payload Internal fragmentation Caused by overhead of maintaining heap data structures padding for alignment purposes explicit policy decisions (e.g., to return a big block to satisfy a small request) Depends only on the pattern of previous requests thus, easy to measure CS33 Intro to Computer Systems XXVII 9 Copyright 2017 Thomas W. Doeppner. All rights reserved.

External Fragmentation Occurs when there is enough aggregate heap memory, but no single free block is large enough p1 = malloc(4) p2 = malloc(5) p3 = malloc(6) free(p2) p4 = malloc(6) Depends on the pattern of future requests thus, difficult to measure Oops! (what would happen now?) CS33 Intro to Computer Systems XXVII 10 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implementation Issues How do we know how much memory to free given just a pointer? How do we keep track of the free blocks? 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? CS33 Intro to Computer Systems XXVII 11 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Knowing How Much to Free Standard method keep the length of a block in the word preceding the block» this word is often called the header field or header requires an extra word for every allocated block p0 p0 = malloc(4) 5 block size data free(p0) CS33 Intro to Computer Systems XXVII 12 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Keeping Track of Free Blocks Method 1: Implicit list using length links all blocks 5 4 6 2 Method 2: Explicit list among the free blocks using pointers 5 4 6 2 Method 3: Segregated free list different free lists for different size classes Method 4: Blocks sorted by size can use a balanced tree (e.g. red-black tree) with pointers within each free block, and the length used as a key CS33 Intro to Computer Systems XXVII 13 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Method 1: Implicit List For each block we need both size and allocation status could store this information in two words: wasteful! Standard trick if blocks are aligned, some low-order address bits are always 0 instead of storing an always-0 bit, use it as a allocated/free flag when reading size word, mask out this bit 1 word Format of allocated and free blocks Size Payload a a = 1: Allocated block a = 0: Free block Size: block size Payload: application data (allocated blocks only) Optional padding CS33 Intro to Computer Systems XXVII 14 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Detailed Implicit Free-List Example Start of heap Unused 2/0 4/1 8/0 4/1 0/1 Double-word aligned Allocated blocks: shaded Free blocks: unshaded Headers: labeled with size in bytes/allocated bit CS33 Intro to Computer Systems XXVII 15 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implicit List: Finding a Free Block First fit: search list from beginning, choose first free block that fits: p = start; while ((p < end) && // not past end ((*p & 1) // already allocated (*p <= len))) // too small p = p + (*p & -2); // goto next block (word addressed) can take linear time in total number of blocks (allocated and free) in practice it can cause splinters at beginning of list Next fit: like first fit, but search list starting where previous search finished should often be faster than first fit: avoids re-scanning unhelpful blocks some research suggests that fragmentation is worse Best fit: search the list, choose the best free block: fits, with fewest bytes left over keeps fragments small usually helps fragmentation will typically run slower than first fit CS33 Intro to Computer Systems XXVII 16 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 1 1300 1200 We have two free blocks of memory, of sizes 1300 and 1200 (appearing in that order). There are three successive requests to malloc for allocations of 1000, 1100, and 250 bytes. Which approach does best? (Hint: one of the two fails the last request.) a) first fit b) best fit CS33 Intro to Computer Systems XXVII 17 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Allocation 1300 First Fit 1200 Best Fit 300 1200 300 100 1000 bytes 1100 bytes 1300 200 200 200 50 100 250 bytes Stuck! CS33 Intro to Computer Systems XXVII 18 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implicit List: Allocating in Free Block Allocating in a free block: splitting since allocated space might be smaller than free space, we might want to split the block addblock(p, 4) 4 4 6 2 p 4 4 4 2 2 void addblock(ptr p, int len) { int newsize = ((len + 1) >> 1) << 1; // round up to even int oldsize = *p & -2; // mask out low bit *p = newsize 1; // set new length if (newsize < oldsize) *(p+newsize) = oldsize - newsize; // set length in remaining } // part of block CS33 Intro to Computer Systems XXVII 19 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implicit List: Freeing a Block Simplest implementation: need only clear the allocated flag void free_block(ptr p) { *p = *p & -2 } but can lead to false fragmentation 4 4 4 2 2 free(p) p 4 4 4 2 2 malloc(5) Oops! There is enough free space, but the allocator won t be able to find it CS33 Intro to Computer Systems XXVII 20 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implicit List: Coalescing Join (coalesce) with next/previous blocks, if they are free coalescing with next block free(p) 4 4 4 2 2 p logically gone 4 4 6 2 2 void free_block(ptr p) { *p = *p & -2; // clear allocated flag next = p + *p; // find next block if ((*next & 1) == 0) *p = *p + *next; // add to this block if } // not allocated but how do we coalesce with previous block? CS33 Intro to Computer Systems XXVII 21 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implicit List: Bidirectional Coalescing Boundary tags [Knuth73] replicate size/allocated word at bottom (end) of free blocks allows us to traverse the list backwards, but requires extra space important and general technique! 4 4 4 4 6 6 4 4 Format of allocated and free blocks Header Boundary tag (footer) Size Payload and padding Size a a a = 1: Allocated block a = 0: Free block Size: Total block size Payload: Application data (allocated blocks only) CS33 Intro to Computer Systems XXVII 22 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Constant Time Coalescing Case 1 Case 2 Case 3 Case 4 Block being freed Allocated Allocated Allocated Free Free Allocated Free Free CS33 Intro to Computer Systems XXVII 23 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Constant Time Coalescing (Case 1) m1 1 m1 1 m1 1 n 1 m1 1 n 0 n 1 m2 1 n 0 m2 1 m2 1 m2 1 CS33 Intro to Computer Systems XXVII 24 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Constant Time Coalescing (Case 2) m1 1 m1 1 m1 1 n 1 m1 1 n+m2 0 n 1 m2 0 m2 0 n+m2 0 CS33 Intro to Computer Systems XXVII 25 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Constant Time Coalescing (Case 3) m1 0 n+m1 0 m1 0 n 1 n 1 m2 1 n+m1 0 m2 1 m2 1 m2 1 CS33 Intro to Computer Systems XXVII 26 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Constant Time Coalescing (Case 4) m1 0 n+m1+m2 0 m1 0 n 1 n 1 m2 0 m2 0 n+m1+m2 0 CS33 Intro to Computer Systems XXVII 27 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Implicit Lists: Summary Implementation: very simple Allocate cost: linear time worst case Free cost: constant time worst case even with coalescing Memory usage: will depend on placement policy first-fit, next-fit or best-fit Not used in practice for malloc/free because of linear-time allocation used in many special purpose applications However, the concepts of splitting and boundary tag coalescing are general to all allocators CS33 Intro to Computer Systems XXVII 28 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Keeping Track of Free Blocks Method 1: implicit free list using length links all blocks 5 4 6 2 Method 2: explicit free list among the free blocks using pointers 5 4 6 2 Method 3: segregated free list different free lists for different size classes Method 4: blocks sorted by size can use a balanced tree (e.g. Red-Black tree) with pointers within each free block, and the length used as a key CS33 Intro to Computer Systems XXVII 29 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Explicit Free Lists Allocated (as before) Free Size a Size a Next Payload and padding Prev Size a Size a Maintain list(s) of free blocks, not all blocks the next free block could be anywhere» so we need to store forward/back pointers, not just sizes» luckily we track only free blocks, so we can use payload area still need boundary tags for coalescing CS33 Intro to Computer Systems XXVII 30 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Explicit Free Lists Logically: A B C Physically: blocks can be in any order Forward (next) links A B 4 4 4 4 6 6 4 4 4 4 C Back (prev) links CS33 Intro to Computer Systems XXVII 31 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Allocating From Explicit Free Lists Before conceptual graphic After (with splitting) = malloc( ) CS33 Intro to Computer Systems XXVII 32 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Freeing With Explicit Free Lists Insertion policy: where in the free list do you put a newly freed block? 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 address-ordered policy» Insert freed blocks so that free list blocks are always in address order: addr(prev) < addr(curr) < addr(next)» con: requires search» pro: studies suggest fragmentation is lower than LIFO CS33 Intro to Computer Systems XXVII 33 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Freeing With a LIFO Policy (Case 1) Before free( ) Root Insert the freed block at the root of the list After Root CS33 Intro to Computer Systems XXVII 34 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Freeing With a LIFO Policy (Case 2) Before free( ) conceptual graphic Root Splice out predecessor block, coalesce both memory blocks, and insert the new block at the root of the list After Root CS33 Intro to Computer Systems XXVII 35 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Freeing With a LIFO Policy (Case 3) Before free( ) conceptual graphic Root Splice out successor block, coalesce both memory blocks and insert the new block at the root of the list After Root CS33 Intro to Computer Systems XXVII 36 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Freeing With a LIFO Policy (Case 4) Before free( ) conceptual graphic Root Splice out predecessor and successor blocks, coalesce all 3 memory blocks and insert the new block at the root of the list After Root CS33 Intro to Computer Systems XXVII 37 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Explicit List Summary Comparison to implicit list: allocate is linear time in number of free blocks instead of all blocks» much faster when most of the memory is full slightly more complicated allocate and free since needs to splice blocks in and out of the list some extra space for the links (2 extra words needed for each block) CS33 Intro to Computer Systems XXVII 38 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Quiz 2 Assume that best-fit results in less external fragmentation than first-fit. We are running an application with modest memory demands. Which allocation strategy is likely to result in better performance (in terms of time) for the application: a) best-fit b) first-fit with LIFO insertion c) first-fit with ordered insertion CS33 Intro to Computer Systems XXVII 39 Copyright 2017 Thomas W. Doeppner. All rights reserved.

C vs. Storage Allocation Size Payload and padding 1 Size 0 Next Prev Size 1 Size 0 typedef struct block { long size; long payload[size/8-2]; long end_size; } block_t; typedef struct free_block { long size; struct free_block *next; struct free_block *prev; long filler[size/8-4]; long end_size; } free_block_t; CS33 Intro to Computer Systems XXVII 40 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Overcoming C Think objects a block is an object» opaque to the outside world define accessor functions to get and set its contents typedef struct block { size_t size; size_t payload[0]; } block_t; CS33 Intro to Computer Systems XXVII 41 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Allocated Block size payload[0] payload[1] payload[2] payload[3] 40+1 40+1 actual payload end size CS33 Intro to Computer Systems XXVII 42 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Free Block size payload[0] payload[1] payload[2] payload[3] 40+0 40+0 next free previous free end size In general, end size is at payload[size/8 2] CS33 Intro to Computer Systems XXVII 43 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Overloading Size Size a size_t block_allocated(block_t *b) { return b->size & 1; } size_t block_size(block_t *b) { return b->size & -2; } CS33 Intro to Computer Systems XXVII 44 Copyright 2017 Thomas W. Doeppner. All rights reserved.

End Size Size payload[0] payload[1] a payload[size/8-3] payload[size/8-2] end size size_t *block_end_tag(block_t *b) { return &b->payload[b->size/8-2]; } CS33 Intro to Computer Systems XXVII 45 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Setting the Size void block_setsize(block_t *b, size_t size) { assert(!(size & 7)); // multiple of 8 size = block_allocated(b); // preserve alloc bit b->size = size; *block_end_tag(b) = size; } void block_set_allocated(block_t *b, size_t a) { assert((a == 0) (a == 1)); if (a) { b->size = 1; *block_end_tag(b) = 1; } else { b->size &= -2; *block_end_tag(b) &= -2; } } CS33 Intro to Computer Systems XXVII 46 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Is Previous Adjacent Block Free? Size? Size Payload a size_t block_prev_allocated( block_t *b) { return b->payload[-2] & 1; } Size a CS33 Intro to Computer Systems XXVII 47 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Is Next Adjacent Block Free? Size a Payload Size a Size? block_t *block_next( block_t *b) { return (block_t *) ((char *)b + block_size(b)); } size_t block_next_allocated( block_t *b) { return block_allocated( block_next(b)); } CS33 Intro to Computer Systems XXVII 48 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Adding a Block to the Free List (1) flist_first Size 0 Next Prev Size 0 Next Prev Size 0 Size 0 Next Prev Size 0 Next Size 0 Size 0 Prev Size 0 CS33 Intro to Computer Systems XXVII 49 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Adding a Block to the Free List (2) flist_first Size 0 Next Prev Size 0 Next Prev Size 0 Size 0 Next Prev Size 0 Next Size 0 Size 0 Prev Size 0 CS33 Intro to Computer Systems XXVII 50 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Accessing the Object block_t *block_next_free(block_t *b) { return (block_t *)b->payload[0]; } void block_set_next_free(block_t *b, block_t *next) { b->payload[0] = (size_t)next; } block_t *block_prev_free(block_t *b) { return (block_t *)b->payload[1]; } void block_set_prev_free(block_t *b, block_t *next) { b->payload[1] = (size_t)next; } CS33 Intro to Computer Systems XXVII 51 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Insertion Code void insert_free_block(block_t *fb) { assert(!block_allocated(fb)); if (flist_first!= NULL) { block_t *last = block_prev_free(flist_first); block_set_next_free(fb, flist_first); block_set_prev_free(fb, last); block_set_next_free(last, fb); block_set_prev_free(flist_first, fb); } else { block_set_next_free(fb, fb); block_set_prev_free(fb, fb); } flist_first = fb; } CS33 Intro to Computer Systems XXVII 52 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Performance Won t all the calls to the accessor functions slow things down a lot? yes not just a lot, but tons Why not use macros (#define) instead? the textbook does this it makes the code impossible to debug» gdb shows only the name of the macro, not its body What to do???? CS33 Intro to Computer Systems XXVII 53 Copyright 2017 Thomas W. Doeppner. All rights reserved.

Inline functions static inline size_t block_size( block_t *b) { return b->size & -2; } when debugging ( O0), the code is implemented as a normal function» easy to debug with gdb when optimized ( O1, O2), calls to the function are replaced with the body of the function» no function-call overhead CS33 Intro to Computer Systems XXVII 54 Copyright 2017 Thomas W. Doeppner. All rights reserved.