37 int seg_exp, n_total_segs;
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");
50 SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
52 SEG->spr = SEG->ncols / SEG->scols;
53 SEG->spill = SEG->ncols % SEG->scols;
61 while (SEG->scols - (1 << seg_exp) > 0)
64 if (SEG->scols - (1 << seg_exp) == 0) {
65 SEG->scolbits = seg_exp;
67 while (SEG->srows - (1 << seg_exp) > 0)
69 if (SEG->srows - (1 << seg_exp) == 0) {
70 SEG->srowbits = seg_exp;
71 SEG->segbits = SEG->srowbits + SEG->scolbits;
73 G_debug(1,
"Segment setup: fast address activated");
83 if (SEG->fast_adrs == 1) {
85 while (SEG->len - (1 << seg_exp) > 0)
87 if (SEG->len - (1 << seg_exp) == 0) {
88 SEG->lenbits = seg_exp;
89 SEG->sizebits = SEG->segbits + SEG->lenbits;
91 G_debug(1,
"Segment setup: fast seek activated");
100 n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
101 if (SEG->nseg > n_total_segs) {
103 "Segment setup: reducing number of open segments from %d to %d",
104 SEG->nseg, n_total_segs);
105 SEG->nseg = n_total_segs;
108 if ((SEG->scb = (
struct scb *)G_malloc(SEG->nseg *
sizeof(
struct scb))) ==
112 if ((SEG->freeslot = (
int *)G_malloc(SEG->nseg *
sizeof(
int))) ==
NULL)
115 if ((SEG->agequeue = (
struct aq *)G_malloc((SEG->nseg + 1) *
116 sizeof(
struct aq))) ==
NULL)
119 SEG->srowscols = SEG->srows * SEG->scols;
120 SEG->size = SEG->srowscols * SEG->len;
122 for (i = 0; i < SEG->nseg; i++) {
123 if ((SEG->scb[i].buf = G_malloc(SEG->size)) ==
NULL)
127 SEG->scb[i].dirty = 0;
128 SEG->scb[i].age =
NULL;
129 SEG->freeslot[i] = i;
130 SEG->agequeue[i].cur = -1;
132 SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
133 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
136 SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
137 SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
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]);
146 SEG->nfreeslots = SEG->nseg;
156 SEG->load_idx = G_malloc(n_total_segs *
sizeof(
int));
158 for (i = 0; i < n_total_segs; i++)
159 SEG->load_idx[i] = -1;