Skip to content

Commit

Permalink
Promote mmap check in common header
Browse files Browse the repository at this point in the history
  • Loading branch information
jserv committed Dec 7, 2023
1 parent 53a9c70 commit 90b42a6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
18 changes: 8 additions & 10 deletions src/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@
#include "io.h"
#include "utils.h"

#if defined(_WIN32)
/* fallback to standard I/O text stream */
#include <stdio.h>
#else
/* Assume POSIX-compatible runtime */
#define USE_MMAP 1
#if HAVE_MMAP
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
#else
/* fallback to standard I/O text stream */
#include <stdio.h>
#endif

enum {
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if !defined(_WIN32)
#define USE_MMAP 1
#if HAVE_MMAP
#include <sys/mman.h>
#include <unistd.h>
#endif
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/mpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
*/
#include <stdlib.h>
#include <string.h>
#if !defined(_WIN32)
#define USE_MMAP 1
#if HAVE_MMAP
#include <sys/mman.h>
#include <unistd.h>
#endif
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 90b42a6

Please sign in to comment.