initial check-in, version 0.5.0
[musl] / src / malloc / calloc.c
1 #include <stdlib.h>
2 #include <errno.h>
3 #include <string.h>
4
5 void *calloc(size_t m, size_t n)
6 {
7         void *p;
8         size_t *z;
9         if (n && m > (size_t)-1/n) {
10                 errno = ENOMEM;
11                 return 0;
12         }
13         n *= m;
14         p = malloc(n);
15         if (!p) return 0;
16         /* Only do this for non-mmapped chunks */
17         if (((size_t *)p)[-1] & 7) {
18                 /* Only write words that are not already zero */
19                 m = (n + sizeof *z - 1)/sizeof *z;
20                 for (z=p; m; m--, z++) if (*z) *z=0;
21         }
22         return p;
23 }