calloc.c 587 B

123456789101112131415161718192021222324
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Header$ */
  6. #include <stdlib.h>
  7. #define ALIGN(sz) (((sz)+(sizeof(long)-1)/sizeof(long))*sizeof(long))
  8. void *
  9. calloc(size_t nelem, size_t elsize)
  10. {
  11. register void *p;
  12. register long *q;
  13. unsigned int size = ALIGN(nelem * elsize);
  14. if ((p = malloc(size)) == (void *)NULL)
  15. return (void *)NULL;
  16. /* "The space is intialized to all bits zero" */
  17. q = (long *) (p + size);
  18. while ((void *) q > p) *--q = 0;
  19. return p;
  20. }