orccodemem.c 1.45 KB
Newer Older
1 2 3 4 5 6 7 8 9

#include "config.h"

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

#include <unistd.h>
#include <sys/types.h>
David Schleef's avatar
David Schleef committed
10
#ifdef HAVE_CODEMEM_MMAP
11
#include <sys/mman.h>
David Schleef's avatar
David Schleef committed
12
#endif
13 14

#include <orc/orcprogram.h>
15
#include <orc/orcdebug.h>
16 17 18 19

#define SIZE 65536


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

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

36
  n = ftruncate (fd, SIZE);
37

38 39 40
  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");
41 42
    return;
  }
43 44 45
  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");
46 47 48 49 50
    return;
  }

  close (fd);

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

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

62
  compiler->program->code = malloc(SIZE);
David Schleef's avatar
David Schleef committed
63
  compiler->program->code_exec = compiler->program->code;
64 65
  compiler->program->code_size = SIZE;
  compiler->codeptr = compiler->program->code;
66 67
}
#endif
68