00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 #include "machdefs.h"
00106 #include "except.h"
00107 #include "util.h"
00108 #ifdef USEDMALLOC
00109 #include "dmalloc.h"
00110 #endif
00111
00112 int ILLTRACE_MALLOC = 0;
00113
00114 typedef struct ILLbigchunk
00115 {
00116 char space[ILL_BIGCHUNK];
00117 ILLbigchunkptr ptr;
00118 }
00119 ILLbigchunk;
00120
00121 void *ILLutil_allocrus (
00122 size_t size)
00123 {
00124 void *mem = (void *) NULL;
00125
00126 if (size == 0)
00127 {
00128
00129 }
00130
00131 mem = (void *) malloc (size);
00132 if (mem == (void *) NULL)
00133 {
00134 fprintf (stderr, "Out of memory. Asked for %d bytes\n", (int) size);
00135 }
00136 return mem;
00137 }
00138
00139 void ILLutil_freerus (
00140 void *p)
00141 {
00142 if (!p)
00143 {
00144
00145 return;
00146 }
00147
00148 free (p);
00149 }
00150
00151 void *ILLutil_reallocrus (
00152 void *ptr,
00153 size_t size)
00154 {
00155 void *newptr;
00156
00157 if (!ptr)
00158 {
00159 return ILLutil_allocrus (size);
00160 }
00161 else
00162 {
00163 newptr = (void *) realloc (ptr, size);
00164 if (!newptr)
00165 {
00166 fprintf (stderr, "Out of memory. Tried to grow to %d bytes\n",
00167 (int) size);
00168 }
00169 return newptr;
00170 }
00171 }
00172
00173 int ILLutil_reallocrus_scale (
00174 void **pptr,
00175 int *pnnum,
00176 int count,
00177 double scale,
00178 size_t size)
00179 {
00180 int rval = 0;
00181 int newsize = (int) (((double) *pnnum) * scale);
00182 void *p;
00183
00184 if (newsize < *pnnum + 1000)
00185 newsize = *pnnum + 1000;
00186 if (newsize < count)
00187 newsize = count;
00188 p = ILLutil_reallocrus (*pptr, newsize * size);
00189 if (!p)
00190 {
00191 rval = ILL_GENERAL_ERROR;
00192 ILL_REPRT ("ILLutil_reallocrus_scale failed\n");
00193 ILL_CLEANUP;
00194 }
00195 else
00196 {
00197 *pptr = p;
00198 *pnnum = newsize;
00199 }
00200 CLEANUP:
00201 return rval;
00202 }
00203
00204 int ILLutil_reallocrus_count (
00205 void **pptr,
00206 int count,
00207 size_t size)
00208 {
00209 int rval = 0;
00210 void *p = ILLutil_reallocrus (*pptr, count * size);
00211
00212 if (!p)
00213 {
00214 rval = ILL_GENERAL_ERROR;
00215 ILL_REPRT ("ILLutil_reallocrus_count failed\n");
00216 ILL_CLEANUP;
00217 }
00218 else
00219 {
00220 *pptr = p;
00221 }
00222 CLEANUP:
00223 return rval;
00224 }
00225
00226
00227 ILLbigchunkptr *ILLutil_bigchunkalloc (
00228 void)
00229 {
00230 ILLbigchunk *p;
00231
00232 ILL_NEW_no_rval (p, ILLbigchunk);
00233
00234 p->ptr.this_chunk = p;
00235 p->ptr.this_one = (void *) p->space;
00236 CLEANUP:
00237 if (p == (ILLbigchunk *) NULL)
00238 {
00239 return (ILLbigchunkptr *) NULL;
00240 }
00241 return &(p->ptr);
00242 }
00243
00244 void ILLutil_bigchunkfree (
00245 ILLbigchunkptr * bp)
00246 {
00247
00248 ILLbigchunk *p = bp->this_chunk;
00249
00250 ILL_IFFREE (p, ILLbigchunk);
00251 }
00252
00253 void ILLptrworld_init (
00254 ILLptrworld * world)
00255 {
00256 world->refcount = 1;
00257 world->freelist = (void *) NULL;
00258 world->chunklist = (ILLbigchunkptr *) NULL;
00259 }
00260
00261 void ILLptrworld_add (
00262 ILLptrworld * world)
00263 {
00264 world->refcount++;
00265 }
00266
00267 void ILLptrworld_delete (
00268 ILLptrworld * world)
00269 {
00270 world->refcount--;
00271 if (world->refcount <= 0)
00272 {
00273 ILLbigchunkptr *bp, *bpnext;
00274
00275 for (bp = world->chunklist; bp; bp = bpnext)
00276 {
00277 bpnext = bp->next;
00278 ILLutil_bigchunkfree (bp);
00279 }
00280 world->chunklist = (ILLbigchunkptr *) NULL;
00281 world->freelist = (void *) NULL;
00282 world->refcount = 0;
00283 }
00284 }