struct_alloc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. if (node->n_lines + add <= node->alloc_lines)
  57. return 0;
  58. num = node->alloc_lines + add;
  59. p = G_realloc(node->lines, num * sizeof(plus_t));
  60. if (p == NULL)
  61. return -1;
  62. node->lines = (plus_t *) p;
  63. p = G_realloc(node->angles, num * sizeof(float));
  64. if (p == NULL)
  65. return -1;
  66. node->angles = (float *)p;
  67. node->alloc_lines = num;
  68. return 0;
  69. }
  70. /*!
  71. \brief Reallocate array of pointers to nodes
  72. \param Plus pointer to Plus_head structure
  73. \param add number of nodes to be added
  74. \return 0 on success
  75. \return -1 on error
  76. */
  77. int dig_alloc_nodes(struct Plus_head *Plus, int add)
  78. {
  79. int size;
  80. char *p;
  81. size = Plus->alloc_nodes + 1 + add;
  82. p = G_realloc(Plus->Node, size * sizeof(struct P_node *));
  83. if (p == NULL)
  84. return -1;
  85. Plus->Node = (struct P_node **) p;
  86. Plus->alloc_nodes = size - 1;
  87. return 0;
  88. }
  89. /*!
  90. \brief Allocate new line structure
  91. \return pointer to allocated P_node struct
  92. \return NULL on error
  93. */
  94. struct P_line *dig_alloc_line()
  95. {
  96. struct P_line *Line;
  97. Line = (struct P_line *) G_malloc(sizeof(struct P_line));
  98. if (Line == NULL)
  99. return NULL;
  100. G_zero(Line, sizeof(struct P_line));
  101. return Line;
  102. }
  103. /*!
  104. \brief Allocate new topo struct
  105. \param type to of struct to allocate
  106. */
  107. void *dig_alloc_topo(char type)
  108. {
  109. void *Topo = NULL;
  110. switch (type) {
  111. case GV_LINE:
  112. Topo = G_malloc(sizeof(struct P_topo_l));
  113. break;
  114. case GV_BOUNDARY:
  115. Topo = G_malloc(sizeof(struct P_topo_b));
  116. break;
  117. case GV_CENTROID:
  118. Topo = G_malloc(sizeof(struct P_topo_c));
  119. break;
  120. case GV_FACE:
  121. Topo = G_malloc(sizeof(struct P_topo_f));
  122. break;
  123. case GV_KERNEL:
  124. Topo = G_malloc(sizeof(struct P_topo_k));
  125. break;
  126. default:
  127. return NULL;
  128. }
  129. return Topo;
  130. }
  131. /*!
  132. \brief Free line structure
  133. \param pointer to P_line struct to be freed
  134. */
  135. void dig_free_line(struct P_line *Line)
  136. {
  137. if (Line->topo)
  138. G_free(Line->topo);
  139. G_free(Line);
  140. }
  141. /*!
  142. \brief Reallocate array of pointers to lines.
  143. \param Plus pointer to Plus_head structure
  144. \param add space for 'add' number of lines is added.
  145. \return 0 on success
  146. \return -1 on error
  147. */
  148. int dig_alloc_lines(struct Plus_head *Plus, int add)
  149. {
  150. int size;
  151. char *p;
  152. size = Plus->alloc_lines + 1 + add;
  153. p = G_realloc(Plus->Line, size * sizeof(struct P_line *));
  154. if (p == NULL)
  155. return -1;
  156. Plus->Line = (struct P_line **) p;
  157. Plus->alloc_lines = size - 1;
  158. return 0;
  159. }
  160. /*!
  161. \brief Reallocate array of pointers to areas.
  162. \param Plus pointer to Plus_head structure
  163. \param add space for 'add' number of areas is added
  164. \return 0 on success
  165. \return -1 on error
  166. */
  167. int dig_alloc_areas(struct Plus_head *Plus, int add)
  168. {
  169. int size;
  170. char *p;
  171. size = Plus->alloc_areas + 1 + add;
  172. p = G_realloc(Plus->Area, size * sizeof(struct P_area *));
  173. if (p == NULL)
  174. return -1;
  175. Plus->Area = (struct P_area **) p;
  176. Plus->alloc_areas = size - 1;
  177. return 0;
  178. }
  179. /*!
  180. \brief Reallocate array of pointers to isles
  181. \param Plus pointer to Plus_head structure
  182. \param add space for 'add' number of isles is added.
  183. \return 0 on success
  184. \return -1 on error
  185. */
  186. int dig_alloc_isles(struct Plus_head *Plus, int add)
  187. {
  188. int size;
  189. char *p;
  190. G_debug(5, "dig_alloc_isle():");
  191. size = Plus->alloc_isles + 1 + add;
  192. p = G_realloc(Plus->Isle, size * sizeof(struct P_isle *));
  193. if (p == NULL)
  194. return -1;
  195. Plus->Isle = (struct P_isle **) p;
  196. Plus->alloc_isles = size - 1;
  197. return 0;
  198. }
  199. /*!
  200. \brief Allocate new area structure
  201. \return pointer to allocated P_area struct
  202. \return NULL on error
  203. */
  204. struct P_area *dig_alloc_area()
  205. {
  206. struct P_area *Area;
  207. Area = (struct P_area *) G_malloc(sizeof(struct P_area));
  208. if (Area == NULL)
  209. return NULL;
  210. G_zero(Area, sizeof(struct P_area));
  211. return Area;
  212. }
  213. /*!
  214. \brief Free area structure
  215. \param Area pointer to P_area struct to be freed
  216. */
  217. void dig_free_area(struct P_area *Area)
  218. {
  219. if (Area->alloc_lines > 0)
  220. free(Area->lines);
  221. if (Area->alloc_isles > 0)
  222. free(Area->isles);
  223. G_free(Area);
  224. }
  225. /*!
  226. \brief Allocate new isle structure
  227. \return pointer to allocated P_isle struct
  228. \return NULL on error
  229. */
  230. struct P_isle *dig_alloc_isle()
  231. {
  232. struct P_isle *Isle;
  233. Isle = (struct P_isle *) G_malloc(sizeof(struct P_isle));
  234. if (Isle == NULL)
  235. return NULL;
  236. G_zero(Isle, sizeof(struct P_isle));
  237. return Isle;
  238. }
  239. /*!
  240. \brief Free isle structure
  241. \param Isle pointer to P_isle struct to be freed
  242. */
  243. void dig_free_isle(struct P_isle *Isle)
  244. {
  245. if (Isle->alloc_lines > 0)
  246. G_free(Isle->lines);
  247. G_free(Isle);
  248. }
  249. /*!
  250. \brief allocate room for 'num' X and Y arrays in struct line_pnts
  251. \param points pointer to line_pnts struct
  252. \param num number of points
  253. \return 0 on success
  254. \return returns -1 on out of memory
  255. */
  256. int dig_alloc_points(struct line_pnts *points, int num)
  257. {
  258. int alloced;
  259. char *p;
  260. alloced = points->alloc_points;
  261. /* alloc_space will just return if no space is needed */
  262. if (!(p =
  263. dig__alloc_space(num, &alloced, 50, (char *)points->x,
  264. sizeof(double)))) {
  265. return (dig_out_of_memory());
  266. }
  267. points->x = (double *)p;
  268. alloced = points->alloc_points;
  269. /* alloc_space will just return if no space is needed */
  270. if (!(p =
  271. dig__alloc_space(num, &alloced, 50, (char *)points->y,
  272. sizeof(double)))) {
  273. return (dig_out_of_memory());
  274. }
  275. points->y = (double *)p;
  276. alloced = points->alloc_points;
  277. /* alloc_space will just return if no space is needed */
  278. if (!(p =
  279. dig__alloc_space(num, &alloced, 50, (char *)points->z,
  280. sizeof(double)))) {
  281. return (dig_out_of_memory());
  282. }
  283. points->z = (double *)p;
  284. points->alloc_points = alloced;
  285. return 0;
  286. }
  287. /*!
  288. \brief Allocate room for 'num' fields and category arrays
  289. in struct line_cats
  290. \param cats pointer to line_cats struct
  291. \param num number of cats
  292. \return 0 on success
  293. \return returns -1 on out of memory
  294. */
  295. int dig_alloc_cats(struct line_cats *cats, int num)
  296. {
  297. int alloced;
  298. char *p;
  299. /* alloc_space will just return if no space is needed */
  300. alloced = cats->alloc_cats;
  301. if (!(p =
  302. dig__alloc_space(num, &alloced, 1, (int *)cats->field,
  303. sizeof(int)))) {
  304. return dig_out_of_memory();
  305. }
  306. cats->field = (int *)p;
  307. alloced = cats->alloc_cats;
  308. if (!(p =
  309. dig__alloc_space(num, &alloced, 1, (int *)cats->cat,
  310. sizeof(int)))) {
  311. return dig_out_of_memory();
  312. }
  313. cats->cat = (int *)p;
  314. cats->alloc_cats = alloced;
  315. return 0;
  316. }
  317. /*!
  318. \brief allocate space in P_area for add new lines
  319. \param area pointer to P_area struct
  320. \param add number of lines to be added
  321. \return 0 on success
  322. \return -1 on error
  323. */
  324. int dig_area_alloc_line(struct P_area * area, int add)
  325. {
  326. int num;
  327. char *p;
  328. num = area->alloc_lines + add;
  329. p = G_realloc(area->lines, num * sizeof(plus_t));
  330. if (p == NULL)
  331. return -1;
  332. area->lines = (plus_t *) p;
  333. area->alloc_lines = num;
  334. return (0);
  335. }
  336. /*!
  337. \brief Allocate space in P_area for add new isles
  338. \param area pointer to P_area struct
  339. \param add number of isle to be added
  340. \return 0 on success
  341. \return -1 on error
  342. */
  343. int dig_area_alloc_isle(struct P_area * area, int add)
  344. {
  345. int num;
  346. char *p;
  347. G_debug(5, "dig_area_alloc_isle(): add = %d", add);
  348. num = area->alloc_isles + add;
  349. p = G_realloc(area->isles, num * sizeof(plus_t));
  350. if (p == NULL)
  351. return -1;
  352. area->isles = (plus_t *) p;
  353. area->alloc_isles = num;
  354. return (0);
  355. }
  356. /*!
  357. \brief Allocate space in P_isle for add new lines
  358. \param isle pointer to P_area struct
  359. \param add number of isle to be added
  360. \return 0 on success
  361. \return -1 on error
  362. */
  363. int dig_isle_alloc_line(struct P_isle * isle, int add)
  364. {
  365. int num;
  366. char *p;
  367. G_debug(5, "dig_isle_alloc_line():");
  368. num = isle->alloc_lines + add;
  369. p = G_realloc(isle->lines, num * sizeof(plus_t));
  370. if (p == NULL)
  371. return -1;
  372. isle->lines = (plus_t *) p;
  373. isle->alloc_lines = num;
  374. return (0);
  375. }
  376. /*!
  377. \brief For now just print message and return error code
  378. */
  379. int dig_out_of_memory()
  380. {
  381. G_warning(_("Out of memory"));
  382. return -1;
  383. }