I'm trying to rewrite malloc and calloc, my question is about the implementation of calloc, not how to use it.
One should always use calloc()
instead of malloc()+memset()
, because it could take advantage of copy-on-write (COW).
Some calloc
's are implemented like this:
void * calloc(size_t nelem, size_t elsize)
{
void *p;
p = malloc (nelem * elsize);
if (p == 0)
return (p);
bzero (p, nelem * elsize);
return (p);
}
But they don't use COW at all (and they don't check overflow).
If those implementations don't call bzero()
, they must assume that the mmap
'ed pages they receive are zero-filled. They probably are because of security reasons, we don't want data from other processes to leak, but I can't find any standard reference about this.
Instead of using MAP_ANON
, we could mmap
from /dev/zero
:
fd = open("/dev/zero", O_RDWR);
a = mmap (0, 4096e4, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_FILE, fd, 0);
But /dev/zero
is not mandated by POSIX, and one could easily do sudo mv /dev/zero /dev/foo
, breaking my implementation.
What's the correct way to efficiently re-write calloc()
, respecting copy-on-write?