0643405f38e58c754bf86d48a23d64b8da7b6768
[swift-upb.git] / compat.h
1 /*
2  *  compat.h
3  *  compatibility wrappers
4  *
5  *  Created by Arno Bakker, Victor Grishchenko
6  *  Copyright 2009 Delft University of Technology. All rights reserved.
7  *
8  */
9 #ifndef SWIFT_COMPAT_H
10 #define SWIFT_COMPAT_H
11
12 #ifdef _MSC_VER
13 typedef unsigned char uint8_t;
14 typedef signed char int8_t;
15 typedef unsigned short uint16_t;
16 typedef short int16_t;
17 typedef unsigned int uint32_t;
18 typedef int int32_t;
19 typedef __int64 int64_t;
20 typedef unsigned __int64 uint64_t;
21 #else
22 #include <stdint.h>
23 #endif
24 #ifdef _WIN32
25 #include <winsock2.h>
26 #include <sys/stat.h>
27 #include <io.h>
28 #else
29 #include <sys/mman.h>
30 #endif
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #ifdef _WIN32
36 #define open(a,b,c)    _open(a,b,c)
37 #endif
38 #ifndef S_IRUSR
39 #define S_IRUSR _S_IREAD
40 #endif
41 #ifndef S_IWUSR
42 #define S_IWUSR _S_IWRITE
43 #endif
44 #ifndef S_IRGRP
45 #define S_IRGRP _S_IREAD
46 #endif
47 #ifndef S_IROTH
48 #define S_IROTH _S_IREAD
49 #endif
50
51 namespace swift {
52
53 /** tint is the time integer type; microsecond-precise. */
54 typedef int64_t tint;
55 #define TINT_HOUR ((tint)1000000*60*60)
56 #define TINT_MIN ((tint)1000000*60)
57 #define TINT_SEC ((tint)1000000)
58 #define TINT_MSEC ((tint)1000)
59 #define TINT_uSEC ((tint)1)
60 #define TINT_NEVER ((tint)0x3fffffffffffffffLL)
61
62
63 size_t  file_size (int fd);
64
65 int     file_seek (int fd, size_t offset);
66
67 int     file_resize (int fd, size_t new_size);
68
69 void*   memory_map (int fd, size_t size=0);
70 void    memory_unmap (int fd, void*, size_t size);
71
72 void    print_error (const char* msg);
73
74 #ifdef _WIN32
75
76 /** UNIX pread approximation. Does change file pointer. Is not thread-safe */
77 size_t  pread(int fildes, void *buf, size_t nbyte, long offset);
78
79 /** UNIX pwrite approximation. Does change file pointer. Is not thread-safe */
80 size_t  pwrite(int fildes, const void *buf, size_t nbyte, long offset);
81
82 int     inet_aton(const char *cp, struct in_addr *inp);
83
84 #endif
85
86 tint    usec_time ();
87
88 };
89
90 #endif
91