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