GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-0b29c8ef57
struct_alloc.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/diglib/struct_alloc.c
3 
4  \brief Vector library - allocate and zero array space (lower level functions)
5 
6  Lower level functions for reading/writing/manipulating vectors.
7 
8  These routines all eventually call calloc() to allocate and zero the
9  new space. BUT It is not necessarily safe to assume that the memory
10  will be zero. The next memory location asked for could have been
11  previously used and not zeroed. (e.g. compress()).
12 
13  This program is free software under the GNU General Public License
14  (>=v2). Read the file COPYING that comes with GRASS for details.
15 
16  \author CERL (probably Dave Gerdes)
17  \author Radim Blazek
18  */
19 
20 #include <stdlib.h>
21 #include <grass/vector.h>
22 #include <grass/glocale.h>
23 
24 /*!
25  \brief Allocate new node structure
26 
27  \return pointer to allocated P_node struct
28  \return NULL on error
29  */
30 struct P_node *dig_alloc_node(void)
31 {
32  struct P_node *Node;
33 
34  Node = (struct P_node *)G_malloc(sizeof(struct P_node));
35  if (Node == NULL)
36  return NULL;
37 
38  G_zero(Node, sizeof(struct P_node));
39 
40  return Node;
41 }
42 
43 /*!
44  \brief Free node structure
45 
46  \param Node pointer to P_node struct to be freed
47  */
48 void dig_free_node(struct P_node *Node)
49 {
50  if (Node->alloc_lines > 0) {
51  G_free(Node->lines);
52  G_free(Node->angles);
53  }
54 
55  G_free(Node);
56 }
57 
58 /*!
59  \brief Allocate space in P_node struct
60 
61  Lines and angles arrays to add 'add' more lines
62 
63  \param node pointer to P_node struct
64  \param add number lines to be added
65 
66  \return 0 on success
67  \return -1 on error
68  */
69 int dig_node_alloc_line(struct P_node *node, int add)
70 {
71  int num;
72  char *p;
73 
74  G_debug(5, "dig_node_alloc_line(): add = %d", add);
75 
76  if (node->n_lines + add <= node->alloc_lines)
77  return 0;
78 
79  num = node->alloc_lines + add;
80 
81  p = G_realloc(node->lines, num * sizeof(plus_t));
82  if (p == NULL)
83  return -1;
84  node->lines = (plus_t *)p;
85 
86  p = G_realloc(node->angles, num * sizeof(float));
87  if (p == NULL)
88  return -1;
89  node->angles = (float *)p;
90 
91  node->alloc_lines = num;
92 
93  return 0;
94 }
95 
96 /*!
97  \brief Reallocate array of pointers to nodes
98 
99  \param Plus pointer to Plus_head structure
100  \param add number of nodes to be added
101 
102  \return 0 on success
103  \return -1 on error
104  */
105 int dig_alloc_nodes(struct Plus_head *Plus, int add)
106 {
107  int size;
108  char *p;
109 
110  size = Plus->alloc_nodes + 1 + add;
111  p = G_realloc(Plus->Node, size * sizeof(struct P_node *));
112  if (p == NULL)
113  return -1;
114 
115  Plus->Node = (struct P_node **)p;
116  Plus->alloc_nodes = size - 1;
117 
118  return 0;
119 }
120 
121 /*!
122  \brief Allocate new line structure
123 
124  \return pointer to allocated P_node struct
125  \return NULL on error
126  */
127 struct P_line *dig_alloc_line(void)
128 {
129  struct P_line *Line;
130 
131  Line = (struct P_line *)G_malloc(sizeof(struct P_line));
132  if (Line == NULL)
133  return NULL;
134 
135  G_zero(Line, sizeof(struct P_line));
136 
137  return Line;
138 }
139 
140 /*!
141  \brief Allocate new topo struct
142 
143  \param type to of struct to allocate
144  */
145 void *dig_alloc_topo(char type)
146 {
147  void *Topo = NULL;
148 
149  switch (type) {
150  case GV_LINE:
151  Topo = G_malloc(sizeof(struct P_topo_l));
152  break;
153  case GV_BOUNDARY:
154  Topo = G_malloc(sizeof(struct P_topo_b));
155  break;
156  case GV_CENTROID:
157  Topo = G_malloc(sizeof(struct P_topo_c));
158  break;
159  case GV_FACE:
160  Topo = G_malloc(sizeof(struct P_topo_f));
161  break;
162  case GV_KERNEL:
163  Topo = G_malloc(sizeof(struct P_topo_k));
164  break;
165  default:
166  return NULL;
167  }
168 
169  return Topo;
170 }
171 
172 /*!
173  \brief Free line structure
174 
175  \param pointer to P_line struct to be freed
176  */
177 void dig_free_line(struct P_line *Line)
178 {
179  if (Line->topo)
180  G_free(Line->topo);
181  G_free(Line);
182 }
183 
184 /*!
185  \brief Reallocate array of pointers to lines.
186 
187  \param Plus pointer to Plus_head structure
188  \param add space for 'add' number of lines is added.
189 
190  \return 0 on success
191  \return -1 on error
192  */
193 int dig_alloc_lines(struct Plus_head *Plus, int add)
194 {
195  int size;
196  char *p;
197 
198  size = Plus->alloc_lines + 1 + add;
199  p = G_realloc(Plus->Line, size * sizeof(struct P_line *));
200  if (p == NULL)
201  return -1;
202 
203  Plus->Line = (struct P_line **)p;
204  Plus->alloc_lines = size - 1;
205 
206  return 0;
207 }
208 
209 /*!
210  \brief Reallocate array of pointers to areas.
211 
212  \param Plus pointer to Plus_head structure
213  \param add space for 'add' number of areas is added
214 
215  \return 0 on success
216  \return -1 on error
217  */
218 int dig_alloc_areas(struct Plus_head *Plus, int add)
219 {
220  int size;
221  char *p;
222 
223  size = Plus->alloc_areas + 1 + add;
224  p = G_realloc(Plus->Area, size * sizeof(struct P_area *));
225  if (p == NULL)
226  return -1;
227 
228  Plus->Area = (struct P_area **)p;
229  Plus->alloc_areas = size - 1;
230 
231  return 0;
232 }
233 
234 /*!
235  \brief Reallocate array of pointers to isles
236 
237  \param Plus pointer to Plus_head structure
238  \param add space for 'add' number of isles is added.
239 
240  \return 0 on success
241  \return -1 on error
242  */
243 int dig_alloc_isles(struct Plus_head *Plus, int add)
244 {
245  int size;
246  char *p;
247 
248  G_debug(5, "dig_alloc_isle():");
249  size = Plus->alloc_isles + 1 + add;
250  p = G_realloc(Plus->Isle, size * sizeof(struct P_isle *));
251  if (p == NULL)
252  return -1;
253 
254  Plus->Isle = (struct P_isle **)p;
255  Plus->alloc_isles = size - 1;
256 
257  return 0;
258 }
259 
260 /*!
261  \brief Allocate new area structure
262 
263  \return pointer to allocated P_area struct
264  \return NULL on error
265  */
266 struct P_area *dig_alloc_area(void)
267 {
268  struct P_area *Area;
269 
270  Area = (struct P_area *)G_malloc(sizeof(struct P_area));
271  if (Area == NULL)
272  return NULL;
273 
274  G_zero(Area, sizeof(struct P_area));
275 
276  return Area;
277 }
278 
279 /*!
280  \brief Free area structure
281 
282  \param Area pointer to P_area struct to be freed
283  */
284 void dig_free_area(struct P_area *Area)
285 {
286  if (Area->alloc_lines > 0)
287  free(Area->lines);
288 
289  if (Area->alloc_isles > 0)
290  free(Area->isles);
291 
292  G_free(Area);
293 }
294 
295 /*!
296  \brief Allocate new isle structure
297 
298  \return pointer to allocated P_isle struct
299  \return NULL on error
300  */
301 struct P_isle *dig_alloc_isle(void)
302 {
303  struct P_isle *Isle;
304 
305  Isle = (struct P_isle *)G_malloc(sizeof(struct P_isle));
306  if (Isle == NULL)
307  return NULL;
308 
309  G_zero(Isle, sizeof(struct P_isle));
310 
311  return Isle;
312 }
313 
314 /*!
315  \brief Free isle structure
316 
317  \param Isle pointer to P_isle struct to be freed
318  */
319 void dig_free_isle(struct P_isle *Isle)
320 {
321  if (Isle->alloc_lines > 0)
322  G_free(Isle->lines);
323 
324  G_free(Isle);
325 }
326 
327 /*!
328  \brief allocate room for 'num' X and Y arrays in struct line_pnts
329 
330  \param points pointer to line_pnts struct
331  \param num number of points
332 
333  \return 0 on success
334  \return returns -1 on out of memory
335  */
336 int dig_alloc_points(struct line_pnts *points, int num)
337 {
338  int alloced;
339  char *p;
340 
341  alloced = points->alloc_points;
342  /* alloc_space will just return if no space is needed */
343  if (!(p = dig__alloc_space(num, &alloced, 50, (char *)points->x,
344  sizeof(double)))) {
345  return (dig_out_of_memory());
346  }
347  points->x = (double *)p;
348 
349  alloced = points->alloc_points;
350  /* alloc_space will just return if no space is needed */
351  if (!(p = dig__alloc_space(num, &alloced, 50, (char *)points->y,
352  sizeof(double)))) {
353  return (dig_out_of_memory());
354  }
355  points->y = (double *)p;
356 
357  alloced = points->alloc_points;
358  /* alloc_space will just return if no space is needed */
359  if (!(p = dig__alloc_space(num, &alloced, 50, (char *)points->z,
360  sizeof(double)))) {
361  return (dig_out_of_memory());
362  }
363  points->z = (double *)p;
364 
365  points->alloc_points = alloced;
366 
367  return 0;
368 }
369 
370 /*!
371  \brief Allocate room for 'num' fields and category arrays
372  in struct line_cats
373 
374  \param cats pointer to line_cats struct
375  \param num number of cats
376 
377  \return 0 on success
378  \return returns -1 on out of memory
379  */
380 int dig_alloc_cats(struct line_cats *cats, int num)
381 {
382  int alloced;
383  char *p;
384 
385  /* alloc_space will just return if no space is needed */
386  alloced = cats->alloc_cats;
387  if (!(p = dig__alloc_space(num, &alloced, 1, (int *)cats->field,
388  sizeof(int)))) {
389  return dig_out_of_memory();
390  }
391  cats->field = (int *)p;
392 
393  alloced = cats->alloc_cats;
394  if (!(p = dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
395  sizeof(int)))) {
396  return dig_out_of_memory();
397  }
398  cats->cat = (int *)p;
399 
400  cats->alloc_cats = alloced;
401 
402  return 0;
403 }
404 
405 /*!
406  \brief allocate space in P_area for add new lines
407 
408  \param area pointer to P_area struct
409  \param add number of lines to be added
410 
411  \return 0 on success
412  \return -1 on error
413  */
414 int dig_area_alloc_line(struct P_area *area, int add)
415 {
416  int num;
417  char *p;
418 
419  num = area->alloc_lines + add;
420 
421  p = G_realloc(area->lines, num * sizeof(plus_t));
422  if (p == NULL)
423  return -1;
424  area->lines = (plus_t *)p;
425 
426  area->alloc_lines = num;
427 
428  return (0);
429 }
430 
431 /*!
432  \brief Allocate space in P_area for add new isles
433 
434  \param area pointer to P_area struct
435  \param add number of isle to be added
436 
437  \return 0 on success
438  \return -1 on error
439  */
440 int dig_area_alloc_isle(struct P_area *area, int add)
441 {
442  int num;
443  char *p;
444 
445  G_debug(5, "dig_area_alloc_isle(): add = %d", add);
446  num = area->alloc_isles + add;
447 
448  p = G_realloc(area->isles, num * sizeof(plus_t));
449  if (p == NULL)
450  return -1;
451  area->isles = (plus_t *)p;
452 
453  area->alloc_isles = num;
454  return (0);
455 }
456 
457 /*!
458  \brief Allocate space in P_isle for add new lines
459 
460  \param isle pointer to P_area struct
461  \param add number of isle to be added
462 
463  \return 0 on success
464  \return -1 on error
465  */
466 int dig_isle_alloc_line(struct P_isle *isle, int add)
467 {
468  int num;
469  char *p;
470 
471  G_debug(5, "dig_isle_alloc_line():");
472  num = isle->alloc_lines + add;
473 
474  p = G_realloc(isle->lines, num * sizeof(plus_t));
475  if (p == NULL)
476  return -1;
477  isle->lines = (plus_t *)p;
478 
479  isle->alloc_lines = num;
480 
481  return (0);
482 }
483 
484 /*!
485  \brief For now just print message and return error code
486  */
488 {
489  G_warning(_("Out of memory"));
490  return -1;
491 }
#define NULL
Definition: ccmath.h:32
void G_zero(void *, int)
Zero out a buffer, buf, of length i.
Definition: gis/zero.c:23
void G_free(void *)
Free allocated memory.
Definition: gis/alloc.c:150
#define G_realloc(p, n)
Definition: defs/gis.h:96
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
#define GV_CENTROID
Definition: dig_defines.h:186
#define GV_LINE
Definition: dig_defines.h:184
#define GV_BOUNDARY
Definition: dig_defines.h:185
#define GV_FACE
Definition: dig_defines.h:187
#define GV_KERNEL
Definition: dig_defines.h:188
void * dig__alloc_space(int, int *, int, void *, int)
Definition: allocation.c:49
int plus_t
plus_t size
Definition: dig_structs.h:41
#define _(str)
Definition: glocale.h:10
void free(void *)
Area (topology) info.
Definition: dig_structs.h:1583
plus_t * isles
1st generation interior islands
Definition: dig_structs.h:1617
plus_t * lines
List of boundary lines.
Definition: dig_structs.h:1598
plus_t alloc_lines
Allocated space for lines.
Definition: dig_structs.h:1591
plus_t alloc_isles
Allocated space for isles.
Definition: dig_structs.h:1613
Isle (topology) info.
Definition: dig_structs.h:1623
plus_t * lines
List of boundary lines.
Definition: dig_structs.h:1638
plus_t alloc_lines
Allocated space for lines.
Definition: dig_structs.h:1631
plus_t area
Area it exists w/in, if any.
Definition: dig_structs.h:1645
Vector geometry.
Definition: dig_structs.h:1553
char type
Line type.
Definition: dig_structs.h:1564
void * topo
Topology info.
Definition: dig_structs.h:1577
Topological feature - node.
Definition: dig_structs.h:1433
plus_t alloc_lines
Allocated space for lines.
Definition: dig_structs.h:1449
plus_t n_lines
Number of attached lines (size of lines, angle)
Definition: dig_structs.h:1456
float * angles
List of angles of connected lines.
Definition: dig_structs.h:1472
plus_t * lines
List of connected lines.
Definition: dig_structs.h:1463
Boundary topology.
Definition: dig_structs.h:1492
Centroid topology.
Definition: dig_structs.h:1514
Face topology.
Definition: dig_structs.h:1524
Kernel topology.
Definition: dig_structs.h:1543
Line topology.
Definition: dig_structs.h:1478
Basic topology-related info.
Definition: dig_structs.h:769
plus_t alloc_lines
Number of allocated lines.
Definition: dig_structs.h:970
struct P_line ** Line
Array of vector geometries.
Definition: dig_structs.h:871
plus_t alloc_areas
Number of allocated areas.
Definition: dig_structs.h:976
struct P_area ** Area
Array of areas.
Definition: dig_structs.h:875
plus_t alloc_isles
Number of allocated isles.
Definition: dig_structs.h:982
plus_t alloc_nodes
Number of allocated nodes.
Definition: dig_structs.h:958
struct P_isle ** Isle
Array of isles.
Definition: dig_structs.h:879
struct P_node ** Node
Array of nodes.
Definition: dig_structs.h:867
void dig_free_isle(struct P_isle *Isle)
Free isle structure.
Definition: struct_alloc.c:319
struct P_isle * dig_alloc_isle(void)
Allocate new isle structure.
Definition: struct_alloc.c:301
int dig_area_alloc_line(struct P_area *area, int add)
allocate space in P_area for add new lines
Definition: struct_alloc.c:414
int dig_alloc_points(struct line_pnts *points, int num)
allocate room for 'num' X and Y arrays in struct line_pnts
Definition: struct_alloc.c:336
void * dig_alloc_topo(char type)
Allocate new topo struct.
Definition: struct_alloc.c:145
int dig_alloc_isles(struct Plus_head *Plus, int add)
Reallocate array of pointers to isles.
Definition: struct_alloc.c:243
int dig_alloc_areas(struct Plus_head *Plus, int add)
Reallocate array of pointers to areas.
Definition: struct_alloc.c:218
int dig_area_alloc_isle(struct P_area *area, int add)
Allocate space in P_area for add new isles.
Definition: struct_alloc.c:440
int dig_alloc_nodes(struct Plus_head *Plus, int add)
Reallocate array of pointers to nodes.
Definition: struct_alloc.c:105
int dig_alloc_lines(struct Plus_head *Plus, int add)
Reallocate array of pointers to lines.
Definition: struct_alloc.c:193
int dig_node_alloc_line(struct P_node *node, int add)
Allocate space in P_node struct.
Definition: struct_alloc.c:69
void dig_free_node(struct P_node *Node)
Free node structure.
Definition: struct_alloc.c:48
int dig_alloc_cats(struct line_cats *cats, int num)
Allocate room for 'num' fields and category arrays in struct line_cats.
Definition: struct_alloc.c:380
void dig_free_line(struct P_line *Line)
Free line structure.
Definition: struct_alloc.c:177
struct P_area * dig_alloc_area(void)
Allocate new area structure.
Definition: struct_alloc.c:266
void dig_free_area(struct P_area *Area)
Free area structure.
Definition: struct_alloc.c:284
struct P_line * dig_alloc_line(void)
Allocate new line structure.
Definition: struct_alloc.c:127
int dig_isle_alloc_line(struct P_isle *isle, int add)
Allocate space in P_isle for add new lines.
Definition: struct_alloc.c:466
int dig_out_of_memory(void)
For now just print message and return error code.
Definition: struct_alloc.c:487
struct P_node * dig_alloc_node(void)
Allocate new node structure.
Definition: struct_alloc.c:30
Feature category info.
Definition: dig_structs.h:1677
int * field
Array of layers (fields)
Definition: dig_structs.h:1681
int alloc_cats
Allocated space for categories.
Definition: dig_structs.h:1693
int * cat
Array of categories.
Definition: dig_structs.h:1685
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
double * y
Array of Y coordinates.
Definition: dig_structs.h:1659
int alloc_points
Allocated space for points.
Definition: dig_structs.h:1671
double * x
Array of X coordinates.
Definition: dig_structs.h:1655
double * z
Array of Z coordinates.
Definition: dig_structs.h:1663