Retro68/binutils/libiberty/calloc.c

35 lines
722 B
C
Raw Permalink Normal View History

2012-03-26 19:18:29 +00:00
/* calloc -- allocate memory which has been initialized to zero.
This function is in the public domain. */
/*
@deftypefn Supplemental void* calloc (size_t @var{nelem}, size_t @var{elsize})
Uses @code{malloc} to allocate storage for @var{nelem} objects of
@var{elsize} bytes each, then zeros the memory.
@end deftypefn
*/
#include "ansidecl.h"
#include <stddef.h>
/* For systems with larger pointers than ints, this must be declared. */
2022-10-27 18:45:45 +00:00
void *malloc (size_t);
void bzero (void *, size_t);
2012-03-26 19:18:29 +00:00
2022-10-27 18:45:45 +00:00
void *
2012-03-26 19:18:29 +00:00
calloc (size_t nelem, size_t elsize)
{
2022-10-27 18:45:45 +00:00
register void *ptr;
2012-03-26 19:18:29 +00:00
if (nelem == 0 || elsize == 0)
nelem = elsize = 1;
ptr = malloc (nelem * elsize);
if (ptr) bzero (ptr, nelem * elsize);
return ptr;
}