struct_alloc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*!
  2. \file lib/vector/diglib/plus_node.c
  3. \brief Vector library - update topo for nodes (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 neccessarily 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 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(3, "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 add space for 'add' number of lines is added.
  142. \return 0 on success
  143. \return -1 on error
  144. */
  145. int dig_alloc_lines(struct Plus_head *Plus, int add)
  146. {
  147. int size;
  148. char *p;
  149. size = Plus->alloc_lines + 1 + add;
  150. p = G_realloc(Plus->Line, size * sizeof(struct P_line *));
  151. if (p == NULL)
  152. return -1;
  153. Plus->Line = (struct P_line **) p;
  154. Plus->alloc_lines = size - 1;
  155. return 0;
  156. }
  157. /*!
  158. \brief Reallocate array of pointers to areas.
  159. \param add space for 'add' number of areas is added
  160. \return 0 on success
  161. \return -1 on error
  162. */
  163. int dig_alloc_areas(struct Plus_head *Plus, int add)
  164. {
  165. int size;
  166. char *p;
  167. size = Plus->alloc_areas + 1 + add;
  168. p = G_realloc(Plus->Area, size * sizeof(struct P_area *));
  169. if (p == NULL)
  170. return -1;
  171. Plus->Area = (struct P_area **) p;
  172. Plus->alloc_areas = size - 1;
  173. return 0;
  174. }
  175. /*!
  176. \brief Reallocate array of pointers to isles
  177. \param add space for 'add' number of isles is added.
  178. \return 0 on success
  179. \return -1 on error
  180. */
  181. int dig_alloc_isles(struct Plus_head *Plus, int add)
  182. {
  183. int size;
  184. char *p;
  185. G_debug(3, "dig_alloc_isle():");
  186. size = Plus->alloc_isles + 1 + add;
  187. p = G_realloc(Plus->Isle, size * sizeof(struct P_isle *));
  188. if (p == NULL)
  189. return -1;
  190. Plus->Isle = (struct P_isle **) p;
  191. Plus->alloc_isles = size - 1;
  192. return 0;
  193. }
  194. /*!
  195. \brief Allocate new area structure
  196. \return pointer to allocated P_area struct
  197. \return NULL on error
  198. */
  199. struct P_area *dig_alloc_area()
  200. {
  201. struct P_area *Area;
  202. Area = (struct P_area *) G_malloc(sizeof(struct P_area));
  203. if (Area == NULL)
  204. return NULL;
  205. G_zero(Area, sizeof(struct P_area));
  206. return Area;
  207. }
  208. /*!
  209. \brief Free area structure
  210. \param pointer to P_area struct to be freed
  211. */
  212. void dig_free_area(struct P_area *Area)
  213. {
  214. if (Area->alloc_lines > 0)
  215. free(Area->lines);
  216. if (Area->alloc_isles > 0)
  217. free(Area->isles);
  218. G_free(Area);
  219. }
  220. /*!
  221. \brief Allocate new isle structure
  222. \return pointer to allocated P_isle struct
  223. \return NULL on error
  224. */
  225. struct P_isle *dig_alloc_isle()
  226. {
  227. struct P_isle *Isle;
  228. Isle = (struct P_isle *) G_malloc(sizeof(struct P_isle));
  229. if (Isle == NULL)
  230. return NULL;
  231. G_zero(Isle, sizeof(struct P_isle));
  232. return Isle;
  233. }
  234. /*!
  235. \brief Free isle structure
  236. \param pointer to P_isle struct to be freed
  237. */
  238. void dig_free_isle(struct P_isle *Isle)
  239. {
  240. if (Isle->alloc_lines > 0)
  241. G_free(Isle->lines);
  242. G_free(Isle);
  243. }
  244. /*!
  245. \brief allocate room for 'num' X and Y arrays in struct line_pnts
  246. \param points pointer to line_pnts struct
  247. \param num number of points
  248. \return 0 on success
  249. \return returns -1 on out of memory
  250. */
  251. int dig_alloc_points(struct line_pnts *points, int num)
  252. {
  253. int alloced;
  254. char *p;
  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->x,
  259. sizeof(double)))) {
  260. return (dig_out_of_memory());
  261. }
  262. points->x = (double *)p;
  263. alloced = points->alloc_points;
  264. /* alloc_space will just return if no space is needed */
  265. if (!(p =
  266. dig__alloc_space(num, &alloced, 50, (char *)points->y,
  267. sizeof(double)))) {
  268. return (dig_out_of_memory());
  269. }
  270. points->y = (double *)p;
  271. alloced = points->alloc_points;
  272. /* alloc_space will just return if no space is needed */
  273. if (!(p =
  274. dig__alloc_space(num, &alloced, 50, (char *)points->z,
  275. sizeof(double)))) {
  276. return (dig_out_of_memory());
  277. }
  278. points->z = (double *)p;
  279. points->alloc_points = alloced;
  280. return 0;
  281. }
  282. /*!
  283. \brief Allocate room for 'num' fields and category arrays
  284. in struct line_cats
  285. \param points pointer to line_cats struct
  286. \param num number of cats
  287. \return 0 on success
  288. \return returns -1 on out of memory
  289. */
  290. int dig_alloc_cats(struct line_cats *cats, int num)
  291. {
  292. int alloced;
  293. char *p;
  294. /* alloc_space will just return if no space is needed */
  295. alloced = cats->alloc_cats;
  296. if (!(p =
  297. dig__alloc_space(num, &alloced, 1, (int *)cats->field,
  298. sizeof(int)))) {
  299. return dig_out_of_memory();
  300. }
  301. cats->field = (int *)p;
  302. alloced = cats->alloc_cats;
  303. if (!(p =
  304. dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
  305. sizeof(int)))) {
  306. return dig_out_of_memory();
  307. }
  308. cats->cat = (int *)p;
  309. cats->alloc_cats = alloced;
  310. return 0;
  311. }
  312. /*!
  313. \brief allocate space in P_area for add new lines
  314. \param area pointer to P_area struct
  315. \param add number of lines to be added
  316. \return 0 on success
  317. \return -1 on error
  318. */
  319. int dig_area_alloc_line(struct P_area * area, int add)
  320. {
  321. int num;
  322. char *p;
  323. num = area->alloc_lines + add;
  324. p = G_realloc(area->lines, num * sizeof(plus_t));
  325. if (p == NULL)
  326. return -1;
  327. area->lines = (plus_t *) p;
  328. area->alloc_lines = num;
  329. return (0);
  330. }
  331. /*!
  332. \brief Allocate space in P_area for add new isles
  333. \param area pointer to P_area struct
  334. \param add number of isle to be added
  335. \return 0 on success
  336. \return -1 on error
  337. */
  338. int dig_area_alloc_isle(struct P_area * area, int add)
  339. {
  340. int num;
  341. char *p;
  342. G_debug(5, "dig_area_alloc_isle(): add = %d", add);
  343. num = area->alloc_isles + add;
  344. p = G_realloc(area->isles, num * sizeof(plus_t));
  345. if (p == NULL)
  346. return -1;
  347. area->isles = (plus_t *) p;
  348. area->alloc_isles = num;
  349. return (0);
  350. }
  351. /*!
  352. \brief Allocate space in P_isle for add new lines
  353. \param area pointer to P_area struct
  354. \param add number of isle to be added
  355. \return 0 on success
  356. \return -1 on error
  357. */
  358. int dig_isle_alloc_line(struct P_isle * isle, int add)
  359. {
  360. int num;
  361. char *p;
  362. G_debug(3, "dig_isle_alloc_line():");
  363. num = isle->alloc_lines + add;
  364. p = G_realloc(isle->lines, num * sizeof(plus_t));
  365. if (p == NULL)
  366. return -1;
  367. isle->lines = (plus_t *) p;
  368. isle->alloc_lines = num;
  369. return (0);
  370. }
  371. /*!
  372. \brief For now just print message and return error code
  373. */
  374. int dig_out_of_memory()
  375. {
  376. G_warning(_("Out of memmory"));
  377. return -1;
  378. }