Enforce a 2GB memory space for a 64-bit process
Rather a theoretical question -- would anyone know how it were possible开发者_StackOverflow to make sure a 64-bit process is allocated no more than 2GB of continuous memory.
This came up during the porting of a 32-bit C++ application that does pointer arithmetic (bad!) and relies on subtraction results to fit on a 32-bit integer. Before fixing the pointer arithmetic to correctly handle >2GB ptrdiff_t
values, enforcing a 2GB memory space for a process might prove to be be a quick fix.
NOTE: Targeted platforms include Solaris 10, Linux, and Windows.
The C and C++ standards do not require calls to malloc()
or operator new
to return memory that is contiguous with previously-returned memory, and few modern systems would given how virtual memory is doled out in a process.
You may be SOL on that front unless you can tell us which platform you're targetting (there may be a platform-specific solution.)
Redefine global new
and delete
operators such that any allocation of chunk more then 2GB will fail...
精彩评论