~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/vector/diglib/struct_alloc.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 *****************************************************************************/
 
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 neccessarily 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
*/
18
19
 
19
20
#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()
 
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()
41
31
{
42
 
    P_NODE *Node;
 
32
    struct P_node *Node;
43
33
 
44
 
    Node = (P_NODE *) G_malloc(sizeof(P_NODE));
 
34
    Node = (struct P_node *) G_malloc(sizeof(struct P_node));
45
35
    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)
 
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)
64
70
{
65
71
    int num;
66
72
    char *p;
71
77
 
72
78
    p = G_realloc(node->lines, num * sizeof(plus_t));
73
79
    if (p == NULL)
74
 
        return -1;
 
80
        return -1;
75
81
    node->lines = (plus_t *) p;
76
82
 
77
83
    p = G_realloc(node->angles, num * sizeof(float));
78
84
    if (p == NULL)
79
 
        return -1;
 
85
        return -1;
80
86
    node->angles = (float *)p;
81
87
 
82
88
    node->alloc_lines = num;
83
 
    return (0);
 
89
    
 
90
    return 0;
84
91
}
85
92
 
 
93
/*!
 
94
  \brief Reallocate array of pointers to nodes
 
95
  
 
96
  \param Plus pointer to Plus_head structure
 
97
  \param add number of nodes to be added
86
98
 
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
 
 */
 
99
  \return 0 on success
 
100
  \return -1 on error
 
101
*/
93
102
int dig_alloc_nodes(struct Plus_head *Plus, int add)
94
103
{
95
104
    int size;
96
105
    char *p;
97
106
 
98
107
    size = Plus->alloc_nodes + 1 + add;
99
 
    p = G_realloc(Plus->Node, size * sizeof(P_NODE *));
 
108
    p = G_realloc(Plus->Node, size * sizeof(struct P_node *));
100
109
    if (p == NULL)
101
 
        return -1;
 
110
        return -1;
102
111
 
103
 
    Plus->Node = (P_NODE **) p;
 
112
    Plus->Node = (struct P_node **) p;
104
113
    Plus->alloc_nodes = size - 1;
105
114
 
106
 
    return (0);
 
115
    return 0;
107
116
}
108
117
 
109
 
/* allocate new line structure */
110
 
P_LINE *dig_alloc_line()
 
118
/*!
 
119
  \brief Allocate new line structure
 
120
 
 
121
  \return pointer to allocated P_node struct
 
122
  \return NULL on error
 
123
*/
 
124
struct P_line *dig_alloc_line()
111
125
{
112
 
    P_LINE *Line;
 
126
    struct P_line *Line;
113
127
 
114
 
    Line = (P_LINE *) G_malloc(sizeof(P_LINE));
 
128
    Line = (struct P_line *) G_malloc(sizeof(struct P_line));
115
129
    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
 
 */
 
130
        return NULL;
 
131
    
 
132
    G_zero(Line, sizeof(struct P_line));
 
133
    
 
134
    return Line;
 
135
}
 
136
 
 
137
/*!
 
138
  \brief Allocate new topo struct
 
139
 
 
140
  \param type to of struct to allocate
 
141
*/
 
142
void *dig_alloc_topo(char type)
 
143
{
 
144
    void *Topo = NULL;
 
145
 
 
146
    switch (type) {
 
147
        case GV_LINE:
 
148
        Topo = G_malloc(sizeof(struct P_topo_l));
 
149
        break;
 
150
    case GV_BOUNDARY:
 
151
        Topo = G_malloc(sizeof(struct P_topo_b));
 
152
        break;
 
153
    case GV_CENTROID:
 
154
        Topo = G_malloc(sizeof(struct P_topo_c));
 
155
        break;
 
156
    case GV_FACE:
 
157
        Topo = G_malloc(sizeof(struct P_topo_f));
 
158
        break;
 
159
    case GV_KERNEL:
 
160
        Topo = G_malloc(sizeof(struct P_topo_k));
 
161
        break;
 
162
    default:
 
163
        return NULL;
 
164
    }
 
165
 
 
166
    return Topo;
 
167
}
 
