使用mremap进行内存重新分配 [英] Memory reallocation using mremap

查看:167
本文介绍了使用mremap进行内存重新分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用malloc分配两个不同的4096字节,并使用不同的值初始化这些分配.之后,我希望其中一个指针指向另一个分配,不带"更改p1的值,不带"复制数据.我想将第二个分配重新映射"到第一个分配,这基本上应该更改进程页面表中的虚拟地址,即不涉及复制.当我运行此代码时,我得到mremap失败.任何想法如何做到这一点?

I am trying to allocate two different 4096 bytes using malloc and initialize these allocations with different values. Afterwards, I want one of the pointers to point to the other allocation "without" changing the value of p1 and "without" copying the data. I want to "remap" the second allocation to the first allocation which should basically change the virtual address in the page table of the process, i.e. no copying is involved. When I run this code, I get that mremap failed. Any idea how to accomplish this?

int main(){
    char *p1 = (char *)malloc(4096);
    memset(p1, 'a', 4096);
    char *p2 = (char *)malloc(4096);
    memset(p2, 'b', 4096);
    printf("p1 is %c at address %p\n", p1[0], p1);
    printf("p2 is %c at address &p\n", p2[0], p2);
    free(p1);
    /* remapping virtual addresses */
    void *p0 = mremap(p2, 4096, 4096, MREMAP_MAYMOVE | MREMAP_FIXED, p1);
    /* checking */
    printf("p0 is %c at address %p\n", p0[c], p0);
    return 0;
}

预期输出:p1是地址xxx处的ap2在地址yyy处为bp0是地址xxx处的b

expected output: p1 is a at address xxx p2 is b at address yyy p0 is b at address xxx

推荐答案

malloc()返回的地址可能没有页面对齐,因此 mremap()是将 errno 设置为 EINVAL .如果我使用 mmap()而不是 malloc(),并修复代码中的语法错误:

The addresses returned by malloc() are probably not page-aligned, so mremap() is setting errno to EINVAL. If I use mmap() instead of malloc(), and fix the syntax errors in your code:

#define _GNU_SOURCE 1

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>

int main(void) {
    char *p1 = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    memset(p1, 'a', 4096);

    char *p2 = mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                    MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
    memset(p2, 'b', 4096);

    printf("p1 is %c at address %p\n", p1[0], (void *)p1);
    printf("p2 is %c at address %p\n", p2[0], (void *)p2);

    void *p0 = mremap(p2, 4096, 4096, MREMAP_MAYMOVE | MREMAP_FIXED, p1);
    if ( p0 == MAP_FAILED ) {
        perror("mremap: mremap failed");
        return EXIT_FAILURE;
    }

    printf("p0 is %c at address %p\n", ((char *)p0)[0], p0);
    return EXIT_SUCCESS;
}

然后我得到:

paul@local:~/src/c/scratch$ ./mremap
p1 is a at address 0x7f5addb71000
p2 is b at address 0x7f5addb70000
p0 is b at address 0x7f5addb71000
paul@local:~/src/c/scratch$ 

这篇关于使用mremap进行内存重新分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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