University of New Mexico
1
Memory Virtualization: The UNIX Memory API
- Prof. Patrick G. Bridges
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.
University of New Mexico
1
University of New Mexico
2
Main programmer-level API in C. Language-level interface, not the actual OS interface Allocate a memory region on the heap.
▪ size_t size : size of the memory block(in bytes) ▪ size_t is an unsigned integer type.
▪ Success : a void type pointer to the memory block allocated by
▪ Fail : a null pointer
free/calloc/realloc also exist in the same vein
#include <stdlib.h> void* malloc(size_t size)
University of New Mexico
3
Some sample things we’ve all done
What does each of these actually do? Requires understanding how the language API is built on
University of New Mexico
4
malloc malloc library call use brk and/or mmap system calls.
▪ break: The location of the end of the heap in address space
What does this actually do?
#include <unistd.h> int brk(void *addr) void *sbrk(intptr_t increment);
University of New Mexico
5
The greyed-out area of an address
To use it, the OS has to make it
Brk/sbrk sets the location of the
(free) Code (Text) Stack stack heap Address Space Data Heap
0x400000 0xcf2000 0x7fff9ca49000 0x401000 0xd13000 0x7fff9ca28000
University of New Mexico
6
#include <sys/mman.h> void *mmap(void *ptr, size_t length, int port, int flags, int fd, off_t offset)
University of New Mexico
7
mmap lets the program request finer-
More than just moving the program
Note that the address space is now
mmap can also do implicit file I/O –
(free) Code (Text) Stack stack heap Address Space Data Heap
0x400000 0xcf2000 0x7fff9ca49000 0x401000 0xd13000 0x7fff9ca28000
mmap region
University of New Mexico
8
The language runtime (libc) gets memory in chunks from
The language runtime divides these big blocks up to
Important questions
University of New Mexico
9
We generally have to divide big blocks into smaller blocks,
Wasted space from storage allocation is called
How do we allocate storage to handle fragmentation?