GRASS 8 Programmer's Manual 8.6.0dev(2026)-1d1e47ad9d
Loading...
Searching...
No Matches
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
19#include <grass/gis.h>
20#include <grass/glocale.h>
21
22#include "local_proto.h"
23
24/**
25 * \brief Internal use only
26 *
27 * Segment pagein.
28 *
29 * Finds <b>n</b> in the segment file, <b>seg</b>, and selects it as the
30 * current segment.
31 *
32 * \param[in] SEG segment
33 * \param[in] n segment number
34 * \return 1 if successful
35 * \return -1 if unable to seek or read segment file
36 */
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 if (SEG->seek(SEG, SEG->scb[cur].n, 0) == -1) {
94 int err = errno;
95 G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
96 err);
97 return -1;
98 }
99
100 read_result = read(SEG->fd, SEG->scb[cur].buf, SEG->size);
101
102 if (read_result == 0) {
103 /* this can happen if the file was not zero-filled,
104 * i.e. formatted with Segment_format_nofill() or
105 * Segment_format() used lseek for file initialization */
106 G_debug(1, "Segment pagein: zero read");
107 memset(SEG->scb[cur].buf, 0, SEG->size);
108 }
109 else if (read_result != SEG->size) {
110 G_debug(2, "Segment pagein: read_result=%d SEG->size=%d", read_result,
111 SEG->size);
112
113 if (read_result < 0)
114 G_warning("Segment pagein: %s", strerror(errno));
115 else
116 G_warning("Segment pagein: short count during read(), got %d, "
117 "expected %d",
118 read_result, SEG->size);
119
120 return -1;
121 }
122
123 /* add loaded segment to index */
124 SEG->load_idx[n] = cur;
125
126 /* make it youngest segment */
127 SEG->youngest = SEG->youngest->younger;
128 SEG->scb[cur].age = SEG->youngest;
129 SEG->youngest->cur = cur;
130
131 return SEG->cur = cur;
132}
AMI_err seek(off_t offset)
Definition ami_stream.h:445
void G_warning(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
#define _(str)
Definition glocale.h:10
int seg_pagein(SEGMENT *SEG, int n)
Internal use only.
Definition pagein.c:37
int seg_pageout(SEGMENT *SEG, int i)
Internal use only.
Definition pageout.c:37
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
#define read
Definition unistd.h:5