calloc_dbg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2009 by egnite GmbH
  3. * Copyright (C) 2004 by egnite Software GmbH
  4. * Copyright (c) 1990 The Regents of the University of California.
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. /*
  37. * $Id: calloc_dbg.c 4473 2012-08-20 15:12:45Z haraldkipp $
  38. */
  39. #include <compiler.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <limits.h>
  43. #include <errno.h>
  44. #include <memdebug.h>
  45. /*!
  46. * \addtogroup xgCrtStdLib
  47. */
  48. /*@{*/
  49. /*!
  50. * \brief Allocate space for an array.
  51. *
  52. * \param num Number of elements.
  53. * \param size Size of a single element.
  54. *
  55. * \return A pointer to the allocated space or a null pointer if
  56. * not enough memory space is available.
  57. */
  58. void *dbg_calloc(size_t num, size_t size, const char *file, int line)
  59. {
  60. register void *p;
  61. #ifdef SIZE_T_MAX
  62. if (num && size && SIZE_T_MAX / num < size) {
  63. errno = ENOMEM;
  64. return NULL;
  65. }
  66. #endif
  67. size *= num;
  68. p = dbg_malloc(size, file, line);
  69. if (p)
  70. memset(p, 0, size);
  71. return (p);
  72. }
  73. /*@}*/