Dynamic Memory Allocation. Zhaoguo Wang

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

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

Dynamic Memory Allocation I Nov 5, 2002

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

Dynamic Memory Allocation I

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

Dynamic Memory Allocation

Dynamic Memory Allocation

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

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

Dynamic Memory Management! Goals of this Lecture!

Dynamic Memory Alloca/on: Basic Concepts

Dynamic Memory Allocation

Dynamic Memory Alloca/on: Basic Concepts

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

10.1. CS356 Unit 10. Memory Allocation & Heap Management

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

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

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

Dynamic Memory Management

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

ECE 598 Advanced Operating Systems Lecture 10

Memory management. Johan Montelius KTH

CSE 509: Computer Security

CSCI 237 Sample Final Exam

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Dynamic Memory Allocation. CS 351: Systems Programming Michael Saelee

ANITA S SUPER AWESOME RECITATION SLIDES

Foundations of Computer Systems

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

Dynamic Memory Allocation: Advanced Concepts

CS 153 Design of Operating Systems

Dynamic Memory Allocation

ECE 598 Advanced Operating Systems Lecture 12

Dynamic Memory Allocation

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

o Code, executable, and process o Main memory vs. virtual memory

Operating System Labs. Yuanbin Wu

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

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

Dynamic Memory Management

Dynamic Memory Allocation: Advanced Concepts

Lecture 07 Heap control data. Stephen Checkoway University of Illinois at Chicago

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

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Lectures 13 & 14. memory management

Optimizing Dynamic Memory Management

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

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

Virtual Memory. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

CS61C : Machine Structures

Systems Programming and Computer Architecture ( )

Dynamic Memory: Alignment and Fragmentation

2/9/18. Secure Coding. CYSE 411/AIT681 Secure Software Engineering. Agenda. Dynamic Memory Interface. Dynamic Memory Interface

My malloc: mylloc and mhysa. Johan Montelius HT2016

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

15-213/18-243, Spring 2011 Exam 2

Run-time Environments - 3

CMSC 313 Spring 2010 Exam 3 May 17, 2010

Secure Coding in C and C++ Dynamic Memory Management Lecture 5 Jan 29, 2013

CS 11 C track: lecture 5

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

UW CSE 351, Winter 2013 Final Exam

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

Memory Allocation II. CSE 351 Autumn Instructor: Justin Hsia

Recitation #11 Malloc Lab. November 7th, 2017

Foundations of Computer Systems

CSC 1600 Memory Layout for Unix Processes"

! What is main memory? ! What is static and dynamic allocation? ! What is segmentation? Maria Hybinette, UGA. High Address (0x7fffffff) !

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

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

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

Lecture 4 September Required reading materials for this class

An Experience Like No Other. Stack Discipline Aug. 30, 2006

Secure Coding in C and C++

A program execution is memory safe so long as memory access errors never occur:

Lecture 8 Dynamic Memory Allocation

Internal Fragmentation

Programming Languages

