moved utilities.cpp functions to vm

Phil Dawes 2009-08-17 21:37:09 +01:00
parent b38191ae0e
commit eefa976e6f
2 changed files with 65 additions and 9 deletions

63
vm/utilities.cpp Normal file → Executable file
View File

@ -4,57 +4,102 @@ namespace factor
{ {
/* If memory allocation fails, bail out */ /* If memory allocation fails, bail out */
void *safe_malloc(size_t size) void *factorvm::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 *safe_strdup(const vm_char *str) void *safe_malloc(size_t size)
{
return vm->safe_malloc(size);
}
vm_char *factorvm::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);
return ptr; return ptr;
} }
vm_char *safe_strdup(const vm_char *str)
{
return vm->safe_strdup(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 nl() void factorvm::nl()
{ {
fputs("\n",stdout); fputs("\n",stdout);
} }
void print_string(const char *str) void nl()
{
return vm->nl();
}
void factorvm::print_string(const char *str)
{ {
fputs(str,stdout); fputs(str,stdout);
} }
void print_cell(cell x) void print_string(const char *str)
{
return vm->print_string(str);
}
void factorvm::print_cell(cell x)
{ {
printf(CELL_FORMAT,x); printf(CELL_FORMAT,x);
} }
void print_cell_hex(cell x) void print_cell(cell x)
{
return vm->print_cell(x);
}
void factorvm::print_cell_hex(cell x)
{ {
printf(CELL_HEX_FORMAT,x); printf(CELL_HEX_FORMAT,x);
} }
void print_cell_hex_pad(cell x) void print_cell_hex(cell x)
{
return vm->print_cell_hex(x);
}
void factorvm::print_cell_hex_pad(cell x)
{ {
printf(CELL_HEX_PAD_FORMAT,x); printf(CELL_HEX_PAD_FORMAT,x);
} }
void print_fixnum(fixnum x) void print_cell_hex_pad(cell x)
{
return vm->print_cell_hex_pad(x);
}
void factorvm::print_fixnum(fixnum x)
{ {
printf(FIXNUM_FORMAT,x); printf(FIXNUM_FORMAT,x);
} }
cell read_cell_hex() void print_fixnum(fixnum x)
{
return vm->print_fixnum(x);
}
cell factorvm::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);
return cell; return cell;
}
cell read_cell_hex()
{
return vm->read_cell_hex();
}; };
} }

View File

@ -512,6 +512,17 @@ struct factorvm {
void factor_eval_free(char *result); void factor_eval_free(char *result);
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();
// next method here: // next method here: