dalloc.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * \file dalloc.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 double *G_alloc_vector (size_t n)
  28. *
  29. * \brief Vector matrix memory allocation.
  30. *
  31. * Allocate a vector (array) of <b>n</b> doubles initialized to zero.
  32. *
  33. * \param[in] n size of vector to allocate
  34. * \return double *
  35. */
  36. double *G_alloc_vector(size_t n)
  37. {
  38. return (double *)G_calloc(n, sizeof(double));
  39. }
  40. /**
  41. * \fn double **G_alloc_matrix (int rows,int cols)
  42. *
  43. * \brief Matrix memory allocation.
  44. *
  45. * Allocate a matrix of <b>rows</b> by <b>cols</b> doubles 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 double **
  51. */
  52. double **G_alloc_matrix(int rows, int cols)
  53. {
  54. double **m;
  55. int i;
  56. m = (double **)G_calloc(rows, sizeof(double *));
  57. m[0] = (double *)G_calloc((size_t) rows * cols, sizeof(double));
  58. for (i = 1; i < rows; i++)
  59. m[i] = m[i - 1] + cols;
  60. return m;
  61. }
  62. /**
  63. * \fn float *G_alloc_fvector (size_t n)
  64. *
  65. * \brief Floating point vector memory allocation.
  66. *
  67. * Allocate a vector (array) of <b>n</b> floats initialized to zero.
  68. *
  69. * \param[in] n size of vector to allocate
  70. * \return float *
  71. */
  72. float *G_alloc_fvector(size_t n)
  73. {
  74. return (float *)G_calloc(n, sizeof(float));
  75. }
  76. /**
  77. * \fn float **G_alloc_fmatrix (int rows, int cols)
  78. *
  79. * \brief Floating point matrix memory allocation.
  80. *
  81. * Allocate a matrix of <b>rows</b> by <b>cols</b> floats initialized
  82. * to zero.
  83. *
  84. * \param[in] rows number of rows in matrix
  85. * \param[in] cols number of columns in matrix
  86. * \return float **
  87. */
  88. float **G_alloc_fmatrix(int rows, int cols)
  89. {
  90. float **m;
  91. int i;
  92. m = (float **)G_calloc(rows, sizeof(float *));
  93. m[0] = (float *)G_calloc((size_t) rows * cols, sizeof(float));
  94. for (i = 1; i < rows; i++)
  95. m[i] = m[i - 1] + cols;
  96. return m;
  97. }
  98. /**
  99. * \fn void G_free_vector (double *v)
  100. *
  101. * \brief Vector memory deallocation.
  102. *
  103. * Deallocate a vector (array) of doubles.
  104. *
  105. * \param[in,out] v vector to free
  106. * \return void
  107. */
  108. void G_free_vector(double *v)
  109. {
  110. G_free(v);
  111. v = NULL;
  112. return;
  113. }
  114. /**
  115. * \fn void G_free_fvector (float *v)
  116. *
  117. * \brief Vector memory deallocation.
  118. *
  119. * Deallocate a vector (array) of floats.
  120. *
  121. * \param[in,out] v vector to free
  122. * \return void
  123. */
  124. void G_free_fvector(float *v)
  125. {
  126. G_free(v);
  127. v = NULL;
  128. return;
  129. }
  130. /**
  131. * \fn void G_free_matrix (double **m)
  132. *
  133. * \brief Matrix memory deallocation.
  134. *
  135. * Deallocate a matrix of doubles.
  136. *
  137. * \param[in,out] m matrix to free
  138. * \return void
  139. */
  140. void G_free_matrix(double **m)
  141. {
  142. G_free(m[0]);
  143. G_free(m);
  144. m = NULL;
  145. return;
  146. }
  147. /**
  148. * \fn void G_free_fmatrix (float **m)
  149. *
  150. * \brief Floating point matrix memory deallocation.
  151. *
  152. * Deallocate a matrix of floats.
  153. *
  154. * \param[in,out] m matrix to free
  155. * \return void
  156. */
  157. void G_free_fmatrix(float **m)
  158. {
  159. G_free(m[0]);
  160. G_free(m);
  161. m = NULL;
  162. return;
  163. }