美文网首页OpenChannelSSD_Qemu
posix_memalign vs. malloc

posix_memalign vs. malloc

作者: Quasars | 来源:发表于2017-02-02 10:50 被阅读60次
    #include <errno.h>
    #include <stdio.h>
    //#include <stdint.h>
    #include <inttypes.h>
    #include <stdlib.h> // for posix_memalign()
    void test()
    {
        char* buf;
        int ret;
        int align = 4096;
        uintptr_t align2 = 4096;
        int alc_size = 17;
        char* buf2 = (char*)malloc(alc_size);
        ret = posix_memalign((void**)&buf, align, alc_size);
        if(ret){
            errno = ret;
            perror("posix_memalign");
            return ;
        }
        printf("%p %llx\n", buf, (unsigned long long)buf);
        printf("%llu %llu\n", (unsigned long long)buf % align, (unsigned long long)buf2 % align);
        printf("%08" PRIdPTR ":" "%08" PRIdPTR "\n", (uintptr_t)buf % align2, (uintptr_t)buf2 % align2);
        
        free(buf);
        free(buf2);
    }
    int main()
    {
        test();
        return 0;
    }
    
    work:[TestKit]$./test 
    0x1ca4000 1ca4000
    0 16
    00000000:00000016
    

    相关文章

      网友评论

        本文标题:posix_memalign vs. malloc

        本文链接:https://www.haomeiwen.com/subject/yjtrittx.html