Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find holes in the address space?

I have a set of files whose lengths are all multiples of the page-size of my operating system (FreeBSD 10). I would like to mmap() these files to consecutive pages of RAM, giving me the ability to treat a collection of files as one large array of data.

Preferably using portable functions, how can I find a sufficiently large region of unmapped address space so I can be sure that a series of mmap() calls to this region is going to be successful?

like image 242
fuz Avatar asked Jan 01 '16 21:01

fuz


Video Answer


2 Answers

Follow these steps:

  1. First compute the total size needed by enumerating your files and summing their sizes.
  2. Map a single area of anonymous memory of this size with mmap. If this fails, you lose.
  3. Save the pointer and unmap the area (actually, unmap may not be necessary if your system's mmap with a fixed address implicitly unmaps any previous overlapping region).
  4. Map the first file at this address with the appropriate MAP_FIXED flag.
  5. Increment the address by the file size.
  6. loop to step 4 until all files have been mmapped.

This should be fully portable to any POSIX system, but some OSes might have quirks that prevent this method. Try it.

like image 54
chqrlie Avatar answered Sep 18 '22 15:09

chqrlie


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

This will reserve but not commit memory.

You could then open file filename1 at [baseAddr, size1) and open filename2 at [baseAddr + size1, baseAddr + size1 + size2), and so on.

I believe the flags for this are MAP_FIXED | MAP_PRIVATE.

like image 30
Aaditya Kalsi Avatar answered Sep 16 '22 15:09

Aaditya Kalsi