add .gitignore
[swift-upb.git] / compat.cpp
1 /*
2  *  compat.cpp
3  *  swift
4  *
5  *  Created by Arno Bakker, Victor Grishchenko
6  *  Copyright 2009 Delft University of Technology. All rights reserved.
7  *
8  */
9
10 #include "compat.h"
11 #include <sys/stat.h>
12 #include <stdio.h>
13 #include <assert.h>
14 #ifdef _WIN32
15 #include <Tchar.h>
16 #include <io.h>
17 #include <sys/timeb.h>
18 #include <vector>
19 #include <stdexcept>
20 #else
21 #include <unistd.h>
22 #include <sys/time.h>
23 #endif
24
25 namespace swift {
26
27 #ifdef _WIN32
28 static HANDLE map_handles[1024];
29 #endif
30
31 size_t file_size (int fd) {
32     struct stat st;
33     fstat(fd, &st);
34     return st.st_size;
35 }
36
37 int     file_seek (int fd, size_t offset) {
38 #ifndef _WIN32
39     return lseek(fd,offset,SEEK_SET);
40 #else
41     return _lseek(fd,offset,SEEK_SET);
42 #endif
43 }
44
45 int     file_resize (int fd, size_t new_size) {
46 #ifndef _WIN32
47     return ftruncate(fd, new_size);
48 #else
49     return _chsize(fd,new_size);
50 #endif
51 }
52
53 void print_error(const char* msg) {
54     perror(msg);
55 #ifdef _WIN32
56     int e = WSAGetLastError();
57     if (e)
58         fprintf(stderr,"network error #%u\n",e);
59 #endif
60 }
61
62 void*   memory_map (int fd, size_t size) {
63     if (!size)
64         size = file_size(fd);
65     void *mapping;
66 #ifndef _WIN32
67     mapping = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
68     if (mapping==MAP_FAILED)
69         return NULL;
70     return mapping;
71 #else
72     HANDLE fhandle = (HANDLE)_get_osfhandle(fd);
73     assert(fd<1024);
74     HANDLE maphandle = CreateFileMapping(     fhandle,
75                                        NULL,
76                                        PAGE_READWRITE,
77                                        0,
78                                        0,
79                                        NULL    );
80     if (maphandle == NULL)
81         return NULL;
82     map_handles[fd] = maphandle;
83
84     mapping = MapViewOfFile         (  maphandle,
85                                        FILE_MAP_WRITE,
86                                        0,
87                                        0,
88                                        0  );
89
90     return mapping;
91 #endif
92 }
93
94 void    memory_unmap (int fd, void* mapping, size_t size) {
95 #ifndef _WIN32
96     munmap(mapping,size);
97     close(fd);
98 #else
99     UnmapViewOfFile(mapping);
100     CloseHandle(map_handles[fd]);
101 #endif
102 }
103
104 #ifdef _WIN32
105
106 size_t pread(int fildes, void *buf, size_t nbyte, long offset)
107 {
108     _lseek(fildes,offset,SEEK_SET);
109     return read(fildes,buf,nbyte);
110 }
111
112 size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset)
113 {
114     _lseek(fildes,offset,SEEK_SET);
115     return write(fildes,buf,nbyte);
116 }
117
118
119 int inet_aton(const char *cp, struct in_addr *inp)
120 {
121     inp->S_un.S_addr = inet_addr(cp);
122     return 1;
123 }
124
125 #endif
126
127 #ifdef _WIN32
128
129 LARGE_INTEGER get_freq() {
130     LARGE_INTEGER proc_freq;
131     if (!::QueryPerformanceFrequency(&proc_freq))
132         print_error("HiResTimeOfDay: QueryPerformanceFrequency() failed");
133     return proc_freq;
134 }
135
136 tint usec_time(void)
137 {
138         static LARGE_INTEGER last_time;
139         LARGE_INTEGER cur_time;
140         QueryPerformanceCounter(&cur_time);
141         if (cur_time.QuadPart<last_time.QuadPart)
142                 print_error("QueryPerformanceCounter wrapped"); // does this happen?
143         last_time = cur_time;
144         static float freq = 1000000.0/get_freq().QuadPart;
145         tint usec = cur_time.QuadPart * freq;
146         return usec;
147 }
148
149
150 #else
151
152 tint usec_time(void)
153 {
154     struct timeval t;
155     gettimeofday(&t,NULL);
156     tint ret;
157     ret = t.tv_sec;
158     ret *= 1000000;
159     ret += t.tv_usec;
160     return ret;
161 }
162
163 #endif
164
165 void LibraryInit(void)
166 {
167 #ifdef _WIN32
168         static WSADATA _WSAData;
169         // win32 requires you to initialize the Winsock DLL with the desired
170         // specification version
171         WORD wVersionRequested;
172     wVersionRequested = MAKEWORD(2, 2);
173         WSAStartup(wVersionRequested, &_WSAData);
174 #endif
175 }
176
177
178 std::string gettmpdir(void)
179 {
180 #ifdef _WIN32
181   DWORD result = ::GetTempPath(0, _T(""));
182   if (result == 0)
183         throw std::runtime_error("Could not get system temp path");
184
185   std::vector<TCHAR> tempPath(result + 1);
186   result = ::GetTempPath(static_cast<DWORD>(tempPath.size()), &tempPath[0]);
187   if((result == 0) || (result >= tempPath.size()))
188         throw std::runtime_error("Could not get system temp path");
189
190   return std::string(tempPath.begin(), tempPath.begin() + static_cast<std::size_t>(result));
191 #else
192           return std::string("/tmp/");
193 #endif
194 }
195
196 bool    make_socket_nonblocking(SOCKET fd) {
197 #ifdef _WIN32
198     u_long enable = 1;
199     return 0==ioctlsocket(fd, FIONBIO, &enable);
200 #else
201     int enable=1;
202     return 0==fcntl(fd, F_SETFL, O_NONBLOCK);
203 #endif
204 }
205
206 bool    close_socket (SOCKET sock) {
207 #ifdef _WIN32
208     return 0==closesocket(sock);
209 #else
210     return 0==::close(sock);
211 #endif
212 }
213
214
215 }