GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
struct_alloc.c
Go to the documentation of this file.
1 /*
2  ****************************************************************************
3  *
4  * MODULE: Vector library
5  *
6  * AUTHOR(S): Dave Gerdes, CERL.
7  * Update to GRASS 5.7 Radim Blazek.
8  *
9  * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
10  *
11  * COPYRIGHT: (C) 2001 by the GRASS Development Team
12  *
13  * This program is free software under the GNU General Public
14  * License (>=v2). Read the file COPYING that comes with GRASS
15  * for details.
16  *
17  *****************************************************************************/
18 
19 #include <stdlib.h>
20 #include <grass/Vect.h>
21 
22 /* These routines all eventually call calloc() to allocate and zero
23  ** the new space. BUT It is not neccessarily safe to assume that
24  ** the memory will be zero. The next memory location asked for could
25  ** have been previously used and not zeroed. (e.g. compress())
26  */
27 
28 /* alloc_node (map, add)
29  ** alloc_line (map, add)
30  ** alloc_area (map, add)
31  ** alloc_points (map, num)
32  ** node_alloc_line (node, add)
33  ** area_alloc_line (node, add)
34  **
35  ** Allocate array space to add 'add' elements
36  */
37 
38 
39 /* allocate new node structure */
40 P_NODE *dig_alloc_node()
41 {
42  P_NODE *Node;
43 
44  Node = (P_NODE *) G_malloc(sizeof(P_NODE));
45  if (Node == NULL)
46  return NULL;
47 
48  Node->n_lines = 0;
49  Node->alloc_lines = 0;
50  Node->lines = NULL;
51  Node->angles = NULL;
52 
53  return (Node);
54 }
55 
56 /* dig_node_alloc_line (node, add)
57  ** allocate space in P_node, lines and angles arrays to add 'add' more
58  ** lines
59  **
60  ** Returns 0 ok or -1 on error
61  */
62 
63 int dig_node_alloc_line(P_NODE * node, int add)
64 {
65  int num;
66  char *p;
67 
68  G_debug(3, "dig_node_alloc_line(): add = %d", add);
69 
70  num = node->n_lines + add;
71 
72  p = G_realloc(node->lines, num * sizeof(plus_t));
73  if (p == NULL)
74  return -1;
75  node->lines = (plus_t *) p;
76 
77  p = G_realloc(node->angles, num * sizeof(float));
78  if (p == NULL)
79  return -1;
80  node->angles = (float *)p;
81 
82  node->alloc_lines = num;
83  return (0);
84 }
85 
86 
87 /* Reallocate array of pointers to nodes.
88  * Space for 'add' number of nodes is added.
89  *
90  * Returns: 0 success
91  * -1 error
92  */
93 int dig_alloc_nodes(struct Plus_head *Plus, int add)
94 {
95  int size;
96  char *p;
97 
98  size = Plus->alloc_nodes + 1 + add;
99  p = G_realloc(Plus->Node, size * sizeof(P_NODE *));
100  if (p == NULL)
101  return -1;
102 
103  Plus->Node = (P_NODE **) p;
104  Plus->alloc_nodes = size - 1;
105 
106  return (0);
107 }
108 
109 /* allocate new line structure */
110 P_LINE *dig_alloc_line()
111 {
112  P_LINE *Line;
113 
114  Line = (P_LINE *) G_malloc(sizeof(P_LINE));
115  if (Line == NULL)
116  return NULL;
117 
118  return (Line);
119 }
120 
121 /* Reallocate array of pointers to lines.
122  * Space for 'add' number of lines is added.
123  *
124  * Returns: 0 success
125  * -1 error
126  */
127 int dig_alloc_lines(struct Plus_head *Plus, int add)
128 {
129  int size;
130  char *p;
131 
132  size = Plus->alloc_lines + 1 + add;
133  p = G_realloc(Plus->Line, size * sizeof(P_LINE *));
134  if (p == NULL)
135  return -1;
136 
137  Plus->Line = (P_LINE **) p;
138  Plus->alloc_lines = size - 1;
139 
140  return (0);
141 }
142 
143 /* Reallocate array of pointers to areas.
144  * Space for 'add' number of areas is added.
145  *
146  * Returns: 0 success
147  * -1 error
148  */
149 int dig_alloc_areas(struct Plus_head *Plus, int add)
150 {
151  int size;
152  char *p;
153 
154  size = Plus->alloc_areas + 1 + add;
155  p = G_realloc(Plus->Area, size * sizeof(P_AREA *));
156  if (p == NULL)
157  return -1;
158 
159  Plus->Area = (P_AREA **) p;
160  Plus->alloc_areas = size - 1;
161 
162  return (0);
163 }
164 
165 /* Reallocate array of pointers to isles.
166  * Space for 'add' number of isles is added.
167  *
168  * Returns: 0 success
169  * -1 error
170  */
171 int dig_alloc_isles(struct Plus_head *Plus, int add)
172 {
173  int size;
174  char *p;
175 
176  G_debug(3, "dig_alloc_isle():");
177  size = Plus->alloc_isles + 1 + add;
178  p = G_realloc(Plus->Isle, size * sizeof(P_ISLE *));
179  if (p == NULL)
180  return -1;
181 
182  Plus->Isle = (P_ISLE **) p;
183  Plus->alloc_isles = size - 1;
184 
185  return (0);
186 }
187 
188 /* allocate new area structure */
189 P_AREA *dig_alloc_area()
190 {
191  P_AREA *Area;
192 
193  Area = (P_AREA *) G_malloc(sizeof(P_AREA));
194  if (Area == NULL)
195  return NULL;
196 
197  Area->n_lines = 0;
198  Area->alloc_lines = 0;
199  Area->lines = NULL;
200 
201  Area->alloc_isles = 0;
202  Area->n_isles = 0;
203  Area->isles = NULL;
204 
205  Area->centroid = 0;
206 
207  return (Area);
208 }
209 
210 /* alloc new isle structure */
211 P_ISLE *dig_alloc_isle()
212 {
213  P_ISLE *Isle;
214 
215  Isle = (P_ISLE *) G_malloc(sizeof(P_ISLE));
216  if (Isle == NULL)
217  return NULL;
218 
219  Isle->n_lines = 0;
220  Isle->alloc_lines = 0;
221  Isle->lines = NULL;
222 
223  Isle->area = 0;
224 
225  return (Isle);
226 }
227 
228 
229 /* allocate room for 'num' X and Y arrays in struct line_pnts
230  ** returns -1 on out of memory
231  */
232 int dig_alloc_points(struct line_pnts *points, int num)
233 {
234  int alloced;
235  char *p;
236 
237  alloced = points->alloc_points;
238  /* alloc_space will just return if no space is needed */
239  if (!(p =
240  dig__alloc_space(num, &alloced, 50, (char *)points->x,
241  sizeof(double)))) {
242  return (dig_out_of_memory());
243  }
244  points->x = (double *)p;
245 
246  alloced = points->alloc_points;
247  /* alloc_space will just return if no space is needed */
248  if (!(p =
249  dig__alloc_space(num, &alloced, 50, (char *)points->y,
250  sizeof(double)))) {
251  return (dig_out_of_memory());
252  }
253  points->y = (double *)p;
254 
255  alloced = points->alloc_points;
256  /* alloc_space will just return if no space is needed */
257  if (!(p =
258  dig__alloc_space(num, &alloced, 50, (char *)points->z,
259  sizeof(double)))) {
260  return (dig_out_of_memory());
261  }
262  points->z = (double *)p;
263 
264  points->alloc_points = alloced;
265  return (0);
266 }
267 
268 /* allocate room for 'num' fields and category arrays
269  ** in struct line_cats
270  ** returns -1 on out of memory
271  */
272 int dig_alloc_cats(struct line_cats *cats, int num)
273 {
274  int alloced;
275  char *p;
276 
277  /* alloc_space will just return if no space is needed */
278  alloced = cats->alloc_cats;
279  if (!(p =
280  dig__alloc_space(num, &alloced, 1, (int *)cats->field,
281  sizeof(int)))) {
282  return (dig_out_of_memory());
283  }
284  cats->field = (int *)p;
285 
286  alloced = cats->alloc_cats;
287  if (!(p =
288  dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
289  sizeof(int)))) {
290  return (dig_out_of_memory());
291  }
292  cats->cat = (int *)p;
293 
294  cats->alloc_cats = alloced;
295  return (0);
296 }
297 
298 /* area_alloc_line (area, add)
299  ** allocate space in P_area for add new lines
300  **
301  ** Returns 0 ok or -1 on error
302  */
303 int dig_area_alloc_line(P_AREA * area, int add)
304 {
305  int num;
306  char *p;
307 
308  num = area->alloc_lines + add;
309 
310  p = G_realloc(area->lines, num * sizeof(plus_t));
311  if (p == NULL)
312  return -1;
313  area->lines = (plus_t *) p;
314 
315  area->alloc_lines = num;
316 
317  return (0);
318 }
319 
320 /* area_alloc_isle (area, add)
321  ** allocate space in P_area for add new isles
322  **
323  ** Returns 0 ok or -1 on error
324  */
325 int dig_area_alloc_isle(P_AREA * area, int add)
326 {
327  int num;
328  char *p;
329 
330  G_debug(5, "dig_area_alloc_isle(): add = %d", add);
331  num = area->alloc_isles + add;
332 
333  p = G_realloc(area->isles, num * sizeof(plus_t));
334  if (p == NULL)
335  return -1;
336  area->isles = (plus_t *) p;
337 
338  area->alloc_isles = num;
339  return (0);
340 }
341 
342 /* dig_isle_alloc_line (isle, add)
343  ** allocate space in P_isle for add new lines
344  **
345  ** Returns 0 ok or -1 on error
346  */
347 
348 int dig_isle_alloc_line(P_ISLE * isle, int add)
349 {
350  int num;
351  char *p;
352 
353  G_debug(3, "dig_isle_alloc_line():");
354  num = isle->alloc_lines + add;
355 
356  p = G_realloc(isle->lines, num * sizeof(plus_t));
357  if (p == NULL)
358  return -1;
359  isle->lines = (plus_t *) p;
360 
361  isle->alloc_lines = num;
362 
363  return (0);
364 }
365 
366 
367 
368 /* for now just print message and return error code */
370 {
371  fprintf(stderr, "OUT OF MEMORY!\n");
372  return (-1);
373 }
int dig_alloc_nodes(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:93
int dig_area_alloc_isle(P_AREA *area, int add)
Definition: struct_alloc.c:325
P_NODE * dig_alloc_node()
Definition: struct_alloc.c:40
Definition: index.h:56
long num
Definition: g3dcats.c:93
int dig_alloc_areas(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:149
int dig_node_alloc_line(P_NODE *node, int add)
Definition: struct_alloc.c:63
int dig_alloc_cats(struct line_cats *cats, int num)
Definition: struct_alloc.c:272
tuple size
value.Bind(wx.EVT_TEXT, self.OnVolumeIsosurfMap)
Definition: tools.py:2334
RectReal area[2]
Definition: split_q.h:30
int dig_out_of_memory()
Definition: struct_alloc.c:369
P_ISLE * dig_alloc_isle()
Definition: struct_alloc.c:211
int dig_area_alloc_line(P_AREA *area, int add)
Definition: struct_alloc.c:303
int dig_alloc_isles(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:171
int dig_alloc_points(struct line_pnts *points, int num)
Definition: struct_alloc.c:232
int dig_isle_alloc_line(P_ISLE *isle, int add)
Definition: struct_alloc.c:348
P_LINE * dig_alloc_line()
Definition: struct_alloc.c:110
P_AREA * dig_alloc_area()
Definition: struct_alloc.c:189
return NULL
Definition: dbfopen.c:1394
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: gis/debug.c:51
int dig_alloc_lines(struct Plus_head *Plus, int add)
Definition: struct_alloc.c:127
void * dig__alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr, int element_size)
Definition: allocation.c:50