1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
|
/* xlimage - xlisp memory image save/restore functions */
/* Copyright (c) 1989, by David Michael Betz. */
/* You may give out copies of this software; for conditions see the file */
/* COPYING included with this distribution. */
/* modified so that offset is in sizeof(node) units */
#include "xlisp.h"
#ifdef XLISP_STAT
#include "xlstat.h"
#include "version.h"
#endif /* XLISP_STAT */
#ifdef SAVERESTORE
#ifdef NEWGC
#define NUM_OFFSETS 6
#else
#define NUM_OFFSETS 0
#endif /* NEWGC */
#define FILENIL ((OFFTYPE)0) /* value of NIL in a file */
/* external variables from xldmem.c */
extern struct segment *segs,*lastseg,*fixseg,*charseg;
extern int anodes,nsegs;
extern long nnodes;
#ifdef NEWGC
extern struct segment *lastpermseg;
#else
extern LVAL fnodes;
#endif /* NEWGC */
/* local variables */
static OFFTYPE off,foff;
static FILEP fp;
/* forward declarations */
LOCAL OFFTYPE readptr(V);
LOCAL OFFTYPE cvoptr P1H(LVAL);
LOCAL LVAL cviptr P1H(OFFTYPE);
LOCAL VOID freeimage(V);
LOCAL VOID setoffset(V);
LOCAL VOID writenode P1H(LVAL);
LOCAL VOID writeptr P1H(OFFTYPE);
LOCAL VOID readnode P2H(int, LVAL);
/* macro to convert between size in bytes and vector size */
#define btow_size(n) (((unsigned)(n)+(sizeof(LVAL)-1))/(unsigned)sizeof(LVAL))
/* xlisave - save the memory image */
int xlisave P1C(char *, fname)
{
SEGMENT *seg;
int n,i,max;
LVAL p;
#ifndef MACINTOSH
char fullname[STRMAX+1];
/* default the extension */
if (needsextension(fname)) {
strcpy(fullname,fname);
strcat(fullname,".wks");
fname = fullname;
}
#endif /* MACINTOSH */
/* open the output file */
if ((fp = OSBOPEN(fname,CREATE_WR)) == CLOSED)
return (FALSE);
disable_interrupts();
/* first call the garbage collector to clean up memory */
gc();
#ifdef NEWGC
sweep_free_nodes();
#endif /* NEWGC */
#ifdef XLISP_STAT
/* write out file type and version numbers as a validity check */
OSWRITE("X1Ws",1,4,fp);
writeptr((OFFTYPE) XLS_MAJOR_RELEASE);
writeptr((OFFTYPE) XLS_MINOR_RELEASE);
writeptr((OFFTYPE) XLS_SUBMINOR_RELEASE);
#ifdef _Windows
#ifdef WIN32
writeptr((OFFTYPE) TRUE);
#else
writeptr((OFFTYPE) FALSE);
#endif /* WIN32 */
#endif /* _Windows */
#else
/* write out size of ftab (used as validity check) TAA MOD */
writeptr((OFFTYPE)ftabsize);
#endif /* XLISP_STAT */
/* write out the pointer to the unbound marker TAA MOD 1/93 */
writeptr(cvoptr(s_unbound));
/* write out the pointer to the *obarray* symbol */
writeptr(cvoptr(obarray));
/* write out components of NIL other than value, which must be NIL */
writeptr(cvoptr(getfunction(NIL)));
writeptr(cvoptr(getplist(NIL)));
writeptr(cvoptr(getpname(NIL)));
#ifdef PACKAGES
writeptr(cvoptr(getpackage(NIL)));
#endif
/* write out the weak box list head */
writeptr(cvoptr(xlweakboxes));
/* setup the initial file offsets */
off = foff = (OFFTYPE)2;
/* write out all nodes that are still in use */
for (seg = segs; seg != NULL; seg = seg->sg_next) {
p = &seg->sg_nodes[0] + NUM_OFFSETS;
for (n = seg->sg_size - NUM_OFFSETS; --n >= 0; ++p, off++)
switch (ntype(p)) {
case FREE:
break;
#ifdef BIGNUMS
case RATIO:
#endif
case COMPLEX:
case CONS:
case USTREAM:
case RNDSTATE:
#ifdef BYTECODE
case BCCLOSURE:
#endif /* BYTECODE */
case DARRAY:
case NATPTR:
case WEAKBOX:
setoffset();
OSPUTC(ntype(p),fp);
if (ntype(p) != NATPTR)
writeptr(cvoptr(car(p)));
writeptr(cvoptr(cdr(p)));
#ifdef NEWGC
OSPUTC(p->n_flags,fp);
#endif /* NEWGC */
foff++;
break;
default:
setoffset();
writenode(p);
break;
}
}
/* write the terminator */
OSPUTC(FREE,fp);
writeptr((OFFTYPE)0);
/* write out data portion of SYMBOL/VECTOR/OBJECT/STRING/CLOSURE nodes */
for (seg = segs; seg != NULL; seg = seg->sg_next) {
p = &seg->sg_nodes[0] + NUM_OFFSETS;
for (n = seg->sg_size - NUM_OFFSETS; --n >= 0; ++p)
switch (ntype(p)) {
/* $putpatch.c$: "MODULE_XLIMAGE_C_XLISAVE" */
case SYMBOL:
case OBJECT:
case VECTOR:
case CLOSURE:
case STRUCT:
#ifdef BYTECODE
case CPSNODE:
case BCODE:
#endif /* BYTECODE */
#ifdef PACKAGES
case PACKAGE:
#endif /* PACKAGES */
max = getsize(p);
for (i = 0; i < max; ++i)
writeptr(cvoptr(getelement(p,i)));
break;
#ifdef BIGNUMS
case BIGNUM:
max = (getbignumsize(p)+1)*sizeof(BIGNUMDATA);
OSWRITE(getbignumarray(p),1,max,fp);
break;
#endif
case STRING:
case TVEC:
max = getslength(p)+1;
OSWRITE(getstring(p),1,max,fp);
break;
#ifdef XLISP_STAT
case ADATA:
max = getadsize(p);
OSWRITE(getadaddr(p),1,max,fp);
break;
#endif /* XLISP_STAT */
#ifdef FILETABLE
case STREAM:
if (getfile(p) > CONSOLE ) {
long offset;
OSWRITE(filetab[getfile(p)].tname,1,FNAMEMAX,fp);
offset = OSTELL(getfile(p));
OSWRITE(&offset,1,sizeof(long),fp);
}
break;
#endif
}
}
/* close the output file */
OSCLOSE(fp);
#ifdef MACINTOSH
fsetfileinfo (fname, 'X1St', 'X1Ws');
#endif /* MACINTOSH */
enable_interrupts();
/* return successfully */
return (TRUE);
}
/* xlirestore - restore a saved memory image */
int xlirestore P1C(char *, fname)
{
int n,i,max,type,size;
SEGMENT *seg;
LVAL p;
#ifndef MACINTOSH
char fullname[STRMAX+1];
/* default the extension */
if (needsextension(fname)) {
strncpy(fullname,fname,STRMAX-4);
strcat(fullname,".wks");
fname = fullname;
}
#endif /* MACINTOSH */
/* open the file */
#ifdef PATHNAMES
if ((fp = ospopen(fname,FALSE)) == CLOSED)
#else
if ((fp = OSBOPEN(fname,OPEN_RO)) == CLOSED)
#endif
return (FALSE);
#ifdef XLISP_STAT
/* Check for file and version validity */
if (OSREAD(buf, 1, 4, fp) != 4 || strncmp(buf, "X1Ws", 4) != 0)
xlfatal("bad image file");
if (readptr() != (OFFTYPE) XLS_MAJOR_RELEASE ||
readptr() != (OFFTYPE) XLS_MINOR_RELEASE ||
readptr() != (OFFTYPE) XLS_SUBMINOR_RELEASE)
xlfatal("image file version does not match system version");
#ifdef _Windows
#ifdef WIN32
if (readptr() != (OFFTYPE) TRUE)
xlfatal("not a Win32 image file");
#else
if (readptr() != (OFFTYPE) FALSE)
xlfatal("not a Win16 image file");
#endif /* WIN32 */
#endif /* _Windows */
#else
/* Check for file validity TAA MOD */
if (readptr() != (OFFTYPE) ftabsize) {
OSCLOSE(fp); /* close it -- we failed */
return (FALSE);
}
#endif /* XLISP_STAT */
disable_interrupts();
/* free the old memory image */
freeimage();
/* initialize */
off = (OFFTYPE)2;
total = nnodes = nfree = 0L;
#ifndef NEWGC
fnodes = NIL;
#endif /* NEWGC */
segs = lastseg = NULL;
nsegs = gccalls = 0;
#ifdef BIGNUMS
n_bigzero=n_bigmone=NULL; /* TAA fix 3/13/96 -- added */
#endif
xlenv = xlfenv = xldenv = s_gchook = s_gcflag = NIL;
xlstack = xlstkbase + EDEPTH;
xlfp = xlsp = xlargstkbase;
*xlsp++ = NIL;
xlcontext = NULL;
#ifdef MULVALS /* TAA BUG FIX 01/94 */
xlnumresults = 0;
#endif
/* create the fixnum segment */
if ((fixseg = newsegment(SFIXSIZE)) == NULL)
xlfatal("insufficient memory - fixnum segment");
/* create the character segment */
if ((charseg = newsegment(CHARSIZE)) == NULL)
xlfatal("insufficient memory - character segment");
#ifdef NEWGC
/* set pointer to last permanent segment */
lastpermseg = lastseg;
#endif /* NEWGC */
/* read in the pointer to the unbound marker TAA MOD 1/93 */
s_unbound = cviptr(readptr());
/* read the pointer to the *obarray* symbol */
obarray = cviptr(readptr());
/* read components of NIL other than value, which must be NIL */
setvalue(NIL, NIL);
setfunction(NIL, cviptr(readptr()));
setplist(NIL, cviptr(readptr()));
setpname(NIL, cviptr(readptr()));
#ifdef PACKAGES
setpackage(NIL, cviptr(readptr()));
#endif
/* read in the weak box list head */
xlweakboxes = cviptr(readptr());
/* read each node */
while ((type = OSGETC(fp)) >= 0) {
switch (type) {
case FREE:
if ((off = readptr()) == (OFFTYPE)0)
goto done;
break;
#ifdef BIGNUMS
case RATIO:
#endif
case COMPLEX:
case CONS:
case USTREAM:
case RNDSTATE:
#ifdef BYTECODE
case BCCLOSURE:
#endif /* BYTECODE */
case DARRAY:
case NATPTR:
case WEAKBOX:
p = cviptr(off);
setntype(p, type);
rplaca(p,type==NATPTR ? NULL : cviptr(readptr()));
rplacd(p,cviptr(readptr()));
#ifdef NEWGC
p->n_flags = OSGETC(fp);
initialize_node(p);
#endif /* NEWGC */
off++;
break;
default:
readnode(type,cviptr(off));
off++;
break;
}
}
done:
/* read the data portion of SYMBOL/VECTOR/OBJECT/STRING/CLOSURE nodes */
for (seg = segs; seg != NULL; seg = seg->sg_next) {
p = &seg->sg_nodes[0] + NUM_OFFSETS;
for (n = seg->sg_size - NUM_OFFSETS; --n >= 0; ++p)
switch (ntype(p)) {
/* $putpatch.c$: "MODULE_XLIMAGE_C_XLIRESTORE" */
case SYMBOL:
case OBJECT:
case VECTOR:
case CLOSURE:
case STRUCT:
#ifdef BYTECODE
case CPSNODE:
case BCODE:
#endif /* BYTECODE */
#ifdef PACKAGES
case PACKAGE:
#endif /* PACKAGES */
max = getsize(p);
if (max != 0 && (p->n_vdata = (LVAL *)VALLOC(max)) == NULL)
xlfatal("insufficient memory - vector");
total += (long)(max * sizeof(LVAL));
for (i = 0; i < max; ++i)
setelement(p,i,cviptr(readptr()));
break;
case STRING:
case TVEC:
max = getslength(p) + 1;
size = btow_size(max);
if ((p->n_string = (char *)VALLOC(size)) == NULL)
xlfatal("insufficient memory - string");
total += (long)max;
if (OSREAD(getstring(p),1,max,fp) != max)
xlfatal("image file corrupted");
break;
#ifdef XLISP_STAT
case ADATA:
max = getadsize(p);
p->n_adaddr = (getadreloc(p)) ? StRCalloc(1, max)
: StCalloc(1, max);
if (p->n_adaddr == NULL)
xlfatal("insufficient memory - string");
total += (long)max;
if (OSREAD(getadaddr(p),1,max,fp) != max)
xlfatal("image file corrupted");
break;
#endif /* XLISP_STAT */
#ifdef BIGNUMS
case BIGNUM:
max = (getbignumsize(p) + 1) * sizeof(BIGNUMDATA);
size = btow_size(max);
if ((p->n_string = (char *)VALLOC(size)) == NULL)
xlfatal("insufficient memory - bignum");
total += (long)max;
if (OSREAD(getbignumarray(p),1,max,fp)!=max)
xlfatal("image file corrupted");
break;
#endif
case STREAM:
#ifdef FILETABLE
if (getfile(p) > CONSOLE) { /* actual file to modify */
unsigned long fpos;
FILEP f;
if (OSREAD(buf, 1, FNAMEMAX, fp) != FNAMEMAX ||
OSREAD(&fpos, 1, sizeof(long), fp) != sizeof(long))
xlfatal("image file corrupted");
/* open file in same type, file must exist to succeed */
f = ((p->n_sflags & S_BINARY)? OSBOPEN : OSAOPEN)
(buf, (p->n_sflags&S_FORWRITING)? OPEN_UPDATE: OPEN_RO);
setfile(p, f);
if (f != CLOSED) {/* position to same point,
or end if file too short */
OSSEEKEND(f);
if (OSTELL(f) > fpos) OSSEEK(f, fpos);
}
}
break;
#else
setfile(p, CLOSED);
break;
#endif
case SUBR:
case FSUBR:
p->n_subr = funtab[getoffset(p)].fd_subr;
break;
}
}
/* close the input file */
OSCLOSE(fp);
/* collect to initialize the free space */
gc();
/* lookup all of the symbols the interpreter uses */
xlsymbols();
enable_interrupts();
/* return successfully */
return (TRUE);
}
/* freeimage - free the current memory image */
LOCAL VOID freeimage(V)
{
SEGMENT *seg,*next;
FILEP fp;
LVAL p;
int n;
/* free the data portion of SYMBOL/VECTOR/OBJECT/STRING nodes */
for (seg = segs; seg != NULL; seg = next) {
p = &seg->sg_nodes[0] + NUM_OFFSETS;
for (n = seg->sg_size - NUM_OFFSETS; --n >= 0; ++p)
switch (ntype(p)) {
/* $putpatch.c$: "MODULE_XLIMAGE_C_FREEIMAGE" */
case SYMBOL:
case OBJECT:
case VECTOR:
case CLOSURE:
case STRUCT:
#ifdef BYTECODE
case CPSNODE:
case BCODE:
#endif /* BYTECODE */
#ifdef PACKAGES
case PACKAGE:
#endif /* PACKAGES */
if (p->n_vsize)
VRELEASE((ALLOCTYPE *)p->n_vdata, p->n_vsize);
break;
case STRING:
case TVEC:
if (getstring(p)!=NULL)
VRELEASE(getstring(p),btow_size(getslength(p) + 1));
break;
#ifdef BIGNUMS
case BIGNUM:
if (getbignumarray(p)!=NULL)
VRELEASE(getbignumarray(p),
btow_size((getbignumsize(p)+1)*sizeof(BIGNUMDATA)));
break;
#endif
#ifdef XLISP_STAT
case ADATA:
if (getadaddr(p) != NULL)
if (getadreloc(p)) StRFree(getadaddr(p));
else StFree(getadaddr(p));
break;
#endif /* XLISP_STAT */
case STREAM:
if (((fp = getfile(p)) != CLOSED) &&
(fp != STDIN && fp != STDOUT && fp != CONSOLE)) /* TAA BUG FIX */
OSCLOSE(fp);
break;
}
next = seg->sg_next;
MFREE(seg);
}
}
/* setoffset - output a positioning command if nodes have been skipped */
LOCAL VOID setoffset(V)
{
if (off != foff) {
OSPUTC(FREE,fp);
writeptr(off);
foff = off;
}
}
/* writenode - write a node to a file */
LOCAL VOID writenode P1C(LVAL, node)
{
OSPUTC(ntype(node),fp);
OSWRITE(&node->n_info, sizeof(union ninfo), 1, fp);
#ifdef NEWGC
OSPUTC(node->n_flags,fp);
#else
#ifdef MULVALS
OSPUTC(node->n_flags,fp);
#else
if (ntype(node) == SYMBOL) OSPUTC(node->n_flags,fp);
#endif /* MULVALS */
#endif /* NEWGC */
foff++;
}
/* writeptr - write a pointer to a file */
LOCAL VOID writeptr P1C(OFFTYPE, off)
{
OSWRITE(&off, sizeof(OFFTYPE), 1, fp);
}
/* readnode - read a node */
LOCAL VOID readnode P2C(int, type, LVAL, node)
{
setntype(node, type);
if (OSREAD(&node->n_info, sizeof(union ninfo), 1, fp) != 1)
xlfatal("image file corrupted");
#ifdef NEWGC
node->n_flags = OSGETC(fp);
initialize_node(node);
#else
#ifdef MULVALS
node->n_flags = OSGETC(fp);
#else
if (type == SYMBOL) node->n_flags = OSGETC(fp);
#endif /* MULVALS */
#endif /* NEWGC */
#ifdef HASHFCNS
/* to get hash tables rehashed */
if (type == STRUCT) setnuflags(node, TRUE);
#endif /* HASHFCNS */
}
/* readptr - read a pointer */
LOCAL OFFTYPE readptr(V)
{
OFFTYPE off;
if (OSREAD(&off, sizeof(OFFTYPE), 1, fp) != 1)
xlfatal("image file corrupted");
return (off);
}
/* cviptr - convert a pointer on input */
LOCAL LVAL cviptr P1C(OFFTYPE, o)
{
OFFTYPE off = (OFFTYPE)2;
SEGMENT *seg;
/* check for nil */
if (o == FILENIL)
return (NIL);
/* compute a pointer for this offset */
for (seg = segs; seg != NULL; seg = seg->sg_next) {
if (o < off + (OFFTYPE)seg->sg_size - NUM_OFFSETS)
return (seg->sg_nodes + NUM_OFFSETS + (unsigned int)(o - off));
off += (OFFTYPE)seg->sg_size - NUM_OFFSETS;
}
/* create new segments if necessary */
for (;;) {
/* create the next segment */
if ((seg = newsegment(anodes)) == NULL)
xlfatal("insufficient memory - segment");
/* check to see if the offset is in this segment */
if (o < off + (OFFTYPE)seg->sg_size - NUM_OFFSETS)
return (seg->sg_nodes + NUM_OFFSETS + (unsigned int)(o - off));
off += (OFFTYPE)seg->sg_size - NUM_OFFSETS;
}
}
/* cvoptr - convert a pointer on output */
LOCAL OFFTYPE cvoptr P1C(LVAL, p)
{
OFFTYPE off = (OFFTYPE)2;
SEGMENT *seg;
OFFTYPE np = CVPTR(p);
/* check for nil */
if (null(p))
return (FILENIL);
/* compute an offset for this pointer */
for (seg = segs; seg != NULL; seg = seg->sg_next) {
if (np >= CVPTR(&seg->sg_nodes[NUM_OFFSETS]) &&
np < CVPTR(&seg->sg_nodes[seg->sg_size]))
return (off + (p-(seg->sg_nodes+NUM_OFFSETS)));
off += (OFFTYPE)seg->sg_size - NUM_OFFSETS;
}
/* pointer not within any segment */
xlerror("bad pointer found during image save",p);
return (0); /* fake out compiler warning */
}
#endif
|