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

types of memory mapping
On this page
  1. All types at a glance
  2. Memory-mapped files and mmap
  3. Memory-mapped I/O vs port-mapped I/O
  4. Cache mapping techniques
  5. Related concepts
  6. FAQ

"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

TypeAreaWhat it mapsTypical use
File-backed mappingOS / programsA file on disk into memoryLoading programs and libraries, databases, large files
Anonymous mappingOS / programsZero-filled memory, no fileLarge allocations (malloc), heaps, stacks
Shared mappingOS / programsSame pages visible to several processesInter-process communication, shared caches
Private (copy-on-write) mappingOS / programsProcess's own copy; changes not written backLoading executables, fork()
Memory-mapped I/O (MMIO)HardwareDevice registers/buffers into the physical address spaceGPUs, network cards, PCIe devices
Port-mapped I/OHardware (x86)Devices in a separate I/O address space (IN/OUT instructions)Legacy devices
Direct-mapped cacheCPU cacheEach memory block to exactly one cache lineSimple, fast caches
Fully associative cacheCPU cacheAny block to any lineTLBs, small caches
Set-associative cacheCPU cacheEach block to one set, any line in that setMost 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-backedChanges written back to the file and seen by other processesCopy-on-write: changes stay in this process only
AnonymousShared 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/OPort-mapped I/O
Address spaceShared with memorySeparate I/O space
InstructionsNormal load/storeSpecial IN/OUT (x86)
Used byAlmost all modern devices (PCIe, GPUs, ARM SoCs)Legacy x86 devices
CaveatRegions must be uncached/volatileLimited 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-mappedFully associativek-way set-associative
Where a block can goOne fixed lineAny lineAny of k lines in one set
Formulaline = block mod (number of lines)No index; compare all tagsset = block mod (number of sets)
Hardware costLowestHighest (many comparators)Medium
Conflict missesMostFewestFew
Replacement policyNot neededNeeded (LRU, random)Needed within the set
Used inSimple/embedded cachesTLBs, small buffersMost 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.

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.

Related guides