6.172 Performance Engineering of Software Systems Spring Lecture 9. P after. Figure 1: A diagram of the stack (Image by MIT OpenCourseWare.

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

Binghamton University Dynamic Memory Alloca/on

CSE 153 Design of Operating Systems

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

IAGO ATTACKS: WHY THE SYSTEM CALL API IS A BAD UNTRUSTED RPC INTERFACE

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

Segmentation. Multiple Segments. Lecture Notes Week 6

Computer Systems. Virtual Memory. Han, Hwansoo

There are 16 total numbered pages, 7 Questions. You have 2 hours. Budget your time carefully!

Memory Management william stallings, maurizio pizzonia - sistemi operativi

Dynamic Memory Allocation II October 22, 2008

Carnegie Mellon. Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition

Engine Support System. asyrani.com

Secure Software Programming and Vulnerability Analysis

Memory Allocation III CSE 351 Spring (original source unknown)

Transcription:

Dynamic Memory Allocation Zhaoguo Wang

Why dynamic memory allocation? Do not know the size until the program runs (at runtime). #define MAXN 15213 int array[maxn]; int main(void) { int i, n; scanf("%d", &n); if (n > MAXN) app_error("input file too big"); for (i = 0; i < n; i++) scanf("%d", &array[i]); exit(0); }

Why dynamic memory allocation? Do not know the size until the program runs (at runtime). int main(void) { int *array, i, n; } scanf("%d", &n); array = (int *)malloc(n * sizeof(int)); for (i = 0; i < n; i++) scanf("%d", &array[i]); exit(0);

Dynamic allocation on heap Question: Is it possible to dynamically allocate memory on stack? Kernel virtual memory User stack Memory invisible to user code %rsp (stack pointer) Shared libraries Runtime heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file

Dynamic allocation on heap Question: Is it possible to dynamically allocate memory on stack? Answer: Yes void *alloca(size_t size); Allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called alloca() returns to its caller. n bytes User stack... ret array %rsp void func(int n) { array = alloca(n); } subq $n,%rsp

Dynamic allocation on heap Question: Is it possible to dynamically allocate memory on stack? Answer: Yes void *alloca(size_t size); Allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called alloca() returns to its caller. n bytes User stack... ret array %rsp void func(int n) { array = alloca(n); } subq $n,%rsp Not good practice!

Dynamic allocation on heap Question: Is it possible to dynamically allocate memory on stack? Answer: Yes void *alloca(size_t size); Issue I Can not free memory until current function returns à Blowing the stack up n bytes User stack... ret array %rsp Issue II Can not pass the memory out of the scope à Copy memories across different functions Issue III Return value points to the top of the stack à buffer overflow (attack) Not good practice! subq $n,%rsp

Dynamic allocation on heap Question: How to allocate memory on heap? Kernel virtual memory User stack Shared libraries Memory invisible to user code %rsp (stack pointer) Runtime heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file

Dynamic allocation on heap Question: How to allocate memory on heap? OS is responsible for heap. Kernel virtual memory User stack Shared libraries Memory invisible to user code %rsp (stack pointer) Process Process Process Process brk Runtime heap Operating System system call Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Loaded from the executable file Unused

Dynamic allocation on heap Question: How to allocate memory on heap? OS is responsible for heap System calls Kernel virtual memory User stack Memory invisible to user code %rsp (stack pointer) void *sbrk(intptr_t size); sbrk Shared libraries It increases the top of heap by size and returns a pointer to the base of new storage. The size can be a negative number. Runtime heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file

Dynamic allocation on heap Question: How to allocate memory on heap? OS is responsible for heap System calls Kernel virtual memory User stack Memory invisible to user code %rsp (stack pointer) void *sbrk(intptr_t size); sbrk Shared libraries It increases the top of heap by size and returns a pointer to the base of new storage. The size can be a negative number. p = sbrk(1024) //allocate 1KB 1KB Runtime heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file

Dynamic allocation on heap Question: How to allocate memory on heap? OS is responsible for heap System calls Kernel virtual memory User stack Memory invisible to user code %rsp (stack pointer) void *sbrk(intptr_t size); sbrk Shared libraries It increases the top of heap by size and returns a pointer to the base of new storage. The size can be a negative number. p = sbrk(1024) //allocate 1KB sbrk(-1024) //free p 1KB Runtime heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file

Dynamic allocation on heap Question: How to allocate memory on heap? OS is responsible for heap System calls Kernel virtual memory User stack Memory invisible to user code %rsp (stack pointer) void *sbrk(intptr_t size); sbrk Shared libraries Issue I can only free the memory on the top of heap p1 = sbrk(1024) //allocate 1KB p2 = sbrk(2048) //allocate 4KB How to free p1? 1KB 4KB Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file

Dynamic allocation on heap Question: How to allocate memory on heap? OS is responsible for heap System calls Kernel virtual memory User stack Memory invisible to user code %rsp (stack pointer) void *sbrk(intptr_t size); sbrk Shared libraries Issue I can only free the memory on the top of heap Issue II system call has high performance cost > 10X Runtime heap Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused brk Loaded from the executable file

Dynamic allocation on heap Question: How to effciently allocate memory on heap? Basic idea request a large of memory region from heap once, then manage this memory region by itself. à allocator in the library sbrk Kernel virtual memory User stack Shared libraries Memory invisible to user code %rsp (stack pointer) Memory management in the user space. malloc /free Runtime heap brk Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Loaded from the executable file Unused

Dynamic allocation on heap Question: How to effciently allocate memory on heap? Basic idea request a large of memory region from heap once, then manage this memory region by itself. à allocator in the library sbrk Kernel virtual memory User stack Shared libraries Memory invisible to user code %rsp (stack pointer) Process Process Process Process malloc /free Runtime heap brk User library Operating System system call /func call Read/write segment (.data,.bss) Read-only segment (.init,.text,.rodata) Unused Loaded from the executable file

Memory Allocator Assumption in this lecture At the beginning, the allocator requests enough memory with sbrk Goal Highly utilize the acquired memory with high throughput high throughput how many mallocs / frees can be done per second high utilization fraction of allocated size / total heap size

Memory Allocator Assumed behavior of applications Can issue arbitrary sequence of malloc (for arbitary sizes)/free The argument of free must be the return value of a previous malloc No double free Requirements of allocator Must allocate from free memory (correctness) Once allocated, cannot be moved

Questions 1. How to keep track which bytes are free and which are not? 2. Which of the free chunks to allocate? 3. free only supplies a pointer, how to know its corresponding chunk size?

Track allocated/free chunks Keep the metadata (addr, size) in a linked list Allocated chunks Free chunks p 1B next addr size null pp 1KB null p pp 1B payload a1kb payload address p = malloc(1) pp = malloc(1024) Free chunks What is the size of each linked list entry? Memory buffer on heap

Track allocated/free chunks Keep the metadata (addr, size) in a linked list Allocated chunks Free chunks p 1B next addr size null pp 1KB null p pp 1B payload a1kb payload address p = malloc(1) pp = malloc(1024) Free chunks What is the size of each linked list entry? >= 20B Can we further reduce the size of metadata? Memory buffer on heap

Implicit list Embed the metadata in the chunks (blocks) - Each block has a one-word (8 bytes) header - Block is double-word (16 bytes) alignment à Size is multiple of 16 Payload header (8 bytes) 0 allocated: header & 0x1 size: header & ~(0x1) Padding (optional)

Implicit list Embed the metadata in the chunks (blocks) - Each block has a one-word (8 bytes) header - Block is double-word (16 bytes) alignment à Size is multiple of 16 p = malloc(1024) p 0x411 1KB payload 8B padding

Implicit list Embed the metadata in the chunks (blocks) - Each block has a one-word (8 bytes) header - Block is double-word (16 bytes) alignment à Size is multiple of 16 p = malloc(1)??? p??????

Implicit list Embed the metadata in the chunks (blocks) - Each block has a one-word (8 bytes) header - Block is double-word (16 bytes) alignment à Size is multiple of 16 p = malloc(1) p 0x11 1B payload 7B padding

Exercises Alignment Request Block size Header (hex) 8 bytes malloc(5) 4 bytes malloc(13) 16 bytes malloc(20) 8 bytes malloc(3)

Exercises Alignment Request Block size Header (hex) 8 bytes malloc(5) 16 0x11 4 bytes malloc(13) 24 0x19 16 bytes malloc(20) 32 0x21 8 bytes malloc(3) 16 0x11

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) start heap header allocated payload padding free block address

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) start heap address header allocated payload padding free block

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(8) start cur heap address header allocated payload padding free block First fit Search list from beginning, choose first free block that fits

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(8) start cur heap address header allocated payload padding free block First fit Search list from beginning, choose first free block that fits Issue: cause splinters/fragments at beginning of the buffer

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(8) start cur heap address header allocated payload padding free block Best fit choose the free block with the closest size that fits

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(8) start cur heap address header allocated payload padding free block Best fit choose the free block with the closest size that fits

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(8) start cur heap address header allocated payload padding free block Best fit Search list from beginning, choose first free block that fits Best fit keeps fragments small, but typically run slower than first fit.

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(8) p8 = malloc(56) start cur heap address header allocated payload padding free block Next fit like first-fit, but search list from the location where previous search left off.

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(8) p8 = malloc(56) start cur heap address header allocated payload padding free block Next fit like first-fit, but search list from from the location where previous search left off. Next fit runs faster than first fit, but fragmentation is worse.

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(24) p8 = malloc(24) start heap address header allocated payload padding free block

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(24) p8 = malloc(24) start cur heap address header allocated payload padding free block

