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