Memory allocators

I want to create a virtual allocator using C ++ on windows that allocates data to a file on the hard drive to reduce the use of physical memory when allocating large objects! .. I do not want to use system virtual memory with virtualAlloc ... I want to create a file on the disk and use it to select all objects, and then move the part or object that I need for RAM.

I tried to use a file with memory mapping, but I had some problems: I used the mapped file to highlight vector elements, but when I baked to remove any of them, the address of the element changed, I can not find a method to map the object only when need "in my test I displayed the whole file"!

Any open source resources or projects can help.

+3
source share
5 answers

Google can help here. A few years ago, I introduced a dedicated STL allocator that used shared memory storage. The same methods can be used to implement a disk-based allocator. I would start by looking at this SourceForge project for inspiration.

+3
source

You can find inspiration from Boost.Interprocess, which provides support for memory mapped files , as well as allocators and containers for that memory.

http://www.boost.org/doc/libs/1_37_0/doc/html/interprocess/architecture.html

+2

, , () . , , " " ", ", , , , " ".

. , () , , , . , , . , .

, , , . .

+1

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=213

POSIX . , Windows, POSIX.

mmap():

caddr_t mmap(caddress_t map_addr, 
       size_t length, 
       int protection, 
       int flags, 
       int fd, 
       off_t offset);

, .

4 , , , int:

#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>

 int main(int argc, char *argv[])
 {
 int fd;
 void * pregion;
 if (fd= open(argv[1], O_RDONLY) <0)
 {
 perror("failed on open");
 return1;
 }
 /*map first 4 kilobytes of fd*/
 pregion=mmap(NULL, 4096, PROT_READ,MAP_SHARED,fd,0);
 if (pregion==(caddr_t)-1)
 {
 perror("mmap failed")
 return1;
 }
 close(fd); //close the physical file because we don't need it
 //access mapped memory; read the first int in the mapped file
 int val= *((int*) pregion);
}

, munmap():

int munmap(caddr_t addr, int length);

addr - . length , ( ). . - :

munmap(pregion, 1024);
0

, - . . - . - , , . - LRU . , , .

0

Source: https://habr.com/ru/post/1712553/


All Articles