setup.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * \file lib/segment/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 "local_proto.h"
  18. /**
  19. * \brief Internal use only
  20. *
  21. * 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 seg_setup(SEGMENT * SEG)
  32. {
  33. int i;
  34. int seg_exp, n_total_segs;
  35. SEG->open = 0;
  36. SEG->cache = NULL;
  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->fast_adrs = 0;
  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->fast_adrs = 1;
  63. G_debug(1, "Segment setup: fast address activated");
  64. }
  65. }
  66. if (SEG->fast_adrs)
  67. SEG->address = seg_address_fast;
  68. else
  69. SEG->address = seg_address_slow;
  70. /* fast seek */
  71. SEG->fast_seek = 0;
  72. if (SEG->fast_adrs == 1) {
  73. seg_exp = 0;
  74. while (SEG->len - (1 << seg_exp) > 0)
  75. seg_exp++;
  76. if (SEG->len - (1 << seg_exp) == 0) {
  77. SEG->lenbits = seg_exp;
  78. SEG->sizebits = SEG->segbits + SEG->lenbits;
  79. SEG->fast_seek = 1;
  80. G_debug(1, "Segment setup: fast seek activated");
  81. }
  82. }
  83. if (SEG->fast_seek)
  84. SEG->seek = seg_seek_fast;
  85. else
  86. SEG->seek = seg_seek_slow;
  87. /* adjust number of open segments if larger than number of total segments */
  88. n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
  89. if (SEG->nseg > n_total_segs) {
  90. G_debug(1, "Segment setup: reducing number of open segments from %d to %d",
  91. SEG->nseg, n_total_segs);
  92. SEG->nseg = n_total_segs;
  93. }
  94. if ((SEG->scb =
  95. (struct scb *)G_malloc(SEG->nseg *
  96. sizeof(struct scb))) == NULL)
  97. return -2;
  98. if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
  99. return -2;
  100. if ((SEG->agequeue =
  101. (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
  102. return -2;
  103. SEG->srowscols = SEG->srows * SEG->scols;
  104. SEG->size = SEG->srowscols * SEG->len;
  105. for (i = 0; i < SEG->nseg; i++) {
  106. if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
  107. return -2;
  108. SEG->scb[i].n = -1; /* mark free */
  109. SEG->scb[i].dirty = 0;
  110. SEG->scb[i].age = NULL;
  111. SEG->freeslot[i] = i;
  112. SEG->agequeue[i].cur = -1;
  113. if (i > 0) {
  114. SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
  115. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  116. }
  117. else if (i == 0) {
  118. SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
  119. SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
  120. }
  121. }
  122. SEG->agequeue[SEG->nseg].cur = -1;
  123. SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
  124. SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
  125. SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
  126. SEG->nfreeslots = SEG->nseg;
  127. SEG->cur = 0;
  128. SEG->open = 1;
  129. /* index for each segment, same like cache of r.proj */
  130. /* alternative using less memory: RB Tree */
  131. /* SEG->loaded = rbtree_create(cmp, sizeof(SEGID)); */
  132. /* SEG->loaded = NULL; */
  133. SEG->load_idx = G_malloc(n_total_segs * sizeof(int));
  134. for (i = 0; i < n_total_segs; i++)
  135. SEG->load_idx[i] = -1;
  136. return 1;
  137. }