C/C++ - 使用 mmap 的内存映射文件 [英] C/C++ - Memory map file using mmap

查看:339
本文介绍了C/C++ - 使用 mmap 的内存映射文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是内存映射文件的新手,有点困惑.是否可以映射大于内存总量的文件,因为据我所知,内存映射使用需求分页,并且只会在内存中保存当前相关的页面.这是正确的,还是在使用比实际内存更多的空间时我的应用程序会崩溃.

I'm new to memory mapping files and am a bit confused. Is it possible to map files larger than the total amount of memory because as I understood memory mapping uses demand paging and will hold only the currently relevant pages in memory. Is this right or will my application crash when using more space than the actual memory.

谢谢

编辑操作系统:Ubuntu 14.04 LTS x86_64App-Bitness:64bit(我猜:指针是 8 字节)

EDIT OS: Ubuntu 14.04 LTS x86_64 App-Bitness: 64bit (I guess: pointers are 8 byte)

我正在尝试从映射文件分配内存,以便将树存储在映射文件中.

I'm trying to allocate memory from the mapped file in order to store the tree inside the mapped file.

#define MEMORY_SIZE 300000000

unsigned char *mem_buffer;
void *start_ptr;

void *my_malloc(int size) {
    unsigned char *ptr = mem_buffer;
    mem_buffer += size;

    return ptr;
}

void *my_calloc(int size, int object_size) {
    unsigned char *ptr = mem_buffer;
    mem_buffer += (size * object_size);

    return ptr;
}

void init(const char *file_path) {
    int fd = open(file_path, O_RDWR, S_IREAD | S_IWRITE);

    if (fd < 0) {
        perror("Could not open file for memory mapping");
        exit(1);
    }

    start_ptr = mmap(NULL, MEMORY_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
    mem_buffer = (unsigned char *) start_ptr;

    if (mem_buffer == MAP_FAILED) {
        perror("Could not memory map file");
        exit(1);
    }

    printf("Successfully mapped file.\n");
}

void unmap() {
    if (munmap(start_ptr, MEMORY_SIZE) < 0) {
        perror("Could not unmap file");
        exit(1);
    }

    printf("Successfully unmapped file.\n");
}

主要方法:

int main(int argc, char **argv) {

    init(argv[1]);

    unsigned char *arr = (unsigned char *) my_malloc(6);
    arr[0] = 'H';
    arr[1] = 'E';
    arr[2] = 'L';
    arr[3] = 'L';
    arr[4] = 'O';
    arr[5] = '\0';

    unsigned char *arr2 = (unsigned char *) my_malloc(5);
    arr2[0] = 'M';
    arr2[1] = 'I';
    arr2[2] = 'A';
    arr2[3] = 'U';
    arr2[4] = '\0';

    printf("Memory mapped string1: %s\n", arr);
    printf("Memory mapped string2: %s\n", arr2);

    struct my_btree_node *root = NULL;

    insert(&root, arr, 10);
    insert(&root, arr2, 20);

    print_tree(root, 0, false);

//  cin.ignore();

    unmap();

    return EXIT_SUCCESS;
}

推荐答案

这是摘自 GNU C 库:内存映射 I/O

由于可以在物理内存不足时将映射的页面存储回其文件,因此可以将映射文件的数量级大于物理内存和交换空间.唯一的限制是地址空间.32 位机器上的理论限制为 4GB - 但是,实际限制会更小,因为某些区域将被保留用于其他目的.

Since mmapped pages can be stored back to their file when physical memory is low, it is possible to mmap files orders of magnitude larger than both the physical memory and swap space. The only limit is address space. The theoretical limit is 4GB on a 32-bit machine - however, the actual limit will be smaller since some areas will be reserved for other purposes.

这篇关于C/C++ - 使用 mmap 的内存映射文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