allocation.c 3.9 KB

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