据我了解,我必须做出选择calloc
,这将分配归零内存,并且malloc
,可以按需分配内存。
是否有一个函数结合了这两个属性?或许可以直接拨打电话mmap
?
如果可以的话为什么calloc
不做吗?
有几种机制可以从操作系统获取预清零内存:
mmap(2)
's MAP_ANONYMOUS
flag 强制内容初始化为零。
POSIX 共享内存段也可以清零
-
shm_open(3)
为您提供文件描述符
-
ftruncate(2)
将“文件”调整为您想要的大小
-
mmap(2)
将“文件”放入您的地址空间
内存已预先清零:
This volume of IEEE Std 1003.1-2001 specifies that memory
objects have initial contents of zero when created. This is
consistent with current behavior for both files and newly
allocated memory. For those implementations that use physical
memory, it would be possible that such implementations could
simply use available memory and give it to the process
uninitialized. This, however, is not consistent with standard
behavior for the uninitialized data area, the stack, and of
course, files. Finally, it is highly desirable to set the
allocated memory to zero for security reasons. Thus,
initializing memory objects to zero is required.
看来该内存已归零use: mm/shmem.c
功能shmem_zero_setup()
:
/**
* shmem_zero_setup - setup a shared anonymous mapping
* @vma: the vma to be mmapped is prepared by do_mmap_pgoff
*/
int shmem_zero_setup(struct vm_area_struct *vma)
{
struct file *file;
loff_t size = vma->vm_end - vma->vm_start;
file = shmem_file_setup("dev/zero", size, vma->vm_flags);
if (IS_ERR(file))
return PTR_ERR(file);
if (vma->vm_file)
fput(vma->vm_file);
vma->vm_file = file;
vma->vm_ops = &shmem_vm_ops;
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)