GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
pagein.c
Go to the documentation of this file.
1 
2 /**
3  * \file pagein.c
4  *
5  * \brief Segment page-in routines.
6  *
7  * This program is free software under the GNU General Public License
8  * (>=v2). Read the file COPYING that comes with GRASS for details.
9  *
10  * \author GRASS GIS Development Team
11  *
12  * \date 2005-2009
13  */
14 
15 #include <stdio.h>
16 #include <unistd.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <grass/gis.h>
20 #include "local_proto.h"
21 
22 
23 /**
24  * \brief Internal use only
25  *
26  * Segment pagein.
27  *
28  * Finds <b>n</b> in the segment file, <b>seg</b>, and selects it as the
29  * current segment.
30  *
31  * \param[in] SEG segment
32  * \param[in] n segment number
33  * \return 1 if successful
34  * \return -1 if unable to seek or read segment file
35  */
36 
37 int seg_pagein(SEGMENT * SEG, int n)
38 {
39  int cur;
40  int read_result;
41 
42  /* is n the current segment? */
43  if (n == SEG->scb[SEG->cur].n)
44  return SEG->cur;
45 
46  /* segment n is in memory ? */
47 
48  if (SEG->load_idx[n] >= 0) {
49  cur = SEG->load_idx[n];
50 
51  if (SEG->scb[cur].age != SEG->youngest) {
52  /* splice out */
53  SEG->scb[cur].age->younger->older = SEG->scb[cur].age->older;
54  SEG->scb[cur].age->older->younger = SEG->scb[cur].age->younger;
55  /* splice in */
56  SEG->scb[cur].age->younger = SEG->youngest->younger;
57  SEG->scb[cur].age->older = SEG->youngest;
58  SEG->scb[cur].age->older->younger = SEG->scb[cur].age;
59  SEG->scb[cur].age->younger->older = SEG->scb[cur].age;
60  /* make it youngest */
61  SEG->youngest = SEG->scb[cur].age;
62  }
63 
64  return SEG->cur = cur;
65  }
66 
67  /* find a slot to use to hold segment */
68  if (!SEG->nfreeslots) {
69  /* use oldest segment */
70  SEG->oldest = SEG->oldest->younger;
71  cur = SEG->oldest->cur;
72  SEG->oldest->cur = -1;
73 
74  /* unload segment */
75  if (SEG->scb[cur].n >= 0) {
76  SEG->load_idx[SEG->scb[cur].n] = -1;
77 
78  /* write it out if dirty */
79  if (SEG->scb[cur].dirty) {
80  if (seg_pageout(SEG, cur) < 0)
81  return -1;
82  }
83  }
84  }
85  else {
86  /* free slots left */
87  cur = SEG->freeslot[--SEG->nfreeslots];
88  }
89 
90  /* read in the segment */
91  SEG->scb[cur].n = n;
92  SEG->scb[cur].dirty = 0;
93  SEG->seek(SEG, SEG->scb[cur].n, 0);
94 
95  read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
96 
97  if (read_result == 0) {
98  /* this can happen if the file was not zero-filled,
99  * i.e. formatted with Segment_format_nofill() or
100  * Segment_format() used lseek for file initialization */
101  G_debug(1, "Segment pagein: zero read");
102  memset(SEG->scb[cur].buf, 0, SEG->size);
103  }
104  else if (read_result != SEG->size) {
105  G_debug(2, "Segment pagein: read_result=%d SEG->size=%d",
106  read_result, SEG->size);
107 
108  if (read_result < 0)
109  G_warning("Segment pagein: %s", strerror(errno));
110  else
111  G_warning
112  ("Segment pagein: short count during read(), got %d, expected %d",
113  read_result, SEG->size);
114 
115  return -1;
116  }
117 
118  /* add loaded segment to index */
119  SEG->load_idx[n] = cur;
120 
121  /* make it youngest segment */
122  SEG->youngest = SEG->youngest->younger;
123  SEG->scb[cur].age = SEG->youngest;
124  SEG->youngest->cur = cur;
125 
126  return SEG->cur = cur;
127 }
int fd
Definition: segment.h:44
int cur
Definition: segment.h:59
int size
Definition: segment.h:28
struct aq * oldest
Definition: segment.h:55
int * load_idx
Definition: segment.h:52
struct aq * age
Definition: segment.h:49
char * buf
Definition: segment.h:47
struct aq * younger
Definition: segment.h:16
struct aq * older
Definition: segment.h:16
char dirty
Definition: segment.h:48
struct aq * youngest
Definition: segment.h:55
void G_warning(const char *,...) __attribute__((format(printf
int(* seek)()
Definition: segment.h:41
int seg_pagein(SEGMENT *SEG, int n)
Internal use only.
Definition: pagein.c:37
int * freeslot
Definition: segment.h:54
int cur
Definition: segment.h:15
int seg_pageout(SEGMENT *SEG, int i)
Internal use only.
Definition: pageout.c:37
int G_debug(int, const char *,...) __attribute__((format(printf
struct SEGMENT::scb * scb
int nfreeslots
Definition: segment.h:53