168
 
 
169
/*!
 
170
  \brief Free line structure
 
171
 
 
172
  \param pointer to P_line struct to be freed
 
173
*/
 
174
void dig_free_line(struct P_line *Line)
 
175
{
 
176
    if (Line->topo)
 
177
        G_free(Line->topo);
 
178
    G_free(Line);
 
179
}
 
180
 
 
181
/*!
 
182
  \brief Reallocate array of pointers to lines.
 
183
  
 
184
  \param Plus pointer to Plus_head structure
 
185
  \param add space for 'add' number of lines is added.
 
186
 
 
187
  \return 0 on success
 
188
  \return -1 on error
 
189
*/
127
190
int dig_alloc_lines(struct Plus_head *Plus, int add)
128
191
{
129
192
    int size;
130
193
    char *p;
131
194
 
132
195
    size = Plus->alloc_lines + 1 + add;
133
 
    p = G_realloc(Plus->Line, size * sizeof(P_LINE *));
 
196
    p = G_realloc(Plus->Line, size * sizeof(struct P_line *));
134
197
    if (p == NULL)
135
 
        return -1;
 
198
        return -1;
136
199
 
137
 
    Plus->Line = (P_LINE **) p;
 
200
    Plus->Line = (struct P_line **) p;
138
201
    Plus->alloc_lines = size - 1;
139
202
 
140
 
    return (0);
 
203
    return 0;
141
204
}
142
205
 
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
 
 */
 
206
/*!
 
207
  \brief Reallocate array of pointers to areas.
 
208
 
 
209
  \param Plus pointer to Plus_head structure
 
210
  \param add space for 'add' number of areas is added
 
211
 
 
212
  \return 0 on success
 
213
  \return -1 on error
 
214
*/
149
215
int dig_alloc_areas(struct Plus_head *Plus, int add)
150
216
{
151
217
    int size;
152
218
    char *p;
153
219
 
154
220
    size = Plus->alloc_areas + 1 + add;
155
 
    p = G_realloc(Plus->Area, size * sizeof(P_AREA *));
 
221
    p = G_realloc(Plus->Area, size * sizeof(struct P_area *));
156
222
    if (p == NULL)
157
 
        return -1;
 
223
        return -1;
158
224
 
159
 
    Plus->Area = (P_AREA **) p;
 
225
    Plus->Area = (struct P_area **) p;
160
226
    Plus->alloc_areas = size - 1;
161
227
 
162
 
    return (0);
 
228
    return 0;
163
229
}
164
230
 
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
 
 */
 
231
/*!
 
232
  \brief Reallocate array of pointers to isles
 
233
 
 
234
  \param Plus pointer to Plus_head structure
 
235
  \param add space for 'add' number of isles is added.
 
236
 
 
237
  \return 0 on success
 
238
  \return -1 on error
 
239
*/
171
240
int dig_alloc_isles(struct Plus_head *Plus, int add)
172
241
{
173
242
    int size;
175
244
 
176
245
    G_debug(3, "dig_alloc_isle():");
177
246
    size = Plus->alloc_isles + 1 + add;
178
 
    p = G_realloc(Plus->Isle, size * sizeof(P_ISLE *));
 
247
    p = G_realloc(Plus->Isle, size * sizeof(struct P_isle *));
179
248
    if (p == NULL)
180
 
        return -1;
 
249
        return -1;
181
250
 
182
 
    Plus->Isle = (P_ISLE **) p;
 
251
    Plus->Isle = (struct P_isle **) p;
183
252
    Plus->alloc_isles = size - 1;
184
253
 
185
 
    return (0);
 
254
    return 0;
186
255
}
187
256
 
188
 
/* allocate new area structure */
189
 
P_AREA *dig_alloc_area()
 
257
/*!
 
258
  \brief Allocate new area structure 
 
259
 
 
260
  \return pointer to allocated P_area struct
 
261
  \return NULL on error
 
262
*/
 
263
struct P_area *dig_alloc_area()
190
264
{
191
 
    P_AREA *Area;
 
265
    struct P_area *Area;
192
266
 
193
 
    Area = (P_AREA *) G_malloc(sizeof(P_AREA));
 
267
    Area = (struct P_area *) G_malloc(sizeof(struct P_area));
194
268
    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));
 
