forward possible errors

This commit is contained in:
Vítor Santos Costa II 2010-03-22 16:56:58 +00:00
parent b068721c68
commit a9b979144c
1 changed files with 173 additions and 141 deletions

314
C/save.c
View File

@ -83,19 +83,19 @@ static char end_msg[256] ="*** End of YAP saved state *****";
#endif #endif
STATIC_PROTO(int myread, (int, char *, Int)); STATIC_PROTO(int myread, (int, char *, Int));
STATIC_PROTO(void mywrite, (int, char *, Int)); STATIC_PROTO(Int mywrite, (int, char *, Int));
STATIC_PROTO(int open_file, (char *, int)); STATIC_PROTO(int open_file, (char *, int));
STATIC_PROTO(void close_file, (void)); STATIC_PROTO(int close_file, (void));
STATIC_PROTO(void putout, (CELL)); STATIC_PROTO(Int putout, (CELL));
STATIC_PROTO(void putcellptr, (CELL *)); STATIC_PROTO(Int putcellptr, (CELL *));
STATIC_PROTO(CELL get_cell, (void)); STATIC_PROTO(CELL get_cell, (void));
STATIC_PROTO(CELL *get_cellptr, ( /* CELL * */ void)); STATIC_PROTO(CELL *get_cellptr, ( /* CELL * */ void));
STATIC_PROTO(void put_info, (int, int)); STATIC_PROTO(int put_info, (int, int));
STATIC_PROTO(void save_regs, (int)); STATIC_PROTO(int save_regs, (int));
STATIC_PROTO(void save_code_info, (void)); STATIC_PROTO(int save_code_info, (void));
STATIC_PROTO(void save_heap, (void)); STATIC_PROTO(int save_heap, (void));
STATIC_PROTO(void save_stacks, (int)); STATIC_PROTO(int save_stacks, (int));
STATIC_PROTO(void save_crc, (void)); STATIC_PROTO(int save_crc, (void));
STATIC_PROTO(Int do_save, (int)); STATIC_PROTO(Int do_save, (int));
STATIC_PROTO(Int p_save, (void)); STATIC_PROTO(Int p_save, (void));
STATIC_PROTO(Int p_save_program, (void)); STATIC_PROTO(Int p_save_program, (void));
@ -156,7 +156,7 @@ LightBug(s)
#endif /* LIGHT */ #endif /* LIGHT */
static int static Int
do_system_error(yap_error_number etype, const char *msg) do_system_error(yap_error_number etype, const char *msg)
{ {
#if HAVE_SNPRINTF #if HAVE_SNPRINTF
@ -177,42 +177,10 @@ do_system_error(yap_error_number etype, const char *msg)
return -1; return -1;
} }
#if SHORT_INTS
#ifdef M_WILLIAMS
#include <fcntl.h>
#endif
static int
myread(int fd, char *buff, Int len)
{
while (len > 0) {
int nchars = read(fd, buff, len);
if (nchars <= 0) {
return do_system_error(PERMISSION_ERROR_INPUT_PAST_END_OF_STREAM, "bad read on saved state");
}
len -= nchars;
buff += nchars;
}
return len;
}
static void
mywrite(int fd, char *buff, Int len)
{
while (len > 16000) {
write(fd, buff, 16000);
len -= 16000;
buff += 16000;
}
write(fd, buff, (unsigned) len);
}
#else /* SHORT_INTS */
inline static inline static
int myread(int fd, char *buffer, Int len) { int myread(int fd, char *buffer, Int len) {
int nread; ssize_t nread;
while (len > 0) { while (len > 0) {
nread = read(fd, buffer, (int)len); nread = read(fd, buffer, (int)len);
@ -226,22 +194,21 @@ int myread(int fd, char *buffer, Int len) {
} }
inline static inline static
void mywrite(int fd, char *buff, Int len) { Int
Int nwritten; mywrite(int fd, char *buff, Int len) {
ssize_t nwritten;
while (len > 0) { while (len > 0) {
nwritten = (Int)write(fd, buff, (int)len); nwritten = write(fd, buff, (size_t)len);
if (nwritten < 0) { if (nwritten < 0) {
Yap_ErrorMessage = "bad write on saved state"; return do_system_error(SYSTEM_ERROR,"bad write on saved state");
Yap_Error(SYSTEM_ERROR,TermNil,Yap_ErrorMessage);
} }
buff += nwritten; buff += nwritten;
len -= nwritten; len -= nwritten;
} }
return len;
} }
#endif /* SHORT_INTS */
#define FullSaved 1 #define FullSaved 1
/* Where the code was before */ /* Where the code was before */
@ -299,27 +266,29 @@ open_file(char *my_file, int flag)
return splfild; return splfild;
} }
static void static int
close_file(void) close_file(void)
{ {
if (splfild == 0) if (splfild == 0)
return; return 0;
close(splfild); if (close(splfild) < 0)
return do_system_error(SYSTEM_ERROR,"bad close on saved state");
splfild = 0; splfild = 0;
return 1;
} }
/* stores a cell in a file */ /* stores a cell in a file */
static void static Int
putout(CELL l) putout(CELL l)
{ {
mywrite(splfild, (char *) &l, sizeof(CELL)); return mywrite(splfild, (char *) &l, sizeof(CELL));
} }
/* stores a pointer to a cell in a file */ /* stores a pointer to a cell in a file */
static void static Int
putcellptr(CELL *l) putcellptr(CELL *l)
{ {
mywrite(splfild, (char *) &l, sizeof(CELLPOINTER)); return mywrite(splfild, (char *) &l, sizeof(CELLPOINTER));
} }
/* gets a cell from a file */ /* gets a cell from a file */
@ -353,7 +322,8 @@ get_cellptr(void)
{ {
CELL *l; CELL *l;
myread(splfild, (char *) &l, Unsigned(sizeof(CELLPOINTER))); if (myread(splfild, (char *) &l, Unsigned(sizeof(CELLPOINTER))) < 0)
return NULL;
return (l); return (l);
} }
@ -361,96 +331,144 @@ get_cellptr(void)
* writes the header (at the moment YAPV*), info about what kind of saved * writes the header (at the moment YAPV*), info about what kind of saved
* set, the work size, and the space ocuppied * set, the work size, and the space ocuppied
*/ */
static void static int
put_info(int info, int mode) put_info(int info, int mode)
{ {
char msg[256]; char msg[256];
sprintf(msg, "#!/bin/sh\nexec_dir=${YAPBINDIR:-%s}\nexec $exec_dir/yap $0 \"$@\"\n%cYAP-%s", YAP_BINDIR, 1, YAP_SVERSION); sprintf(msg, "#!/bin/sh\nexec_dir=${YAPBINDIR:-%s}\nexec $exec_dir/yap $0 \"$@\"\n%cYAP-%s", YAP_BINDIR, 1, YAP_SVERSION);
mywrite(splfild, msg, strlen(msg) + 1); if (mywrite(splfild, msg, strlen(msg) + 1))
putout(Unsigned(info)); return -1;
if (putout(Unsigned(info)) < 0)
return -1;
/* say whether we just saved the heap or everything */ /* say whether we just saved the heap or everything */
putout(mode); if (putout(mode) < 0)
return -1;
/* current state of stacks, to be used by SavedInfo */ /* current state of stacks, to be used by SavedInfo */
/* space available in heap area */ /* space available in heap area */
putout(Unsigned(Yap_GlobalBase)-Unsigned(Yap_HeapBase)); if (putout(Unsigned(Yap_GlobalBase)-Unsigned(Yap_HeapBase)) < 0)
return -1;
/* space available for stacks */ /* space available for stacks */
putout(Unsigned(Yap_LocalBase)-Unsigned(Yap_GlobalBase)); if (putout(Unsigned(Yap_LocalBase)-Unsigned(Yap_GlobalBase)) < 0)
return -1;
/* space available for trail */ /* space available for trail */
putout(Unsigned(Yap_TrailTop)-Unsigned(Yap_TrailBase)); if (putout(Unsigned(Yap_TrailTop)-Unsigned(Yap_TrailBase)) < 0)
return -1;
/* Space used in heap area */ /* Space used in heap area */
putout(Unsigned(HeapTop)-Unsigned(Yap_HeapBase)); if (putout(Unsigned(HeapTop)-Unsigned(Yap_HeapBase)) < 0)
return -1;
/* Space used for local stack */ /* Space used for local stack */
putout(Unsigned(LCL0)-Unsigned(ASP)); if (putout(Unsigned(LCL0)-Unsigned(ASP)) < 0)
return -1;
/* Space used for global stack */ /* Space used for global stack */
putout(Unsigned(H) - Unsigned(Yap_GlobalBase)); if (putout(Unsigned(H) - Unsigned(Yap_GlobalBase)) < 0)
return -1;
/* Space used for trail */ /* Space used for trail */
putout(Unsigned(TR) - Unsigned(Yap_TrailBase)); if (putout(Unsigned(TR) - Unsigned(Yap_TrailBase)) < 0)
return -1;
return 0;
} }
static void static int
save_regs(int mode) save_regs(int mode)
{ {
/* save all registers */ /* save all registers */
putout((CELL)compile_arrays); if (putout((CELL)compile_arrays) < 0)
return -1;
if (mode == DO_EVERYTHING) { if (mode == DO_EVERYTHING) {
putcellptr((CELL *)CP); if (putcellptr((CELL *)CP) < 0)
putcellptr(ENV); return -1;
putcellptr(ASP); if (putcellptr(ENV) < 0)
/* putout((CELL)N); */ return -1;
putcellptr(H0); if (putcellptr(ASP) < 0)
putcellptr(LCL0); return -1;
putcellptr(H); /* if (putout((CELL)N) < 0)
putcellptr(HB); return -1; */
putcellptr((CELL *)B); if (putcellptr(H0) < 0)
putcellptr((CELL *)TR); return -1;
putcellptr(YENV); if (putcellptr(LCL0) < 0)
putcellptr(S); return -1;
putcellptr((CELL *)P); if (putcellptr(H) < 0)
putout(CreepFlag); return -1;
putout(EX); if (putcellptr(HB) < 0)
return -1;
if (putcellptr((CELL *)B) < 0)
return -1;
if (putcellptr((CELL *)TR) < 0)
return -1;
if (putcellptr(YENV) < 0)
return -1;
if (putcellptr(S) < 0)
return -1;
if (putcellptr((CELL *)P) < 0)
return -1;
if (putout(CreepFlag) < 0)
return -1;
if (putout(EX) < 0)
return -1;
#if defined(SBA) || defined(TABLING) #if defined(SBA) || defined(TABLING)
putcellptr(H_FZ); if (putcellptr(H_FZ) < 0)
putcellptr((CELL *)B_FZ); return -1;
putcellptr((CELL *)TR_FZ); if (putcellptr((CELL *)B_FZ) < 0)
return -1;
if (putcellptr((CELL *)TR_FZ) < 0)
return -1;
#endif /* SBA || TABLING */ #endif /* SBA || TABLING */
} }
putout(CurrentModule); if (putout(CurrentModule) < 0)
return -1;
if (mode == DO_EVERYTHING) { if (mode == DO_EVERYTHING) {
#ifdef COROUTINING #ifdef COROUTINING
putout(WokenGoals); if (putout(WokenGoals) < 0)
return -1;
#endif #endif
#ifdef DEPTH_LIMIT #ifdef DEPTH_LIMIT
putout(DEPTH); if (putout(DEPTH) < 0)
return -1;
#endif #endif
putout(GcGeneration); if (putout(GcGeneration) < 0)
putout(GcPhase); return -1;
putout(GcCurrentPhase); if (putout(GcPhase) < 0)
return -1;
if (putout(GcCurrentPhase) < 0)
return -1;
} }
/* The operand base */ /* The operand base */
putcellptr(CellPtr(XREGS)); if (putcellptr(CellPtr(XREGS)) < 0)
putout(which_save); return -1;
if (putout(which_save) < 0)
return -1;
/* Now start by saving the code */ /* Now start by saving the code */
/* the heap boundaries */ /* the heap boundaries */
putcellptr(CellPtr(Yap_HeapBase)); if (putcellptr(CellPtr(Yap_HeapBase)) < 0)
putcellptr(CellPtr(HeapTop)); return -1;
if (putcellptr(CellPtr(HeapTop)) < 0)
return -1;
/* and the space it ocuppies */ /* and the space it ocuppies */
putout(Unsigned(Yap_heap_regs->heap_used)); if (putout(Unsigned(Yap_heap_regs->heap_used)) < 0)
return -1;
/* Then the start of the free code */ /* Then the start of the free code */
putcellptr(CellPtr(FreeBlocks)); if (putcellptr(CellPtr(FreeBlocks)) < 0)
putcellptr(AuxSp); return -1;
putcellptr(CellPtr(AuxTop)); if (putcellptr(AuxSp) < 0)
return -1;
if (putcellptr(CellPtr(AuxTop)) < 0)
return -1;
if (mode == DO_EVERYTHING) { if (mode == DO_EVERYTHING) {
/* put the old trail base, just in case it moves again */ /* put the old trail base, just in case it moves again */
putout(ARG1); if (putout(ARG1) < 0)
return -1;
if (which_save == 2) { if (which_save == 2) {
putout(ARG2); if (putout(ARG2) < 0)
return -1;
} }
putcellptr(CellPtr(Yap_TrailBase)); if (putcellptr(CellPtr(Yap_TrailBase)) < 0)
return -1;
} }
return 0;
} }
static void static int
save_code_info(void) save_code_info(void)
{ {
@ -461,13 +479,16 @@ save_code_info(void)
OPCODE my_ops[_std_top+1]; OPCODE my_ops[_std_top+1];
for (i = _Ystop; i <= _std_top; ++i) for (i = _Ystop; i <= _std_top; ++i)
my_ops[i] = Yap_opcode(i); my_ops[i] = Yap_opcode(i);
mywrite(splfild, (char *)my_ops, sizeof(OPCODE)*(_std_top+1)); if (mywrite(splfild, (char *)my_ops, sizeof(OPCODE)*(_std_top+1)) < 0)
return -1;
} }
/* and the current character codes */ /* and the current character codes */
mywrite(splfild, Yap_chtype, NUMBER_OF_CHARS); if (mywrite(splfild, Yap_chtype, NUMBER_OF_CHARS) < 0)
return -1;
return 0;
} }
static void static int
save_heap(void) save_heap(void)
{ {
#ifdef USE_SYSTEM_MALLOC #ifdef USE_SYSTEM_MALLOC
@ -481,19 +502,24 @@ save_heap(void)
Yap_CloseScratchPad(); Yap_CloseScratchPad();
/* skip the local and global data structures */ /* skip the local and global data structures */
j = Unsigned(&GLOBAL) - Unsigned(Yap_HeapBase); j = Unsigned(&GLOBAL) - Unsigned(Yap_HeapBase);
putout(j); if (putout(j) < 0)
return -1;
mywrite(splfild, (char *) Yap_HeapBase, j); mywrite(splfild, (char *) Yap_HeapBase, j);
j = Unsigned(HeapTop) - Unsigned(REMOTE+MAX_WORKERS); j = Unsigned(HeapTop) - Unsigned(REMOTE+MAX_WORKERS);
putout(j); if (putout(j) < 0)
mywrite(splfild, (char *) &(REMOTE[MAX_WORKERS]), j); return -1;
if (mywrite(splfild, (char *) &(REMOTE[MAX_WORKERS]), j) < 0)
return -1;
#else #else
j = Unsigned(HeapTop) - Unsigned(Yap_HeapBase); j = Unsigned(HeapTop) - Unsigned(Yap_HeapBase);
/* store 10 more cells because of the memory manager */ /* store 10 more cells because of the memory manager */
mywrite(splfild, (char *) Yap_HeapBase, j); if (mywrite(splfild, (char *) Yap_HeapBase, j) < 0)
return -1;
#endif /* YAPOR || (TABLING && !YAP_MEMORY_ALLOC_SCHEME) */ #endif /* YAPOR || (TABLING && !YAP_MEMORY_ALLOC_SCHEME) */
return 0;
} }
static void static int
save_stacks(int mode) save_stacks(int mode)
{ {
int j; int j;
@ -503,13 +529,16 @@ save_stacks(int mode)
/* Now, go and save the state */ /* Now, go and save the state */
/* Save the local stack */ /* Save the local stack */
j = Unsigned(LCL0) - Unsigned(ASP); j = Unsigned(LCL0) - Unsigned(ASP);
mywrite(splfild, (char *) ASP, j); if (mywrite(splfild, (char *) ASP, j) < 0)
return -1;
/* Save the global stack */ /* Save the global stack */
j = Unsigned(H) - Unsigned(Yap_GlobalBase); j = Unsigned(H) - Unsigned(Yap_GlobalBase);
mywrite(splfild, (char *) Yap_GlobalBase, j); if (mywrite(splfild, (char *) Yap_GlobalBase, j) < 0)
return -1;
/* Save the trail */ /* Save the trail */
j = Unsigned(TR) - Unsigned(Yap_TrailBase); j = Unsigned(TR) - Unsigned(Yap_TrailBase);
mywrite(splfild, (char *) Yap_TrailBase, j); if (mywrite(splfild, (char *) Yap_TrailBase, j) < 0)
return -1;
break; break;
case DO_ONLY_CODE: case DO_ONLY_CODE:
{ {
@ -518,39 +547,36 @@ save_stacks(int mode)
CELL val = TrailTerm(tr_ptr-1); CELL val = TrailTerm(tr_ptr-1);
if (IsVarTerm(val)) { if (IsVarTerm(val)) {
CELL *d1 = VarOfTerm(val); CELL *d1 = VarOfTerm(val);
if (d1 < (CELL *)HeapTop) if (d1 < (CELL *)HeapTop) {
putout(val); if (putout(val) < 0)
return -1;
}
} else if (IsPairTerm(val)) { } else if (IsPairTerm(val)) {
CELL *d1 = RepPair(val); CELL *d1 = RepPair(val);
if (d1 < (CELL *)HeapTop) if (d1 < (CELL *)HeapTop) {
putout(val); if (putout(val) < 0)
return -1;
}
} }
tr_ptr--; tr_ptr--;
} }
} }
putcellptr(NULL); if (putcellptr(NULL) < 0)
return -1;
break; break;
} }
return 0;
} }
static void static int
save_crc(void) save_crc(void)
{ {
/* Save a CRC */ /* Save a CRC */
mywrite(splfild, end_msg, 256); return mywrite(splfild, end_msg, 256);
#ifdef MACYAP
NewFileInfo('TEXT', 'MYap');
if (DefVol)
SetVol(0l, DefVol);
DefVol = 0;
#endif
} }
static Int static Int
do_save(int mode) { do_save(int mode) {
#ifdef MACYAP
NewFileInfo('YAPS', 'MYap');
#endif
Term t1 = Deref(ARG1); Term t1 = Deref(ARG1);
if (Yap_HoleSize) { if (Yap_HoleSize) {
@ -568,12 +594,18 @@ do_save(int mode) {
"restore/1, open(%s)", strerror(errno)); "restore/1, open(%s)", strerror(errno));
return(FALSE); return(FALSE);
} }
put_info(FullSaved, mode); if (put_info(FullSaved, mode) < 0)
save_regs(mode); return -1;
save_code_info(); if (save_regs(mode) < 0)
save_heap(); return -1;
save_stacks(mode); if (save_code_info() < 0)
save_crc(); return -1;
if (save_heap() < 0)
return -1;
if (save_stacks(mode) < 0)
return -1;
if (save_crc() < 0)
return -1;
close_file(); close_file();
return (TRUE); return (TRUE);
} }