moved utility functions and fatal_error out of vm struct since doesn't need state
parent
26586c24f0
commit
f60b1e1dd0
|
@ -17,7 +17,7 @@ void factorvm::out_of_memory()
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void factorvm::fatal_error(const char* msg, cell tagged)
|
void fatal_error(const char* msg, cell tagged)
|
||||||
{
|
{
|
||||||
print_string("fatal_error: "); print_string(msg);
|
print_string("fatal_error: "); print_string(msg);
|
||||||
print_string(": "); print_cell_hex(tagged); nl();
|
print_string(": "); print_cell_hex(tagged); nl();
|
||||||
|
|
|
@ -27,6 +27,7 @@ PRIMITIVE(die);
|
||||||
PRIMITIVE(call_clear);
|
PRIMITIVE(call_clear);
|
||||||
PRIMITIVE(unimplemented);
|
PRIMITIVE(unimplemented);
|
||||||
|
|
||||||
|
void fatal_error(const char* msg, cell tagged);
|
||||||
void memory_signal_handler_impl();
|
void memory_signal_handler_impl();
|
||||||
void fp_signal_handler_impl();
|
void fp_signal_handler_impl();
|
||||||
void misc_signal_handler_impl();
|
void misc_signal_handler_impl();
|
||||||
|
|
|
@ -203,13 +203,13 @@ void mach_initialize ()
|
||||||
/* Allocate a port on which the thread shall listen for exceptions. */
|
/* Allocate a port on which the thread shall listen for exceptions. */
|
||||||
if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
|
if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
|
||||||
!= KERN_SUCCESS)
|
!= KERN_SUCCESS)
|
||||||
vm->fatal_error("mach_port_allocate() failed",0);
|
fatal_error("mach_port_allocate() failed",0);
|
||||||
|
|
||||||
/* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html. */
|
/* See http://web.mit.edu/darwin/src/modules/xnu/osfmk/man/mach_port_insert_right.html. */
|
||||||
if (mach_port_insert_right (self, our_exception_port, our_exception_port,
|
if (mach_port_insert_right (self, our_exception_port, our_exception_port,
|
||||||
MACH_MSG_TYPE_MAKE_SEND)
|
MACH_MSG_TYPE_MAKE_SEND)
|
||||||
!= KERN_SUCCESS)
|
!= KERN_SUCCESS)
|
||||||
vm->fatal_error("mach_port_insert_right() failed",0);
|
fatal_error("mach_port_insert_right() failed",0);
|
||||||
|
|
||||||
/* The exceptions we want to catch. */
|
/* The exceptions we want to catch. */
|
||||||
mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC;
|
mask = EXC_MASK_BAD_ACCESS | EXC_MASK_ARITHMETIC;
|
||||||
|
@ -226,7 +226,7 @@ void mach_initialize ()
|
||||||
if (task_set_exception_ports (self, mask, our_exception_port,
|
if (task_set_exception_ports (self, mask, our_exception_port,
|
||||||
EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
|
EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
|
||||||
!= KERN_SUCCESS)
|
!= KERN_SUCCESS)
|
||||||
vm->fatal_error("task_set_exception_ports() failed",0);
|
fatal_error("task_set_exception_ports() failed",0);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ const char *default_image_path()
|
||||||
const char *iter = path;
|
const char *iter = path;
|
||||||
while(*iter) { len++; iter++; }
|
while(*iter) { len++; iter++; }
|
||||||
|
|
||||||
char *new_path = (char *)vm->safe_malloc(PATH_MAX + SUFFIX_LEN + 1);
|
char *new_path = (char *)safe_malloc(PATH_MAX + SUFFIX_LEN + 1);
|
||||||
memcpy(new_path,path,len + 1);
|
memcpy(new_path,path,len + 1);
|
||||||
memcpy(new_path + len,SUFFIX,SUFFIX_LEN + 1);
|
memcpy(new_path + len,SUFFIX,SUFFIX_LEN + 1);
|
||||||
return new_path;
|
return new_path;
|
||||||
|
|
|
@ -6,18 +6,18 @@ namespace factor
|
||||||
/* Snarfed from SBCL linux-so.c. You must free() this yourself. */
|
/* Snarfed from SBCL linux-so.c. You must free() this yourself. */
|
||||||
const char *vm_executable_path()
|
const char *vm_executable_path()
|
||||||
{
|
{
|
||||||
char *path = (char *)vm->safe_malloc(PATH_MAX + 1);
|
char *path = (char *)safe_malloc(PATH_MAX + 1);
|
||||||
|
|
||||||
int size = readlink("/proc/self/exe", path, PATH_MAX);
|
int size = readlink("/proc/self/exe", path, PATH_MAX);
|
||||||
if (size < 0)
|
if (size < 0)
|
||||||
{
|
{
|
||||||
vm->fatal_error("Cannot read /proc/self/exe",0);
|
fatal_error("Cannot read /proc/self/exe",0);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
path[size] = '\0';
|
path[size] = '\0';
|
||||||
return vm->safe_strdup(path);
|
return safe_strdup(path);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,11 @@ void *start_thread(void *(*start_routine)(void *),void *args)
|
||||||
pthread_attr_t attr;
|
pthread_attr_t attr;
|
||||||
pthread_t thread;
|
pthread_t thread;
|
||||||
if (pthread_attr_init (&attr) != 0)
|
if (pthread_attr_init (&attr) != 0)
|
||||||
vm->fatal_error("pthread_attr_init() failed",0);
|
fatal_error("pthread_attr_init() failed",0);
|
||||||
if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0)
|
if (pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED) != 0)
|
||||||
vm->fatal_error("pthread_attr_setdetachstate() failed",0);
|
fatal_error("pthread_attr_setdetachstate() failed",0);
|
||||||
if (pthread_create (&thread, &attr, start_routine, args) != 0)
|
if (pthread_create (&thread, &attr, start_routine, args) != 0)
|
||||||
vm->fatal_error("pthread_create() failed",0);
|
fatal_error("pthread_create() failed",0);
|
||||||
pthread_attr_destroy (&attr);
|
pthread_attr_destroy (&attr);
|
||||||
return (void*)thread;
|
return (void*)thread;
|
||||||
}
|
}
|
||||||
|
@ -85,12 +85,12 @@ segment *alloc_segment(cell size)
|
||||||
vm->out_of_memory();
|
vm->out_of_memory();
|
||||||
|
|
||||||
if(mprotect(array,pagesize,PROT_NONE) == -1)
|
if(mprotect(array,pagesize,PROT_NONE) == -1)
|
||||||
vm->fatal_error("Cannot protect low guard page",(cell)array);
|
fatal_error("Cannot protect low guard page",(cell)array);
|
||||||
|
|
||||||
if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1)
|
if(mprotect(array + pagesize + size,pagesize,PROT_NONE) == -1)
|
||||||
vm->fatal_error("Cannot protect high guard page",(cell)array);
|
fatal_error("Cannot protect high guard page",(cell)array);
|
||||||
|
|
||||||
segment *retval = (segment *)vm->safe_malloc(sizeof(segment));
|
segment *retval = (segment *)safe_malloc(sizeof(segment));
|
||||||
|
|
||||||
retval->start = (cell)(array + pagesize);
|
retval->start = (cell)(array + pagesize);
|
||||||
retval->size = size;
|
retval->size = size;
|
||||||
|
@ -107,7 +107,7 @@ void dealloc_segment(segment *block)
|
||||||
pagesize + block->size + pagesize);
|
pagesize + block->size + pagesize);
|
||||||
|
|
||||||
if(retval)
|
if(retval)
|
||||||
vm->fatal_error("dealloc_segment failed",0);
|
fatal_error("dealloc_segment failed",0);
|
||||||
|
|
||||||
free(block);
|
free(block);
|
||||||
}
|
}
|
||||||
|
@ -165,7 +165,7 @@ static void sigaction_safe(int signum, const struct sigaction *act, struct sigac
|
||||||
while(ret == -1 && errno == EINTR);
|
while(ret == -1 && errno == EINTR);
|
||||||
|
|
||||||
if(ret == -1)
|
if(ret == -1)
|
||||||
vm->fatal_error("sigaction failed", 0);
|
fatal_error("sigaction failed", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void unix_init_signals()
|
void unix_init_signals()
|
||||||
|
@ -227,7 +227,7 @@ extern "C" {
|
||||||
void safe_close(int fd)
|
void safe_close(int fd)
|
||||||
{
|
{
|
||||||
if(close(fd) < 0)
|
if(close(fd) < 0)
|
||||||
vm->fatal_error("error closing fd",errno);
|
fatal_error("error closing fd",errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool check_write(int fd, void *data, ssize_t size)
|
bool check_write(int fd, void *data, ssize_t size)
|
||||||
|
@ -246,7 +246,7 @@ bool check_write(int fd, void *data, ssize_t size)
|
||||||
void safe_write(int fd, void *data, ssize_t size)
|
void safe_write(int fd, void *data, ssize_t size)
|
||||||
{
|
{
|
||||||
if(!check_write(fd,data,size))
|
if(!check_write(fd,data,size))
|
||||||
vm->fatal_error("error writing fd",errno);
|
fatal_error("error writing fd",errno);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool safe_read(int fd, void *data, ssize_t size)
|
bool safe_read(int fd, void *data, ssize_t size)
|
||||||
|
@ -258,7 +258,7 @@ bool safe_read(int fd, void *data, ssize_t size)
|
||||||
return safe_read(fd,data,size);
|
return safe_read(fd,data,size);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
vm->fatal_error("error reading fd",errno);
|
fatal_error("error reading fd",errno);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,7 +277,7 @@ void *stdin_loop(void *arg)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if(buf[0] != 'X')
|
if(buf[0] != 'X')
|
||||||
vm->fatal_error("stdin_loop: bad data on control fd",buf[0]);
|
fatal_error("stdin_loop: bad data on control fd",buf[0]);
|
||||||
|
|
||||||
for(;;)
|
for(;;)
|
||||||
{
|
{
|
||||||
|
@ -314,19 +314,19 @@ void open_console()
|
||||||
int filedes[2];
|
int filedes[2];
|
||||||
|
|
||||||
if(pipe(filedes) < 0)
|
if(pipe(filedes) < 0)
|
||||||
vm->fatal_error("Error opening control pipe",errno);
|
fatal_error("Error opening control pipe",errno);
|
||||||
|
|
||||||
control_read = filedes[0];
|
control_read = filedes[0];
|
||||||
control_write = filedes[1];
|
control_write = filedes[1];
|
||||||
|
|
||||||
if(pipe(filedes) < 0)
|
if(pipe(filedes) < 0)
|
||||||
vm->fatal_error("Error opening size pipe",errno);
|
fatal_error("Error opening size pipe",errno);
|
||||||
|
|
||||||
size_read = filedes[0];
|
size_read = filedes[0];
|
||||||
size_write = filedes[1];
|
size_write = filedes[1];
|
||||||
|
|
||||||
if(pipe(filedes) < 0)
|
if(pipe(filedes) < 0)
|
||||||
vm->fatal_error("Error opening stdin pipe",errno);
|
fatal_error("Error opening stdin pipe",errno);
|
||||||
|
|
||||||
stdin_read = filedes[0];
|
stdin_read = filedes[0];
|
||||||
stdin_write = filedes[1];
|
stdin_write = filedes[1];
|
||||||
|
@ -341,7 +341,7 @@ VM_C_API void wait_for_stdin()
|
||||||
if(errno == EINTR)
|
if(errno == EINTR)
|
||||||
wait_for_stdin();
|
wait_for_stdin();
|
||||||
else
|
else
|
||||||
vm->fatal_error("Error writing control fd",errno);
|
fatal_error("Error writing control fd",errno);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,14 +4,14 @@ namespace factor
|
||||||
{
|
{
|
||||||
|
|
||||||
/* If memory allocation fails, bail out */
|
/* If memory allocation fails, bail out */
|
||||||
void *factorvm::safe_malloc(size_t size)
|
void *safe_malloc(size_t size)
|
||||||
{
|
{
|
||||||
void *ptr = malloc(size);
|
void *ptr = malloc(size);
|
||||||
if(!ptr) fatal_error("Out of memory in safe_malloc", 0);
|
if(!ptr) fatal_error("Out of memory in safe_malloc", 0);
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
vm_char *factorvm::safe_strdup(const vm_char *str)
|
vm_char *safe_strdup(const vm_char *str)
|
||||||
{
|
{
|
||||||
vm_char *ptr = STRDUP(str);
|
vm_char *ptr = STRDUP(str);
|
||||||
if(!ptr) fatal_error("Out of memory in safe_strdup", 0);
|
if(!ptr) fatal_error("Out of memory in safe_strdup", 0);
|
||||||
|
@ -21,38 +21,38 @@ vm_char *factorvm::safe_strdup(const vm_char *str)
|
||||||
|
|
||||||
/* We don't use printf directly, because format directives are not portable.
|
/* We don't use printf directly, because format directives are not portable.
|
||||||
Instead we define the common cases here. */
|
Instead we define the common cases here. */
|
||||||
void factorvm::nl()
|
void nl()
|
||||||
{
|
{
|
||||||
fputs("\n",stdout);
|
fputs("\n",stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void factorvm::print_string(const char *str)
|
void print_string(const char *str)
|
||||||
{
|
{
|
||||||
fputs(str,stdout);
|
fputs(str,stdout);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void factorvm::print_cell(cell x)
|
void print_cell(cell x)
|
||||||
{
|
{
|
||||||
printf(CELL_FORMAT,x);
|
printf(CELL_FORMAT,x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void factorvm::print_cell_hex(cell x)
|
void print_cell_hex(cell x)
|
||||||
{
|
{
|
||||||
printf(CELL_HEX_FORMAT,x);
|
printf(CELL_HEX_FORMAT,x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void factorvm::print_cell_hex_pad(cell x)
|
void print_cell_hex_pad(cell x)
|
||||||
{
|
{
|
||||||
printf(CELL_HEX_PAD_FORMAT,x);
|
printf(CELL_HEX_PAD_FORMAT,x);
|
||||||
}
|
}
|
||||||
|
|
||||||
void factorvm::print_fixnum(fixnum x)
|
void print_fixnum(fixnum x)
|
||||||
{
|
{
|
||||||
printf(FIXNUM_FORMAT,x);
|
printf(FIXNUM_FORMAT,x);
|
||||||
}
|
}
|
||||||
|
|
||||||
cell factorvm::read_cell_hex()
|
cell read_cell_hex()
|
||||||
{
|
{
|
||||||
cell cell;
|
cell cell;
|
||||||
if(scanf(CELL_HEX_FORMAT,&cell) < 0) exit(1);
|
if(scanf(CELL_HEX_FORMAT,&cell) < 0) exit(1);
|
||||||
|
|
|
@ -1,4 +1,12 @@
|
||||||
namespace factor
|
namespace factor
|
||||||
{
|
{
|
||||||
|
void *safe_malloc(size_t size);
|
||||||
|
vm_char *safe_strdup(const vm_char *str);
|
||||||
|
void print_string(const char *str);
|
||||||
|
void nl();
|
||||||
|
void print_cell(cell x);
|
||||||
|
void print_cell_hex(cell x);
|
||||||
|
void print_cell_hex_pad(cell x);
|
||||||
|
void print_fixnum(fixnum x);
|
||||||
|
cell read_cell_hex();
|
||||||
}
|
}
|
||||||
|
|
15
vm/vm.hpp
15
vm/vm.hpp
|
@ -66,7 +66,6 @@ struct factorvm {
|
||||||
unsigned int signal_fpu_status;
|
unsigned int signal_fpu_status;
|
||||||
stack_frame *signal_callstack_top;
|
stack_frame *signal_callstack_top;
|
||||||
void out_of_memory();
|
void out_of_memory();
|
||||||
void fatal_error(const char* msg, cell tagged);
|
|
||||||
void critical_error(const char* msg, cell tagged);
|
void critical_error(const char* msg, cell tagged);
|
||||||
void throw_error(cell error, stack_frame *callstack_top);
|
void throw_error(cell error, stack_frame *callstack_top);
|
||||||
void not_implemented_error();
|
void not_implemented_error();
|
||||||
|
@ -658,20 +657,6 @@ struct factorvm {
|
||||||
void factor_yield();
|
void factor_yield();
|
||||||
void factor_sleep(long us);
|
void factor_sleep(long us);
|
||||||
|
|
||||||
//utilities
|
|
||||||
void *safe_malloc(size_t size);
|
|
||||||
vm_char *safe_strdup(const vm_char *str);
|
|
||||||
void nl();
|
|
||||||
void print_string(const char *str);
|
|
||||||
void print_cell(cell x);
|
|
||||||
void print_cell_hex(cell x);
|
|
||||||
void print_cell_hex_pad(cell x);
|
|
||||||
void print_fixnum(fixnum x);
|
|
||||||
cell read_cell_hex();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// os-*
|
// os-*
|
||||||
inline void vmprim_existsp();
|
inline void vmprim_existsp();
|
||||||
long thread_id();
|
long thread_id();
|
||||||
|
|
Loading…
Reference in New Issue