I would like to map a file into memory using mmap function and would like to know if the amount of virtual memory on the current platform is sufficient to map a huge file. For a 32 system I cannot map file larger than 4 Gb.
Would std::numeric_limits<size_t>::max() give me the amount of addressable memory or is there any other type that I should test (off_t or something else)?
As Lie Ryan has pointed out in his comment the “virtual memory” here is misused. The question, however holds: there is a type associated with a pointer and it has the maximum value that defines the upper limit of what you can possibly adress on your system. What is this type? Is it size_t or perhaps ptrdiff_t?
size_tis only required to be big enough to store the biggest possible single contiguous object. That may not be the same as the size of the address space (on systems with a segmented memory model, for example)However, on common platforms with a flat memory space, the two are equal, and so you can get away with using
size_tin practice if you know the target CPU.Anyway, this doesn’t really tell you anything useful. Sure, a 32-bit CPU has a 4GB memory space, and so
size_tis a 32-bit unsigned integer. But that says nothing about how much you can allocate. Some part of the memory space is used by the OS. And some parts are already used by your own application: for mapping the executable into memory (as well as any dynamic libraries it may use), for each thread’s stack, allocated memory on the heap and so on.So no, tricks such as taking the size of
size_ttells you a little bit about the address space you’re running in, but nothing very usable. You can ask the OS how much memory is in use by your process and other metrics, but again, that doesn’t really help you much. It is possible for a process to use just a couple of megabytes, but have that spread out over so many small allocations that it’s impossible to find a contiguous block of memory larger than 100MB, say. And so, on a 32-bit machine, with a process that uses nearly no memory, you’d be unlikely to make such an allocation. (And even if the OS had a magicalWhatIsTheLargestPossibleMemoryAllocationICanMake()API, that still wouldn’t help you. It would tell you what you needed from a moment ago. You have no guarantee that the answer would still be valid by the time you tried to map the file.So really, the best you can do is try to map the file, and see if it fails.