GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
segment/setup.c
Go to the documentation of this file.
1 
2 /**
3  * \file lib/segment/setup.c
4  *
5  * \brief Segment setup routines.
6  *
7  * This program is free software under the GNU General Public License
8  * (>=v2). Read the file COPYING that comes with GRASS for details.
9  *
10  * \author GRASS GIS Development Team
11  *
12  * \date 2005-2009
13  */
14 
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <math.h>
18 #include <grass/gis.h>
19 #include "local_proto.h"
20 
21 
22 /**
23  * \brief Internal use only
24  *
25  * Setup segment.
26  *
27  * <b>SEG</b> must have the following parms set:
28  * fd (open for read and write), nrows, ncols, srows, scols, len, nseg
29  *
30  * \param[in,out] SEG segment
31  * \return 1 if successful
32  * \return -1 if illegal parameters are passed in <b>SEG</b>
33  * \return -2 if unable to allocate memory
34  */
35 
36 int seg_setup(SEGMENT * SEG)
37 {
38  int i;
39  int seg_exp, n_total_segs;
40 
41  SEG->open = 0;
42  SEG->cache = NULL;
43 
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");
48  return -1;
49  }
50 
51  /* This is close to the beginning of the file, so doesn't need to be an off_t */
52  SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
53 
54  SEG->spr = SEG->ncols / SEG->scols;
55  SEG->spill = SEG->ncols % SEG->scols;
56  if (SEG->spill)
57  SEG->spr++;
58 
59  /* fast address */
60  SEG->fast_adrs = 0;
61 
62  seg_exp = 0;
63  while (SEG->scols - (1 << seg_exp) > 0)
64  seg_exp++;
65 
66  if (SEG->scols - (1 << seg_exp) == 0) {
67  SEG->scolbits = seg_exp;
68  seg_exp = 0;
69  while (SEG->srows - (1 << seg_exp) > 0)
70  seg_exp++;
71  if (SEG->srows - (1 << seg_exp) == 0) {
72  SEG->srowbits = seg_exp;
73  SEG->segbits = SEG->srowbits + SEG->scolbits;
74  SEG->fast_adrs = 1;
75  G_debug(1, "Segment setup: fast address activated");
76  }
77  }
78  if (SEG->fast_adrs)
80  else
82 
83  /* fast seek */
84  SEG->fast_seek = 0;
85  if (SEG->fast_adrs == 1) {
86  seg_exp = 0;
87  while (SEG->len - (1 << seg_exp) > 0)
88  seg_exp++;
89  if (SEG->len - (1 << seg_exp) == 0) {
90  SEG->lenbits = seg_exp;
91  SEG->sizebits = SEG->segbits + SEG->lenbits;
92  SEG->fast_seek = 1;
93  G_debug(1, "Segment setup: fast seek activated");
94  }
95  }
96  if (SEG->fast_seek)
97  SEG->seek = seg_seek_fast;
98  else
99  SEG->seek = seg_seek_slow;
100 
101  /* adjust number of open segments if larger than number of total segments */
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;
107  }
108 
109  if ((SEG->scb =
110  (struct scb *)G_malloc(SEG->nseg *
111  sizeof(struct scb))) == NULL)
112  return -2;
113 
114  if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
115  return -2;
116 
117  if ((SEG->agequeue =
118  (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
119  return -2;
120 
121  SEG->srowscols = SEG->srows * SEG->scols;
122  SEG->size = SEG->srowscols * SEG->len;
123 
124  for (i = 0; i < SEG->nseg; i++) {
125  if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
126  return -2;
127 
128  SEG->scb[i].n = -1; /* mark free */
129  SEG->scb[i].dirty = 0;
130  SEG->scb[i].age = NULL;
131  SEG->freeslot[i] = i;
132  SEG->agequeue[i].cur = -1;
133  if (i > 0) {
134  SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
135  SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
136  }
137  else if (i == 0) {
138  SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
139  SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
140  }
141  }
142 
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]);
147 
148  SEG->nfreeslots = SEG->nseg;
149  SEG->cur = 0;
150  SEG->open = 1;
151 
152  /* index for each segment, same like cache of r.proj */
153 
154  /* alternative using less memory: RB Tree */
155  /* SEG->loaded = rbtree_create(cmp, sizeof(SEGID)); */
156  /* SEG->loaded = NULL; */
157 
158  SEG->load_idx = G_malloc(n_total_segs * sizeof(int));
159 
160  for (i = 0; i < n_total_segs; i++)
161  SEG->load_idx[i] = -1;
162 
163  return 1;
164 }
#define G_malloc(n)
Definition: defs/gis.h:112
int fd
Definition: segment.h:44
int nseg
Definition: segment.h:58
int offset
Definition: segment.h:60
int(* address)()
Definition: segment.h:40
int open
Definition: segment.h:21
off_t srowbits
Definition: segment.h:35
off_t scolbits
Definition: segment.h:34
int seg_address_slow(const SEGMENT *SEG, off_t row, off_t col, int *n, int *index)
Definition: address.c:73
int len
Definition: segment.h:24
int spill
Definition: segment.h:30
int seg_address_fast(const SEGMENT *SEG, off_t row, off_t col, int *n, int *index)
Definition: address.c:30
#define NULL
Definition: ccmath.h:32
int cur
Definition: segment.h:59
int size
Definition: segment.h:28
struct aq * oldest
Definition: segment.h:55
int lenbits
Definition: segment.h:38
Definition: segment.h:14
int * load_idx
Definition: segment.h:52
struct aq * age
Definition: segment.h:49
int seg_seek_fast(const SEGMENT *SEG, int n, int index)
Definition: segment/seek.c:42
char * buf
Definition: segment.h:47
int fast_adrs
Definition: segment.h:33
struct aq * younger
Definition: segment.h:16
struct aq * older
Definition: segment.h:16
off_t ncols
Definition: segment.h:23
off_t nrows
Definition: segment.h:22
char dirty
Definition: segment.h:48
off_t segbits
Definition: segment.h:36
int sizebits
Definition: segment.h:39
char * cache
Definition: segment.h:62
struct aq * agequeue
Definition: segment.h:55
int srowscols
Definition: segment.h:27
int srows
Definition: segment.h:25
struct aq * youngest
Definition: segment.h:55
void G_warning(const char *,...) __attribute__((format(printf
int seg_seek_slow(const SEGMENT *SEG, int n, int index)
Definition: segment/seek.c:52
int spr
Definition: segment.h:29
int(* seek)()
Definition: segment.h:41
int scols
Definition: segment.h:26
int * freeslot
Definition: segment.h:54
int cur
Definition: segment.h:15
int seg_setup(SEGMENT *SEG)
Internal use only.
Definition: segment/setup.c:36
int fast_seek
Definition: segment.h:37
int G_debug(int, const char *,...) __attribute__((format(printf
struct SEGMENT::scb * scb
int nfreeslots
Definition: segment.h:53