I cannot figure how to get the virtual address of the standard mmap objects in Python (from the mmap module). The documented methods only seem to access the memory as array of bytes or as character strings.
But I need to access the mmap'ped memory by precisely 2 or 4 bytes at once - because this memory in my application is mapped to hardware registers (think /dev/mem or GPIO or such). Accessing memory in this way is possible with ctypes module - but for this I need the pointer - or virtual address - of the mapping.
Currently I overcome this by using the native open() and mmap() functions from libc (thanks to the same ctypes) but would rather not.
Why the mmap module won't provide easy way to get the memory address? Hope I'm missing something obvious...
-- dd
mmap
objects support the writable buffer interface, so you can use the from_buffer
class method that ctypes
classes have with the mmap
object as an argument to get a ctypes
object that shares the memory of the mmap
file.
buf = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE)
int_pointer = ctypes.c_int.from_buffer(buf)