orccodemem.c 2.1 KB
Newer Older
1 2 3 4 5 6 7

#include "config.h"

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

David Schleef's avatar
David Schleef committed
8
#ifdef HAVE_UNISTD_H
9
#include <unistd.h>
David Schleef's avatar
David Schleef committed
10
#endif
11
#include <sys/types.h>
12 13
#include <sys/stat.h>
#include <fcntl.h>
David Schleef's avatar
David Schleef committed
14
#ifdef HAVE_CODEMEM_MMAP
15
#include <sys/mman.h>
David Schleef's avatar
David Schleef committed
16
#endif
17 18 19
#ifdef HAVE_CODEMEM_VIRTUALALLOC
#include <windows.h>
#endif
20 21

#include <orc/orcprogram.h>
22
#include <orc/orcdebug.h>
23

24

25 26 27
#define SIZE 65536


28
#ifdef HAVE_CODEMEM_MMAP
29
void
30
orc_compiler_allocate_codemem (OrcCompiler *compiler)
31 32
{
  int fd;
33
  int n;
34

35 36 37
  {
    char *filename;

38 39 40 41
    filename = malloc (strlen ("/tmp/orcexec..") +
        strlen (compiler->program->name) + 6 + 1);
    sprintf(filename, "/tmp/orcexec.%s.XXXXXX", compiler->program->name);
    fd = mkstemp (filename);
42 43 44 45 46
    if (fd == -1) {
      /* FIXME oh crap */
      ORC_COMPILER_ERROR (compiler, "failed to create temp file");
      return;
    }
47 48 49
    if (!_orc_compiler_flag_debug) {
      unlink (filename);
    }
50 51
    free (filename);
  }
52

53
  n = ftruncate (fd, SIZE);
54

55 56
  compiler->program->code = mmap (NULL, SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  if (compiler->program->code == MAP_FAILED) {
David Schleef's avatar
David Schleef committed
57
    ORC_COMPILER_ERROR(compiler, "failed to create write map");
58 59
    return;
  }
60 61
  compiler->program->code_exec = mmap (NULL, SIZE, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);
  if (compiler->program->code_exec == MAP_FAILED) {
David Schleef's avatar
David Schleef committed
62
    ORC_COMPILER_ERROR(compiler, "failed to create exec map");
63 64 65 66 67
    return;
  }

  close (fd);

68 69
  compiler->program->code_size = SIZE;
  compiler->codeptr = compiler->program->code;
70
}
71 72
#endif

73
#ifdef HAVE_CODEMEM_VIRTUALALLOC
74
void
75
orc_compiler_allocate_codemem (OrcCompiler *compiler)
76
{
77 78 79 80 81 82
  compiler->program->code = VirtualAlloc(NULL, SIZE, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  compiler->program->code_exec = compiler->program->code;
  compiler->program->code_size = SIZE;
  compiler->codeptr = compiler->program->code;
}
#endif
83

84 85 86 87
#ifdef HAVE_CODEMEM_MALLOC
void
orc_compiler_allocate_codemem (OrcCompiler *compiler)
{
88
  compiler->program->code = malloc(SIZE);
David Schleef's avatar
David Schleef committed
89
  compiler->program->code_exec = compiler->program->code;
90 91
  compiler->program->code_size = SIZE;
  compiler->codeptr = compiler->program->code;
92 93
}
#endif
94