pagein.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * \file pagein.c
  3. *
  4. * \brief Segment page-in 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 <stdio.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <grass/segment.h>
  18. #include "rbtree.h"
  19. /**
  20. * \fn int segment_pagein (SEGMENT *SEG, int n)
  21. *
  22. * \brief Segment pagein.
  23. *
  24. * Finds <b>n</b> in the segment file, <b>seg</b>, and selects it as the
  25. * current segment.
  26. *
  27. * \param[in] seg segment
  28. * \param[in] n segment number
  29. * \return 1 if successful
  30. * \return -1 if unable to seek or read segment file
  31. */
  32. int segment_pagein(SEGMENT * SEG, int n)
  33. {
  34. int cur;
  35. int read_result;
  36. SEGID *seg_found, seg_search;
  37. /* is n the current segment? */
  38. if (n == SEG->scb[SEG->cur].n)
  39. return SEG->cur;
  40. /* search the in memory segments */
  41. seg_search.i = 0;
  42. seg_search.n = n;
  43. seg_found = rbtree_find(SEG->loaded, &seg_search);
  44. if (seg_found) {
  45. cur = seg_found->i;
  46. if (SEG->scb[cur].age != SEG->youngest) {
  47. /* splice out */
  48. SEG->scb[cur].age->younger->older = SEG->scb[cur].age->older;
  49. SEG->scb[cur].age->older->younger = SEG->scb[cur].age->younger;
  50. /* splice in */
  51. SEG->scb[cur].age->younger = SEG->youngest->younger;
  52. SEG->scb[cur].age->older = SEG->youngest;
  53. SEG->scb[cur].age->older->younger = SEG->scb[cur].age;
  54. SEG->scb[cur].age->younger->older = SEG->scb[cur].age;
  55. /* make it youngest */
  56. SEG->youngest = SEG->scb[cur].age;
  57. }
  58. return SEG->cur = cur;
  59. }
  60. /* find a slot to use to hold segment */
  61. if (SEG->nfreeslots) { /* any free slots left ? */
  62. cur = SEG->freeslot[--SEG->nfreeslots];
  63. }
  64. else { /* find oldest segment */
  65. SEG->oldest = SEG->oldest->younger;
  66. cur = SEG->oldest->cur;
  67. SEG->oldest->cur = -1;
  68. SEG->scb[cur].age = NULL;
  69. }
  70. /* if slot is used, write it out, if dirty */
  71. if (SEG->scb[cur].n >= 0 && SEG->scb[cur].dirty) {
  72. if (segment_pageout(SEG, cur) < 0)
  73. return -1;
  74. }
  75. if (SEG->scb[cur].n >= 0) {
  76. seg_search.n = SEG->scb[cur].n;
  77. if (rbtree_remove(SEG->loaded, &seg_search) == 0)
  78. G_fatal_error("could not remove segment");
  79. seg_search.n = n;
  80. }
  81. /* read in the segment */
  82. SEG->scb[cur].n = n;
  83. SEG->scb[cur].dirty = 0;
  84. segment_seek(SEG, SEG->scb[cur].n, 0);
  85. read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
  86. if (read_result != SEG->size) {
  87. G_debug(2, "segment_pagein: read_result=%d SEG->size=%d",
  88. read_result, SEG->size);
  89. if (read_result < 0)
  90. G_warning("segment_pagein: %s", strerror(errno));
  91. else if (read_result == 0)
  92. G_warning("segment_pagein: read EOF");
  93. else
  94. G_warning
  95. ("segment_pagein: short count during read(), got %d, expected %d",
  96. read_result, SEG->size);
  97. return -1;
  98. }
  99. if (cur < 0 || n < 0)
  100. G_fatal_error("segment not loaded");
  101. /* remember loaded segment */
  102. seg_search.i = cur;
  103. if (rbtree_insert(SEG->loaded, &seg_search) == 0)
  104. G_fatal_error("could not insert segment");
  105. /* make it youngest segment */
  106. SEG->youngest = SEG->youngest->younger;
  107. SEG->scb[cur].age = SEG->youngest;
  108. SEG->youngest->cur = cur;
  109. return SEG->cur = cur;
  110. }