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