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