-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcompat.cpp
More file actions
215 lines (182 loc) · 4.46 KB
/
compat.cpp
File metadata and controls
215 lines (182 loc) · 4.46 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* compat.cpp
* swift
*
* Created by Arno Bakker, Victor Grishchenko
* Copyright 2009 Delft University of Technology. All rights reserved.
*
*/
#include "compat.h"
#include <sys/stat.h>
#include <stdio.h>
#include <assert.h>
#ifdef _WIN32
#include <Tchar.h>
#include <io.h>
#include <sys/timeb.h>
#include <vector>
#include <stdexcept>
#else
#include <unistd.h>
#include <sys/time.h>
#endif
namespace swift {
#ifdef _WIN32
static HANDLE map_handles[1024];
#endif
size_t file_size (int fd) {
struct stat st;
fstat(fd, &st);
return st.st_size;
}
int file_seek (int fd, size_t offset) {
#ifndef _WIN32
return lseek(fd,offset,SEEK_SET);
#else
return _lseek(fd,offset,SEEK_SET);
#endif
}
int file_resize (int fd, size_t new_size) {
#ifndef _WIN32
return ftruncate(fd, new_size);
#else
return _chsize(fd,new_size);
#endif
}
void print_error(const char* msg) {
perror(msg);
#ifdef _WIN32
int e = WSAGetLastError();
if (e)
fprintf(stderr,"network error #%u\n",e);
#endif
}
void* memory_map (int fd, size_t size) {
if (!size)
size = file_size(fd);
void *mapping;
#ifndef _WIN32
mapping = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (mapping==MAP_FAILED)
return NULL;
return mapping;
#else
HANDLE fhandle = (HANDLE)_get_osfhandle(fd);
assert(fd<1024);
HANDLE maphandle = CreateFileMapping( fhandle,
NULL,
PAGE_READWRITE,
0,
0,
NULL );
if (maphandle == NULL)
return NULL;
map_handles[fd] = maphandle;
mapping = MapViewOfFile ( maphandle,
FILE_MAP_WRITE,
0,
0,
0 );
return mapping;
#endif
}
void memory_unmap (int fd, void* mapping, size_t size) {
#ifndef _WIN32
munmap(mapping,size);
close(fd);
#else
UnmapViewOfFile(mapping);
CloseHandle(map_handles[fd]);
#endif
}
#ifdef _WIN32
size_t pread(int fildes, void *buf, size_t nbyte, long offset)
{
_lseek(fildes,offset,SEEK_SET);
return read(fildes,buf,nbyte);
}
size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset)
{
_lseek(fildes,offset,SEEK_SET);
return write(fildes,buf,nbyte);
}
int inet_aton(const char *cp, struct in_addr *inp)
{
inp->S_un.S_addr = inet_addr(cp);
return 1;
}
#endif
#ifdef _WIN32
LARGE_INTEGER get_freq() {
LARGE_INTEGER proc_freq;
if (!::QueryPerformanceFrequency(&proc_freq))
print_error("HiResTimeOfDay: QueryPerformanceFrequency() failed");
return proc_freq;
}
tint usec_time(void)
{
static LARGE_INTEGER last_time;
LARGE_INTEGER cur_time;
QueryPerformanceCounter(&cur_time);
if (cur_time.QuadPart<last_time.QuadPart)
print_error("QueryPerformanceCounter wrapped"); // does this happen?
last_time = cur_time;
static float freq = 1000000.0/get_freq().QuadPart;
tint usec = cur_time.QuadPart * freq;
return usec;
}
#else
tint usec_time(void)
{
struct timeval t;
gettimeofday(&t,NULL);
tint ret;
ret = t.tv_sec;
ret *= 1000000;
ret += t.tv_usec;
return ret;
}
#endif
void LibraryInit(void)
{
#ifdef _WIN32
static WSADATA _WSAData;
// win32 requires you to initialize the Winsock DLL with the desired
// specification version
WORD wVersionRequested;
wVersionRequested = MAKEWORD(2, 2);
WSAStartup(wVersionRequested, &_WSAData);
#endif
}
std::string gettmpdir(void)
{
#ifdef _WIN32
DWORD result = ::GetTempPath(0, _T(""));
if (result == 0)
throw std::runtime_error("Could not get system temp path");
std::vector<TCHAR> tempPath(result + 1);
result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
if((result == 0) || (result >= tempPath.size()))
throw std::runtime_error("Could not get system temp path");
return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));
#else
return std::string("/tmp/");
#endif
}
bool make_socket_nonblocking(SOCKET fd) {
#ifdef _WIN32
u_long enable = 1;
return 0==ioctlsocket(fd, FIONBIO, &enable);
#else
int enable=1;
return 0==fcntl(fd, F_SETFL, O_NONBLOCK);
#endif
}
bool close_socket (SOCKET sock) {
#ifdef _WIN32
return 0==closesocket(sock);
#else
return 0==::close(sock);
#endif
}
}