269
        return NULL;
 
270
 
 
271
    G_zero(Area, sizeof(struct P_area));
 
272
 
 
273
    return Area;
 
274
}
 
275
 
 
276
/*!
 
277
  \brief Free area structure
 
278
 
 
279
  \param Area pointer to P_area struct to be freed
 
280
*/
 
281
void dig_free_area(struct P_area *Area)
 
282
{
 
283
    if (Area->alloc_lines > 0)
 
284
        free(Area->lines);
 
285
 
 
286
    if (Area->alloc_isles > 0)
 
287
        free(Area->isles);
 
288
 
 
289
    G_free(Area);
 
290
}
 
291
 
 
292
/*!
 
293
  \brief Allocate new isle structure 
 
294
 
 
295
  \return pointer to allocated P_isle struct
 
296
  \return NULL on error
 
297
*/
 
298
struct P_isle *dig_alloc_isle()
 
299
{
 
300
    struct P_isle *Isle;
 
301
 
 
302
    Isle = (struct P_isle *) G_malloc(sizeof(struct P_isle));
216
303
    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
 
 */
 
304
        return NULL;
 
305
 
 
306
    G_zero(Isle, sizeof(struct P_isle));
 
307
 
 
308
    return Isle;
 
309
}
 
310
 
 
311
/*!
 
312
  \brief Free isle structure
 
313
 
 
314
  \param Isle pointer to P_isle struct to be freed
 
315
*/
 
316
void dig_free_isle(struct P_isle *Isle)
 
317
{
 
318
    if (Isle->alloc_lines > 0)
 
319
        G_free(Isle->lines);
 
320
 
 
321
    G_free(Isle);
 
322
}
 
323
 
 
324
/*!
 
325
  \brief allocate room for 'num' X and Y  arrays in struct line_pnts 
 
326
  
 
327
  \param points pointer to line_pnts struct
 
328
  \param num number of points
 
329
  
 
330
  \return 0 on success
 
331
  \return returns -1 on out of memory 
 
332
*/
232
333
int dig_alloc_points(struct line_pnts *points, int num)
233
334
{
234
335
    int alloced;
237
338
    alloced = points->alloc_points;
238
339
    /* alloc_space will just return if no space is needed */
239
340
    if (!(p =
240
 
          dig__alloc_space(num, &alloced, 50, (char *)points->x,
241
 
                           sizeof(double)))) {
242
 
        return (dig_out_of_memory());
 
341
          dig__alloc_space(num, &alloced, 50, (char *)points->x,
 
342
                           sizeof(double)))) {
 
343
        return (dig_out_of_memory());
243
344
    }
244
345
    points->x = (double *)p;
245
346
 
246
347
    alloced = points->alloc_points;
247
348
    /* alloc_space will just return if no space is needed */
248
349
    if (!(p =
249
 
          dig__alloc_space(num, &alloced, 50, (char *)points->y,
250
 
                           sizeof(double)))) {
251
 
        return (dig_out_of_memory());
 
350
          dig__alloc_space(num, &alloced, 50, (char *)points->y,
 
351
                           sizeof(double)))) {
 
352
        return (dig_out_of_memory());
252
353
    }
253
354
    points->y = (double *)p;
254
355
 
255
356
    alloced = points->alloc_points;
256
357
    /* alloc_space will just return if no space is needed */
257
358
    if (!(p =
258
 
          dig__alloc_space(num, &alloced, 50, (char *)points->z,
259
 
                           sizeof(double)))) {
260
 
        return (dig_out_of_memory());
 
359
          dig__alloc_space(num, &alloced, 50, (char *)points->z,
 
360
                           sizeof(double)))) {
 
361
        return (dig_out_of_memory());
261
362
    }
262
363
    points->z = (double *)p;
263
364
 
264
365
    points->alloc_points = alloced;
265
 
    return (0);
 
366
    
 
367
    return 0;
266
368
}
267
369
 
268
 
