Is there a way to allocate memory to a file?

Hi,

Is there a way to allocate memory to a file instead of physical memory?
I mean, I want to write a function "file_new" which will mimic new operator and can used like this:

size_t size = 1<<35;
char * ptr = file_new(size, "c:\\myswap.tmp"); // Create a 1<<35 byte length file and map this file to virtual address
memset(ptr, 0, size); // Here solutions I have found, create a 1<<35 byte buffer in physical memory


The target system is supposed to be 64bit.



I have found solution which map memory to file (VirtualAlloc, CreateFileMapping, MapViewOfFile), but all finished to allocate memory in physical memory during the memset call.

Is there a solution to avoid to allocate this memory to physical memory?

Thanks,
Best Regards,
Eric
Use a memory mapped file using CreateFileMapping and pass INVALID_HANDLE_VALUE as first argument. The "memory" is allocated using paging file on disk.
Thank you for the answer, I tried the CreateFileMapping, but, during the memset operation, all the memory is copied in "RAM".
The only way I have found is to do manually a VirtualUnlock to free it. But, I can't call VirtualUnlock everytime I write in memory. And I may manipulate several big portions of memory >4GB at the same time.
The best I have found is to call these 2 functions after each read/write to my MMF:
VirtualUnlock(ptr, size);
FlushViewOfFile(ptr, size);

It seems to accept any ptr inside my MMF address space. And it has small impact if ptr is not mapped. So I can use it independantly, if ptr is mapped to file or not.

Maybe there are better solutions...
Just a thought why allocate space cant you allocate space in memory then patch it up there that way you staticlly allocated space without Physical Allocation.
Topic archived. No new replies allowed.