calloc.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. void * calloc(size_t nmemb, size_t lsize)
  23. {
  24. void *result;
  25. size_t size=lsize * nmemb;
  26. /* guard vs integer overflow, but allow nmemb
  27. * to fall through and call malloc(0) */
  28. if (nmemb && lsize != (size / nmemb)) {
  29. __set_errno(ENOMEM);
  30. return NULL;
  31. }
  32. if ((result=malloc(size)) != NULL) {
  33. memset(result, 0, size);
  34. }
  35. return result;
  36. }