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