5 * Created by Arno Bakker, Victor Grishchenko
6 * Copyright 2009-2012 TECHNISCHE UNIVERSITEIT DELFT. All rights reserved.
17 #include <sys/timeb.h>
28 static HANDLE map_handles[1024];
31 int64_t file_size (int fd) {
43 int file_seek (int fd, int64_t offset) {
45 return lseek(fd,offset,SEEK_SET);
47 return _lseeki64(fd,offset,SEEK_SET);
51 int file_resize (int fd, int64_t new_size) {
53 return ftruncate(fd, new_size);
55 // Arno, 2011-10-27: Use 64-bit version
56 if (_chsize_s(fd,new_size) != 0)
63 void print_error(const char* msg) {
66 int e = WSAGetLastError();
68 fprintf(stderr,"windows error #%u\n",e);
72 void* memory_map (int fd, size_t size) {
77 mapping = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
78 if (mapping==MAP_FAILED)
82 HANDLE fhandle = (HANDLE)_get_osfhandle(fd);
84 HANDLE maphandle = CreateFileMapping( fhandle,
90 if (maphandle == NULL)
92 map_handles[fd] = maphandle;
94 mapping = MapViewOfFile ( maphandle,
104 void memory_unmap (int fd, void* mapping, size_t size) {
106 munmap(mapping,size);
109 UnmapViewOfFile(mapping);
110 CloseHandle(map_handles[fd]);
116 size_t pread(int fildes, void *buf, size_t nbyte, __int64 offset)
118 _lseeki64(fildes,offset,SEEK_SET);
119 return read(fildes,buf,nbyte);
122 size_t pwrite(int fildes, const void *buf, size_t nbyte, __int64 offset)
124 _lseeki64(fildes,offset,SEEK_SET);
125 return write(fildes,buf,nbyte);
129 int inet_aton(const char *cp, struct in_addr *inp)
131 inp->S_un.S_addr = inet_addr(cp);
139 LARGE_INTEGER get_freq() {
140 LARGE_INTEGER proc_freq;
141 if (!::QueryPerformanceFrequency(&proc_freq))
142 print_error("HiResTimeOfDay: QueryPerformanceFrequency() failed");
148 static LARGE_INTEGER last_time;
149 LARGE_INTEGER cur_time;
150 QueryPerformanceCounter(&cur_time);
151 if (cur_time.QuadPart<last_time.QuadPart)
152 print_error("QueryPerformanceCounter wrapped"); // does this happen?
153 last_time = cur_time;
154 static float freq = 1000000.0/get_freq().QuadPart;
155 tint usec = cur_time.QuadPart * freq;
165 gettimeofday(&t,NULL);
175 void LibraryInit(void)
178 static WSADATA _WSAData;
179 // win32 requires you to initialize the Winsock DLL with the desired
180 // specification version
181 WORD wVersionRequested;
182 wVersionRequested = MAKEWORD(2, 2);
183 WSAStartup(wVersionRequested, &_WSAData);
188 std::string gettmpdir(void)
191 DWORD result = ::GetTempPath(0, _T(""));
193 throw std::runtime_error("Could not get system temp path");
195 std::vector<TCHAR> tempPath(result + 1);
196 result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
197 if((result == 0) || (result >= tempPath.size()))
198 throw std::runtime_error("Could not get system temp path");
200 return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));
202 return std::string("/tmp/");
206 bool make_socket_nonblocking(evutil_socket_t fd) {
209 return 0==ioctlsocket(fd, FIONBIO, &enable);
211 return 0==fcntl(fd, F_SETFL, O_NONBLOCK);
215 bool close_socket (evutil_socket_t sock) {
217 return 0==closesocket(sock);
219 return 0==::close(sock);
224 // Arno: not thread safe!
225 struct timeval* tint2tv (tint t) {
226 static struct timeval tv;
227 tv.tv_usec = t%TINT_SEC;
228 tv.tv_sec = t/TINT_SEC;