negative cwnd is a nonsense
[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 "compat/hirestimeofday.h"
19 #else
20 #include <unistd.h>
21 #include <sys/time.h>
22 #endif
23
24 namespace swift {
25
26 #ifdef _WIN32
27 static HANDLE map_handles[1024];
28 #endif
29
30 size_t file_size (int fd) {
31     struct stat st;
32     fstat(fd, &st);
33     return st.st_size;
34 }
35
36 int     file_seek (int fd, size_t offset) {
37 #ifndef _WIN32
38     return lseek(fd,offset,SEEK_SET);
39 #else
40     return _lseek(fd,offset,SEEK_SET);
41 #endif
42 }
43
44 int     file_resize (int fd, size_t new_size) {
45 #ifndef _WIN32
46     return ftruncate(fd, new_size);
47 #else
48     return _chsize(fd,new_size);
49 #endif
50 }
51
52 void print_error(const char* msg) {
53     perror(msg);
54 #ifdef _WIN32
55     int e = WSAGetLastError();
56     if (e)
57         fprintf(stderr,"network error #%u\n",e);
58 #endif
59 }
60
61 void*   memory_map (int fd, size_t size) {
62     if (!size)
63         size = file_size(fd);
64     void *mapping;
65 #ifndef _WIN32
66     mapping = mmap (NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
67     if (mapping==MAP_FAILED)
68         return NULL;
69     return mapping;
70 #else
71     HANDLE fhandle = (HANDLE)_get_osfhandle(fd);
72     assert(fd<1024);
73     HANDLE maphandle = CreateFileMapping(     fhandle,
74                                        NULL,
75                                        PAGE_READWRITE,
76                                        0,
77                                        0,
78                                        NULL    );
79     if (maphandle == NULL)
80         return NULL;
81     map_handles[fd] = maphandle;
82
83     mapping = MapViewOfFile         (  maphandle,
84                                        FILE_MAP_WRITE,
85                                        0,
86                                        0,
87                                        0  );
88
89     return mapping;
90 #endif
91 }
92
93 void    memory_unmap (int fd, void* mapping, size_t size) {
94 #ifndef _WIN32
95     munmap(mapping,size);
96     close(fd);
97 #else
98     UnmapViewOfFile(mapping);
99     CloseHandle(map_handles[fd]);
100 #endif
101 }
102
103 #ifdef _WIN32
104
105 size_t pread(int fildes, void *buf, size_t nbyte, long offset)
106 {
107     _lseek(fildes,offset,SEEK_SET);
108     return read(fildes,buf,nbyte);
109 }
110
111 size_t pwrite(int fildes, const void *buf, size_t nbyte, long offset)
112 {
113     _lseek(fildes,offset,SEEK_SET);
114     return write(fildes,buf,nbyte);
115 }
116
117
118 int inet_aton(const char *cp, struct in_addr *inp)
119 {
120     inp->S_un.S_addr = inet_addr(cp);
121     return 1;
122 }
123
124 tint usec_time(void)
125 {
126     HiResTimeOfDay* tod = HiResTimeOfDay::Instance();
127     return tod->getTimeUSec();
128 }
129
130
131 #else
132
133 tint usec_time(void)
134 {
135     struct timeval t;
136     gettimeofday(&t,NULL);
137     tint ret;
138     ret = t.tv_sec;
139     ret *= 1000000;
140     ret += t.tv_usec;
141     return ret;
142 }
143
144 #endif
145
146 }