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 #include <unistd.h>
00091 #include <sys/errno.h>
00092 #include "udanax.h"
00093
00094
00095 extern int ingrimreaper;
00096
00097 extern int incrementalallocsize ;
00098 extern int allocsize ;
00099
00100 static HEADER base;
00101 static HEADER *allocp = NULL;
00102 static HEADER baseallocated;
00103
00104 static int flag = 0;
00105 static char *zzalloctop = 0, *zzallocbot = 0;
00106 static int alloccount = 0;
00107
00115 void
00116 lookatalloc()
00117 {
00118 lookatalloc2(&baseallocated);
00119 }
00120
00128 static int
00129 statusofalloc(
00130 char * )
00131 {
00132 unsigned number, maxsize, cumsize;
00133
00134
00135 number = maxsize = cumsize = 0;
00136
00137 HEADER *r;
00138 for (r = base.s.ptr; flag && (r != &base); r = r->s.ptr) {
00139 maxsize = (maxsize > r->s.size ? maxsize : r->s.size);
00140 cumsize += r->s.size;
00141 number++;
00142 }
00143 L("%d free hunks %d maxsize %d total free\n", number, maxsize, cumsize);
00144 return true;
00145 }
00146
00154 static int
00155 validallocthinge(
00156 char *ptr)
00157 {
00158 return 1;
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 }
00177
00185 int
00186 checkalloc(
00187 char *c)
00188 {
00189 HEADER *r, *oldr;
00190 unsigned size;
00191
00192 if (!flag)
00193 return 0;
00194
00195 if (debug && *c)
00196 L("%s", c);
00197
00198 unsigned oldsize = 0;
00199 for (oldr = r = base.s.ptr; flag && (r != &base) && (r != (HEADER *) zzalloctop); r = r + r->s.size) {
00200 size = r->s.size;
00201 validallocthinge((char *) r);
00202 if (r->s.size == 0 && r != (HEADER *) zzalloctop) {
00203 L("checkalloc glorphed with ptr = %x bottom = %x top = %x\n", (int) r, (int) zzallocbot, (int) zzalloctop);
00204 statusofalloc("");
00205 assert(0);
00206 return 1;
00207 }
00208 oldr = r;
00209 oldsize = size;
00210 }
00211 return true;
00212 }
00213
00221 static HEADER *
00222 weresurethatthisisourmorecore()
00223 {
00224 HEADER *up;
00225 int temp;
00226
00227 if (flag)
00228 return NULL;
00229
00230 char *cp = (char *) sbrk(allocsize);
00231 zzalloctop = (char *)cp + allocsize;
00232 zzallocbot = (char *)cp;
00233
00234 if ((int) cp == -1) {
00235 flag = 1;
00236 return NULL;
00237 }
00238
00239 up = (HEADER *) cp;
00240 temp = up->s.size = ((unsigned) allocsize) / sizeof(HEADER);
00241
00242 if (temp == 0)
00243 assert(0);
00244
00245 baseallocated.s.ptr = (HEADER *) cp;
00246 baseallocated.s.size = temp;
00247 ffree((char *)(cp + sizeof(HEADER) ));
00248 flag = 1;
00249
00250 return allocp;
00251 }
00252
00260 int *
00261 falloc(
00262 unsigned nbytes)
00263 {
00264 register HEADER *p, *q;
00265 register unsigned nunits;
00266
00267
00268
00269 if (ingrimreaper) {
00270 L("really error in falloc called from under grimreaper\n");
00271 assert(0);
00272 }
00273
00274 assert(nbytes > 0);
00275
00276
00277
00278 alloccount++;
00279 nunits = 1 + (nbytes + sizeof(HEADER) - 1) / sizeof(HEADER);
00280 if ((q = allocp) == NULL) {
00281 base.s.ptr = allocp = q = &base;
00282 base.s.size = 0;
00283 } else {
00284
00285 }
00286 for (p = q->s.ptr;; q = p, p = p->s.ptr) {
00287 if (p->s.size >= nunits) {
00288 if (p->s.size == nunits)
00289 q->s.ptr = p->s.ptr;
00290 else {
00291 p->s.size -= nunits;
00292 p += p->s.size;
00293 p->s.size = nunits;
00294 }
00295 p->s.ptr = NULL;
00296 allocp = q;
00297 return (int *) (p + 1);
00298 }
00299 if (p == allocp)
00300 if ((p = weresurethatthisisourmorecore()) == NULL)
00301 return NULL;
00302 }
00303 }
00304
00312 void
00313 ffree(
00314 char *ap)
00315 {
00316 register HEADER *p, *q;
00317
00318
00319 p = (HEADER *) ap - 1;
00320 if (flag) {
00321
00322 if (!validallocthinge((char *) p))
00323 assert(0);
00324 }
00325
00326
00327
00328 for (q = allocp; !(p > q && p < q->s.ptr); q = q->s.ptr) {
00329 if (q >= q->s.ptr && (p > q || p < q->s.ptr)) {
00330 break;
00331 }
00332 }
00333
00334 if (p + p->s.size == q->s.ptr) {
00335 p->s.size += q->s.ptr->s.size;
00336 p->s.ptr = q->s.ptr->s.ptr;
00337 } else {
00338 p->s.ptr = q->s.ptr;
00339 }
00340 if (q + q->s.size == p) {
00341 q->s.size += p->s.size;
00342 q->s.ptr = p->s.ptr;
00343 } else {
00344 q->s.ptr = p;
00345 }
00346 allocp = q;
00347
00348 }