Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether a memory page is mmaped in C

Tags:

python

c

numpy

mmap

I would like to know a way to determine whether a pointer belongs to a mmaped memory page.

A post on StackOverflow suggest using msync() on the pointer to determine whether it passes or not. But what I found is msync() will success as long as the pointer is a multiple of the page size, no matter the page is mmaped or not.

I calculate the start address of the page by the macro:

#define align_to_page(addr,pagesize) (addr & ~(pagesize - 1))

Please help me, I have been working on it for a long time and blocked here. Thank you in advance :)

Background: I am developing an extension to Python and Numpy. In some functions, it has to distinguish a normal numpy.ndarray and a numpy.memmap which is a subclass of numpy.ndaraay with its data buffer created by mmap() backed by a file on disk.

like image 728
Stan Avatar asked Nov 06 '25 09:11

Stan


1 Answers

All non-empty process address space is mmaped. Either using mmap syscall or indirectly via brk/sbrk syscalls .

You probably need to find another way to distinguish numpy arrays.

numpy.memmap documentation says these arrays have additional attributes filename, offset and mode, may be you can use these to detect whether an array is backed by a file.

like image 125
Maxim Egorushkin Avatar answered Nov 08 '25 00:11

Maxim Egorushkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!