Types of Memory Mapping: mmap, Memory-Mapped I/O and Cache Mapping Explained

On this page
"Memory mapping" means linking something to addresses in memory so it can be accessed like ordinary memory. In operating systems it usually means mapping files or memory regions into a process's virtual address space (the mmap system call on Linux/macOS, CreateFileMapping/MapViewOfFile on Windows). The main types are file-backed vs anonymous and shared vs private mappings. In hardware, memory-mapped I/O maps device registers into the address space, and in computer architecture courses cache mapping describes how main-memory blocks are placed in cache: direct, fully associative and set-associative.
All types at a glance
| Type | Area | What it maps | Typical use |
|---|---|---|---|
| File-backed mapping | OS / programs | A file on disk into memory | Loading programs and libraries, databases, large files |
| Anonymous mapping | OS / programs | Zero-filled memory, no file | Large allocations (malloc), heaps, stacks |
| Shared mapping | OS / programs | Same pages visible to several processes | Inter-process communication, shared caches |
| Private (copy-on-write) mapping | OS / programs | Process's own copy; changes not written back | Loading executables, fork() |
| Memory-mapped I/O (MMIO) | Hardware | Device registers/buffers into the physical address space | GPUs, network cards, PCIe devices |
| Port-mapped I/O | Hardware (x86) | Devices in a separate I/O address space (IN/OUT instructions) | Legacy devices |
| Direct-mapped cache | CPU cache | Each memory block to exactly one cache line | Simple, fast caches |
| Fully associative cache | CPU cache | Any block to any line | TLBs, small caches |
| Set-associative cache | CPU cache | Each block to one set, any line in that set | Most modern L1/L2/L3 caches |
Memory-mapped files and mmap
With mmap, the operating system maps a file (or anonymous memory) into a process's virtual memory. The program reads and writes the file using pointers instead of read()/write() calls; pages are loaded on demand by the page-fault handler.
| Shared (MAP_SHARED) | Private (MAP_PRIVATE) | |
|---|---|---|
| File-backed | Changes written back to the file and seen by other processes | Copy-on-write: changes stay in this process only |
| Anonymous | Shared memory between related processes (e.g. after fork) | Normal private memory (heap) |
Simple example (C, Linux)
int fd = open("data.bin", O_RDONLY);
struct stat st; fstat(fd, &st);
char *p = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
/* read the file through p[0] ... p[st.st_size-1] */
munmap(p, st.st_size); close(fd);
Pros and cons
- Pros: fewer system calls and copies, lazy loading, easy random access to huge files, simple shared memory.
- Cons: page faults can be slow on random access to cold data; errors (like a truncated file) show up as signals (SIGBUS); limited by virtual address space on 32-bit systems.
Memory-mapped I/O vs port-mapped I/O
| Memory-mapped I/O | Port-mapped I/O | |
|---|---|---|
| Address space | Shared with memory | Separate I/O space |
| Instructions | Normal load/store | Special IN/OUT (x86) |
| Used by | Almost all modern devices (PCIe, GPUs, ARM SoCs) | Legacy x86 devices |
| Caveat | Regions must be uncached/volatile | Limited address range |
Cache mapping techniques
When the CPU loads a block from main memory into cache, a mapping rule decides where it can go. The address is split into tag, index (set or line) and offset bits.
| Direct-mapped | Fully associative | k-way set-associative | |
|---|---|---|---|
| Where a block can go | One fixed line | Any line | Any of k lines in one set |
| Formula | line = block mod (number of lines) | No index; compare all tags | set = block mod (number of sets) |
| Hardware cost | Lowest | Highest (many comparators) | Medium |
| Conflict misses | Most | Fewest | Few |
| Replacement policy | Not needed | Needed (LRU, random) | Needed within the set |
| Used in | Simple/embedded caches | TLBs, small buffers | Most CPU caches (8- to 16-way common) |
Worked example
A direct-mapped cache with 8 lines: memory block 21 goes to line 21 mod 8 = 5. In a 2-way set-associative cache with 4 sets, block 21 goes to set 21 mod 4 = 1, in either of its 2 lines.
Related concepts
- Virtual memory and paging: the page table maps virtual pages to physical frames; see memory addressing.
- Read and write operations: read vs write in memory.
FAQ
What is memory mapping in simple words?
Making a file, device or region appear as part of memory so a program can access it with normal memory reads and writes.
Which cache mapping is best?
Set-associative is the usual balance between speed, cost and hit rate.
Is mmap faster than read()?
Often for large files and random access; for simple sequential reads, buffered read() can be just as fast.
What is a memory map in embedded systems?
A chart of which address ranges hold flash, RAM and peripheral registers on a microcontroller.