Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify offset and access in mmap?

Tags:

python

mmap

Given mmap's constructor declaration:

class mmap.mmap(fileno, length[, flags[, prot[, access[, offset]]]])

How do I specify both access & offset?

The documentation states:

access may be specified in lieu of flags and prot as an optional keyword parameter. It is an error to specify both flags, prot and access. See the description of access above for information on how to use this parameter.

So I've tried to do things like

  • mmap.mmap(file_no, length, offset, access=mmap.ACCESS_COPY)
  • mmap.mmap(file_no, length, access=mmap.ACCESS_COPY, offset=offset)

    m = mmap.mmap(f.fileno(), 4, access=mmap.ACCESS_COPY, offset=2)
    Traceback (most recent call last):
    File "", line 1, in mmap.error: [Errno 22] Invalid argument

  • mmap.mmap(file_no, length, mmap.ACCESS_COPY, offset)

But I can't get it to work. Why is this confusing me so much?

like image 201
joslinm Avatar asked Oct 07 '22 14:10

joslinm


2 Answers

This error is unrelated to access. As documented, the offset just must be a multiple of mmap.PAGESIZE or mmap.ALLOCATIONGRANULARITY.

like image 94
phihag Avatar answered Oct 12 '22 10:10

phihag


try with:

m = mmap.mmap(f.fileno(), 4, access=mmap.ACCESS_COPY, offset=2 * mmap.ALLOCATIONGRANULARITY)
like image 45
luke14free Avatar answered Oct 12 '22 12:10

luke14free