Skip to content
This repository was archived by the owner on Aug 16, 2023. It is now read-only.

Commit 50fce45

Browse files
committed
fix compile error under mingw/windows.
Signed-off-by: Ji Bin <[email protected]>
1 parent bb5fc9b commit 50fce45

File tree

3 files changed

+246
-0
lines changed

3 files changed

+246
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ include(cmake/libs/libeasylogging.cmake)
6464
include_directories(thirdparty/faiss)
6565
include_directories(thirdparty/bitset)
6666
include_directories(thirdparty)
67+
if(WIN32)
68+
include_directories(thirdparty/mman-win32)
69+
endif()
6770

6871
find_package(OpenMP REQUIRED)
6972

src/simd/distances_sse.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <immintrin.h>
1616

1717
#include <cassert>
18+
#include <cstdint>
1819

1920
#include "distances_ref.h"
2021

thirdparty/mman-win32/sys/mman.h

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
2+
// This is from https://code.google.com/p/mman-win32/
3+
//
4+
// Licensed under MIT
5+
6+
#ifndef _MMAN_WIN32_H
7+
#define _MMAN_WIN32_H
8+
9+
#ifndef _WIN32_WINNT // Allow use of features specific to Windows XP or later.
10+
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
11+
#endif
12+
13+
#include <sys/types.h>
14+
#include <windows.h>
15+
#include <errno.h>
16+
#include <io.h>
17+
18+
#define PROT_NONE 0
19+
#define PROT_READ 1
20+
#define PROT_WRITE 2
21+
#define PROT_EXEC 4
22+
23+
#define MAP_FILE 0
24+
#define MAP_SHARED 1
25+
#define MAP_PRIVATE 2
26+
#define MAP_TYPE 0xf
27+
#define MAP_FIXED 0x10
28+
#define MAP_ANONYMOUS 0x20
29+
#define MAP_ANON MAP_ANONYMOUS
30+
31+
#define MAP_FAILED ((void *)-1)
32+
33+
/* Flags for msync. */
34+
#define MS_ASYNC 1
35+
#define MS_SYNC 2
36+
#define MS_INVALIDATE 4
37+
38+
#ifndef FILE_MAP_EXECUTE
39+
#define FILE_MAP_EXECUTE 0x0020
40+
#endif
41+
42+
static int __map_mman_error(const DWORD err, const int deferr)
43+
{
44+
if (err == 0)
45+
return 0;
46+
//TODO: implement
47+
return err;
48+
}
49+
50+
static DWORD __map_mmap_prot_page(const int prot)
51+
{
52+
DWORD protect = 0;
53+
54+
if (prot == PROT_NONE)
55+
return protect;
56+
57+
if ((prot & PROT_EXEC) != 0)
58+
{
59+
protect = ((prot & PROT_WRITE) != 0) ?
60+
PAGE_EXECUTE_READWRITE : PAGE_EXECUTE_READ;
61+
}
62+
else
63+
{
64+
protect = ((prot & PROT_WRITE) != 0) ?
65+
PAGE_READWRITE : PAGE_READONLY;
66+
}
67+
68+
return protect;
69+
}
70+
71+
static DWORD __map_mmap_prot_file(const int prot)
72+
{
73+
DWORD desiredAccess = 0;
74+
75+
if (prot == PROT_NONE)
76+
return desiredAccess;
77+
78+
if ((prot & PROT_READ) != 0)
79+
desiredAccess |= FILE_MAP_READ;
80+
if ((prot & PROT_WRITE) != 0)
81+
desiredAccess |= FILE_MAP_WRITE;
82+
if ((prot & PROT_EXEC) != 0)
83+
desiredAccess |= FILE_MAP_EXECUTE;
84+
85+
return desiredAccess;
86+
}
87+
88+
inline void* mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off)
89+
{
90+
HANDLE fm, h;
91+
92+
void * map = MAP_FAILED;
93+
94+
#ifdef _MSC_VER
95+
#pragma warning(push)
96+
#pragma warning(disable: 4293)
97+
#endif
98+
99+
const DWORD dwFileOffsetLow = (sizeof(off_t) <= sizeof(DWORD)) ?
100+
(DWORD)off : (DWORD)(off & 0xFFFFFFFFL);
101+
const DWORD dwFileOffsetHigh = (sizeof(off_t) <= sizeof(DWORD)) ?
102+
(DWORD)0 : (DWORD)((off >> 32) & 0xFFFFFFFFL);
103+
const DWORD protect = __map_mmap_prot_page(prot);
104+
const DWORD desiredAccess = __map_mmap_prot_file(prot);
105+
106+
const off_t maxSize = off + (off_t)len;
107+
108+
const DWORD dwMaxSizeLow = (sizeof(off_t) <= sizeof(DWORD)) ?
109+
(DWORD)maxSize : (DWORD)(maxSize & 0xFFFFFFFFL);
110+
const DWORD dwMaxSizeHigh = (sizeof(off_t) <= sizeof(DWORD)) ?
111+
(DWORD)0 : (DWORD)((maxSize >> 32) & 0xFFFFFFFFL);
112+
113+
#ifdef _MSC_VER
114+
#pragma warning(pop)
115+
#endif
116+
117+
errno = 0;
118+
119+
if (len == 0
120+
/* Unsupported flag combinations */
121+
|| (flags & MAP_FIXED) != 0
122+
/* Usupported protection combinations */
123+
|| prot == PROT_EXEC)
124+
{
125+
errno = EINVAL;
126+
return MAP_FAILED;
127+
}
128+
129+
h = ((flags & MAP_ANONYMOUS) == 0) ?
130+
(HANDLE)_get_osfhandle(fildes) : INVALID_HANDLE_VALUE;
131+
132+
if ((flags & MAP_ANONYMOUS) == 0 && h == INVALID_HANDLE_VALUE)
133+
{
134+
errno = EBADF;
135+
return MAP_FAILED;
136+
}
137+
138+
fm = CreateFileMapping(h, NULL, protect, dwMaxSizeHigh, dwMaxSizeLow, NULL);
139+
140+
if (fm == NULL)
141+
{
142+
errno = __map_mman_error(GetLastError(), EPERM);
143+
return MAP_FAILED;
144+
}
145+
146+
map = MapViewOfFile(fm, desiredAccess, dwFileOffsetHigh, dwFileOffsetLow, len);
147+
148+
CloseHandle(fm);
149+
150+
if (map == NULL)
151+
{
152+
errno = __map_mman_error(GetLastError(), EPERM);
153+
return MAP_FAILED;
154+
}
155+
156+
return map;
157+
}
158+
159+
inline int munmap(void *addr, size_t len)
160+
{
161+
if (UnmapViewOfFile(addr))
162+
return 0;
163+
164+
errno = __map_mman_error(GetLastError(), EPERM);
165+
166+
return -1;
167+
}
168+
169+
inline int mprotect(void *addr, size_t len, int prot)
170+
{
171+
DWORD newProtect = __map_mmap_prot_page(prot);
172+
DWORD oldProtect = 0;
173+
174+
if (VirtualProtect(addr, len, newProtect, &oldProtect))
175+
return 0;
176+
177+
errno = __map_mman_error(GetLastError(), EPERM);
178+
179+
return -1;
180+
}
181+
182+
inline int msync(void *addr, size_t len, int flags)
183+
{
184+
if (FlushViewOfFile(addr, len))
185+
return 0;
186+
187+
errno = __map_mman_error(GetLastError(), EPERM);
188+
189+
return -1;
190+
}
191+
192+
inline int mlock(const void *addr, size_t len)
193+
{
194+
if (VirtualLock((LPVOID)addr, len))
195+
return 0;
196+
197+
errno = __map_mman_error(GetLastError(), EPERM);
198+
199+
return -1;
200+
}
201+
202+
inline int munlock(const void *addr, size_t len)
203+
{
204+
if (VirtualUnlock((LPVOID)addr, len))
205+
return 0;
206+
207+
errno = __map_mman_error(GetLastError(), EPERM);
208+
209+
return -1;
210+
}
211+
212+
#if !defined(__MINGW32__)
213+
inline int ftruncate(const int fd, const int64_t size) {
214+
if (fd < 0) {
215+
errno = EBADF;
216+
return -1;
217+
}
218+
219+
HANDLE h = reinterpret_cast<HANDLE>(_get_osfhandle(fd));
220+
LARGE_INTEGER li_start, li_size;
221+
li_start.QuadPart = static_cast<int64_t>(0);
222+
li_size.QuadPart = size;
223+
if (SetFilePointerEx(h, li_start, NULL, FILE_CURRENT) == ~0 ||
224+
SetFilePointerEx(h, li_size, NULL, FILE_BEGIN) == ~0 ||
225+
!SetEndOfFile(h)) {
226+
unsigned long error = GetLastError();
227+
fprintf(stderr, "I/O error while truncating: %lu\n", error);
228+
switch (error) {
229+
case ERROR_INVALID_HANDLE:
230+
errno = EBADF;
231+
break;
232+
default:
233+
errno = EIO;
234+
break;
235+
}
236+
return -1;
237+
}
238+
return 0;
239+
}
240+
#endif
241+
242+
#endif

0 commit comments

Comments
 (0)