Placing allocated blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(24) p8 = malloc(24) start cur heap address header allocated payload padding free block

p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(24) p8 = malloc(24) Splitting free block start cur heap address header allocated payload padding free block 0x21 24B Payload??????

p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) p7 = malloc(24) p8 = malloc(24) Splitting free block start cur heap address header allocated payload padding free block 0x21 24B Payload 0x20 24B Payload

Coalescing free blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) free(p5) start cur heap address header allocated payload padding free block

Coalescing free blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) free(p5) start cur heap address header allocated payload padding free block 0x20 24B Payload 0x40 56B Payload

Coalescing free blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) free(p5) start cur Coalescing with next block heap address header allocated payload padding free block 0x60 88B Payload

Coalescing free blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) free(p5) start cur Coalescing with next block How to coalesce with previous block? heap address header allocated payload padding free block 0x60 88B Payload

Coalescing free blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) free(p5) start cur Coalescing with next block How to coalesce with previous block? -- search from start? heap address header allocated payload padding free block 0x60 88B Payload

Coalescing free blocks Embed the metadata in the chunks (blocks) - Each block has a one-word (8 bytes) header and (8 bytes) footer - Block is double-word (16 bytes) alignment à Size is multiple of 16 0 header (8 bytes) p 0x411 header Payload 1KB payload Padding (optional) 0 footer (8 bytes) 0x411 p = malloc(1024) footer

