Memory Virtualization: The UNIX Memory API Prof. Patrick G. Bridges - - PowerPoint PPT Presentation

memory virtualization the unix memory api
SMART_READER_LITE
LIVE PREVIEW

Memory Virtualization: The UNIX Memory API Prof. Patrick G. Bridges - - PowerPoint PPT Presentation

University of New Mexico Memory Virtualization: The UNIX Memory API Prof. Patrick G. Bridges 1 University of New Mexico Memory API: malloc() #include <stdlib.h> void* malloc(size_t size) Main programmer-level API in C.


slide-1
SLIDE 1

University of New Mexico

1

Memory Virtualization: The UNIX Memory API

  • Prof. Patrick G. Bridges
slide-2
SLIDE 2

University of New Mexico

2

Memory API: malloc()

 Main programmer-level API in C.  Language-level interface, not the actual OS interface  Allocate a memory region on the heap.

▪ Argument

▪ size_t size : size of the memory block(in bytes) ▪ size_t is an unsigned integer type.

▪ Return

▪ Success : a void type pointer to the memory block allocated by

malloc

▪ Fail : a null pointer

 free/calloc/realloc also exist in the same vein

#include <stdlib.h> void* malloc(size_t size)

slide-3
SLIDE 3

University of New Mexico

3

Lots of ways to go wrong with memory

 Some sample things we’ve all done

▪ Not copying enough data (e.g. terminating nulls) ▪ Not allocating space for the data copied ▪ Not freeing data (memory leaks) ▪ Accessing data after its freed ▪ Freeing an area multiple times

 What does each of these actually do?  Requires understanding how the language API is built on

top of the OS memory API

slide-4
SLIDE 4

University of New Mexico

4

Memory Management System Calls

 malloc  malloc library call use brk and/or mmap system calls.

▪ brk is called to expand the program’s break.

▪ break: The location of the end of the heap in address space

▪ sbrk is an additional call similar with brk. ▪ Programmers should never directly call either brk or sbrk.

 What does this actually do?

#include <unistd.h> int brk(void *addr) void *sbrk(intptr_t increment);

slide-5
SLIDE 5

University of New Mexico

5

How do brk and sbrk work?

 The greyed-out area of an address

space is not actually allocated.

 To use it, the OS has to make it

available.

 Brk/sbrk sets the location of the

boundary between allocated heap memory and unallocated memory!

(free) Code (Text) Stack stack heap Address Space Data Heap

0x400000 0xcf2000 0x7fff9ca49000 0x401000 0xd13000 0x7fff9ca28000

slide-6
SLIDE 6

University of New Mexico

6

System Calls(Cont.)

▪ mmap system call can create an anonymous memory region.

#include <sys/mman.h> void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset)

slide-7
SLIDE 7

University of New Mexico

7

What about mmap?

 mmap lets the program request finer-

grain allocation of parts of its address space

 More than just moving the program

break

 Note that the address space is now

disjoint!

 mmap can also do implicit file I/O –

that’s a later topic…

(free) Code (Text) Stack stack heap Address Space Data Heap

0x400000 0xcf2000 0x7fff9ca49000 0x401000 0xd13000 0x7fff9ca28000

mmap region

slide-8
SLIDE 8

University of New Mexico

8

How are malloc/new implemented?

 The language runtime (libc) gets memory in chunks from

the operating system

▪ Using mmap or sbrk – 4k or more at a time ▪ (Why not in smaller pieces?)

 The language runtime divides these big blocks up to

satisfy malloc/new requests

▪ Basic data structure is a “free list”, a linked list of free chunks of

memory

▪ Malloc/new searches list to find a chunk to satisfy an allocation

request

▪ Free returns things to this list

 Important questions

▪ Where are the pointers for the linked lists stored? ▪ What block do you use to satisfy an allocation request?

slide-9
SLIDE 9

University of New Mexico

9

Fragmentation: Storage Virtualization Enemy #1

 We generally have to divide big blocks into smaller blocks,

and there’s rarely an exact fit

▪ Variable-size allocations can result in with small, hard-to-allocate

blocks

▪ Sometimes have to allocate space bigger than we want to

 Wasted space from storage allocation is called

fragmentation

▪ Internal fragmentation – wasted space that’s allocated but not

  • used. (You want 5 bytes but we have to allocate 8)

▪ External fragmentation – small bits we can’t allocate

 How do we allocate storage to handle fragmentation?