From what I understand, I have to choose between calloc, which will allocate zeroed memory, and malloc, which can allocate memory on demand.
Is there a function that combines both those properties? Maybe direct call to mmap?
If it’s possible, why calloc doesn’t do it?
There are a few mechanisms to get pre-zeroed memory from the operating system:
mmap(2)‘sMAP_ANONYMOUSflag forces the contents to be initialized to zero.The POSIX shared memory segments can also zero
shm_open(3)provides you with a file descriptorftruncate(2)the “file” to the size you wantmmap(2)the “file” into your address spaceThe memory comes pre-zeroed:
It appears that this memory is zeroed at use:
mm/shmem.cfunctionshmem_zero_setup():