Coalescing free blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) free(p5) start cur (p5) heap header allocated payload / padding footer free block 0x30 24B Payload 8B Padding 0x30 0x50 56B Payload 8B Padding 0x50

Coalescing free blocks p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) free(p2) free(p4) free(p6) free(p5) start cur (p5) heap header allocated payload / padding footer free block 0x80 0x80

Coalescing free blocks start p1 = malloc(8) p2 = malloc(24) p3 = malloc(56) p4 = malloc(8) p5 = malloc(24) p6 = malloc(56) cur (p4) free(p2) free(p4) free(p6) free(p5) heap header allocated payload / padding footer free block 0x100 0x100

Exercises Alignment Request Block size Header (hex) 8 bytes malloc(5) 4 bytes malloc(13) 16 bytes malloc(20) 8 bytes malloc(3) Each block has both header and footer

Exercises Alignment Request Block size Header (hex) 8 bytes malloc(5) 24 0x19 4 bytes malloc(13) 32 0x19 16 bytes malloc(20) 32 0x21 8 bytes malloc(3) 24 0x19 Each block has both header and footer

Explicit free lists Problems of implicit free list Block allocation time is linear in the total number of heap blocks Basic idea segregated list Maintain multiple free list, each list holds blocks that are roughly the same size

Segregated list Next Prev 0 Free lists {16} {32-48} {64 128} Determine size class First fit Payload fit? No Search in next free list Padding (optional) 0 Yes Remove and split free block Block layout Insert the fragment

Segregated list Next Prev 0 Free lists {16} {32-48} {64 128} Determine size class First fit Payload fit? No Search in next free list Padding (optional) 0 Yes Remove and split free block Block layout Do we need an allocated lists? Insert the fragment

Segregated list Next Prev 0 Free lists {16} {32-48} {64 128} Determine size class First fit Payload fit? No Search in next free list Padding (optional) 0 Yes Remove and split free block Block layout Do we need an allocated lists? Insert the fragment No, we are able to calculate the start of the block with p passed by free()

Segregated list Next Prev 0 Free lists {16} {32-48} {64 128} Determine size class First fit Payload fit? No Search in next free list Padding (optional) 0 Yes Remove and split free block Block layout Can we get rid of Next and Prev? Insert the fragment

Segregated list Addr returned to user Payloads Next Prev 0 Free lists {16} {32-48} {64 128} Determine size class First fit fit? No Search in next free list Padding (optional) 0 Yes Remove and split free block Block layout Can we get rid of Next and Prev? Allocated blocks do not need Next and Prev Insert the fragment

Segregated list Payloads Next Prev 0 Free lists {16} {32-48} {64 128} Determine size class First fit fit? No Search in next free list Padding (optional) 0 Yes Remove and split free block Block layout Insert the fragment With this design, what is the minimal size for a free block?

Buddy System Adopted by Linux kernel and jemalloc This lecture A simplified binary buddy allocator

Binary buddy system 64K ( 0000 0000 0000 0000) 2

Binary buddy system Split - Split exactly in half Buddy 32K ( 1000 0000 0000 0000) 2 Buddy 32K ( 0000 0000 0000 0000) 2

Binary buddy system Split - Split exactly in half - Each half is the buddy of the other Address - Block of size 2 n begin at memory addresses where the n least significant bits are zero - When a block of size 2 n+1 is split into two blocks of size 2 n, the addresses of these two blocks will differ in exactly one bit, bit n. If a block of size 2 n begins at address addr, what is its buddy address and size? ( 1000 0000 0000 0000) 2 ( 0100 0000 0000 0000) 2 ( 0000 0000 0000 0000) 2 Buddy Buddy 32K 16K 16K

