mem.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. ** Original Algorithm: H. Mitasova, L. Mitas, J. Hofierka, M. Zlocha
  3. ** GRASS Implementation: J. Caplan, M. Ruesink 1995
  4. **
  5. ** US Army Construction Engineering Research Lab, University of Illinois
  6. **
  7. ** Copyright M. Ruesink, J. Caplan, H. Mitasova, L. Mitas, J. Hofierka,
  8. ** M. Zlocha 1995
  9. **
  10. **This program is free software; you can redistribute it and/or
  11. **modify it under the terms of the GNU General Public License
  12. **as published by the Free Software Foundation; either version 2
  13. **of the License, or (at your option) any later version.
  14. **
  15. **This program is distributed in the hope that it will be useful,
  16. **but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. **MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. **GNU General Public License for more details.
  19. **
  20. **You should have received a copy of the GNU General Public License
  21. **along with this program; if not, write to the Free Software
  22. **Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. **
  24. */
  25. #include <grass/gis.h>
  26. #include <grass/raster.h>
  27. #include <grass/glocale.h>
  28. #include "r.flow.h"
  29. #include "io.h"
  30. #include "mem.h"
  31. /************************** MEMORY MGMT/ACCESS **************************/
  32. void put_row_seg(layer l, int row)
  33. {
  34. if (Segment_put_row(l.seg, l.buf[row] - l.col_offset,
  35. row + l.row_offset) < 1)
  36. G_fatal_error(_("Unable to write segment file for %s"), l.name);
  37. }
  38. void allocate_heap(void)
  39. {
  40. int row;
  41. G_debug(1, "Allocating memory: elevation");
  42. /* 3 elevation buffers needed for precomputing aspects */
  43. el.buf =
  44. (DCELL **) G_calloc(region.rows + el.row_offset * 2 + 3,
  45. sizeof(DCELL *));
  46. for (row = 0; row < 3; row++)
  47. el.buf[row] = ((DCELL *) G_calloc(region.cols + el.col_offset * 2,
  48. sizeof(DCELL))) + el.row_offset;
  49. for (row = 3; row <= region.rows + el.row_offset; row++)
  50. el.buf[row] = parm.seg ? el.buf[row % 3] :
  51. ((DCELL *) G_calloc(region.cols + el.col_offset * 2,
  52. sizeof(DCELL))) + el.row_offset;
  53. el.buf += el.col_offset;
  54. if (parm.seg) {
  55. G_debug(1, "Allocating memory: segment");
  56. el.seg = (SEGMENT *) G_malloc(sizeof(SEGMENT));
  57. Segment_init(el.seg, el.sfd, SEGSINMEM);
  58. as.seg = (SEGMENT *) G_malloc(sizeof(SEGMENT));
  59. Segment_init(as.seg, as.sfd, SEGSINMEM);
  60. if (parm.dsout) {
  61. ds.seg = (SEGMENT *) G_malloc(sizeof(SEGMENT));
  62. Segment_init(ds.seg, ds.sfd, SEGSINMEM);
  63. }
  64. }
  65. if (!parm.mem) {
  66. G_debug(1, "Allocating memory: aspect");
  67. as.buf = (DCELL **) G_calloc(region.rows, sizeof(DCELL *));
  68. as.buf[0] = (DCELL *) Rast_allocate_buf(DCELL_TYPE);
  69. for (row = 0; row < region.rows; row++)
  70. as.buf[row] = parm.seg ?
  71. as.buf[0] : (DCELL *) Rast_allocate_buf(DCELL_TYPE);
  72. }
  73. if (parm.barin) {
  74. G_debug(1, "Allocating memory: barrier");
  75. bitbar = BM_create(region.cols, region.rows);
  76. }
  77. if (parm.dsout) {
  78. G_debug(1, "Allocating memory: density");
  79. ds.buf = (DCELL **) G_calloc(region.rows, sizeof(DCELL *));
  80. ds.buf[0] = (DCELL *) Rast_allocate_buf(DCELL_TYPE);
  81. for (row = 0; row < region.rows; row++)
  82. ds.buf[row] = parm.seg ?
  83. ds.buf[0] : (DCELL *) Rast_allocate_buf(DCELL_TYPE);
  84. }
  85. if (parm.flout) {
  86. G_debug(1, "Allocating memory: flowline header");
  87. Vect_hist_command(&fl);
  88. }
  89. G_debug(1, "Allocating memory: e/w distances");
  90. ew_dist = (double *)G_calloc(region.rows, sizeof(double));
  91. G_debug(1, "Allocating memory: quantization tolerances");
  92. epsilon[HORIZ] = (double *)G_calloc(region.rows, sizeof(double));
  93. epsilon[VERT] = (double *)G_calloc(region.rows, sizeof(double));
  94. return;
  95. }
  96. void deallocate_heap(void)
  97. {
  98. int row;
  99. G_debug(1, "De-allocating memory");
  100. if (parm.barin)
  101. BM_destroy(bitbar);
  102. G_free(el.buf[-1] - 1);
  103. if (parm.seg) {
  104. Segment_release(el.seg);
  105. if (!parm.mem)
  106. Segment_release(as.seg);
  107. if (parm.dsout)
  108. Segment_release(ds.seg);
  109. }
  110. else {
  111. G_free(el.buf[region.rows] - 1);
  112. for (row = 0; row < region.rows; row++)
  113. G_free(el.buf[row] - 1);
  114. }
  115. G_free(--el.buf);
  116. if (!parm.mem) {
  117. for (row = 0; row < (parm.seg ? 1 : region.rows); row++)
  118. G_free(as.buf[row]);
  119. G_free(as.buf);
  120. }
  121. G_free(ew_dist);
  122. }