allocation.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  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. *****************************************************************************/
  18. #include <unistd.h>
  19. #include <stdlib.h>
  20. #include <grass/vector.h>
  21. /* functions - alloc_space(), falloc(), frealloc() _falloc() _frealloc() */
  22. /* alloc_space () allocates space if needed.
  23. * All allocated space is created by calloc (2).
  24. *
  25. * args: number of elements wanted, pointer to number of currently allocated
  26. * elements, size of chunks to allocate, pointer to current array, sizeof
  27. * an element.
  28. */
  29. void *dig_alloc_space(int n_wanted,
  30. int *n_elements,
  31. int chunk_size, void *ptr, int element_size)
  32. {
  33. char *p;
  34. p = dig__alloc_space(n_wanted, n_elements, chunk_size, ptr, element_size);
  35. if (p == NULL) {
  36. fprintf(stderr, "\nERROR: out of memory. memory asked for: %d\n",
  37. n_wanted);
  38. exit(EXIT_FAILURE);
  39. }
  40. return (p);
  41. }
  42. void *dig__alloc_space(int n_wanted, int *n_elements, int chunk_size, void *ptr, /* changed char -> void instead of casting. WBH 8/16/1998 */
  43. int element_size)
  44. {
  45. int to_alloc;
  46. to_alloc = *n_elements;
  47. /* do we need to allocate more space */
  48. if (n_wanted < to_alloc)
  49. return (ptr);
  50. /* calculate the number needed by chunk size */
  51. /* ORIGINAL
  52. while (n_wanted >= to_alloc)
  53. to_alloc += chunk_size;
  54. */
  55. /*
  56. ** This was changed as a test on Aug 21, 1990
  57. ** Build.vect was taking outrageous amounts of
  58. ** memory to run, so instead of blaming my
  59. ** code, I decided that it could be the realloc/malloc
  60. ** stuff not making efficient use of the space.
  61. ** So the fix is to instead of asking for many small
  62. ** increments, ask for twice as much space as we are currently
  63. ** using, each time we need more space.
  64. */
  65. while (n_wanted >= to_alloc)
  66. to_alloc += *n_elements ? *n_elements : chunk_size;
  67. /* first time called allocate initial storage */
  68. if (*n_elements == 0)
  69. ptr = G_calloc(to_alloc, element_size);
  70. else
  71. ptr = dig__frealloc((char *)ptr, to_alloc, element_size, *n_elements);
  72. *n_elements = to_alloc;
  73. return (ptr);
  74. }
  75. void *dig_falloc(int nelem, int elsize)
  76. {
  77. void *ret;
  78. if ((ret = dig__falloc(nelem, elsize)) == NULL) {
  79. fprintf(stderr, "Out of Memory.\n");
  80. G_sleep(2);
  81. exit(EXIT_FAILURE);
  82. }
  83. return (ret);
  84. }
  85. void *dig_frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
  86. {
  87. char *ret;
  88. if ((ret = dig__frealloc(oldptr, nelem, elsize, oldnelem)) == NULL) {
  89. fprintf(stderr, "\nOut of Memory on realloc.\n");
  90. G_sleep(2);
  91. exit(EXIT_FAILURE);
  92. }
  93. return (ret);
  94. }
  95. /* these functions don't exit on "no more memory", calling function should
  96. check the return value */
  97. void *dig__falloc(int nelem, int elsize)
  98. {
  99. char *ptr;
  100. if (elsize == 0) {
  101. elsize = 4;
  102. }
  103. if (nelem == 0) {
  104. nelem = 1;
  105. }
  106. ptr = G_calloc(nelem, elsize);
  107. return (ptr);
  108. }
  109. void *dig__frealloc(void *oldptr, int nelem, int elsize, int oldnelem)
  110. {
  111. char *ptr;
  112. if (elsize == 0) {
  113. elsize = 4;
  114. }
  115. if (nelem == 0) {
  116. nelem = 1;
  117. }
  118. ptr = G_calloc(nelem, elsize);
  119. /* out of memory */
  120. if (!ptr)
  121. return (ptr);
  122. {
  123. register char *a;
  124. register char *b;
  125. register size_t n;
  126. n = oldnelem * elsize;
  127. a = ptr;
  128. b = oldptr;
  129. while (n--)
  130. *a++ = *b++;
  131. }
  132. G_free(oldptr);
  133. return (ptr);
  134. }