背景知识
MAP_ANONYMOUS
The mapping is not backed by any file; its contents are initialized to zero. The fd and offset arguments are ignored; however, some implementations require fd to be -1 if
MAP_ANONYMOUS (or MAP_ANON) is specified, and portable applications should ensure this. The use of MAP_ANONYMOUS in conjunction with MAP_SHARED is supported on Linux only since
kernel 2.4.
使用例子
/* Allocate some space and setup a DMA mapping */
dma_map.vaddr = mmap(0, 1024 * 1024, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); // 分配1M内存
dma_map.size = 1024 * 1024;
dma_map.iova = 0; /* 1MB starting at 0x0 from device view */
dma_map.flags = VFIO_DMA_MAP_FLAG_READ | VFIO_DMA_MAP_FLAG_WRITE;
说明
使用 mmap 时, fd 为0, FLGA 为 MAP_ANONYMOUS,相当于用来分配一个匿名文件对应的内存出来,也就是分配内存
使用mmap进行内存分配,主要是用于比较大的内存
参考文章
https://www.jb51.cc/linux/440568.html
https://ask.csdn.net/questions/708096
https://blog.csdn.net/yusiguyuan/article/details/39496057