1 /* calloc for uClibc
2  *
3  * Copyright (C) 2002 by Erik Andersen <andersen@uclibc.org>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Library General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this program; see the file COPYING.LIB.  If not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <errno.h>
23 
24 
calloc(size_t nmemb,size_t lsize)25 void * calloc(size_t nmemb, size_t lsize)
26 {
27 	void *result;
28 	size_t size=lsize * nmemb;
29 
30 	/* guard vs integer overflow, but allow nmemb
31 	 * to fall through and call malloc(0) */
32 	if (nmemb && lsize != (size / nmemb)) {
33 		__set_errno(ENOMEM);
34 		return NULL;
35 	}
36 	if ((result=malloc(size)) != NULL) {
37 		memset(result, 0, size);
38 	}
39 	return result;
40 }
41 
42