是否可以使用mmap映射文件的一部分?

时间:2010-11-24 01:45:03

标签: c memory pointers memory-management memory-mapped-files

我有一个输入文件,其标题如下:

P6\n
width\n
height\n
depth\n

然后将一个struct(像素*)写入此文件,该文件将被映射。

所以,我想跳过标题并让我的mmap函数将ptr返回到该结构。我怎样才能做到这一点?和lseek或许?你能举个例子吗?

我将在此留下部分代码:

printf("Saving header to output file\n");
    if (writeImageHeader(h, fpout) == -1) {
        printf("Could not write to output file\n");
        return -1;
    }

    last_index = (int)ftell(fpout);
    //printf("offset after header= %d\n",last_index);

    //alloc mem space for one row (width * size of one pixel struct)
    row = malloc(h->width * sizeof (pixel));

    /*Create a copy of the original image to the output file, which will be inverted*/
    printf("Starting work\n");
    for (i = 0; i < h->height; i++) {
        printf("Reading row... ");
        if (getImageRow(h->width, row, fpin) == -1) {
            printf("Error while reading row\n");
        }
        printf("Got row %d || ", (i + 1));

        printf("Saving row... ");
        if (writeRow(h->width, row, fpout) == -1) {
            printf("Error while reading row\n");
        }
        printf("Done\n");
    }


    /*Open file descriptor of the ouput file.
     * O_RDWR -  Read and Write operations both permitted
     * O_CREAT - Create file if it doesn't already exist
     * O_TRUNC - Delete existing contents of file*/
    if ((fdout = open(argv[2], O_RDWR, FILE_MODE)) < 0) {
        fprintf(stderr, "Can't create %s for writing\n", argv[2]);
        exit(1);
    }

    /*Get size of the output file*/
    if (fstat(fdout, &sbuf) == -1) {
        perror("Stat error ---------->\n");
        exit(1);
    }
    //printf("Size of output file: %d\n",(int)sbuf.st_size);

    /*Maps output file to memory*/
    if ((data = mmap((caddr_t) 0, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0)) == (caddr_t) (-1)) {
        perror("Error mmaping");
        exit(EXIT_FAILURE);
    }

如您所见,现在我的ppm图像已映射到char*数据,但我想跳过标题并仅映射到pixel*部分。

这是我的代码,建议使用2个指针,来自mmap的char *,另一个等于+ offset。

main
c functions
header
makefile

5 个答案:

答案 0 :(得分:1)

如果您阅读mmap的手册页,您会发现其最终参数为off_t offset。说明:

  

...继续或最多'len'字节要从'fd'描述的对象映射,从字节偏移'offset'开始。

我怀疑如果你把偏移作为那个参数传递,它会做你想要的。

答案 1 :(得分:1)

如果您需要跳过的金额小于系统页面大小,则不能,因为offset必须是某些系统上页面大小的倍数。

答案 2 :(得分:1)

你只需要保留2个指针 - 指向mmap'd块开头的指针,以及指向你想要的数据开头的指针。如:

unsigned char *block = mmap(0, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, 0);
unsigned char *data = block + offset;

其中offset是文件中所需数据的偏移量。

答案 3 :(得分:0)

所以,根据我的理解,我可以做这样的事吗?

off_t offset_after_header = lseek(fdout, last_index, SEEK_SET);
    printf("Pointer is on %d\n",(int)offset_after_header);
    /*Maps output file to memory*/
    if ((data = mmap((caddr_t) 0, sbuf.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fdout, offset_after_header)) == (caddr_t) (-1)) {
        perror("Error mmaping");
        exit(EXIT_FAILURE);
    }

并且,从那里,我可以将我的文件映射到我想要的任何类型,在这种情况下pixel*

如果可以,我应该注意什么?例如,像Ignacio Vazquez-Abrams所说的那样

答案 4 :(得分:-1)

嗯,你注意到你提供零的'offset'参数?假设你知道你想要的绝对偏移量,你就传递它。

相关问题