18 #include <grass/gis.h> 19 #include "local_proto.h" 39 int seg_exp, n_total_segs;
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");
52 SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
54 SEG->spr = SEG->ncols / SEG->scols;
55 SEG->spill = SEG->ncols % SEG->scols;
63 while (SEG->scols - (1 << seg_exp) > 0)
66 if (SEG->scols - (1 << seg_exp) == 0) {
67 SEG->scolbits = seg_exp;
69 while (SEG->srows - (1 << seg_exp) > 0)
71 if (SEG->srows - (1 << seg_exp) == 0) {
72 SEG->srowbits = seg_exp;
73 SEG->segbits = SEG->srowbits + SEG->scolbits;
75 G_debug(1,
"Segment setup: fast address activated");
85 if (SEG->fast_adrs == 1) {
87 while (SEG->len - (1 << seg_exp) > 0)
89 if (SEG->len - (1 << seg_exp) == 0) {
90 SEG->lenbits = seg_exp;
91 SEG->sizebits = SEG->segbits + SEG->lenbits;
93 G_debug(1,
"Segment setup: fast seek activated");
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;
110 (
struct scb *)G_malloc(SEG->nseg *
111 sizeof(
struct scb))) ==
NULL)
114 if ((SEG->freeslot = (
int *)G_malloc(SEG->nseg *
sizeof(
int))) ==
NULL)
118 (
struct aq *)G_malloc((SEG->nseg + 1) *
sizeof(
struct aq))) ==
NULL)
121 SEG->srowscols = SEG->srows * SEG->scols;
122 SEG->size = SEG->srowscols * SEG->len;
124 for (i = 0; i < SEG->nseg; i++) {
125 if ((SEG->scb[i].buf = G_malloc(SEG->size)) ==
NULL)
129 SEG->scb[i].dirty = 0;
130 SEG->scb[i].age =
NULL;
131 SEG->freeslot[i] = i;
132 SEG->agequeue[i].cur = -1;
134 SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
135 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
138 SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
139 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
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]);
148 SEG->nfreeslots = SEG->nseg;
158 SEG->load_idx = G_malloc(n_total_segs *
sizeof(
int));
160 for (i = 0; i < n_total_segs; i++)
161 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.