setup.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * \file setup.c
  3. *
  4. * \brief Segment setup routines.
  5. *
  6. * This program is free software under the GNU General Public License
  7. * (>=v2). Read the file COPYING that comes with GRASS for details.
  8. *
  9. * \author GRASS GIS Development Team
  10. *
  11. * \date 2005-2009
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include <grass/gis.h>
  17. #include <grass/segment.h>
  18. /**
  19. * \fn int segment_setup (SEGMENT *SEG)
  20. *
  21. * \brief Setup segment.
  22. *
  23. * <b>SEG</b> must have the following parms set:
  24. * fd (open for read and write), nrows, ncols, srows, scols, len, nseg
  25. *
  26. * \param[in,out] SEG segment
  27. * \return 1 if successful
  28. * \return -1 if illegal parameters are passed in <b>SEG</b>
  29. * \return -2 if unable to allocate memory
  30. */
  31. int segment_setup(SEGMENT * SEG)
  32. {
  33. int i;
  34. int seg_exp, n_total_segs;
  35. SEG->open = 0;
  36. if (SEG->nrows <= 0 || SEG->ncols <= 0
  37. || SEG->srows <= 0 || SEG->scols <= 0
  38. || SEG->len <= 0 || SEG->nseg <= 0) {
  39. G_warning("segment_setup: illegal segment file parameters");
  40. return -1;
  41. }
  42. /* This is close to the beginning of the file, so doesn't need to be an off_t */
  43. SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
  44. SEG->spr = SEG->ncols / SEG->scols;
  45. SEG->spill = SEG->ncols % SEG->scols;
  46. if (SEG->spill)
  47. SEG->spr++;
  48. /* fast address */
  49. SEG->slow_adrs = 1;
  50. seg_exp = 0;
  51. while (SEG->scols - (1 << seg_exp) > 0)
  52. seg_exp++;
  53. if (SEG->scols - (1 << seg_exp) == 0) {
  54. SEG->scolbits = seg_exp;
  55. seg_exp = 0;
  56. while (SEG->srows - (1 << seg_exp) > 0)
  57. seg_exp++;
  58. if (SEG->srows - (1 << seg_exp) == 0) {
  59. SEG->srowbits = seg_exp;
  60. SEG->segbits = SEG->srowbits + SEG->scolbits;
  61. SEG->slow_adrs = 0;
  62. G_debug(1, "segment lib: fast address activated");
  63. }
  64. }
  65. /* fast seek */
  66. SEG->slow_seek = 1;
  67. if (SEG->slow_adrs == 0) {
  68. seg_exp = 0;
  69. while (SEG->len - (1 << seg_exp) > 0)
  70. seg_exp++;
  71. if (SEG->len - (1 << seg_exp) == 0) {
  72. SEG->lenbits = seg_exp;
  73. SEG->sizebits = SEG->segbits + SEG->lenbits;
  74. SEG->slow_seek = 0;
  75. G_debug(1, "segment lib: fast seek activated");
  76. }
  77. }
  78. /* adjust number of open segments if larger than number of total segments */
  79. n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
  80. if (SEG->nseg > n_total_segs) {
  81. G_debug(1, "segment: reducing number of open segments from %d to %d",
  82. SEG->nseg, n_total_segs);
  83. SEG->nseg = n_total_segs;
  84. }
  85. if ((SEG->scb =
  86. (struct SEGMENT_SCB *)G_malloc(SEG->nseg *
  87. sizeof(struct SEGMENT_SCB))) == NULL)
  88. return -2;
  89. if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
  90. return -2;
  91. if ((SEG->agequeue =
  92. (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
  93. return -2;
  94. SEG->srowscols = SEG->srows * SEG->scols;
  95. SEG->size = SEG->srowscols * SEG->len;
  96. for (i = 0; i < SEG->nseg; i++) {
  97. if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
  98. return -2;
  99. SEG->scb[i].n = -1; /* mark free */
  100. SEG->scb[i].dirty = 0;
  101. SEG->scb[i].age = NULL;
  102. SEG->freeslot[i] = i;
  103. SEG->agequeue[i].cur = -1;
  104. if (i > 0) {
  105. SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
  106. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  107. }
  108. else if (i == 0) {
  109. SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
  110. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  111. }
  112. }
  113. SEG->agequeue[SEG->nseg].cur = -1;
  114. SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
  115. SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
  116. SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
  117. SEG->nfreeslots = SEG->nseg;
  118. SEG->cur = 0;
  119. SEG->open = 1;
  120. /* SEG->loaded = rbtree_create(segment_compare, sizeof(SEGID)); */
  121. /* SEG->loaded = NULL; */
  122. /* index for each segment, same like cache of r.proj */
  123. SEG->load_idx = G_malloc(n_total_segs * sizeof(int));
  124. for (i = 0; i < n_total_segs; i++)
  125. SEG->load_idx[i] = -1;
  126. return 1;
  127. }