From f7d61b8d3053af3e64bd6345e91706c0dc8c3b5f Mon Sep 17 00:00:00 2001 From: David Schleef Date: Mon, 26 Jul 2010 14:52:52 -0700 Subject: [PATCH] Use TMPDIR for temporary code memory files Also better error reporting. --- orc/orccodemem.c | 41 ++++++++++++++++++++++++----------------- orc/orccompiler.c | 1 + 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/orc/orccodemem.c b/orc/orccodemem.c index d56afcd..8863427 100644 --- a/orc/orccodemem.c +++ b/orc/orccodemem.c @@ -31,33 +31,40 @@ orc_compiler_allocate_codemem (OrcCompiler *compiler) { int fd; int n; + static char *tmpdir = NULL; + char *filename; - { - char *filename; - - filename = malloc (strlen ("/tmp/orcexec..") + - strlen (compiler->program->name) + 6 + 1); - sprintf(filename, "/tmp/orcexec.%s.XXXXXX", compiler->program->name); - fd = mkstemp (filename); - if (fd == -1) { - /* FIXME oh crap */ - ORC_COMPILER_ERROR (compiler, "failed to create temp file"); - return; + if (tmpdir == NULL) { + tmpdir = getenv ("TMPDIR"); + if (tmpdir == NULL) { + tmpdir = "/tmp"; } - if (!_orc_compiler_flag_debug) { - unlink (filename); - } - free (filename); } + filename = malloc (strlen ("/orcexec..") + + strlen (tmpdir) + strlen (compiler->program->name) + 6 + 1); + sprintf(filename, "%s/orcexec.%s.XXXXXX", tmpdir, compiler->program->name); + fd = mkstemp (filename); + if (fd == -1) { + /* FIXME oh crap */ + ORC_COMPILER_ERROR (compiler, "failed to create temp file"); + return; + } + if (!_orc_compiler_flag_debug) { + unlink (filename); + } + free (filename); + n = ftruncate (fd, SIZE); - compiler->program->code = mmap (NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0); + compiler->program->code = mmap (NULL, SIZE, PROT_READ|PROT_WRITE, + MAP_SHARED, fd, 0); if (compiler->program->code == MAP_FAILED) { ORC_COMPILER_ERROR(compiler, "failed to create write map"); return; } - compiler->program->code_exec = mmap (NULL, SIZE, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); + compiler->program->code_exec = mmap (NULL, SIZE, PROT_READ|PROT_EXEC, + MAP_SHARED, fd, 0); if (compiler->program->code_exec == MAP_FAILED) { ORC_COMPILER_ERROR(compiler, "failed to create exec map"); return; diff --git a/orc/orccompiler.c b/orc/orccompiler.c index fd8ac58..82eede6 100644 --- a/orc/orccompiler.c +++ b/orc/orccompiler.c @@ -257,6 +257,7 @@ orc_program_compile_full (OrcProgram *program, OrcTarget *target, ORC_INFO("allocating code memory"); orc_compiler_allocate_codemem (compiler); + if (compiler->error) goto error; ORC_INFO("compiling for target"); compiler->target->compile (compiler); -- 2.7.4