GRASS 8 Programmer's Manual 8.6.0dev(2026)-f6f2c534ea
Loading...
Searching...
No Matches
segment/setup.c
Go to the documentation of this file.
1/**
2 * \file lib/segment/setup.c
3 *
4 * \brief Segment setup 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 <stdlib.h>
15#include <stdio.h>
16#include <math.h>
17#include <string.h>
18#include <errno.h>
19
20#include <grass/gis.h>
21#include <grass/glocale.h>
22
23#include "local_proto.h"
24
25/**
26 * \brief Internal use only
27 *
28 * Setup segment.
29 *
30 * <b>SEG</b> must have the following parms set:
31 * fd (open for read and write), nrows, ncols, srows, scols, len, nseg
32 *
33 * \param[in,out] SEG segment
34 * \return 1 if successful
35 * \return -1 if illegal parameters are passed in <b>SEG</b>
36 * \return -2 if unable to allocate memory
37 */
39{
40 int i;
42
43 SEG->open = 0;
44 SEG->cache = NULL;
45
46 if (SEG->nrows <= 0 || SEG->ncols <= 0 || SEG->srows <= 0 ||
47 SEG->scols <= 0 || SEG->len <= 0 || SEG->nseg <= 0) {
48 G_warning("Segment setup: illegal segment file parameters");
49 return -1;
50 }
51
52 /* This is close to the beginning of the file, so doesn't need to be an
53 * off_t */
54 SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
55 if (SEG->offset == -1) {
56 int err = errno;
57 G_warning(_("File read/write operation failed: %s (%d)"), strerror(err),
58 err);
59 return -1;
60 }
61
62 SEG->spr = SEG->ncols / SEG->scols;
63 SEG->spill = SEG->ncols % SEG->scols;
64 if (SEG->spill)
65 SEG->spr++;
66
67 /* fast address */
68 SEG->fast_adrs = 0;
69
70 seg_exp = 0;
71 while (SEG->scols - (1 << seg_exp) > 0)
72 seg_exp++;
73
74 if (SEG->scols - (1 << seg_exp) == 0) {
75 SEG->scolbits = seg_exp;
76 seg_exp = 0;
77 while (SEG->srows - (1 << seg_exp) > 0)
78 seg_exp++;
79 if (SEG->srows - (1 << seg_exp) == 0) {
80 SEG->srowbits = seg_exp;
81 SEG->segbits = SEG->srowbits + SEG->scolbits;
82 SEG->fast_adrs = 1;
83 G_debug(1, "Segment setup: fast address activated");
84 }
85 }
86 if (SEG->fast_adrs)
87 SEG->address = seg_address_fast;
88 else
89 SEG->address = seg_address_slow;
90
91 /* fast seek */
92 SEG->fast_seek = 0;
93 if (SEG->fast_adrs == 1) {
94 seg_exp = 0;
95 while (SEG->len - (1 << seg_exp) > 0)
96 seg_exp++;
97 if (SEG->len - (1 << seg_exp) == 0) {
98 SEG->lenbits = seg_exp;
99 SEG->sizebits = SEG->segbits + SEG->lenbits;
100 SEG->fast_seek = 1;
101 G_debug(1, "Segment setup: fast seek activated");
102 }
103 }
104 if (SEG->fast_seek)
106 else
108
109 /* adjust number of open segments if larger than number of total segments */
110 n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
111 if (SEG->nseg > n_total_segs) {
112 G_debug(1,
113 "Segment setup: reducing number of open segments from %d to %d",
114 SEG->nseg, n_total_segs);
115 SEG->nseg = n_total_segs;
116 }
117
118 if ((SEG->scb = (struct scb *)G_malloc(SEG->nseg * sizeof(struct scb))) ==
119 NULL)
120 return -2;
121
122 if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
123 return -2;
124
125 if ((SEG->agequeue = (struct aq *)G_malloc((SEG->nseg + 1) *
126 sizeof(struct aq))) == NULL)
127 return -2;
128
129 SEG->srowscols = SEG->srows * SEG->scols;
130 SEG->size = SEG->srowscols * SEG->len;
131
132 for (i = 0; i < SEG->nseg; i++) {
133 if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
134 return -2;
135
136 SEG->scb[i].n = -1; /* mark free */
137 SEG->scb[i].dirty = 0;
138 SEG->scb[i].age = NULL;
139 SEG->freeslot[i] = i;
140 SEG->agequeue[i].cur = -1;
141 if (i > 0) {
142 SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
143 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
144 }
145 else if (i == 0) {
146 SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
147 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
148 }
149 }
150
151 SEG->agequeue[SEG->nseg].cur = -1;
152 SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
153 SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
154 SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
155
156 SEG->nfreeslots = SEG->nseg;
157 SEG->cur = 0;
158 SEG->open = 1;
159
160 /* index for each segment, same like cache of r.proj */
161
162 /* alternative using less memory: RB Tree */
163 /* SEG->loaded = rbtree_create(cmp, sizeof(SEGID)); */
164 /* SEG->loaded = NULL; */
165
166 SEG->load_idx = G_malloc(n_total_segs * sizeof(int));
167
168 for (i = 0; i < n_total_segs; i++)
169 SEG->load_idx[i] = -1;
170
171 return 1;
172}
int seg_address_slow(const SEGMENT *SEG, off_t row, off_t col, int *n, int *index)
Definition address.c:74
int seg_address_fast(const SEGMENT *SEG, off_t row, off_t col, int *n, int *index)
Definition address.c:30
#define NULL
Definition ccmath.h:32
AMI_err seek(off_t offset)
Definition ami_stream.h:445
void G_warning(const char *,...) __attribute__((format(printf
#define G_malloc(n)
Definition defs/gis.h:139
int G_debug(int, const char *,...) __attribute__((format(printf
#define _(str)
Definition glocale.h:10
int seg_seek_fast(const SEGMENT *SEG, int n, int index)
int seg_seek_slow(const SEGMENT *SEG, int n, int index)
int seg_setup(SEGMENT *SEG)
Internal use only.
Definition segment.h:14
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)