pagein.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/gis.h>
  18. #include "local_proto.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. /* is n the current segment? */
  37. if (n == SEG->scb[SEG->cur].n)
  38. return SEG->cur;
  39. /* segment n is in memory ? */
  40. if (SEG->load_idx[n] >= 0) {
  41. cur = SEG->load_idx[n];
  42. if (SEG->scb[cur].age != SEG->youngest) {
  43. /* splice out */
  44. SEG->scb[cur].age->younger->older = SEG->scb[cur].age->older;
  45. SEG->scb[cur].age->older->younger = SEG->scb[cur].age->younger;
  46. /* splice in */
  47. SEG->scb[cur].age->younger = SEG->youngest->younger;
  48. SEG->scb[cur].age->older = SEG->youngest;
  49. SEG->scb[cur].age->older->younger = SEG->scb[cur].age;
  50. SEG->scb[cur].age->younger->older = SEG->scb[cur].age;
  51. /* make it youngest */
  52. SEG->youngest = SEG->scb[cur].age;
  53. }
  54. return SEG->cur = cur;
  55. }
  56. /* find a slot to use to hold segment */
  57. if (!SEG->nfreeslots) {
  58. /* use oldest segment */
  59. SEG->oldest = SEG->oldest->younger;
  60. cur = SEG->oldest->cur;
  61. SEG->oldest->cur = -1;
  62. /* unload segment */
  63. if (SEG->scb[cur].n >= 0) {
  64. SEG->load_idx[SEG->scb[cur].n] = -1;
  65. /* write it out if dirty */
  66. if (SEG->scb[cur].dirty) {
  67. if (Segment_pageout(SEG, cur) < 0)
  68. return -1;
  69. }
  70. }
  71. }
  72. else {
  73. /* free slots left */
  74. cur = SEG->freeslot[--SEG->nfreeslots];
  75. }
  76. /* read in the segment */
  77. SEG->scb[cur].n = n;
  78. SEG->scb[cur].dirty = 0;
  79. SEG->Segment_seek(SEG, SEG->scb[cur].n, 0);
  80. read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
  81. if (read_result != SEG->size) {
  82. G_debug(2, "Segment_pagein: read_result=%d SEG->size=%d",
  83. read_result, SEG->size);
  84. if (read_result < 0)
  85. G_warning("Segment_pagein: %s", strerror(errno));
  86. else if (read_result == 0)
  87. G_warning("Segment_pagein: read EOF");
  88. else
  89. G_warning
  90. ("Segment_pagein: short count during read(), got %d, expected %d",
  91. read_result, SEG->size);
  92. return -1;
  93. }
  94. /* add loaded segment to index */
  95. SEG->load_idx[n] = cur;
  96. /* make it youngest segment */
  97. SEG->youngest = SEG->youngest->younger;
  98. SEG->scb[cur].age = SEG->youngest;
  99. SEG->youngest->cur = cur;
  100. return SEG->cur = cur;
  101. }