setup.c 4.1 KB

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