pagein.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <grass/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) {
  62. /* use oldest segment */
  63. SEG->oldest = SEG->oldest->younger;
  64. cur = SEG->oldest->cur;
  65. SEG->oldest->cur = -1;
  66. /* unload segment (remove from search tree) */
  67. if (SEG->scb[cur].n >= 0) {
  68. seg_search.n = SEG->scb[cur].n;
  69. if (rbtree_remove(SEG->loaded, &seg_search) == 0)
  70. G_fatal_error("could not remove segment");
  71. seg_search.n = n;
  72. /* write it out if dirty */
  73. if (SEG->scb[cur].dirty) {
  74. if (segment_pageout(SEG, cur) < 0)
  75. return -1;
  76. }
  77. }
  78. }
  79. else {
  80. /* free slots left */
  81. cur = SEG->freeslot[--SEG->nfreeslots];
  82. }
  83. /* read in the segment */
  84. SEG->scb[cur].n = n;
  85. SEG->scb[cur].dirty = 0;
  86. segment_seek(SEG, SEG->scb[cur].n, 0);
  87. read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
  88. if (read_result != SEG->size) {
  89. G_debug(2, "segment_pagein: read_result=%d SEG->size=%d",
  90. read_result, SEG->size);
  91. if (read_result < 0)
  92. G_warning("segment_pagein: %s", strerror(errno));
  93. else if (read_result == 0)
  94. G_warning("segment_pagein: read EOF");
  95. else
  96. G_warning
  97. ("segment_pagein: short count during read(), got %d, expected %d",
  98. read_result, SEG->size);
  99. return -1;
  100. }
  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. }