18 #include <grass/gis.h> 19 #include "local_proto.h" 39 int seg_exp, n_total_segs;
43 if (SEG->nrows <= 0 || SEG->ncols <= 0
44 || SEG->srows <= 0 || SEG->scols <= 0
45 || SEG->len <= 0 || SEG->nseg <= 0) {
46 G_warning(
"Segment setup: illegal segment file parameters");
51 SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
53 SEG->spr = SEG->ncols / SEG->scols;
54 SEG->spill = SEG->ncols % SEG->scols;
62 while (SEG->scols - (1 << seg_exp) > 0)
65 if (SEG->scols - (1 << seg_exp) == 0) {
66 SEG->scolbits = seg_exp;
68 while (SEG->srows - (1 << seg_exp) > 0)
70 if (SEG->srows - (1 << seg_exp) == 0) {
71 SEG->srowbits = seg_exp;
72 SEG->segbits = SEG->srowbits + SEG->scolbits;
74 G_debug(1,
"Segment setup: fast address activated");
84 if (SEG->fast_adrs == 1) {
86 while (SEG->len - (1 << seg_exp) > 0)
88 if (SEG->len - (1 << seg_exp) == 0) {
89 SEG->lenbits = seg_exp;
90 SEG->sizebits = SEG->segbits + SEG->lenbits;
92 G_debug(1,
"Segment setup: fast seek activated");
101 n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
102 if (SEG->nseg > n_total_segs) {
103 G_debug(1,
"Segment setup: reducing number of open segments from %d to %d",
104 SEG->nseg, n_total_segs);
105 SEG->nseg = n_total_segs;
109 (
struct scb *)G_malloc(SEG->nseg *
110 sizeof(
struct scb))) ==
NULL)
113 if ((SEG->freeslot = (
int *)G_malloc(SEG->nseg *
sizeof(
int))) ==
NULL)
117 (
struct aq *)G_malloc((SEG->nseg + 1) *
sizeof(
struct aq))) ==
NULL)
120 SEG->srowscols = SEG->srows * SEG->scols;
121 SEG->size = SEG->srowscols * SEG->len;
123 for (i = 0; i < SEG->nseg; i++) {
124 if ((SEG->scb[i].buf = G_malloc(SEG->size)) ==
NULL)
128 SEG->scb[i].dirty = 0;
129 SEG->scb[i].age =
NULL;
130 SEG->freeslot[i] = i;
131 SEG->agequeue[i].cur = -1;
133 SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
134 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
137 SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
138 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
142 SEG->agequeue[SEG->nseg].cur = -1;
143 SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
144 SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
145 SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
147 SEG->nfreeslots = SEG->nseg;
157 SEG->load_idx = G_malloc(n_total_segs *
sizeof(
int));
159 for (i = 0; i < n_total_segs; i++)
160 SEG->load_idx[i] = -1;
int seg_address_slow(const SEGMENT *SEG, off_t row, off_t col, int *n, int *index)
int seg_address_fast(const SEGMENT *SEG, off_t row, off_t col, int *n, int *index)
int seg_seek_fast(const SEGMENT *SEG, int n, int index)
int G_debug(int level, const char *msg,...)
Print debugging message.
int seg_seek_slow(const SEGMENT *SEG, int n, int index)
int seg_setup(SEGMENT *SEG)
Internal use only.
void G_warning(const char *msg,...)
Print a warning message to stderr.