Qu

mmap_open

Builtin index · File I/O

result = mmap_open(path)

Input: path · Output: result

What it is

Memory-maps path (string, filesystem path) read-only.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = mmap_open(path)
path stringnot documented yet
result —
Returns an opaque mmap handle. Errors if the file doesn't exist or is empty (mapping a zero-length file is unsupported).

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

# The file is written here first. Mapping one the repository does not

# carry would make this block fail, and a block that fails is dropped

# from the page while the build still reports success.

write_array("big_dataset.bin", 0 to 511, type = "u8")

m = mmap_open("big_dataset.bin")
n = mmap_len(m)
header = mmap_read(m, 0, 16)          # first 16 bytes, wherever they live

record5 = mmap_read(m, 5 * 64, 64)    # the 6th fixed-width 64-byte record

print(n)
print(header)
print(record5)
output
512
[0, 1, 2, 3, 4, 5, 6, 7, ... (16 elements)]
[255, 255, 255, 255, 255, 255, 255, 255, ... (64 elements)]

from file-io

f = fopen("mmap_demo.bin", "wb")
write_byte(f, 65)
write_byte(f, 66)
write_byte(f, 67)
write_byte(f, 68)
write_byte(f, 69)
write_byte(f, 70)
write_byte(f, 71)
write_byte(f, 72)
close(f)

m = mmap_open("mmap_demo.bin")
n = mmap_len(m)
first4 = mmap_read(m, 0, 4)
last4 = mmap_read(m, 4, 4)
print("n = {n}")
print("first4 = {first4}")
print("last4 = {last4}")

from tour