00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <unistd.h>
00021 #include <stdlib.h>
00022 #include <grass/Vect.h>
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 void *
00036 dig_alloc_space (
00037 int n_wanted,
00038 int *n_elements,
00039 int chunk_size,
00040 void *ptr,
00041 int element_size)
00042 {
00043 char *p;
00044
00045 p = dig__alloc_space (n_wanted, n_elements, chunk_size, ptr, element_size);
00046
00047 if (p == NULL)
00048 {
00049 fprintf (stderr, "\nERROR: out of memory. memory asked for: %d\n",
00050 n_wanted);
00051 exit (-1);
00052 }
00053
00054 return (p);
00055 }
00056
00057 void *
00058 dig__alloc_space (
00059 int n_wanted,
00060 int *n_elements,
00061 int chunk_size,
00062 void *ptr,
00063 int element_size)
00064 {
00065 int to_alloc;
00066
00067 to_alloc = *n_elements;
00068
00069
00070 if (n_wanted < to_alloc)
00071 return (ptr);
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 while (n_wanted >= to_alloc)
00089 to_alloc += *n_elements ? *n_elements : chunk_size;
00090
00091
00092 if (*n_elements == 0)
00093 ptr = calloc ((unsigned) to_alloc, (unsigned) element_size);
00094 else
00095 ptr = dig__frealloc ((char *) ptr, to_alloc, element_size, *n_elements);
00096
00097 *n_elements = to_alloc;
00098
00099 return (ptr);
00100 }
00101
00102
00103 void *
00104 dig_falloc (int nelem, int elsize)
00105 {
00106 void *ret;
00107 if ((ret = dig__falloc (nelem, elsize)) == NULL)
00108 {
00109 fprintf (stderr, "Out of Memory.\n");
00110 G_sleep (2);
00111 exit (-1);
00112 }
00113 return (ret);
00114 }
00115
00116 void *
00117 dig_frealloc (
00118 void *oldptr,
00119 int nelem, int elsize,
00120 int oldnelem)
00121 {
00122 char *ret;
00123
00124 if ((ret = dig__frealloc (oldptr, nelem, elsize, oldnelem)) == NULL)
00125 {
00126 fprintf (stderr, "\nOut of Memory on realloc.\n");
00127 G_sleep (2);
00128 exit (-1);
00129 }
00130 return (ret);
00131 }
00132
00133
00134
00135
00136 void *
00137 dig__falloc (int nelem, int elsize)
00138 {
00139 char *ptr;
00140
00141 if (elsize == 0)
00142 {
00143 elsize = 4;
00144 }
00145 if (nelem == 0)
00146 {
00147 nelem = 1;
00148 }
00149
00150 ptr = calloc ((unsigned) nelem, (unsigned) elsize);
00151 return (ptr);
00152 }
00153
00154 void *
00155 dig__frealloc (
00156 void *oldptr,
00157 int nelem, int elsize,
00158 int oldnelem)
00159 {
00160 char *ptr;
00161
00162 if (elsize == 0)
00163 {
00164 elsize = 4;
00165 }
00166 if (nelem == 0)
00167 {
00168 nelem = 1;
00169 }
00170
00171 ptr = calloc ((unsigned) nelem, (unsigned) elsize);
00172
00173
00174 if (!ptr)
00175 return (ptr);
00176
00177 {
00178 register char *a;
00179 register char *b;
00180 register long n;
00181
00182 n = oldnelem * elsize;
00183 a = ptr;
00184 b = oldptr;
00185 while (n--)
00186 *a++ = *b++;
00187 }
00188
00189 free (oldptr);
00190 return (ptr);
00191 }