pagein.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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-2006
  12. */
  13. #include <stdio.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. #include <grass/segment.h>
  18. static int segment_select(SEGMENT *, int);
  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 age;
  35. int cur;
  36. int i;
  37. int read_result;
  38. /* is n the current segment? */
  39. if (n == SEG->scb[SEG->cur].n)
  40. return SEG->cur;
  41. /* search the in memory segments */
  42. for (i = 0; i < SEG->nseg; i++)
  43. if (n == SEG->scb[i].n)
  44. return segment_select(SEG, i);
  45. /* find a slot to use to hold segment */
  46. age = 0;
  47. cur = 0;
  48. for (i = 0; i < SEG->nseg; i++)
  49. if (SEG->scb[i].n < 0) { /* free slot */
  50. cur = i;
  51. break;
  52. }
  53. else if (age < SEG->scb[i].age) { /* find oldest segment */
  54. cur = i;
  55. age = SEG->scb[i].age;
  56. }
  57. /* if slot is used, write it out, if dirty */
  58. if (SEG->scb[cur].n >= 0 && SEG->scb[cur].dirty)
  59. if (segment_pageout(SEG, cur) < 0)
  60. return -1;
  61. /* read in the segment */
  62. SEG->scb[cur].n = n;
  63. SEG->scb[cur].dirty = 0;
  64. segment_seek(SEG, SEG->scb[cur].n, 0);
  65. read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
  66. if (read_result != SEG->size) {
  67. G_debug(2, "segment_pagein: read_result=%d SEG->size=%d",
  68. read_result, SEG->size);
  69. if (read_result < 0)
  70. G_warning("segment_pagein: %s", strerror(errno));
  71. else if (read_result == 0)
  72. G_warning("segment_pagein: read EOF");
  73. else
  74. G_warning
  75. ("segment_pagein: short count during read(), got %d, expected %d",
  76. read_result, SEG->size);
  77. return -1;
  78. }
  79. return segment_select(SEG, cur);
  80. }
  81. static int segment_select(SEGMENT * SEG, int n)
  82. {
  83. int i;
  84. SEG->scb[n].age = 0;
  85. for (i = 0; i < SEG->nseg; i++)
  86. SEG->scb[i].age++;
  87. return SEG->cur = n;
  88. }