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