[PDF] [PDF] Dynamic Memory Allocation Motivation for Dynamic Memory Stack

Storage is used inefficiently Recursive Complex data structures: lists and trees Heap Stack Organization Definition: Memory is freed in opposite order from allocation alloc(A); alloc(B); End up with small chunks of free space Where to 



Previous PDF Next PDF





[PDF] Understanding the heap by breaking it - Black Hat

allocated from this top chunk, but as memory use progresses chunks are usually obtained of the heap and provides a pointer to the arena data structure The next coalesced and stored in a linked list in a bin to be retrieved for an allocation later project, organization or person who has made use of the Heimdal asn 1



[PDF] The heap

Functions can use it to share values, using pointers to data stored on the heap The operation to allocate a chunk of memory on the heap is malloc #include 



[PDF] Comprehensively and Efficiently Protecting the Heap ∗

Heap chunk structure used in GNU C [11] size numeric field are used to store the PIU field and some additional information Freed chunks are organized into bins of equal or similar sizes using a doubly-linked list structure



[PDF] Heap attacks - CSE 127 Computer Security

Data is allocated and deallocated dynamically from the heap by the program – Dynamic memory is Program data stored on the heap – Heap metadata (i e , for organizing the heap itself) ▫ What if the attacker can data In Use Chunk ▫ Malloc returns a pointer to the start of the data block ▫ Free can release the 



[PDF] Run-time Storage Allocation - NPTEL

at run time ❑ For example: nodes of dynamic data structures such as linked list or as bins of free memory chunks (more on this later) ❑To begin with the whole heap is a single chunk of Free space organized into bins according to their



[PDF] A Malloc Tutorial - wiki-prog - Epita

16 fév 2009 · data are stored, some space for constant and global variables and an know that memory is mapped by pages: physical memory and virtual memory is organize in This malloc waste a lot of space in obsolete memory chunks Figure 3 presents an example of heap organisation with meta-data in front of 



[PDF] DieHarder: Securing the Heap - USENIX

