calloc.c 1.2 KB

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