summaryrefslogtreecommitdiffstats
path: root/src/fluent-bit/lib/wasm-micro-runtime-WAMR-1.2.2/core/shared/platform/windows/win_memmap.c
blob: c4a6b075629f7a4144860d5ff668e82027578d32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (C) 2019 Intel Corporation.  All rights reserved.
 * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 */

#include "platform_api_vmcore.h"

#define TRACE_MEMMAP 0

static DWORD
access_to_win32_flags(int prot)
{
    DWORD protect = PAGE_NOACCESS;

    if (prot & MMAP_PROT_EXEC) {
        if (prot & MMAP_PROT_WRITE)
            protect = PAGE_EXECUTE_READWRITE;
        else
            protect = PAGE_EXECUTE_READ;
    }
    else if (prot & MMAP_PROT_WRITE) {
        protect = PAGE_READWRITE;
    }
    else if (prot & MMAP_PROT_READ) {
        protect = PAGE_READONLY;
    }

    return protect;
}

void *
os_mmap(void *hint, size_t size, int prot, int flags)
{
    DWORD alloc_type = MEM_RESERVE;
    DWORD protect;
    size_t request_size, page_size;
    void *addr;

    page_size = os_getpagesize();
    request_size = (size + page_size - 1) & ~(page_size - 1);

    if (request_size < size)
        /* integer overflow */
        return NULL;

#if WASM_ENABLE_JIT != 0
    /**
     * Allocate memory at the highest possible address if the
     * request size is large, or LLVM JIT might report error:
     * IMAGE_REL_AMD64_ADDR32NB relocation requires an ordered
     * section layout.
     */
    if (request_size > 10 * BH_MB)
        alloc_type |= MEM_TOP_DOWN;
#endif

    protect = access_to_win32_flags(prot);
    if (protect != PAGE_NOACCESS) {
        alloc_type |= MEM_COMMIT;
    }

    addr = VirtualAlloc((LPVOID)hint, request_size, alloc_type, protect);

#if TRACE_MEMMAP != 0
    printf("Map memory, request_size: %zu, alloc_type: 0x%x, "
           "protect: 0x%x, ret: %p\n",
           request_size, alloc_type, protect, addr);
#endif
    return addr;
}

void
os_munmap(void *addr, size_t size)
{
    size_t page_size = os_getpagesize();
    size_t request_size = (size + page_size - 1) & ~(page_size - 1);

    if (addr) {
        if (!VirtualFree(addr, request_size, MEM_DECOMMIT)) {
            printf("warning: os_munmap decommit pages failed, "
                   "addr: %p, request_size: %zu, errno: %d\n",
                   addr, request_size, errno);
            return;
        }

        if (!VirtualFree(addr, 0, MEM_RELEASE)) {
            printf("warning: os_munmap release pages failed, "
                   "addr: %p, size: %zu, errno:%d\n",
                   addr, request_size, errno);
        }
    }
#if TRACE_MEMMAP != 0
    printf("Unmap memory, addr: %p, request_size: %zu\n", addr, request_size);
#endif
}

void *
os_mem_commit(void *addr, size_t size, int flags)
{
    DWORD protect = access_to_win32_flags(flags);
    size_t page_size = os_getpagesize();
    size_t request_size = (size + page_size - 1) & ~(page_size - 1);

    if (!addr)
        return NULL;

#if TRACE_MEMMAP != 0
    printf("Commit memory, addr: %p, request_size: %zu, protect: 0x%x\n", addr,
           request_size, protect);
#endif
    return VirtualAlloc((LPVOID)addr, request_size, MEM_COMMIT, protect);
}

void
os_mem_decommit(void *addr, size_t size)
{
    size_t page_size = os_getpagesize();
    size_t request_size = (size + page_size - 1) & ~(page_size - 1);

    if (!addr)
        return;

#if TRACE_MEMMAP != 0
    printf("Decommit memory, addr: %p, request_size: %zu\n", addr,
           request_size);
#endif
    VirtualFree((LPVOID)addr, request_size, MEM_DECOMMIT);
}

int
os_mprotect(void *addr, size_t size, int prot)
{
    DWORD protect;
    size_t page_size = os_getpagesize();
    size_t request_size = (size + page_size - 1) & ~(page_size - 1);

    if (!addr)
        return 0;

    protect = access_to_win32_flags(prot);
#if TRACE_MEMMAP != 0
    printf("Mprotect memory, addr: %p, request_size: %zu, protect: 0x%x\n",
           addr, request_size, protect);
#endif
    return VirtualProtect((LPVOID)addr, request_size, protect, NULL);
}