ialloc.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * \file ialloc.c
  3. *
  4. * \brief Matrix memory management functions.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * \author GRASS GIS Development Team
  21. *
  22. * \date 2004-2006
  23. */
  24. #include <stdlib.h>
  25. #include <grass/gis.h>
  26. /**
  27. * \fn int *G_alloc_ivector (size_t n)
  28. *
  29. * \brief Vector matrix memory allocation.
  30. *
  31. * Allocate a vector (array) of <b>n</b> integers initialized to zero.
  32. *
  33. * \param[in] n size of vector to allocate
  34. * \return integer *
  35. */
  36. int *G_alloc_ivector(size_t n)
  37. {
  38. return (int *)G_calloc(n, sizeof(int));
  39. }
  40. /**
  41. * \fn int **G_alloc_imatrix (int rows, int cols)
  42. *
  43. * \brief Matrix memory allocation.
  44. *
  45. * Allocate a matrix of <b>rows</b> by <b>cols</b> integers initialized
  46. * to zero.
  47. *
  48. * \param[in] rows number of rows in matrix
  49. * \param[in] cols number of columns in matrix
  50. * \return int **
  51. */
  52. int **G_alloc_imatrix(int rows, int cols)
  53. {
  54. int **m;
  55. int i;
  56. m = (int **)G_calloc(rows, sizeof(int *));
  57. m[0] = (int *)G_calloc((size_t) rows * cols, sizeof(int));
  58. for (i = 1; i < rows; i++)
  59. m[i] = m[i - 1] + cols;
  60. return m;
  61. }
  62. /**
  63. * \fn void G_free_ivector(int *v)
  64. *
  65. * \brief Vector memory deallocation.
  66. *
  67. * Deallocate a vector (array) of integers.
  68. *
  69. * \param[in,out] v vector to free
  70. * \return void
  71. */
  72. void G_free_ivector(int *v)
  73. {
  74. G_free(v);
  75. v = NULL;
  76. return;
  77. }
  78. /**
  79. * \fn int G_free_imatrix (int **m)
  80. *
  81. * \brief Matrix memory deallocation.
  82. *
  83. * Deallocate a matrix of integers.
  84. *
  85. * \param[in,out] m matrix to free
  86. * \return void
  87. */
  88. void G_free_imatrix(int **m)
  89. {
  90. G_free(m[0]);
  91. G_free(m);
  92. m = NULL;
  93. return;
  94. }