orccodemem.c 1.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>

#include <orc/orcprogram.h>
13
#include <orc/orcdebug.h>
14 15 16 17

#define SIZE 65536


18
#ifdef HAVE_CODEMEM_MMAP
19
void
20
orc_compiler_allocate_codemem (OrcCompiler *compiler)
21 22 23
{
  char filename[32] = "/tmp/orcexecXXXXXX";
  int fd;
24
  int n;
25 26 27 28

  fd = mkstemp (filename);
  if (fd == -1) {
    /* FIXME oh crap */
29
    ORC_PROGRAM_ERROR (compiler, "failed to create temp file");
30 31 32 33
    return;
  }
  unlink (filename);

34
  n = ftruncate (fd, SIZE);
35

36 37 38
  compiler->program->code = mmap (NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  if (compiler->program->code == MAP_FAILED) {
    ORC_PROGRAM_ERROR(compiler, "failed to create write map");
39 40
    return;
  }
41 42 43
  compiler->program->code_exec = mmap (NULL, SIZE, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
  if (compiler->program->code_exec == MAP_FAILED) {
    ORC_PROGRAM_ERROR(compiler, "failed to create exec map");
44 45 46 47 48
    return;
  }

  close (fd);

49 50
  compiler->program->code_size = SIZE;
  compiler->codeptr = compiler->program->code;
51
}
52 53 54 55
#endif

#ifdef HAVE_CODEMEM_MALLOC
void
56
orc_compiler_allocate_codemem (OrcCompiler *compiler)
57 58 59
{
  /* Now you know why Windows has viruses */

60 61 62 63
  compiler->program->code = malloc(SIZE);
  compiler->program->code_exec = compiler->code;
  compiler->program->code_size = SIZE;
  compiler->codeptr = compiler->program->code;
64 65
}
#endif
66