GRASS GIS 8 Programmer's Manual
8.2.2dev(2023)-3d2c704037
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
}
SEGMENT::fd
int fd
Definition:
segment.h:44
SEGMENT::scb::n
int n
Definition:
segment.h:50
SEGMENT::cur
int cur
Definition:
segment.h:59
SEGMENT::size
int size
Definition:
segment.h:28
SEGMENT::oldest
struct aq * oldest
Definition:
segment.h:55
gis.h
SEGMENT::load_idx
int * load_idx
Definition:
segment.h:52
SEGMENT::scb::age
struct aq * age
Definition:
segment.h:49
SEGMENT::scb::buf
char * buf
Definition:
segment.h:47
aq::younger
struct aq * younger
Definition:
segment.h:16
aq::older
struct aq * older
Definition:
segment.h:16
SEGMENT::scb::dirty
char dirty
Definition:
segment.h:48
SEGMENT::youngest
struct aq * youngest
Definition:
segment.h:55
G_warning
void G_warning(const char *,...) __attribute__((format(printf
SEGMENT::seek
int(* seek)()
Definition:
segment.h:41
seg_pagein
int seg_pagein(SEGMENT *SEG, int n)
Internal use only.
Definition:
pagein.c:37
SEGMENT::freeslot
int * freeslot
Definition:
segment.h:54
aq::cur
int cur
Definition:
segment.h:15
SEGMENT
Definition:
segment.h:19
seg_pageout
int seg_pageout(SEGMENT *SEG, int i)
Internal use only.
Definition:
pageout.c:37
G_debug
int G_debug(int, const char *,...) __attribute__((format(printf
SEGMENT::scb
struct SEGMENT::scb * scb
SEGMENT::nfreeslots
int nfreeslots
Definition:
segment.h:53
lib
segment
pagein.c
Generated on Tue Feb 14 2023 05:56:01 for GRASS GIS 8 Programmer's Manual by
1.8.13