这个程序中posix_memalign的含义是什么?

时间:2016-03-18 16:16:58

标签: c memory posix

我读过一个程序,它使用DIRECT_IO来插入一个寄存器(prof struct)。我怀疑的是,如果我在这里正确使用posix_memalign。我知道必须对齐已分配的内存块才能使用DIRECT_IO方法。

我的节目是(这是有效的,但请告诉我任何建议):

#define _GNU_SOURCE /* for O_DIRECT */

#include <stdio.h>
#include <sys/types.h>  /* required by open() */
#include <unistd.h>     /* open(), write() */
#include <fcntl.h>      /* open() and fcntl() */
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <malloc.h>

#define PAGE_SIZE 4096
#define LENGTH  50

typedef struct {
    int nusp;
    char first_name[LENGTH];
    char last_name[LENGTH];
    char department[LENGTH];
    int year_of_begin;
} prof;

int disk_open() {
    int flag;
    int ret;

    flag = O_CREAT | O_RDWR | O_DIRECT;

    if ((ret = open("test.header", flag, S_IRUSR | S_IWUSR)) < 0) {
        printf("was impossible to create the file");
        return -1;
    }
    return ret;
}

int main() {
    int cmd;
    prof d;
    int arq;
    uint8_t *buf, *loc;
    size_t bufsize;

    do {
        printf("Digite:\n");
        printf("****\n(1) insert\n(2) see register\n(3) leave\n****\n");
        scanf("%d", &cmd);
        switch (cmd) {
            case 1:
                printf("ID: ");
                scanf("%d", &(d.nusp));

                printf("NAME: ");
                scanf("%s", (d.first_name));

                printf("LASTNAME: ");
                scanf("%s", (d.last_name));

                printf("DEPARTAMENT: ");
                scanf("%s", (d.department));

                printf("YEAR: ");
                scanf("%d", &(d.year_of_begin));

                bufsize = sizeof (int) +
                        sizeof (d.first_name) +
                        sizeof (d.last_name) +
                        sizeof (d.department) +
                        sizeof (int);
                printf("bufsize = %d\n", bufsize);
                //buf = (uint8_t*) malloc(bufsize);
                if(posix_memalign((void**)&buf, PAGE_SIZE, bufsize)) {
                    printf("allocation failed\n");
                }

                loc = buf;

                memcpy(loc, &(d.nusp), sizeof (int));
                loc += sizeof (int);

                memcpy(loc, d.first_name, sizeof (d.first_name));
                loc += sizeof (d.first_name);

                memcpy(loc, d.last_name, sizeof (d.first_name));
                loc += sizeof (d.last_name);

                memcpy(loc, d.department, sizeof (d.department));
                loc += sizeof (d.department);

                memcpy(loc, &(d.year_of_begin), sizeof (int));
                loc += sizeof (int);

                arq = disk_open();
                printf("bytes written: %d\n", write(arq, buf, PAGE_SIZE));

                free(buf);

                close(arq);
                break;

            case 2:
                arq = disk_open();

                bufsize = sizeof (int) +
                        sizeof (d.first_name) +
                        sizeof (d.last_name) +
                        sizeof (d.department) +
                        sizeof (int);
                //buf = (uint8_t*) malloc(bufsize);
                 posix_memalign((void**)&buf, PAGE_SIZE, bufsize);

                printf("read %d bytes\n", read(arq, buf, PAGE_SIZE));

                memcpy(&(d.nusp), buf, sizeof (int));
                buf += sizeof (int);

                memcpy(d.first_name, buf, sizeof (d.first_name));
                buf += sizeof (d.first_name);

                memcpy(d.last_name, buf, sizeof (d.last_name));
                buf += sizeof (d.last_name);

                memcpy(d.department, buf, sizeof (d.department));
                buf += sizeof (d.department);

                memcpy(&(d.year_of_begin), buf, sizeof (int));
                buf += sizeof (int);

                printf("ID: %d\n", d.nusp);
                printf("NAME: %s\n", d.first_name);
                printf("LAST NAME: %s\n", d.last_name);
                printf("DEPARTAMENT: %s\n", d.department);
                printf("YEAR: %d\n", d.year_of_begin);
                printf("\n");

                close(arq);
                break;

            default:
                printf("Leaving....");
                break;
        }
    } while (cmd != 3);
    return 0;
}

所以,我的问题是: 在posix_memalign((void**)&buf, PAGE_SIZE, bufsize);,我在4096字节的块存储器(即我的页面大小)中分配158个字节,这意味着我浪费了3938个字节?这是因为我必须写/读对齐的块存储器,在这种情况下是4096(2的幂),对吗?

如果我的PAGE_SIZE等于bufsize会发生什么(考虑到bufsize和PAGE_SIZE在这种情况下的大小等于4096字节)?这是否意味着我不在内存和磁盘中浪费字节?

如果我想在Windows中使用DIRECT_IO,是否可以?如果是,我必须使用哪个库/函数来分配对齐的块存储器?

1 个答案:

答案 0 :(得分:1)

将返回一块内存块,保证与PAGE_SIZE对齐;而已。不要假设最后没有使用3938个字节的空间 - 智能分配器可以很容易地在页面对齐的内存中给出第一个条目,并保留其余的158字节分配。

通过仅使用158个字节的PAGE_SIZE磁盘块,您在磁盘上浪费了大量空间,事实上,您的写入实际上是将未定义的数据放入磁盘的块中 - 所有内容从158 - 4096从未分配给你,你应该试图读取或写入那个记忆。

如果PAGE_SIZE == bufsize,那么您不会浪费任何磁盘空间。

another question,它解释了如何获得O_DIRECT标志的类似行为。