X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdlib%2Fcalloc.c;h=5450b978f07a83c92e7284d5856930792d61bea9;hb=35a80beca8122bc8dc7464c017bf778f872accd4;hp=4169f84c60c8ff80ea338a13dd50044e8ad85bb7;hpb=0a5395faab237ba9008352b0f4bee9659bbd3d5f;p=pdclib diff --git a/functions/stdlib/calloc.c b/functions/stdlib/calloc.c index 4169f84..5450b97 100644 --- a/functions/stdlib/calloc.c +++ b/functions/stdlib/calloc.c @@ -1,34 +1,52 @@ -// ---------------------------------------------------------------------------- -// $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() +{ + char * s; + BEGIN_TESTS; + 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; } -*/ \ No newline at end of file + +#endif