How to find holes in the address space?

I have a set of files whose length is a multiple of the size of my operating system (FreeBSD 10). I would like mmap() these files to consecutive RAM pages, giving me the ability to process a collection of files as one large data array.

Preferably, using portable functions, how can I find a sufficiently large area of ​​unmarked address space, so I can be sure that a series of mmap() calls for this area will succeed?

+5
source share
2 answers

Follow these steps:

  • First, calculate the total size needed to list your files and summarize their sizes.
  • Map one region of anonymous memory of this size with mmap . If this fails, you will lose.
  • Save the pointer and unmap region (in fact, unmap may not be needed if your mmap system with a fixed address implicitly cancels any previously overlapping region).
  • Map the first file at this address to the corresponding MAP_FIXED flag.
  • Increase the address by file size.
  • go to step 4 until all files are marked.

This should be fully portable for any POSIX system, but some operating systems may have quirks that prevent this method. Give it a try.

+5
source

You could mmap large area where the size is the sum of the sizes of all the files using MAP_PRIVATE | MAP_ANON MAP_PRIVATE | MAP_ANON and MAP_PRIVATE | MAP_ANON protection, which would prevent the OS from unnecessarily performing a memory card.

This will reserve, but not freeze, the memory.

You can then open file filename1 in [baseAddr, size1) and open filename2 in [baseAddr + size1, baseAddr + size1 + size2) , etc.

I believe the flags for this are MAP_FIXED | MAP_PRIVATE MAP_FIXED | MAP_PRIVATE .

+1
source

Source: https://habr.com/ru/post/1239638/


All Articles