17 #include <grass/gis.h>
18 #include "local_proto.h"
36 int seg_exp, n_total_segs;
41 if (SEG->nrows <= 0 || SEG->ncols <= 0 || SEG->srows <= 0 ||
42 SEG->scols <= 0 || SEG->len <= 0 || SEG->nseg <= 0) {
43 G_warning(
"Segment setup: illegal segment file parameters");
49 SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
51 SEG->spr = SEG->ncols / SEG->scols;
52 SEG->spill = SEG->ncols % SEG->scols;
60 while (SEG->scols - (1 << seg_exp) > 0)
63 if (SEG->scols - (1 << seg_exp) == 0) {
64 SEG->scolbits = seg_exp;
66 while (SEG->srows - (1 << seg_exp) > 0)
68 if (SEG->srows - (1 << seg_exp) == 0) {
69 SEG->srowbits = seg_exp;
70 SEG->segbits = SEG->srowbits + SEG->scolbits;
72 G_debug(1,
"Segment setup: fast address activated");
82 if (SEG->fast_adrs == 1) {
84 while (SEG->len - (1 << seg_exp) > 0)
86 if (SEG->len - (1 << seg_exp) == 0) {
87 SEG->lenbits = seg_exp;
88 SEG->sizebits = SEG->segbits + SEG->lenbits;
90 G_debug(1,
"Segment setup: fast seek activated");
99 n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
100 if (SEG->nseg > n_total_segs) {
102 "Segment setup: reducing number of open segments from %d to %d",
103 SEG->nseg, n_total_segs);
104 SEG->nseg = n_total_segs;
107 if ((SEG->scb = (
struct scb *)G_malloc(SEG->nseg *
sizeof(
struct scb))) ==
111 if ((SEG->freeslot = (
int *)G_malloc(SEG->nseg *
sizeof(
int))) ==
NULL)
114 if ((SEG->agequeue = (
struct aq *)G_malloc((SEG->nseg + 1) *
115 sizeof(
struct aq))) ==
NULL)
118 SEG->srowscols = SEG->srows * SEG->scols;
119 SEG->size = SEG->srowscols * SEG->len;
121 for (i = 0; i < SEG->nseg; i++) {
122 if ((SEG->scb[i].buf = G_malloc(SEG->size)) ==
NULL)
126 SEG->scb[i].dirty = 0;
127 SEG->scb[i].age =
NULL;
128 SEG->freeslot[i] = i;
129 SEG->agequeue[i].cur = -1;
131 SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
132 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
135 SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
136 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
140 SEG->agequeue[SEG->nseg].cur = -1;
141 SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
142 SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
143 SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
145 SEG->nfreeslots = SEG->nseg;
155 SEG->load_idx = G_malloc(n_total_segs *
sizeof(
int));
157 for (i = 0; i < n_total_segs; i++)
158 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 G_debug(int level, const char *msg,...)
Print debugging message.
void G_warning(const char *msg,...)
Print a warning message to stderr.
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.