vm: Add threadsafe_sterror to the vm.

db4
Doug Coleman 2014-07-04 03:00:08 -07:00
parent 91b16d7ed1
commit 69262be4d8
7 changed files with 39 additions and 6 deletions

View File

@ -200,12 +200,26 @@ bool factor_vm::read_embedded_image_footer(FILE* file,
return footer->magic == image_magic; return footer->magic == image_magic;
} }
char *threadsafe_strerror(int errnum) {
char *buf = (char *) malloc(STRERROR_BUFFER_SIZE);
if(!buf) {
fatal_error("Out of memory in threadsafe_strerror", 0);
}
int ret = THREADSAFE_STRERROR(errnum, buf, STRERROR_BUFFER_SIZE);
if(ret != 0) {
std::cout << "Truncated output from THREADSAFE_STRERROR, error code:" << errnum << ", ret: " << ret << std::endl;
}
return buf;
}
FILE* factor_vm::open_image(vm_parameters* p) { FILE* factor_vm::open_image(vm_parameters* p) {
if (p->embedded_image) { if (p->embedded_image) {
FILE* file = OPEN_READ(p->executable_path); FILE* file = OPEN_READ(p->executable_path);
if (file == NULL) { if (file == NULL) {
std::cout << "Cannot open embedded image" << std::endl; std::cout << "Cannot open embedded image" << std::endl;
std::cout << strerror(errno) << std::endl; char *msg = threadsafe_strerror(errno);
std::cout << "strerror: " << msg << std::endl;
free(msg);
exit(1); exit(1);
} }
embedded_image_footer footer; embedded_image_footer footer;
@ -225,7 +239,9 @@ void factor_vm::load_image(vm_parameters* p) {
FILE* file = open_image(p); FILE* file = open_image(p);
if (file == NULL) { if (file == NULL) {
std::cout << "Cannot open image file: " << p->image_path << std::endl; std::cout << "Cannot open image file: " << p->image_path << std::endl;
std::cout << strerror(errno) << std::endl; char *msg = threadsafe_strerror(errno);
std::cout << "strerror: " << msg << std::endl;
free(msg);
exit(1); exit(1);
} }
@ -265,7 +281,9 @@ bool factor_vm::save_image(const vm_char* saving_filename,
file = OPEN_WRITE(saving_filename); file = OPEN_WRITE(saving_filename);
if (file == NULL) { if (file == NULL) {
std::cout << "Cannot open image file for writing: " << saving_filename << std::endl; std::cout << "Cannot open image file for writing: " << saving_filename << std::endl;
std::cout << strerror(errno) << std::endl; char *msg = threadsafe_strerror(errno);
std::cout << "strerror: " << msg << std::endl;
free(msg);
return false; return false;
} }
@ -295,8 +313,12 @@ bool factor_vm::save_image(const vm_char* saving_filename,
ok = false; ok = false;
safe_fclose(file); safe_fclose(file);
if (!ok) if (!ok) {
std::cout << "save-image failed: " << strerror(errno) << std::endl; std::cout << "save-image failed." << std::endl;
char *msg = threadsafe_strerror(errno);
std::cout << "strerror: " << msg << std::endl;
free(msg);
}
else else
move_file(saving_filename, filename); move_file(saving_filename, filename);

View File

@ -3,6 +3,8 @@ namespace factor {
static const cell image_magic = 0x0f0e0d0c; static const cell image_magic = 0x0f0e0d0c;
static const cell image_version = 4; static const cell image_version = 4;
const size_t STRERROR_BUFFER_SIZE = 1024;
struct embedded_image_footer { struct embedded_image_footer {
cell magic; cell magic;
cell image_offset; cell image_offset;

View File

@ -5,5 +5,4 @@ namespace factor {
void early_init(); void early_init();
const char* vm_executable_path(); const char* vm_executable_path();
const char* default_image_path(); const char* default_image_path();
} }

View File

@ -502,4 +502,8 @@ void abort() {
::abort(); ::abort();
} }
int THREADSAFE_STRERROR(int errnum, char *buf, size_t buflen) {
return strerror_r(errnum, buf, buflen);
}
} }

View File

@ -46,4 +46,6 @@ void move_file(const vm_char* path1, const vm_char* path2);
static inline void breakpoint() { __builtin_trap(); } static inline void breakpoint() { __builtin_trap(); }
int THREADSAFE_STRERROR(int errnum, char *buf, size_t buflen);
} }

View File

@ -373,4 +373,6 @@ void factor_vm::end_sampling_profiler_timer() {
void abort() { ::abort(); } void abort() { ::abort(); }
int THREADSAFE_STRERROR(int errnum, char *buf, size_t buflen) {
return strerror_s(buf, buflen, errnum);
} }

View File

@ -93,4 +93,6 @@ inline static void breakpoint() { DebugBreak(); }
#define FUNCTION_TOC_POINTER(ptr) ptr #define FUNCTION_TOC_POINTER(ptr) ptr
extern HANDLE boot_thread; extern HANDLE boot_thread;
int THREADSAFE_STRERROR(int errnum, char *buf, size_t buflen);
} }