mmapmodule.c: memory-mapped file objects
mmapmodule.c implements the mmap.mmap type. It maps a file (or anonymous region) into the process address space and exposes it as a Python buffer with file-like read/write/seek methods. The same Python API covers both POSIX mmap(2) and Windows MapViewOfFile.
Map
| Lines | Symbol | Notes |
|---|---|---|
| 1–70 | Includes and platform guards | <sys/mman.h> vs <windows.h>, MAP_ANONYMOUS fallback macro |
| 71–160 | mmap_object struct | Holds data pointer, size, pos, fd copy, access flag, Windows handles |
| 161–300 | mmap_new (POSIX path) | Opens fd, calls mmap(2), stores pointer; ACCESS_READ maps to PROT_READ only |
| 301–440 | mmap_new (Windows path) | CreateFileMapping then MapViewOfFile; ACCESS_COPY uses FILE_MAP_COPY |
| 441–520 | mmap_close | munmap / UnmapViewOfFile; marks object closed; called by __del__ and __exit__ |
| 521–620 | mmap_read | Copies bytes from data + pos into a new bytes object; advances pos |
| 621–700 | mmap_read_byte / mmap_write_byte | Single-byte random-access helpers used by __getitem__/__setitem__ |
| 701–800 | mmap_write | Copies from Python buffer into data + pos; enforces ACCESS_READ guard |
| 801–880 | mmap_seek | Updates pos with SEEK_SET/SEEK_CUR/SEEK_END semantics; clamps to [0, size] |
| 881–980 | mmap_find / mmap_rfind | Naive memmem-style search within the mapped region; returns byte offset or -1 |
| 981–1060 | mmap_resize | POSIX: ftruncate then mremap; Windows: remap via new CreateFileMapping |
| 1061–1160 | Buffer protocol (mmap_buffer_getbuf) | Exports Py_buffer with PyBUF_SIMPLE; increments exports counter to block resize |
| 1161–1240 | ACCESS_* constants | ACCESS_READ=1, ACCESS_WRITE=2, ACCESS_COPY=3, ACCESS_NONE=4 (3.14 addition) |
| 1241–1400 | Type object and module init | PyType_Ready, method table, mmap.error alias for OSError |