41 int seg_exp, n_total_segs;
46 if (SEG->nrows <= 0 || SEG->ncols <= 0 || SEG->srows <= 0 ||
47 SEG->scols <= 0 || SEG->len <= 0 || SEG->nseg <= 0) {
48 G_warning(
"Segment setup: illegal segment file parameters");
54 SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
55 if (SEG->offset == -1) {
57 G_warning(_(
"File read/write operation failed: %s (%d)"), strerror(
err),
62 SEG->spr = SEG->ncols / SEG->scols;
63 SEG->spill = SEG->ncols % SEG->scols;
71 while (SEG->scols - (1 << seg_exp) > 0)
74 if (SEG->scols - (1 << seg_exp) == 0) {
75 SEG->scolbits = seg_exp;
77 while (SEG->srows - (1 << seg_exp) > 0)
79 if (SEG->srows - (1 << seg_exp) == 0) {
80 SEG->srowbits = seg_exp;
81 SEG->segbits = SEG->srowbits + SEG->scolbits;
83 G_debug(1,
"Segment setup: fast address activated");
93 if (SEG->fast_adrs == 1) {
95 while (SEG->len - (1 << seg_exp) > 0)
97 if (SEG->len - (1 << seg_exp) == 0) {
98 SEG->lenbits = seg_exp;
99 SEG->sizebits = SEG->segbits + SEG->lenbits;
101 G_debug(1,
"Segment setup: fast seek activated");
110 n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
111 if (SEG->nseg > n_total_segs) {
113 "Segment setup: reducing number of open segments from %d to %d",
114 SEG->nseg, n_total_segs);
115 SEG->nseg = n_total_segs;
118 if ((SEG->scb = (
struct scb *)G_malloc(SEG->nseg *
sizeof(
struct scb))) ==
122 if ((SEG->freeslot = (
int *)G_malloc(SEG->nseg *
sizeof(
int))) ==
NULL)
125 if ((SEG->agequeue = (
struct aq *)G_malloc((SEG->nseg + 1) *
126 sizeof(
struct aq))) ==
NULL)
129 SEG->srowscols = SEG->srows * SEG->scols;
130 SEG->size = SEG->srowscols * SEG->len;
132 for (i = 0; i < SEG->nseg; i++) {
133 if ((SEG->scb[i].buf = G_malloc(SEG->size)) ==
NULL)
137 SEG->scb[i].dirty = 0;
138 SEG->scb[i].age =
NULL;
139 SEG->freeslot[i] = i;
140 SEG->agequeue[i].cur = -1;
142 SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
143 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
146 SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
147 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
151 SEG->agequeue[SEG->nseg].cur = -1;
152 SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
153 SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
154 SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
156 SEG->nfreeslots = SEG->nseg;
166 SEG->load_idx = G_malloc(n_total_segs *
sizeof(
int));
168 for (i = 0; i < n_total_segs; i++)
169 SEG->load_idx[i] = -1;