X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Fcalloc.c;h=fc1c80e767241ea033f2377d548aae42e0da4b37;hb=12e17136786afb1775c9dc946cbe41f5e230c24a;hp=a9a485fee3ff64c7e4fa64492843e146c31f7f6b;hpb=c8f799d852e3698468a78954d82588e841cc0b70;p=pdclib.old diff --git a/functions/stdlib/calloc.c b/functions/stdlib/calloc.c index a9a485f..fc1c80e 100644 --- a/functions/stdlib/calloc.c +++ b/functions/stdlib/calloc.c @@ -1,34 +1,51 @@ -/* ---------------------------------------------------------------------------- - * $Id$ - * ---------------------------------------------------------------------------- - * Public Domain C Library - http://pdclib.sourceforge.net - * This code is Public Domain. Use, modify, and redistribute at will. - * --------------------------------------------------------------------------*/ +/* $Id$ */ -void * calloc( size_t nelem, size_t size ) { /* TODO */ }; +/* Release $Name$ */ -/* PDPC code - unreviewed +/* void * calloc( size_t, size_t ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include + +#ifndef REGTEST + +void * calloc( size_t nmemb, size_t size ) { - void *ptr; - size_t total; - - if (nmemb == 1) - { - total = size; - } - else if (size == 1) - { - total = nmemb; - } - else + /* assign memory for nmemb elements of given size */ + void * rc = malloc( nmemb * size ); + if ( rc != NULL ) { - total = nmemb * size; + /* zero-initialize the memory */ + memset( rc, 0, nmemb * size ); } - ptr = malloc(total); - if (ptr != NULL) - { - memset(ptr, '\0', total); - } - return (ptr); + return rc; } -*/ + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + char * s; + TESTCASE( ( s = calloc( 3, 2 ) ) != NULL ); + TESTCASE( s[0] == '\0' ); + TESTCASE( s[5] == '\0' ); + free( s ); + TESTCASE( ( s = calloc( 6, 1 ) ) != NULL ); + TESTCASE( s[0] == '\0' ); + TESTCASE( s[5] == '\0' ); + free( s ); + TESTCASE( ( s = calloc( 1, 6 ) ) != NULL ); + TESTCASE( s[0] == '\0' ); + TESTCASE( s[5] == '\0' ); + free( s ); + return TEST_RESULTS; +} + +#endif