GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72886
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
43  if (SEG->nrows <= 0 || SEG->ncols <= 0
44  || SEG->srows <= 0 || SEG->scols <= 0
45  || SEG->len <= 0 || SEG->nseg <= 0) {
46  G_warning("Segment setup: illegal segment file parameters");
47  return -1;
48  }
49 
50  /* This is close to the beginning of the file, so doesn't need to be an off_t */
51  SEG->offset = (int)lseek(SEG->fd, 0L, SEEK_CUR);
52 
53  SEG->spr = SEG->ncols / SEG->scols;
54  SEG->spill = SEG->ncols % SEG->scols;
55  if (SEG->spill)
56  SEG->spr++;
57 
58  /* fast address */
59  SEG->fast_adrs = 0;
60 
61  seg_exp = 0;
62  while (SEG->scols - (1 << seg_exp) > 0)
63  seg_exp++;
64 
65  if (SEG->scols - (1 << seg_exp) == 0) {
66  SEG->scolbits = seg_exp;
67  seg_exp = 0;
68  while (SEG->srows - (1 << seg_exp) > 0)
69  seg_exp++;
70  if (SEG->srows - (1 << seg_exp) == 0) {
71  SEG->srowbits = seg_exp;
72  SEG->segbits = SEG->srowbits + SEG->scolbits;
73  SEG->fast_adrs = 1;
74  G_debug(1, "Segment setup: fast address activated");
75  }
76  }
77  if (SEG->fast_adrs)
79  else
81 
82  /* fast seek */
83  SEG->fast_seek = 0;
84  if (SEG->fast_adrs == 1) {
85  seg_exp = 0;
86  while (SEG->len - (1 << seg_exp) > 0)
87  seg_exp++;
88  if (SEG->len - (1 << seg_exp) == 0) {
89  SEG->lenbits = seg_exp;
90  SEG->sizebits = SEG->segbits + SEG->lenbits;
91  SEG->fast_seek = 1;
92  G_debug(1, "Segment setup: fast seek activated");
93  }
94  }
95  if (SEG->fast_seek)
96  SEG->seek = seg_seek_fast;
97  else
98  SEG->seek = seg_seek_slow;
99 
100  /* adjust number of open segments if larger than number of total segments */
101  n_total_segs = SEG->spr * ((SEG->nrows + SEG->srows - 1) / SEG->srows);
102  if (SEG->nseg > n_total_segs) {
103  G_debug(1, "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 =
109  (struct scb *)G_malloc(SEG->nseg *
110  sizeof(struct scb))) == NULL)
111  return -2;
112 
113  if ((SEG->freeslot = (int *)G_malloc(SEG->nseg * sizeof(int))) == NULL)
114  return -2;
115 
116  if ((SEG->agequeue =
117  (struct aq *)G_malloc((SEG->nseg + 1) * sizeof(struct aq))) == NULL)
118  return -2;
119 
120  SEG->srowscols = SEG->srows * SEG->scols;
121  SEG->size = SEG->srowscols * SEG->len;
122 
123  for (i = 0; i < SEG->nseg; i++) {
124  if ((SEG->scb[i].buf = G_malloc(SEG->size)) == NULL)
125  return -2;
126 
127  SEG->scb[i].n = -1; /* mark free */
128  SEG->scb[i].dirty = 0;
129  SEG->scb[i].age = NULL;
130  SEG->freeslot[i] = i;
131  SEG->agequeue[i].cur = -1;
132  if (i > 0) {
133  SEG->agequeue[i].younger = &(SEG->agequeue[i - 1]);
134  SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
135  }
136  else if (i == 0) {
137  SEG->agequeue[i].younger = &(SEG->agequeue[SEG->nseg]);
138  SEG->agequeue[i].older = &(SEG->agequeue[i + 1]);
139  }
140  }
141 
142  SEG->agequeue[SEG->nseg].cur = -1;
143  SEG->agequeue[SEG->nseg].younger = &(SEG->agequeue[SEG->nseg - 1]);
144  SEG->agequeue[SEG->nseg].older = &(SEG->agequeue[0]);
145  SEG->youngest = SEG->oldest = &(SEG->agequeue[SEG->nseg]);
146 
147  SEG->nfreeslots = SEG->nseg;
148  SEG->cur = 0;
149  SEG->open = 1;
150 
151  /* index for each segment, same like cache of r.proj */
152 
153  /* alternative using less memory: RB Tree */
154  /* SEG->loaded = rbtree_create(cmp, sizeof(SEGID)); */
155  /* SEG->loaded = NULL; */
156 
157  SEG->load_idx = G_malloc(n_total_segs * sizeof(int));
158 
159  for (i = 0; i < n_total_segs; i++)
160  SEG->load_idx[i] = -1;
161 
162  return 1;
163 }
int fd
Definition: segment.h:44
int nseg
Definition: segment.h:58
int offset
Definition: segment.h:60
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
int(* address)()
Definition: segment.h:40
struct aq * younger
Definition: segment.h:16
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
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
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
int seg_seek_slow(const SEGMENT *SEG, int n, int index)
Definition: segment/seek.c:52
int spr
Definition: segment.h:29
int scols
Definition: segment.h:26
int * freeslot
Definition: segment.h:54
int cur
Definition: segment.h:15
int
Reads the categories file for map name in mapset and stores the categories in the pcats structure...
int seg_setup(SEGMENT *SEG)
Internal use only.
Definition: segment/setup.c:36
int fast_seek
Definition: segment.h:37
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204
int(* seek)()
Definition: segment.h:41
struct SEGMENT::scb * scb
int nfreeslots
Definition: segment.h:53