setup.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. 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. if (SEG->scols - pow(2, (log(SEG->scols) / log(2))) == 0) {
  51. if (SEG->srows - pow(2, (log(SEG->srows) / log(2))) == 0) {
  52. SEG->scolbits = log(SEG->scols) / log(2) + 0.1;
  53. SEG->srowbits = log(SEG->srows) / log(2) + 0.1;
  54. SEG->segbits = SEG->srowbits + SEG->scolbits;
  55. SEG->slow_adrs = 0;
  56. G_debug(1, "segment lib: fast address activated");
  57. }
  58. }
  59. /* fast seek */
  60. SEG->slow_seek = 1;
  61. if (SEG->slow_adrs == 0) {
  62. if (SEG->len - pow(2, (log(SEG->len) / log(2))) == 0) {
  63. SEG->lenbits = log(SEG->len) / log(2) + 0.1;
  64. SEG->sizebits = SEG->segbits + SEG->lenbits;
  65. SEG->slow_seek = 0;
  66. G_debug(1, "segment lib: fast seek activated");
  67. }
  68. }
  69. /* adjust number of open segments if larger than number of total segments */
  70. if (SEG->nseg > SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows)) {
  71. G_warning("segment: reducing number of open segments from %d to %d",
  72. SEG->nseg,
  73. SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows));
  74. SEG->nseg = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
  75. }
  76. if ((SEG->scb =
  77. (struct SEGMENT_SCB *)G_malloc(SEG->nseg *
  78. sizeof(struct SEGMENT_SCB))) == NULL)
  79. return -2;
  80. if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
  81. return -2;
  82. if ((SEG->agequeue =
  83. (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
  84. return -2;
  85. SEG->srowscols = SEG->srows * SEG->scols;
  86. SEG->size = SEG->srowscols * SEG->len;
  87. for (i = 0; i < SEG->nseg; i++) {
  88. if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
  89. return -2;
  90. SEG->scb[i].n = -1; /* mark free */
  91. SEG->scb[i].dirty = 0;
  92. SEG->scb[i].age = NULL;
  93. SEG->freeslot[i] = i;
  94. SEG->agequeue[i].cur = -1;
  95. if (i > 0) {
  96. SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
  97. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  98. }
  99. else if (i == 0) {
  100. SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
  101. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  102. }
  103. }
  104. SEG->agequeue[SEG->nseg].cur = -1;
  105. SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
  106. SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
  107. SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
  108. SEG->nfreeslots = SEG->nseg;
  109. SEG->cur = 0;
  110. SEG->open = 1;
  111. SEG->loaded = rbtree_create(segment_compare, sizeof(SEGID));
  112. return 1;
  113. }
  114. int segment_compare(const void *sega, const void *segb)
  115. {
  116. SEGID *a = (SEGID *) sega;
  117. SEGID *b = (SEGID *) segb;
  118. return a->n < b->n ? -1 : (a->n > b->n);
  119. /* short version of
  120. if (a->n > b->n)
  121. return 1;
  122. else if (a->n < b->n)
  123. return -1;
  124. return 0;
  125. */
  126. }