moved utility functions and fatal_error out of vm struct since doesn't need state

db4
Phil Dawes 2009-08-25 08:20:58 +01:00
parent b02c602a89
commit 784b8d16ae
9 changed files with 43 additions and 49 deletions

View File

@ -17,7 +17,7 @@ void factorvm::out_of_memory()
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(": "); print_cell_hex(tagged); nl();

View File

@ -27,6 +27,7 @@ PRIMITIVE(die);
PRIMITIVE(call_clear);
PRIMITIVE(unimplemented);
void fatal_error(const char* msg, cell tagged);
void memory_signal_handler_impl();
void fp_signal_handler_impl();
void misc_signal_handler_impl();

View File

@ -203,13 +203,13 @@ void mach_initialize ()
/* Allocate a port on which the thread shall listen for exceptions. */
if (mach_port_allocate (self, MACH_PORT_RIGHT_RECEIVE, &our_exception_port)
!= 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. */
if (mach_port_insert_right (self, our_exception_port, our_exception_port,
MACH_MSG_TYPE_MAKE_SEND)
!= 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. */
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,
EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
!= KERN_SUCCESS)
vm->fatal_error("task_set_exception_ports() failed",0);
fatal_error("task_set_exception_ports() failed",0);
}
}

View File

@ -31,7 +31,7 @@ const char *default_image_path()
const char *iter = path;
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 + len,SUFFIX,SUFFIX_LEN + 1);
return new_path;

View File

@ -6,18 +6,18 @@ namespace factor
/* Snarfed from SBCL linux-so.c. You must free() this yourself. */
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);
if (size < 0)
{
vm->fatal_error("Cannot read /proc/self/exe",0);
fatal_error("Cannot read /proc/self/exe",0);
return NULL;
}
else
{
path[size] = '\0';
return vm->safe_strdup(path);
return safe_strdup(path);
}
}

View File

@ -8,11 +8,11 @@ void *start_thread(void *(*start_routine)(void *),void *args)
pthread_attr_t attr;
pthread_t thread;
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)
vm->fatal_error("pthread_attr_setdetachstate() failed",0);
fatal_error("pthread_attr_setdetachstate() failed",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);
return (void*)thread;
}
@ -85,12 +85,12 @@ segment *alloc_segment(cell size)
vm->out_of_memory();
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)
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->size = size;
@ -107,7 +107,7 @@ void dealloc_segment(segment *block)
pagesize + block->size + pagesize);
if(retval)
vm->fatal_error("dealloc_segment failed",0);
fatal_error("dealloc_segment failed",0);
free(block);
}
@ -165,7 +165,7 @@ static void sigaction_safe(int signum, const struct sigaction *act, struct sigac
while(ret == -1 && errno == EINTR);
if(ret == -1)
vm->fatal_error("sigaction failed", 0);
fatal_error("sigaction failed", 0);
}
void unix_init_signals()
@ -227,7 +227,7 @@ extern "C" {
void safe_close(int fd)
{
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)
@ -246,7 +246,7 @@ bool check_write(int fd, void *data, ssize_t size)
void safe_write(int fd, void *data, ssize_t 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)
@ -258,7 +258,7 @@ bool safe_read(int fd, void *data, ssize_t size)
return safe_read(fd,data,size);
else
{
vm->fatal_error("error reading fd",errno);
fatal_error("error reading fd",errno);
return false;
}
}
@ -277,7 +277,7 @@ void *stdin_loop(void *arg)
break;
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(;;)
{
@ -314,19 +314,19 @@ void open_console()
int filedes[2];
if(pipe(filedes) < 0)
vm->fatal_error("Error opening control pipe",errno);
fatal_error("Error opening control pipe",errno);
control_read = filedes[0];
control_write = filedes[1];
if(pipe(filedes) < 0)
vm->fatal_error("Error opening size pipe",errno);
fatal_error("Error opening size pipe",errno);
size_read = filedes[0];
size_write = filedes[1];
if(pipe(filedes) < 0)
vm->fatal_error("Error opening stdin pipe",errno);
fatal_error("Error opening stdin pipe",errno);
stdin_read = filedes[0];
stdin_write = filedes[1];
@ -341,7 +341,7 @@ VM_C_API void wait_for_stdin()
if(errno == EINTR)
wait_for_stdin();
else
vm->fatal_error("Error writing control fd",errno);
fatal_error("Error writing control fd",errno);
}
}

View File

@ -4,14 +4,14 @@ namespace factor
{
/* If memory allocation fails, bail out */
void *factorvm::safe_malloc(size_t size)
void *safe_malloc(size_t size)
{
void *ptr = malloc(size);
if(!ptr) fatal_error("Out of memory in safe_malloc", 0);
return ptr;
}
vm_char *factorvm::safe_strdup(const vm_char *str)
vm_char *safe_strdup(const vm_char *str)
{
vm_char *ptr = STRDUP(str);
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.
Instead we define the common cases here. */
void factorvm::nl()
void nl()
{
fputs("\n",stdout);
}
void factorvm::print_string(const char *str)
void print_string(const char *str)
{
fputs(str,stdout);
}
void factorvm::print_cell(cell x)
void print_cell(cell x)
{
printf(CELL_FORMAT,x);
}
void factorvm::print_cell_hex(cell x)
void print_cell_hex(cell 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);
}
void factorvm::print_fixnum(fixnum x)
void print_fixnum(fixnum x)
{
printf(FIXNUM_FORMAT,x);
}
cell factorvm::read_cell_hex()
cell read_cell_hex()
{
cell cell;
if(scanf(CELL_HEX_FORMAT,&cell) < 0) exit(1);

View File

@ -1,4 +1,12 @@
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();
}

View File

@ -66,7 +66,6 @@ struct factorvm {
unsigned int signal_fpu_status;
stack_frame *signal_callstack_top;
void out_of_memory();
void fatal_error(const char* msg, cell tagged);
void critical_error(const char* msg, cell tagged);
void throw_error(cell error, stack_frame *callstack_top);
void not_implemented_error();
@ -658,20 +657,6 @@ struct factorvm {
void factor_yield();
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-*
inline void vmprim_existsp();
long thread_id();