Pio*_*zmo 3 c linux memory-management
据我了解,我必须在calloc分配零内存的,和malloc可以按需分配内存的之间进行选择。
是否有结合了这两个属性的功能?也许直接打电话给mmap?
如果有可能,为什么calloc不这样做呢?
有几种机制可以从操作系统中获取预先清零的内存:
mmap(2)的MAP_ANONYMOUS标志会强制将内容初始化为零。
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.
Run Code Online (Sandbox Code Playgroud)
看来此内存在使用时已归零:mm/shmem.cfunction 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;
}
Run Code Online (Sandbox Code Playgroud)