GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
pagein.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <grass/segment.h>
20 
21 
22 static int segment_select(SEGMENT *, int);
23 
24 
39 int segment_pagein(SEGMENT * SEG, int n)
40 {
41  int age;
42  int cur;
43  int i;
44  int read_result;
45 
46  /* is n the current segment? */
47  if (n == SEG->scb[SEG->cur].n)
48  return SEG->cur;
49 
50  /* search the in memory segments */
51  for (i = 0; i < SEG->nseg; i++)
52  if (n == SEG->scb[i].n)
53  return segment_select(SEG, i);
54 
55  /* find a slot to use to hold segment */
56  age = 0;
57  cur = 0;
58  for (i = 0; i < SEG->nseg; i++)
59  if (SEG->scb[i].n < 0) { /* free slot */
60  cur = i;
61  break;
62  }
63  else if (age < SEG->scb[i].age) { /* find oldest segment */
64  cur = i;
65  age = SEG->scb[i].age;
66  }
67 
68  /* if slot is used, write it out, if dirty */
69  if (SEG->scb[cur].n >= 0 && SEG->scb[cur].dirty)
70  if (segment_pageout(SEG, cur) < 0)
71  return -1;
72 
73  /* read in the segment */
74  SEG->scb[cur].n = n;
75  SEG->scb[cur].dirty = 0;
76  segment_seek(SEG, SEG->scb[cur].n, 0);
77 
78  read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
79  if (read_result != SEG->size) {
80  G_debug(2, "segment_pagein: read_result=%d SEG->size=%d",
81  read_result, SEG->size);
82 
83  if (read_result < 0)
84  G_warning("segment_pagein: %s", strerror(errno));
85  else if (read_result == 0)
86  G_warning("segment_pagein: read EOF");
87  else
88  G_warning
89  ("segment_pagein: short count during read(), got %d, expected %d",
90  read_result, SEG->size);
91 
92  return -1;
93  }
94 
95  return segment_select(SEG, cur);
96 }
97 
98 
99 static int segment_select(SEGMENT * SEG, int n)
100 {
101  int i;
102 
103  SEG->scb[n].age = 0;
104  for (i = 0; i < SEG->nseg; i++)
105  SEG->scb[i].age++;
106 
107  return SEG->cur = n;
108 }
int segment_pagein(SEGMENT *SEG, int n)
Segment pagein.
Definition: pagein.c:39
int segment_seek(const SEGMENT *SEG, int n, int index)
Definition: segment/seek.c:37
int segment_pageout(SEGMENT *SEG, int i)
Pages segment to disk.
Definition: pageout.c:36
G_warning("category support for [%s] in mapset [%s] %s", name, mapset, type)
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int errno
int n
Definition: dataquad.c:291