summaryrefslogtreecommitdiff
path: root/src/sys-mmap.h
blob: bf8e815dcef3910c251adc911307d06193aec62a (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
#ifndef LI_SYS_MMAP_H
#define LI_SYS_MMAP_H
#include "first.h"

#if defined(HAVE_SYS_MMAN_H)

#include <sys/mman.h>

#elif defined(_WIN32)

#include <Windows.h>
#include <HandleAPI.h>
#include <io.h>
#include <MemoryAPI.h>

#ifndef MAP_FAILED
#define MAP_FAILED ((void *)-1)
#endif
#ifndef MAP_PRIVATE
#define MAP_PRIVATE 0
#endif
#ifndef MAP_SHARED
#define MAP_SHARED 0
#endif
#ifndef PROT_READ
#define PROT_READ PAGE_READONLY
#endif

#define HAVE_MMAP 1

#define munmap(addr, length) UnmapViewOfFile((LPCVOID)(addr))

static inline void *
mmap (void *addr, size_t length, int prot, int flags, int fd, off_t offset)
{
    /* XXX: this limited implementation maps args only to read-only mmap */
    if (prot != PAGE_READONLY) /*(for PAGE_READONLY and FILE_MAP_READ)*/
        return MAP_FAILED;
    UNUSED(flags);

    HANDLE mh = CreateFileMapping((HANDLE) _get_osfhandle(fd),
                                  NULL, PAGE_READONLY,
                                  (sizeof(size_t) > 4) ? length >> 32 : 0,
                                  length & 0xffffffff, NULL);
    if (NULL == mh)
        return MAP_FAILED;

    LPVOID p = MapViewOfFileEx(mh, FILE_MAP_READ,
                               (sizeof(off_t) > 4) ? offset >> 32 : 0,
                               offset & 0xffffffff, length, addr);
    CloseHandle(mh);
    return (NULL != p) ? (void *)p : MAP_FAILED;
}

#else

# define MAP_SHARED 0
# define MAP_PRIVATE 0
# define PROT_READ 0

# define mmap(a, b, c, d, e, f) (-1)
# define munmap(a, b) (-1)

#endif /* HAVE_SYS_MMAN_H */

/* NetBSD 1.3.x needs it; also make it available if mmap() is not present */
#if !defined(MAP_FAILED)
# define MAP_FAILED ((char*)-1)
#endif

#endif