Memory Mapped IO

Preliminaries

Binary Trees!

Binary Tree

This is how we store them in arrays!

Serializing Them In Arrays

Our Old Friend: Pointer Arithmetic

typedef struct {
	uint32_t left_child;
	uint32_t right_child;
	uint32_t count;
	float price;
	char word[0];
} BinaryTreeNode;

What is word[0]?

C-Files

Fseek Juggle

Fseek Map

More Juggling

More File IO

Where is the end pointer?

What to do

Read in the FILE*, skip the first 4 bytes and then treat the next few bytes as the meta data for the binary tree node, then after that is the string. The string will be a valid c-string delimited by a null byte.

MMAP

Lol what?

mmap is a really cool function call. it uses memory mapped IO to emulate writing to files. This means that the file is loaded lazily into memory page by page and any writes write back to the original file.

Remember This?

Virtual Memory Pics

Now, the pages can be tied to file pages.

Address Space

MMAP read-many

MMAP Read a lot

MMAP

Mmapping is lazy! Entire files may not be mmapped, you may just use parts of files and assign them to memory pages as they are needed because you don’t need a file until the first time you need it.

Just like the function name says, mmap creates a memory mapping in the kernel and the kernel/CPU is free to do whatever under the hood so long as when a process asks for a memory address it will get the correct bytes and when the write happens, the write eventually goes through to the actual disk.

How Do I Use It?

Read the man page!