struct_alloc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /*!
  2. \file lib/vector/diglib/struct_alloc.c
  3. \brief Vector library - allocate and zero array space (lower level functions)
  4. Lower level functions for reading/writing/manipulating vectors.
  5. These routines all eventually call calloc() to allocate and zero the
  6. new space. BUT It is not necessarily safe to assume that the memory
  7. will be zero. The next memory location asked for could have been
  8. previously used and not zeroed. (e.g. compress()).
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. \author CERL (probably Dave Gerdes)
  12. \author Radim Blazek
  13. */
  14. #include <stdlib.h>
  15. #include <grass/vector.h>
  16. #include <grass/glocale.h>
  17. /*!
  18. \brief Allocate new node structure
  19. \return pointer to allocated P_node struct
  20. \return NULL on error
  21. */
  22. struct P_node *dig_alloc_node()
  23. {
  24. struct P_node *Node;
  25. Node = (struct P_node *) G_malloc(sizeof(struct P_node));
  26. if (Node == NULL)
  27. return NULL;
  28. G_zero(Node, sizeof(struct P_node));
  29. return Node;
  30. }
  31. /*!
  32. \brief Free node structure
  33. \param Node pointer to P_node struct to be freed
  34. */
  35. void dig_free_node(struct P_node *Node)
  36. {
  37. if (Node->alloc_lines > 0) {
  38. G_free(Node->lines);
  39. G_free(Node->angles);
  40. }
  41. G_free(Node);
  42. }
  43. /*!
  44. \brief Allocate space in P_node struct
  45. Lines and angles arrays to add 'add' more lines
  46. \param node pointer to P_node struct
  47. \param add number lines to be added
  48. \return 0 on success
  49. \return -1 on error
  50. */
  51. int dig_node_alloc_line(struct P_node * node, int add)
  52. {
  53. int num;
  54. char *p;
  55. G_debug(5, "dig_node_alloc_line(): add = %d", add);
  56. num = node->n_lines + add;
  57. p = G_realloc(node->lines, num * sizeof(plus_t));
  58. if (p == NULL)
  59. return -1;
  60. node->lines = (plus_t *) p;
  61. p = G_realloc(node->angles, num * sizeof(float));
  62. if (p == NULL)
  63. return -1;
  64. node->angles = (float *)p;
  65. node->alloc_lines = num;
  66. return 0;
  67. }
  68. /*!
  69. \brief Reallocate array of pointers to nodes
  70. \param Plus pointer to Plus_head structure
  71. \param add number of nodes to be added
  72. \return 0 on success
  73. \return -1 on error
  74. */
  75. int dig_alloc_nodes(struct Plus_head *Plus, int add)
  76. {
  77. int size;
  78. char *p;
  79. size = Plus->alloc_nodes + 1 + add;
  80. p = G_realloc(Plus->Node, size * sizeof(struct P_node *));
  81. if (p == NULL)
  82. return -1;
  83. Plus->Node = (struct P_node **) p;
  84. Plus->alloc_nodes = size - 1;
  85. return 0;
  86. }
  87. /*!
  88. \brief Allocate new line structure
  89. \return pointer to allocated P_node struct
  90. \return NULL on error
  91. */
  92. struct P_line *dig_alloc_line()
  93. {
  94. struct P_line *Line;
  95. Line = (struct P_line *) G_malloc(sizeof(struct P_line));
  96. if (Line == NULL)
  97. return NULL;
  98. G_zero(Line, sizeof(struct P_line));
  99. return Line;
  100. }
  101. /*!
  102. \brief Allocate new topo struct
  103. \param type to of struct to allocate
  104. */
  105. void *dig_alloc_topo(char type)
  106. {
  107. void *Topo = NULL;
  108. switch (type) {
  109. case GV_LINE:
  110. Topo = G_malloc(sizeof(struct P_topo_l));
  111. break;
  112. case GV_BOUNDARY:
  113. Topo = G_malloc(sizeof(struct P_topo_b));
  114. break;
  115. case GV_CENTROID:
  116. Topo = G_malloc(sizeof(struct P_topo_c));
  117. break;
  118. case GV_FACE:
  119. Topo = G_malloc(sizeof(struct P_topo_f));
  120. break;
  121. case GV_KERNEL:
  122. Topo = G_malloc(sizeof(struct P_topo_k));
  123. break;
  124. default:
  125. return NULL;
  126. }
  127. return Topo;
  128. }
  129. /*!
  130. \brief Free line structure
  131. \param pointer to P_line struct to be freed
  132. */
  133. void dig_free_line(struct P_line *Line)
  134. {
  135. if (Line->topo)
  136. G_free(Line->topo);
  137. G_free(Line);
  138. }
  139. /*!
  140. \brief Reallocate array of pointers to lines.
  141. \param Plus pointer to Plus_head structure
  142. \param add space for 'add' number of lines is added.
  143. \return 0 on success
  144. \return -1 on error
  145. */
  146. int dig_alloc_lines(struct Plus_head *Plus, int add)
  147. {
  148. int size;
  149. char *p;
  150. size = Plus->alloc_lines + 1 + add;
  151. p = G_realloc(Plus->Line, size * sizeof(struct P_line *));
  152. if (p == NULL)
  153. return -1;
  154. Plus->Line = (struct P_line **) p;
  155. Plus->alloc_lines = size - 1;
  156. return 0;
  157. }
  158. /*!
  159. \brief Reallocate array of pointers to areas.
  160. \param Plus pointer to Plus_head structure
  161. \param add space for 'add' number of areas is added
  162. \return 0 on success
  163. \return -1 on error
  164. */
  165. int dig_alloc_areas(struct Plus_head *Plus, int add)
  166. {
  167. int size;
  168. char *p;
  169. size = Plus->alloc_areas + 1 + add;
  170. p = G_realloc(Plus->Area, size * sizeof(struct P_area *));
  171. if (p == NULL)
  172. return -1;
  173. Plus->Area = (struct P_area **) p;
  174. Plus->alloc_areas = size - 1;
  175. return 0;
  176. }
  177. /*!
  178. \brief Reallocate array of pointers to isles
  179. \param Plus pointer to Plus_head structure
  180. \param add space for 'add' number of isles is added.
  181. \return 0 on success
  182. \return -1 on error
  183. */
  184. int dig_alloc_isles(struct Plus_head *Plus, int add)
  185. {
  186. int size;
  187. char *p;
  188. G_debug(5, "dig_alloc_isle():");
  189. size = Plus->alloc_isles + 1 + add;
  190. p = G_realloc(Plus->Isle, size * sizeof(struct P_isle *));
  191. if (p == NULL)
  192. return -1;
  193. Plus->Isle = (struct P_isle **) p;
  194. Plus->alloc_isles = size - 1;
  195. return 0;
  196. }
  197. /*!
  198. \brief Allocate new area structure
  199. \return pointer to allocated P_area struct
  200. \return NULL on error
  201. */
  202. struct P_area *dig_alloc_area()
  203. {
  204. struct P_area *Area;
  205. Area = (struct P_area *) G_malloc(sizeof(struct P_area));
  206. if (Area == NULL)
  207. return NULL;
  208. G_zero(Area, sizeof(struct P_area));
  209. return Area;
  210. }
  211. /*!
  212. \brief Free area structure
  213. \param Area pointer to P_area struct to be freed
  214. */
  215. void dig_free_area(struct P_area *Area)
  216. {
  217. if (Area->alloc_lines > 0)
  218. free(Area->lines);
  219. if (Area->alloc_isles > 0)
  220. free(Area->isles);
  221. G_free(Area);
  222. }
  223. /*!
  224. \brief Allocate new isle structure
  225. \return pointer to allocated P_isle struct
  226. \return NULL on error
  227. */
  228. struct P_isle *dig_alloc_isle()
  229. {
  230. struct P_isle *Isle;
  231. Isle = (struct P_isle *) G_malloc(sizeof(struct P_isle));
  232. if (Isle == NULL)
  233. return NULL;
  234. G_zero(Isle, sizeof(struct P_isle));
  235. return Isle;
  236. }
  237. /*!
  238. \brief Free isle structure
  239. \param Isle pointer to P_isle struct to be freed
  240. */
  241. void dig_free_isle(struct P_isle *Isle)
  242. {
  243. if (Isle->alloc_lines > 0)
  244. G_free(Isle->lines);
  245. G_free(Isle);
  246. }
  247. /*!
  248. \brief allocate room for 'num' X and Y arrays in struct line_pnts
  249. \param points pointer to line_pnts struct
  250. \param num number of points
  251. \return 0 on success
  252. \return returns -1 on out of memory
  253. */
  254. int dig_alloc_points(struct line_pnts *points, int num)
  255. {
  256. int alloced;
  257. char *p;
  258. alloced = points->alloc_points;
  259. /* alloc_space will just return if no space is needed */
  260. if (!(p =
  261. dig__alloc_space(num, &alloced, 50, (char *)points->x,
  262. sizeof(double)))) {
  263. return (dig_out_of_memory());
  264. }
  265. points->x = (double *)p;
  266. alloced = points->alloc_points;
  267. /* alloc_space will just return if no space is needed */
  268. if (!(p =
  269. dig__alloc_space(num, &alloced, 50, (char *)points->y,
  270. sizeof(double)))) {
  271. return (dig_out_of_memory());
  272. }
  273. points->y = (double *)p;
  274. alloced = points->alloc_points;
  275. /* alloc_space will just return if no space is needed */
  276. if (!(p =
  277. dig__alloc_space(num, &alloced, 50, (char *)points->z,
  278. sizeof(double)))) {
  279. return (dig_out_of_memory());
  280. }
  281. points->z = (double *)p;
  282. points->alloc_points = alloced;
  283. return 0;
  284. }
  285. /*!
  286. \brief Allocate room for 'num' fields and category arrays
  287. in struct line_cats
  288. \param cats pointer to line_cats struct
  289. \param num number of cats
  290. \return 0 on success
  291. \return returns -1 on out of memory
  292. */
  293. int dig_alloc_cats(struct line_cats *cats, int num)
  294. {
  295. int alloced;
  296. char *p;
  297. /* alloc_space will just return if no space is needed */
  298. alloced = cats->alloc_cats;
  299. if (!(p =
  300. dig__alloc_space(num, &alloced, 1, (int *)cats->field,
  301. sizeof(int)))) {
  302. return dig_out_of_memory();
  303. }
  304. cats->field = (int *)p;
  305. alloced = cats->alloc_cats;
  306. if (!(p =
  307. dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
  308. sizeof(int)))) {
  309. return dig_out_of_memory();
  310. }
  311. cats->cat = (int *)p;
  312. cats->alloc_cats = alloced;
  313. return 0;
  314. }
  315. /*!
  316. \brief allocate space in P_area for add new lines
  317. \param area pointer to P_area struct
  318. \param add number of lines to be added
  319. \return 0 on success
  320. \return -1 on error
  321. */
  322. int dig_area_alloc_line(struct P_area * area, int add)
  323. {
  324. int num;
  325. char *p;
  326. num = area->alloc_lines + add;
  327. p = G_realloc(area->lines, num * sizeof(plus_t));
  328. if (p == NULL)
  329. return -1;
  330. area->lines = (plus_t *) p;
  331. area->alloc_lines = num;
  332. return (0);
  333. }
  334. /*!
  335. \brief Allocate space in P_area for add new isles
  336. \param area pointer to P_area struct
  337. \param add number of isle to be added
  338. \return 0 on success
  339. \return -1 on error
  340. */
  341. int dig_area_alloc_isle(struct P_area * area, int add)
  342. {
  343. int num;
  344. char *p;
  345. G_debug(5, "dig_area_alloc_isle(): add = %d", add);
  346. num = area->alloc_isles + add;
  347. p = G_realloc(area->isles, num * sizeof(plus_t));
  348. if (p == NULL)
  349. return -1;
  350. area->isles = (plus_t *) p;
  351. area->alloc_isles = num;
  352. return (0);
  353. }
  354. /*!
  355. \brief Allocate space in P_isle for add new lines
  356. \param isle pointer to P_area struct
  357. \param add number of isle to be added
  358. \return 0 on success
  359. \return -1 on error
  360. */
  361. int dig_isle_alloc_line(struct P_isle * isle, int add)
  362. {
  363. int num;
  364. char *p;
  365. G_debug(5, "dig_isle_alloc_line():");
  366. num = isle->alloc_lines + add;
  367. p = G_realloc(isle->lines, num * sizeof(plus_t));
  368. if (p == NULL)
  369. return -1;
  370. isle->lines = (plus_t *) p;
  371. isle->alloc_lines = num;
  372. return (0);
  373. }
  374. /*!
  375. \brief For now just print message and return error code
  376. */
  377. int dig_out_of_memory()
  378. {
  379. G_warning(_("Out of memory"));
  380. return -1;
  381. }