Re: [MapViewOfFile] How to know the maximum number of bytes to map ?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



There are a couple of things that can limit this:

- Available physical memory for system structures used to maintain the
mapped view (PTEs, etc).
- Available contiguous address space in the process.

I suspect that the latter is much more likely to be a constraining factor
for you on x86-based systems, unless the system is already under significant
memory pressure.

In a virgin process with correctly based DLLs, you have a bit under 2GB of
address space available on a typical x86 system with the default 2GB
user/2GB kernel address space split.

In WinDbg, the `!address' command is a good way to quickly check how much
free address space you have - this might be useful to run on some typical
usage configurations for your process to get a feel for what kind of size
range you might be looking for.

If you want to programmically calculate the amount of free address space,
you can call the VirtualQuery function iteratively over all address regions
returned by it and find the largest free region (starting at an allocation
granularity-aligned boundary, typically 64K).

Due to how mapped file views work, you don't necessarily need to have an
amount of physical/paged memory equal to the size of the file view you are
mapping. Data can be paged in/out of a mapped view similar in some respects
to how a paging file works, so you will not be, say, committing 2GB of
physical memory if you map a 2GB view of a file.

(Of course, if you are making a 2GB page-file-backed file mapping view,
then, yes you will consume 2GB of paging file quota.)

"Rodrigue" <rodrigue.roland@xxxxxxxxx> wrote in message
news:%23tHluW$dGHA.1276@xxxxxxxxxxxxxxxxxxxxxxx
Hello,

Could you tell me please how is it possible to know the maximum number of
bytes to map in the function MapViewOfFile ?

LPVOID MapViewOfFile(
HANDLE hFileMappingObject,
DWORD dwDesiredAccess,
DWORD dwFileOffsetHigh,
DWORD dwFileOffsetLow,
SIZE_T dwNumberOfBytesToMap
);

For the moment, I get the granularity of the system using the function
GetSytemInfo() and I use this value in MapViewOfFile (because
dwNumberOfBytesToMap has to be a multiple of the system granularity) but
after many tests I saw that it's possible to map a lot more memory...

Thanks in advance!
Cordially,
Rodrigue


.



Relevant Pages

  • [RFC][PATCH 5/5] mm: highmem documentation
    ... David asked if I could make a start at documenting some of the highmem issues. ... +Highmem comes about when the physical memory size approaches the virtual ... This means the kernel needs to start managing ... +On i386 for example we chose to map the kernel into every process so as not ...
    (Linux-Kernel)
  • Re: amd64: change VM_KMEM_SIZE_SCALE to 1?
    ... more than e.g. 1/3 of physical memory means needing more PTEs. ... takes about 4MB reserved as PTE pages to map 2GB of kernel virtual ... But I guess 4MB pages are no good for sparse mappings. ...
    (freebsd-arch)
  • Re: Either a kernel developer or a bit of assistance needed..
    ... Map a specified area of physical memory so that it's accessible ... from the user-space process - doesn't particularly matter where it's ... Just write a driver in the kernel and export the required ...
    (comp.unix.bsd.freebsd.misc)
  • RE: reading data from RAM
    ... To expand upon what Ryan said, one way of accessing physical memory is ... user mode wrapper for the kernel mode function ZwMapViewOfSection(). ... illegal to map a physical address concurrently with 2 different ... leads to greater stability on XP, however some physical pages may not be ...
    (Focus-Microsoft)
  • Either a kernel developer or a bit of assistance needed..
    ... I've a requirement to be able to do the following from a user-space ... Map a specified area of physical memory so that it's accessible ... Allocate buffers, and get pointers to both their physical and ...
    (comp.unix.bsd.freebsd.misc)