to overwrite data in an adjacent vulnerable object like a function effectively identical to running with the Linux allocator PHKmalloc) employ a different heap organization (see chunk at the head of the freelist and storing a reference



[PDF] HW 2: Malloc - EECS: www-insteecsberkeleyedu

23 juil 2015 · Also note that any heap-allocated data structures you use in your code, a stack where local and volatile data are stored, some space for memory is mapped by pages: physical memory and virtual memory is organized in pages of keep the last visited chunk, so the malloc function can easily extends 



[PDF] Dynamic Memory Allocation Motivation for Dynamic Memory Stack

Storage is used inefficiently Recursive Complex data structures: lists and trees Heap Stack Organization Definition: Memory is freed in opposite order from allocation alloc(A); alloc(B); End up with small chunks of free space Where to 

[PDF] how decision making evolves at each stage of administrative system

[PDF] how did 100 pure new zealand start

[PDF] how did algeria and vietnam gain independence from france

[PDF] how did algeria gain independence

[PDF] how did alvin ailey die

[PDF] how did china and india slow their population growth

[PDF] how did queen elizabeth 1 change the world

[PDF] how did slaves acquire their last names

[PDF] how did the 14th amendment change the relationship between the states and the bill of rights

[PDF] how did the 14th amendment's due process clause change the constitution

[PDF] how did the catholic church change as a result of the protestant reformation

[PDF] how did the catholic church respond to the protestant reformation quizlet

[PDF] how did the city of jacksonville attempt to curb the spreading of the spanish flu?

[PDF] how did the economy recover after 2008

[PDF] how did the french and indian war influence the development of american government

1

Dynamic Memory Allocation

Questions answered in this lecture:

When is a stack appropriate? When is a heap?

What are best-fit, first-fit, worst-fit, and buddy allocation algorithms? How can memory be freed (using reference counts or garbage collection)?

UNIVERSITY of WISCONSIN-MADISON

Computer Sciences Department

CS 537

Introduction to Operating Systems

Andrea C. Arpaci-Dusseau

Remzi H. Arpaci-Dusseau

Motivation for Dynamic Memory

Why do processes need dynamic allocation of memory? •Do not know amount of memory needed at compile time •Must be pessimistic when allocate memory statically -Allocate enough for worst possible case -Storage is used inefficiently

Recursive procedures

•Do not know how many times procedure will be nested

Complex data structures: lists and trees

•struct my_t *p=(struct my_t *)malloc(sizeof(struct my_t));

Two types of dynamic allocation

•Stack •Heap

Stack Organization

Definition: Memory is freed in opposite order from allocation alloc(A); alloc(B); alloc(C); free(C); alloc(D); free(D); free(B); free(A); Implementation: Pointer separates allocated and freed space •Allocate: Increment pointer •Free: Decrement pointer

Stack Discussion

OS uses stack for procedure call frames (local variables) main () { int A = 0; foo (A); printf("A: %d\n", A); void foo (int Z) { int A = 2;

Z = 5;

printf("A: %d Z: %d\n", A, Z);

Advantages

•Keeps all free space contiguous •Simple to implement •Efficient at run time

Disadvantages

•Not appropriate for all data structures 2

Heap Organization

Advantage

•Works for all data structures

Disadvantages

•Allocation can be slow •End up with small chunks of free space

Where to allocate 16 bytes? 12 bytes? 24 bytes??

Definition: Allocate from any random location

•Memory consists of allocated areas and free areas (holes) •Order of allocation and free is unpredictable Free Free Alloc Alloc

16 bytes

24 bytes

12bytes

16 bytes

A B

Fragmentation

Definition: Free memory that is too small to be usefully allocated •External: Visible to allocator •Internal: Visible to requester (e.g., if must allocate at some granularity)

Goal: Minimize fragmentation

•Few holes, each hole is large •Free space is contiguous Stack •All free space is contiguous •No fragmentation Heap •How to allocate to minimize fragmentation?

Heap Implementation

Data structure: free list

•Linked list of free blocks, tracks memory not in use •Header in each block -size of block -ptr to next block in list void *Allocate(x bytes) •Choose block large enough for request (>= x bytes) •Keep remainder of free block on free list •Update list pointers and size variable •Return pointer to allocated memory

Free(ptr)

•Add block back to free list •Merge adjacent blocks in free list, update ptrs and size variables

Heap Allocation Policies

Best fit

•Search entire list for each allocation •Choose free block that most closely matches size of request •Optimization: Stop searching if see exact match

First fit

•Allocate first block that is large enough

Rotating first fit (or "Next fit"):

•Variant of first fit, remember place in list •Start with next free block each time

Worst fit

•Allocate largest block to request (most leftover space) 3

Heap Allocation Examples

Scenario: Two free blocks of size 20 and 15 bytes

Allocation stream: 10, 20

•Best •First •Worst

Allocation stream: 8, 12, 12

•Best •First •Worst

Buddy Allocation

Fast, simple allocation for blocks of 2

n bytes [Knuth68] void *Allocate (k bytes) •Raise allocation request to nearest s = 2 n •Search free list for appropriate size -Represent free list with bitmap -Recursively divide larger free blocks until find block of size s -"Buddy" block remains free •Mark corresponding bits as allocated

Free(ptr)

•Mark bits as free •Recursively coalesce block with buddy, if buddy is free -May coalesce lazily (later, in background) to avoid overhead

Buddy Allocation Example

Scenario: 1MB of free memory

Request stream:

•Allocate 70KB, 35KB, 80KB •Free 35KB, 80KB, 70KB

Comparison of Allocation

Strategies

No optimal algorithm

•Fragmentation highly dependent on workload

Best fit

•Tends to leave some very large holes and some very small holes - Can't use very small holes easily

First fit

•Tends to leave "average" sized holes •Advantage: Faster than best fit •Next fit used often in practice

Buddy allocation

•Minimizes external fragmentation •Disadvantage: Internal fragmentation when not 2^n request 4

Memory Allocation in Practice

How is malloc() implemented?

Data structure: Free lists

•Header for each element of free list -pointer to next free block -size of block -magic number •Where is header stored? •What if remainder of block is smaller than header?

Two free lists

•One organized by size -Separate list for each popular, small size (e.g., 1 KB) -Allocation is fast, no external fragmentation •Second is sorted by address -Use next fit to search appropriately -Free blocks shuffled between two lists

Freeing Memory

C: Expect programmer to explicitly call free(ptr)

Two possible problems

•Dangling pointers: Recycle storage that is still in-use -Have two pointers to same memory, free one and use second foo_t *a = malloc(sizeof(foo_t)); foo_t *b = a; b->bar = 50; free(a); foo_t *c = malloc(sizeof(foo_t)); c->bar = 20; printf("b->bar: %d\n", b->bar); •Memory leaks: Forget to free storage -If lose pointer, can never free associated memory -Okay in short jobs, not okay for OS or long-running servers foo_t *a = malloc(sizeof(foo_t)); foo_t *b = malloc(sizeof(foo_t)); b = a;

Reference Counts

Idea: Reference counts

•Track number of references to each memory chunk -Increment count when new pointer references it -Decrement count when pointer no longer references it •When reference count = 0, free memory

Examples

•Hard links in Unix echo Hi > file ln file new rm file cat new •Smalltalk

Disadvantages

•Circular data structures --> Memory leaks

Garbage Collection

Observation: To use data, must have pointer to it

•Without pointer, cannot access (or find) data •Memory is free implicitly when no longer referenced -Programmer does not call free()

Approach

•When system needs more memory, free unreachable chunks

Requirements

•Must be able to find all objects (referenced and not) •Must be able to find all pointers to objects -Strongly typed language -Compiler cooperates by marking data type in memory •Size of each object •Which fields are pointers 5

Mark and Sweep

Garbage Collection

Pass 1: Mark all reachable data

•Start with all statically allocated and local (stack) variables •Mark each data object as reachable •Recursively mark all data objects can reach through pointers

Pass 2: Sweep through all memory

•Examine each data object •Free those objects not marked

Advantages

•Works with circular data structures •Simple for application programmers

Disadvantages

•Often CPU-intensive (poor caching behavior too) •Difficult to implement such that can execute job during g.c. •Requires language support (Java, LISP)quotesdbs_dbs17.pdfusesText_23