pagein.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. {
  51. cur = i;
  52. break;
  53. }
  54. else if (age < SEG->scb[i].age) /* find oldest segment */
  55. {
  56. cur = i;
  57. age = SEG->scb[i].age;
  58. }
  59. /* if slot is used, write it out, if dirty */
  60. if (SEG->scb[cur].n >= 0 && SEG->scb[cur].dirty)
  61. if(segment_pageout (SEG, cur) < 0)
  62. return -1;
  63. /* read in the segment */
  64. SEG->scb[cur].n = n;
  65. SEG->scb[cur].dirty = 0;
  66. segment_seek (SEG, SEG->scb[cur].n, 0);
  67. read_result = read (SEG->fd, SEG->scb[cur].buf, SEG->size);
  68. if (read_result != SEG->size)
  69. {
  70. G_debug(2, "segment_pagein: read_result=%d SEG->size=%d", read_result, SEG->size);
  71. if(read_result < 0)
  72. G_warning ("segment_pagein: %s", strerror(errno));
  73. else if(read_result == 0)
  74. G_warning ("segment_pagein: read EOF");
  75. else
  76. G_warning ("segment_pagein: short count during read(), got %d, expected %d",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. }