65oo2/vm/jit/jit.c

83 lines
1.3 KiB
C
Raw Normal View History

2024-02-22 04:10:55 +00:00
#include <stdint.h>
#include <string.h>
#ifdef _WIN32
# include <memoryapi.h>
# include <sysinfoapi.h>
#else
# include <sys/mman.h>
# include <unistd.h>
#endif
#include "jit.h"
static size_t page_size = 0;
JitBuffer *
jit_buffer_new(void)
{
#ifdef _WIN32
DWORD type;
DWORD prot;
SYSTEM_INFO sysinfo;
if (page_size == 0)
{
GetSystemInfo(&sysinfo);
page_size = sysinfo.dwPageSize;
}
type = MEM_RESERVE | MEM_COMMIT;
prot = PAGE_READWRITE;
return (VirtualAlloc(NULL, page_size, type, prot));
#else
int prot;
int flags;
if (page_size == 0)
{
page_size = sysconf(_SC_PAGESIZE);
}
prot = PROT_READ | PROT_WRITE;
flags = MAP_ANONYMOUS | MAP_PRIVATE;
return (mmap(NULL, page_size, prot, flags, -1, 0));
#endif
}
void
jit_buffer_finalize(JitBuffer *buff)
{
#ifdef _WIN32
DWORD old;
VirtualProtect(buff, page_size, PAGE_EXECUTE_READ, &old);
#else
mprotect(buff, page_size, PROT_READ | PROT_EXEC);
#endif
}
void
jit_buffer_add(JitBuffer *buff, int size, void *value)
{
memcpy(buff->code + buff->count, value, size);
buff->count += size;
}
void
jit_buffer_execute(JitBuffer *buff)
{
}
void
jit_buffer_destroy(JitBuffer *buff)
{
#ifdef _WIN32
VirtualFree(buff, page_size, MEM_RELEASE);
#else
munmap(buff, page_size);
#endif
}