GRASS 8 Programmer's Manual  8.5.0dev(2025)-c070206eb1
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 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 int seg_setup(SEGMENT *SEG)
34 {
35  int i;
36  int seg_exp, n_total_segs;
37 
38  SEG->open = 0;
39  SEG->cache = NULL;
40 
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");
44  return -1;
45  }
46 
47  /* This is close to the beginning of the file, so doesn't need to be an
48  * off_t */
49  SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
50 
51  SEG->spr = SEG->ncols / SEG->scols;
52  SEG->spill = SEG->ncols % SEG->scols;
53  if (SEG->spill)
54  SEG->spr++;
55 
56  /* fast address */
57  SEG->fast_adrs = 0;
58 
59  seg_exp = 0;
60  while (SEG->scols - (1 << seg_exp) > 0)
61  seg_exp++;
62 
63  if (SEG->scols - (1 << seg_exp) == 0) {
64  SEG->scolbits = seg_exp;
65  seg_exp = 0;
66  while (SEG->srows - (1 << seg_exp) > 0)
67  seg_exp++;
68  if (SEG->srows - (1 << seg_exp) == 0) {
69  SEG->srowbits = seg_exp;
70  SEG->segbits = SEG->srowbits + SEG->scolbits;
71  SEG->fast_adrs = 1;
72  G_debug(1, "Segment setup: fast address activated");
73  }
74  }
75  if (SEG->fast_adrs)
77  else
79 
80  /* fast seek */
81  SEG->fast_seek = 0;
82  if (SEG->fast_adrs == 1) {
83  seg_exp = 0;
84  while (SEG->len - (1 << seg_exp) > 0)
85  seg_exp++;
86  if (SEG->len - (1 << seg_exp) == 0) {
87  SEG->lenbits = seg_exp;
88  SEG->sizebits = SEG->segbits + SEG->lenbits;
89  SEG->fast_seek = 1;
90  G_debug(1, "Segment setup: fast seek activated");
91  }
92  }
93  if (SEG->fast_seek)
94  SEG->seek = seg_seek_fast;
95  else
96  SEG->seek = seg_seek_slow;
97 
98  /* adjust number of open segments if larger than number of total segments */
99  n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
100  if (SEG->nseg > n_total_segs) {
101  G_debug(1,
102  "Segment setup: reducing number of open segments from %d to %d",
103  SEG->nseg, n_total_segs);
104  SEG->nseg = n_total_segs;
105  }
106 
107  if ((SEG->scb = (struct scb *)G_malloc(SEG->nseg * sizeof(struct scb))) ==
108  NULL)
109  return -2;
110 
111  if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
112  return -2;
113 
114  if ((SEG->agequeue = (struct aq *)G_malloc((SEG->nseg + 1) *
115  sizeof(struct aq))) == NULL)
116  return -2;
117 
118  SEG->srowscols = SEG->srows * SEG->scols;
119  SEG->size = SEG->srowscols * SEG->len;
120 
121  for (i = 0; i < SEG->nseg; i++) {
122  if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
123  return -2;
124 
125  SEG->scb[i].n = -1; /* mark free */
126  SEG->scb[i].dirty = 0;
127  SEG->scb[i].age = NULL;
128  SEG->freeslot[i] = i;
129  SEG->agequeue[i].cur = -1;
130  if (i > 0) {
131  SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
132  SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
133  }
134  else if (i == 0) {
135  SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
136  SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
137  }
138  }
139 
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]);
144 
145  SEG->nfreeslots = SEG->nseg;
146  SEG->cur = 0;
147  SEG->open = 1;
148 
149  /* index for each segment, same like cache of r.proj */
150 
151  /* alternative using less memory: RB Tree */
152  /* SEG->loaded = rbtree_create(cmp, sizeof(SEGID)); */
153  /* SEG->loaded = NULL; */
154 
155  SEG->load_idx = G_malloc(n_total_segs * sizeof(int));
156 
157  for (i = 0; i < n_total_segs; i++)
158  SEG->load_idx[i] = -1;
159 
160  return 1;
161 }
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:33
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