]> pd.if.org Git - pdclib/blob - opt/malloc-solar/calloc.c
* Change the style of inclusion of the internal/ headers. Modern preprocessors
[pdclib] / opt / malloc-solar / calloc.c
1 /* $Id$ */
2
3 /* void * calloc( size_t, size_t )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdlib.h>
10 #include <string.h>
11
12 #ifndef REGTEST
13
14 void * calloc( size_t nmemb, size_t size )
15 {
16     /* assign memory for nmemb elements of given size */
17     void * rc = malloc( nmemb * size );
18     if ( rc != NULL )
19     {
20         /* zero-initialize the memory */
21         memset( rc, 0, nmemb * size );
22     }
23     return rc;
24 }
25
26 #endif
27
28 #ifdef TEST
29 #include <_PDCLIB_test.h>
30
31 int main( void )
32 {
33     char * s;
34     TESTCASE( ( s = calloc( 3, 2 ) ) != NULL );
35     TESTCASE( s[0] == '\0' );
36     TESTCASE( s[5] == '\0' );
37     free( s );
38     TESTCASE( ( s = calloc( 6, 1 ) ) != NULL );
39     TESTCASE( s[0] == '\0' );
40     TESTCASE( s[5] == '\0' );
41     free( s );
42     TESTCASE( ( s = calloc( 1, 6 ) ) != NULL );
43     TESTCASE( s[0] == '\0' );
44     TESTCASE( s[5] == '\0' );
45     free( s );
46     return TEST_RESULTS;
47 }
48
49 #endif