Hello guys! I have a quick question to resolve. I mmap some file from disk with memfiles module as streams and it works great, but some cases involve indexing mmaped file. Here is what i tried to do:
var mmaped_file = memfiles.open("file", mode = fm_read_write)
var mem_pointer = mmaped_file.mem
var first_byte = mem_pointer[]
results into:
Error: type mismatch: got <pointer>
Why? How can i access mmaped file as buffer?
You can probably use cstring if on a C backend, but if you wanted to index some other element type than char (like int or Foo) you can also do
let foos = cast[ptr UncheckedArray[Foo]](mmaped_file.mem)
if i * Foo.sizeof < mmapped_file.size:
doSomethingWith foos[i] # e.g. i == 0 for 1st Foo
You may well be able to skip the range check every time if you are, e.g. looping over the file & know you won't overflow from that, etc. To recover automatic bounds checks, you can write new/use existing procs accepting openArray[Foo] (e.g. openArray[T]) and pass via toOpenArray explicitly in the parameter list:
fun(toOpenArray(mf.mem, 0, mf.size div Foo.size - 1), a, b, c)
That can be abbreviated with a template oaFoo(mf) if you need to pass it or various files in several places or something. If such procs want to modify the data the file needs to have been opened writable or you will get segmentation violations.