سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

Well, if you do know the start address of your contiguous memory pool and its size you just have to check you pointer:

if( (my_ptr >= start_addr) && (my_ptr < start_addr + pool_size) )
{
    return true; // my_ptr belongs to the memory pool
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top