Binary buddy system Split - Split exactly in half - Each half is the buddy of the other Address - Block of size 2 n begin at memory addresses where the n least significant bits are zero - When a block of size 2 n+1 is split into two blocks of size 2 n, the addresses of these two blocks will differ in exactly one bit, bit n. If a block of size 2 n begins at address addr, what is its buddy address and size? addr of buddy = addr ^ (n<<1) ( 1000 0000 0000 0000) 2 ( 0100 0000 0000 0000) 2 ( 0000 0000 0000 0000) 2 Buddy Buddy 32K 16K 16K

Binary buddy system Split - Split exactly in half - Each half is the buddy of the other Address - Block of size 2 n begin at memory addresses where the n least significant bits are zero - When a block of size 2 n+1 is split into two blocks of size 2 n, the addresses of these two blocks will differ in exactly one bit, bit n. Combine - We assume only combine with its buddy block in our lecture ( 1000 0000 0000 0000) 2 ( 0100 0000 0000 0000) 2 ( 0000 0000 0000 0000) 2 Buddy Buddy 32K 16K 16K

Buddy system (100) 2 Observation Free lists 2 4 2 5 2 6 Determine size class First fit fit? No Search in next free list (110) 2 Yes recursively split free block in half until fit Insert the fragment Each list has the same size of blocks which is a power of 2.

Buddy system p = malloc(1) Step 1. search in 2 6 list Free lists 2 4 2 5 2 6 (x000000) 2 Determine size class First fit Step 2. recursive split fit? No Search in next free list Yes recursively split free block in half until fit Insert the fragment Each list has the same size of blocks which is a power of 2.

Buddy system p = malloc(1) Step 1. search in 2 6 list Free lists 2 4 2 5 2 6 (x000000) 2 (x100000) 2 Determine size class First fit Step 2. recursive split fit? Yes recursively split free block in half until fit No Search in next free list Insert the fragment

Buddy system p = malloc(1) Step 1. search in 2 6 list Free lists 2 4 2 5 2 6 (x000000) 2 (x010000) 2 (x100000) 2 Determine size class First fit Step 2. recursive split fit? Yes recursively split free block in half until fit No Search in next free list Insert the fragment

Buddy system p = malloc(1) Step 1. search in 2 6 list Free lists 2 4 2 5 2 6 (x000000) 2 (x010000) 2 (x100000) 2 Determine size class First fit Step 2. recursive split fit? Yes recursively split free block in half until fit No Search in next free list Insert the fragment

Buddy system p = malloc(1) Step 1. search in 2 6 list Step 2. recursive split Step 3. insert free blocks into the list Free lists 2 4 2 5 2 6 (x010000) 2 (x100000) 2 Step 4. return, p is (x000000) 2 Determine size class First fit fit? Yes recursively split free block in half until fit No Search in next free list Insert the fragment

Buddy system p = malloc(1) (x010000) 2 Free lists 2 4 2 5 2 6 (x100000) 2 Determine size class First fit free(p) (x010000) 2 fit? No Search in next free list Yes recursively split free block in half until fit Insert the fragment

Buddy system p = malloc(1) free(p) (x010000) 2 (x010000) 2 Free lists 2 4 2 5 2 6 (x100000) 2 Determine size class First fit p s address is (x000000) 2, size is 16B (no footer in this example) à p s buddy is 16 B block begins at x000000 ^ (1<<4) which is (x010000) 2 fit? Yes recursively split free block in half until fit Insert the fragment No Search in next free list

Buddy system p = malloc(1) free(p) Free lists 2 4 2 5 2 6 (x000000) 2 (x100000) 2 p s address is (x000000) 2, size is 32B (no footer in this example) à p s buddy is 32 B block begins at x000000 ^ (1<<5) which is (x100000) 2 Determine size class First fit fit? Yes recursively split free block in half until fit Insert the fragment No Search in next free list

Buddy system p = malloc(1) free(p) Free lists 2 4 2 5 (x000000) 2 2 6 Determine size class First fit p s address is (x000000) 2, size is 32B à p s next block address is p + 32B which is (x100000) 2 fit? Yes recursively split free block in half until fit Insert the fragment No Search in next free list