src/fmap.c file

File mapping implementation.

Classes

struct FileMap
Read-only view of a file.

Functions

static auto map_alignment(void) -> size_t
static void drop_views(FileMap* map)
static void close_file(FileMap* map)
static auto read_at(FileMap* map, void* dst, const size_t n, const uint64_t offset) -> int
static auto map_span(FileMap* map, const uint64_t offset, const size_t actual, void** view_out) -> int
static auto read_span(FileMap* map, const uint64_t offset, const size_t actual, void** view_out) -> int
auto fmap_open(const char* path) -> FileMap*
Open a regular file for reading.
void fmap_close(FileMap* map)
Close a map and drop its current view.
auto fmap_size(const FileMap* map) -> uint64_t
File length in bytes.
auto fmap_data(FileMap* map, void** out, size_t* size) -> int
Map the whole file.
auto fmap_window(FileMap* map, uint64_t offset, size_t length, void** out, size_t* actual) -> int
Map or read [offset, offset + length).

Defines

#define _FILE_OFFSET_BITS
#define _POSIX_C_SOURCE
#define _DEFAULT_SOURCE

Function documentation

FileMap* fmap_open(const char* path)

Open a regular file for reading.

Parameters
path File path. Passed to open or CreateFileA.
Returns New map, or NULL if path is NULL or empty, the path is not a regular file, or the file cannot be opened or sized.

void fmap_close(FileMap* map)

Close a map and drop its current view.

Parameters
map Map returned by fmap_open.

NULL is ignored.

uint64_t fmap_size(const FileMap* map)

File length in bytes.

Parameters
map Map to query.
Returns Length, or 0 if map is NULL. An empty file is a successful open whose size is 0.

int fmap_data(FileMap* map, void** out, size_t* size)

Map the whole file.

Parameters
map Map returned by fmap_open.
out Receives the view. Unchanged on failure.
size Receives the view length. Unchanged on failure.
Returns 0 on success, or -1.

On success *out is the first byte and *size is the length. The bytes are readable. A store is not part of the contract. The pointer is valid until the next fmap_data, fmap_window, or fmap_close.

An empty file, a length that does not fit in size_t, and an OS that refuses the mapping return -1. That refusal does not read the file into a buffer. On failure *out and *size are left unchanged.

int fmap_window(FileMap* map, uint64_t offset, size_t length, void** out, size_t* actual)

Map or read [offset, offset + length).

Parameters
map Map returned by fmap_open.
offset First file byte of the view.
length Requested length. Shortened at the end of the file.
out Receives the view. Unchanged on failure.
actual Receives the byte count. Unchanged on failure.
Returns 0 on success, or -1.

On success *actual is the number of file bytes available there. It is shorter than length only at the end of the file, and *out points at the first of those bytes. The pointer addresses offset even when offset is not aligned to a page. It is valid until the next fmap_data, fmap_window, or fmap_close.

*actual == 0 stores NULL in *out and is success when length is 0 and offset <= fmap_size, or when offset == fmap_size. offset past the end is -1. On failure *out and *actual are left unchanged.

The OS mapping is tried first. If that fails, one buffer of *actual bytes is read. The read is never the size of the whole file.