diff --git a/src/common.h b/src/common.h index b4c2ab98..f6337f8d 100644 --- a/src/common.h +++ b/src/common.h @@ -79,6 +79,14 @@ #define MUST_TAIL #endif +/* Assume that all POSIX-compatible environments provide mmap system call. */ +#if defined(_WIN32) +#define HAVE_MMAP 0 +#else +/* Assume POSIX-compatible runtime */ +#define HAVE_MMAP 1 +#endif + /* Pattern Matching for C macros. * https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms */ diff --git a/src/elf.c b/src/elf.c index e123235a..ceb9c252 100644 --- a/src/elf.c +++ b/src/elf.c @@ -10,16 +10,14 @@ #include "io.h" #include "utils.h" -#if defined(_WIN32) -/* fallback to standard I/O text stream */ -#include -#else -/* Assume POSIX-compatible runtime */ -#define USE_MMAP 1 +#if HAVE_MMAP #include #include #include #include +#else +/* fallback to standard I/O text stream */ +#include #endif enum { @@ -85,7 +83,7 @@ void elf_delete(elf_t *e) return; map_delete(e->symbols); -#if defined(USE_MMAP) +#if HAVE_MMAP if (e->raw_data) munmap(e->raw_data, e->raw_size); #else @@ -97,7 +95,7 @@ void elf_delete(elf_t *e) /* release a loaded ELF file */ static void release(elf_t *e) { -#if !defined(USE_MMAP) +#if !HAVE_MMAP free(e->raw_data); #endif @@ -301,7 +299,7 @@ bool elf_open(elf_t *e, const char *input) if (!path) return false; -#if defined(USE_MMAP) +#if HAVE_MMAP int fd = open(path, O_RDONLY); if (fd < 0) { free(path); @@ -353,7 +351,7 @@ bool elf_open(elf_t *e, const char *input) free(path); return false; } -#endif /* USE_MMAP */ +#endif /* HAVE_MMAP */ /* point to the header */ e->hdr = (const struct Elf32_Ehdr *) e->raw_data; diff --git a/src/io.c b/src/io.c index a6273737..cbf5d3a4 100644 --- a/src/io.c +++ b/src/io.c @@ -8,8 +8,7 @@ #include #include #include -#if !defined(_WIN32) -#define USE_MMAP 1 +#if HAVE_MMAP #include #include #endif @@ -32,7 +31,7 @@ static uint8_t *data_memory_base; memory_t *memory_new() { memory_t *mem = malloc(sizeof(memory_t)); -#if defined(USE_MMAP) +#if HAVE_MMAP data_memory_base = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (data_memory_base == MAP_FAILED) { @@ -53,7 +52,7 @@ memory_t *memory_new() void memory_delete(memory_t *mem) { -#if defined(USE_MMAP) +#if HAVE_MMAP munmap(mem->mem_base, MEM_SIZE); #else free(mem->mem_base); diff --git a/src/mpool.c b/src/mpool.c index feb8b194..0815b4bd 100644 --- a/src/mpool.c +++ b/src/mpool.c @@ -4,8 +4,7 @@ */ #include #include -#if !defined(_WIN32) -#define USE_MMAP 1 +#if HAVE_MMAP #include #include #endif @@ -31,7 +30,7 @@ typedef struct mpool { static void *mem_arena(size_t sz) { -#if defined(USE_MMAP) +#if HAVE_MMAP void *p = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0); if (p == MAP_FAILED) @@ -134,7 +133,7 @@ void mpool_free(mpool_t *mp, void *target) void mpool_destroy(mpool_t *mp) { -#if defined(USE_MMAP) +#if HAVE_MMAP size_t mem_size = mp->page_count * getpagesize(); area_t *cur = &mp->area, *tmp = NULL; while (cur) {