Is there a portable way to detect (programmatically) the memory page size using C or C++ code ?
相关问题
- Sorting 3 numbers without branching [closed]
- Multiple sockets for clients to connect to
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
Yes, this is platform-specific. On Linux there's
sysconf(_SC_PAGESIZE)
, which also seems to be POSIX. A typical C library implements this using the auxiliary vector. If for some reason you don't have a C library or the auxiliary vector you could determine the page size like this:That's also POSIX, I think. It relies on there being some free memory, but it only needs two consecutive pages. It might be useful in some (weird) circumstances.
It is entirely platform dependent which address-ranges are mapped to which page-sizes. Further the pagesize is not system-wide. You can allocate memory from different page-size regions according to the use case. And you can even have platforms without any virtual memory managment.
So, code handling this topic must be platform specific.
C doesn't know anything about memory pages. On posix systems you can use
long pagesize = sysconf(_SC_PAGE_SIZE);
I think this function helps.
[DllImport("kernel32.dll")] public static extern void GetSystemInfo([MarshalAs(UnmanagedType.Struct)] ref SYSTEM_INFO lpSystemInfo);
Since
Boost
is a pretty portable library you could usemapped_region::get_page_size()
function to retrieve the memory page size.As for C++ Standard it gives no such a possibility.
Windows 10, Visual Studio 2017, C++. Get the page size in bytes.