/* allocate room for  'num'  fields and category arrays 
269
 
 ** in struct line_cats 
270
 
 **   returns -1 on out of memory 
271
 
 */
 
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
*/
272
380
int dig_alloc_cats(struct line_cats *cats, int num)
273
381
{
274
382
    int alloced;
277
385
    /* alloc_space will just return if no space is needed */
278
386
    alloced = cats->alloc_cats;
279
387
    if (!(p =
280
 
          dig__alloc_space(num, &alloced, 1, (int *)cats->field,
281
 
                           sizeof(int)))) {
282
 
        return (dig_out_of_memory());
 
388
          dig__alloc_space(num, &alloced, 1, (int *)cats->field,
 
389
                           sizeof(int)))) {
 
390
        return dig_out_of_memory();
283
391
    }
284
392
    cats->field = (int *)p;
285
393
 
286
394
    alloced = cats->alloc_cats;
287
395
    if (!(p =
288
 
          dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
289
 
                           sizeof(int)))) {
290
 
        return (dig_out_of_memory());
 
396
          dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
 
397
                           sizeof(int)))) {
 
398
        return dig_out_of_memory();
291
399
    }
292
400
    cats->cat = (int *)p;
293
401
 
294
402
    cats->alloc_cats = alloced;
295
 
    return (0);
 
403
    
 
404
    return 0;
296
405
}
297
406
 
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
 
407
/*!
 
408
  \brief allocate space in  P_area for add new lines
 
409
 
 
410
  \param area pointer to P_area struct
 
411
  \param add number of lines to be added
 
412
 
 
413
  \return 0 on success
 
414
  \return -1 on error
302
415
 */
303
 
int dig_area_alloc_line(P_AREA * area, int add)
 
416
int dig_area_alloc_line(struct P_area * area, int add)
304
417
{
305
418
    int num;
306
419
    char *p;
309
422
 
310
423
    p = G_realloc(area->lines, num * sizeof(plus_t));
311
424
    if (p == NULL)
312
 
        return -1;
 
425
        return -1;
313
426
    area->lines = (plus_t *) p;
314
427
 
315
428
    area->alloc_lines = num;
317
430
    return (0);
318
431
}
319
432
 
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)
 
433
/*!
 
434
  \brief Allocate space in  P_area for add new isles
 
435
 
 
436
  \param area pointer to P_area struct
 
437
  \param add number of isle to be added
 
438
 
 
439
  \return 0 on success
 
440
  \return -1 on error
 
441
*/
 
442
int dig_area_alloc_isle(struct P_area * area, int add)
326
443
{
327
444
    int num;
328
445
    char *p;
332
449
 
333
450
    p = G_realloc(area->isles, num * sizeof(plus_t));
334
451
    if (p == NULL)
335
 
        return -1;
 
452
        return -1;
336
453
    area->isles = (plus_t *) p;
337
454
 
338
455
    area->alloc_isles = num;
339
456
    return (0);
340
457
}
341
458
 
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)
 
459
/*!
 
460
  \brief Allocate space in  P_isle for add new lines
 
461
 
 
462
  \param isle pointer to P_area struct
 
463
  \param add number of isle to be added
 
464
 
 
465
  \return 0 on success
 
466
  \return -1 on error
 
467
*/
 
468
int dig_isle_alloc_line(struct P_isle * isle, int add)
349
469
{
350
470
    int num;
351
471
    char *p;
355
475
 
356
476
    p = G_realloc(isle->lines, num * sizeof(plus_t));
357
477
    if (p == NULL)
358
 
        return -1;
 
478
        return -1;
359
479
    isle->lines = (plus_t *) p;
360
480
 
361
481
    isle->alloc_lines = num;
363
483
    return (0);
364
484
}
365
485
 
366
 
 
367
 
 
368
 
/* for now just print message and return error code */
 
486
/*!
 
487
  \brief For now just print message and return error code
 
488
*/
369
489
int dig_out_of_memory()
370
490
{
371
 
    fprintf(stderr, "OUT OF MEMORY!\n");
372
 
    return (-1);
 
491
    G_warning(_("Out of memmory"));
 
492
    return -1;
373
493
}