setup.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #include "rbtree.h"
  19. /**
  20. * \fn int segment_setup (SEGMENT *SEG)
  21. *
  22. * \brief Setup segment.
  23. *
  24. * <b>SEG</b> must have the following parms set:
  25. * fd (open for read and write), nrows, ncols, srows, scols, len, nseg
  26. *
  27. * \param[in,out] SEG segment
  28. * \return 1 if successful
  29. * \return -1 if illegal parameters are passed in <b>SEG</b>
  30. * \return -2 if unable to allocate memory
  31. */
  32. int segment_setup(SEGMENT * SEG)
  33. {
  34. int i;
  35. int seg_exp;
  36. SEG->open = 0;
  37. if (SEG->nrows <= 0 || SEG->ncols <= 0
  38. || SEG->srows <= 0 || SEG->scols <= 0
  39. || SEG->len <= 0 || SEG->nseg <= 0) {
  40. G_warning("segment_setup: illegal segment file parameters");
  41. return -1;
  42. }
  43. /* This is close to the beginning of the file, so doesn't need to be an off_t */
  44. SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
  45. SEG->spr = SEG->ncols / SEG->scols;
  46. SEG->spill = SEG->ncols % SEG->scols;
  47. if (SEG->spill)
  48. SEG->spr++;
  49. /* fast address */
  50. SEG->slow_adrs = 1;
  51. seg_exp = 0;
  52. while (SEG->scols - (1 << seg_exp) > 0)
  53. seg_exp++;
  54. if (SEG->scols - (1 << seg_exp) == 0) {
  55. SEG->scolbits = seg_exp;
  56. seg_exp = 0;
  57. while (SEG->srows - (1 << seg_exp) > 0)
  58. seg_exp++;
  59. if (SEG->srows - (1 << seg_exp) == 0) {
  60. SEG->srowbits = seg_exp;
  61. SEG->segbits = SEG->srowbits + SEG->scolbits;
  62. SEG->slow_adrs = 0;
  63. G_debug(1, "segment lib: fast address activated");
  64. }
  65. }
  66. /* fast seek */
  67. SEG->slow_seek = 1;
  68. if (SEG->slow_adrs == 0) {
  69. seg_exp = 0;
  70. while (SEG->len - (1 << seg_exp) > 0)
  71. seg_exp++;
  72. if (SEG->len - (1 << seg_exp) == 0) {
  73. SEG->lenbits = seg_exp;
  74. SEG->sizebits = SEG->segbits + SEG->lenbits;
  75. SEG->slow_seek = 0;
  76. G_debug(1, "segment lib: fast seek activated");
  77. }
  78. }
  79. /* adjust number of open segments if larger than number of total segments */
  80. if (SEG->nseg > SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows)) {
  81. G_warning("segment: reducing number of open segments from %d to %d",
  82. SEG->nseg,
  83. SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows));
  84. SEG->nseg = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
  85. }
  86. if ((SEG->scb =
  87. (struct SEGMENT_SCB *)G_malloc(SEG->nseg *
  88. sizeof(struct SEGMENT_SCB))) == NULL)
  89. return -2;
  90. if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
  91. return -2;
  92. if ((SEG->agequeue =
  93. (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
  94. return -2;
  95. SEG->srowscols = SEG->srows * SEG->scols;
  96. SEG->size = SEG->srowscols * SEG->len;
  97. for (i = 0; i < SEG->nseg; i++) {
  98. if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
  99. return -2;
  100. SEG->scb[i].n = -1; /* mark free */
  101. SEG->scb[i].dirty = 0;
  102. SEG->scb[i].age = NULL;
  103. SEG->freeslot[i] = i;
  104. SEG->agequeue[i].cur = -1;
  105. if (i > 0) {
  106. SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
  107. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  108. }
  109. else if (i == 0) {
  110. SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
  111. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  112. }
  113. }
  114. SEG->agequeue[SEG->nseg].cur = -1;
  115. SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
  116. SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
  117. SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
  118. SEG->nfreeslots = SEG->nseg;
  119. SEG->cur = 0;
  120. SEG->open = 1;
  121. SEG->loaded = rbtree_create(segment_compare, sizeof(SEGID));
  122. return 1;
  123. }
  124. int segment_compare(const void *sega, const void *segb)
  125. {
  126. SEGID *a = (SEGID *) sega;
  127. SEGID *b = (SEGID *) segb;
  128. return a->n < b->n ? -1 : (a->n > b->n);
  129. /* short version of
  130. if (a->n > b->n)
  131. return 1;
  132. else if (a->n < b->n)
  133. return -1;
  134. return 0;
  135. */
  136. }