allocation.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/Vect.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 *
  30. dig_alloc_space (
  31. int n_wanted,
  32. int *n_elements,
  33. int chunk_size,
  34. void *ptr,
  35. int element_size)
  36. {
  37. char *p;
  38. p = dig__alloc_space (n_wanted, n_elements, chunk_size, ptr, element_size);
  39. if (p == NULL)
  40. {
  41. fprintf (stderr, "\nERROR: out of memory. memory asked for: %d\n",
  42. n_wanted);
  43. exit (-1);
  44. }
  45. return (p);
  46. }
  47. void *
  48. dig__alloc_space (
  49. int n_wanted,
  50. int *n_elements,
  51. int chunk_size,
  52. void *ptr, /* changed char -> void instead of casting. WBH 8/16/1998 */
  53. int element_size)
  54. {
  55. int to_alloc;
  56. to_alloc = *n_elements;
  57. /* do we need to allocate more space */
  58. if (n_wanted < to_alloc)
  59. return (ptr);
  60. /* calculate the number needed by chunk size */
  61. /* ORIGINAL
  62. while (n_wanted >= to_alloc)
  63. to_alloc += chunk_size;
  64. */
  65. /*
  66. ** This was changed as a test on Aug 21, 1990
  67. ** Build.vect was taking outrageous amounts of
  68. ** memory to run, so instead of blaming my
  69. ** code, I decided that it could be the realloc/malloc
  70. ** stuff not making efficient use of the space.
  71. ** So the fix is to instead of asking for many small
  72. ** increments, ask for twice as much space as we are currently
  73. ** using, each time we need more space.
  74. */
  75. while (n_wanted >= to_alloc)
  76. to_alloc += *n_elements ? *n_elements : chunk_size;
  77. /* first time called allocate initial storage */
  78. if (*n_elements == 0)
  79. ptr = calloc ((unsigned) to_alloc, (unsigned) element_size);
  80. else
  81. ptr = dig__frealloc ((char *) ptr, to_alloc, element_size, *n_elements);
  82. *n_elements = to_alloc;
  83. return (ptr);
  84. }
  85. void *
  86. dig_falloc (int nelem, int elsize)
  87. {
  88. void *ret;
  89. if ((ret = dig__falloc (nelem, elsize)) == NULL)
  90. {
  91. fprintf (stderr, "Out of Memory.\n");
  92. G_sleep (2);
  93. exit (-1);
  94. }
  95. return (ret);
  96. }
  97. void *
  98. dig_frealloc (
  99. void *oldptr,
  100. int nelem, int elsize,
  101. int oldnelem)
  102. {
  103. char *ret;
  104. if ((ret = dig__frealloc (oldptr, nelem, elsize, oldnelem)) == NULL)
  105. {
  106. fprintf (stderr, "\nOut of Memory on realloc.\n");
  107. G_sleep (2);
  108. exit (-1);
  109. }
  110. return (ret);
  111. }
  112. /* these functions don't exit on "no more memory", calling funtion should
  113. check the return value */
  114. void *
  115. dig__falloc (int nelem, int elsize)
  116. {
  117. char *ptr;
  118. if (elsize == 0)
  119. {
  120. elsize = 4;
  121. }
  122. if (nelem == 0)
  123. {
  124. nelem = 1;
  125. }
  126. ptr = calloc ((unsigned) nelem, (unsigned) elsize);
  127. return (ptr);
  128. }
  129. void *
  130. dig__frealloc (
  131. void *oldptr,
  132. int nelem, int elsize,
  133. int oldnelem)
  134. {
  135. char *ptr;
  136. if (elsize == 0)
  137. {
  138. elsize = 4;
  139. }
  140. if (nelem == 0)
  141. {
  142. nelem = 1;
  143. }
  144. ptr = calloc ((unsigned) nelem, (unsigned) elsize);
  145. /* out of memory */
  146. if (!ptr)
  147. return (ptr);
  148. {
  149. register char *a;
  150. register char *b;
  151. register long n;
  152. n = oldnelem * elsize;
  153. a = ptr;
  154. b = oldptr;
  155. while (n--)
  156. *a++ = *b++;
  157. }
  158. free (oldptr);
  159. return (ptr);
  160. }