1/* calloc() function that is glibc compatible. 2 This wrapper function is required at least on Tru64 UNIX 5.1 and mingw. 3 Copyright (C) 2004-2007, 2009-2010 Free Software Foundation, Inc. 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18/* written by Jim Meyering and Bruno Haible */ 19 20#include <config.h> 21/* Only the AC_FUNC_CALLOC macro defines 'calloc' already in config.h. */ 22#ifdef calloc 23# define NEED_CALLOC_GNU 24# undef calloc 25#endif 26 27/* Specification. */ 28#include <stdlib.h> 29 30#include <errno.h> 31 32/* Call the system's calloc below. */ 33#undef calloc 34 35/* Allocate and zero-fill an NxS-byte block of memory from the heap. 36 If N or S is zero, allocate and zero-fill a 1-byte block. */ 37 38void * 39rpl_calloc (size_t n, size_t s) 40{ 41 void *result; 42 43#ifdef NEED_CALLOC_GNU 44 if (n == 0 || s == 0) 45 { 46 n = 1; 47 s = 1; 48 } 49 else 50 { 51 /* Defend against buggy calloc implementations that mishandle 52 size_t overflow. */ 53 size_t bytes = n * s; 54 if (bytes / s != n) 55 { 56 errno = ENOMEM; 57 return NULL; 58 } 59 } 60#endif 61 62 result = calloc (n, s); 63 64#if !HAVE_CALLOC_POSIX 65 if (result == NULL) 66 errno = ENOMEM; 67#endif 68 69 return result; 70} 71