1 #ifndef TYPES_H
2 #define TYPES_H
3
4 #include <sys/types.h>
5 #include <stdbool.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdbool.h>
9 #include <stdio.h>
10
11 #undef eprintf
12 #define eprintf(...) fprintf(stderr,__VA_ARGS__)
13
14 // Copied from https://gcc.gnu.org/wiki/Visibility
15 #ifndef SDB_API
16 #undef SDB_IPI
17 #if defined _WIN32 || defined __CYGWIN__
18 #ifdef __GNUC__
19 #define SDB_API __attribute__ ((dllexport))
20 #else
21 #define SDB_API __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
22 #endif
23 #define SDB_IPI
24 #else
25 #if __GNUC__ >= 4
26 #define SDB_API __attribute__ ((visibility ("default")))
27 #define SDB_IPI __attribute__ ((visibility ("hidden")))
28 #else
29 #define SDB_API
30 #define SDB_IPI
31 #endif
32 #endif
33 #endif
34
35 #if MINGW || __MINGW32__ || __MINGW64__
36 #define __MINGW__ 1
37 #endif
38
39 #if __WIN32__ || __MINGW__ || __WINDOWS__ || _MSC_VER
40 #define __SDB_WINDOWS__ 1
41 #define DIRSEP '\\'
42 #define lseek _lseek
43 #include <windows.h>
44 #include <io.h>
45 #else
46 // CYGWIN AND UNIX
47 #define __SDB_WINDOWS__ 0
48 #define DIRSEP '/'
49 #include <unistd.h>
50 #endif
51
52 #include <inttypes.h>
53 #if __SDB_WINDOWS__ && !__CYGWIN__
54 #define HAVE_MMAN 0
55 #define ULLFMT "I64"
56 #else
57 #define HAVE_MMAN 1
58 #define ULLFMT "ll"
59 #endif
60
61 #ifndef USE_MMAN
62 #define USE_MMAN HAVE_MMAN
63 #endif
64
65 #ifndef UNUSED
66 # define UNUSED
67 # ifdef __GNUC__
68 # if __GNUC__ >= 4
69 # undef UNUSED
70 # define UNUSED __attribute__((__unused__))
71 # endif
72 # endif
73 #endif
74
75 #ifndef ut8
76 #define ut8 unsigned char
77 #define ut32 unsigned int
78 #define ut64 unsigned long long
79 #define st64 long long
80 #define boolt int
81 // TODO: deprecate R_NEW
82 #ifndef R_NEW
83 //it means we are within sdb
84 #define R_NEW(x) (x*)malloc(sizeof(x))
85 #endif
86 #ifndef R_NEW0
87 #define R_NEW0(x) (x*)calloc(1, sizeof(x))
88 #endif
89 #ifndef R_FREE
90 #define R_FREE(x) { free (x); x = NULL; }
91 #endif
92 #define UT32_MAX ((ut32)0xffffffff)
93 #define UT64_MAX ((ut64)(0xffffffffffffffffLL))
94 #endif
95 #ifndef R_MAX_DEFINED
96 #define R_MAX(x,y) (((x)>(y))?(x):(y))
97 #define R_MAX_DEFINED 1
98 #endif
99
100 #ifndef R_MIN_DEFINED
101 #define R_MIN(x,y) (((x)>(y))?(y):(x))
102 #define R_MIN_DEFINED 1
103 #endif
104
105 #include "config.h"
106
seek_set(int fd,off_t pos)107 static inline int seek_set(int fd, off_t pos) {
108 return ((fd == -1) || (lseek (fd, (off_t) pos, SEEK_SET) == -1))? 0:1;
109 }
110
ut32_pack(char s[4],ut32 u)111 static inline void ut32_pack(char s[4], ut32 u) {
112 s[0] = u & 255;
113 u >>= 8;
114 s[1] = u & 255;
115 u >>= 8;
116 s[2] = u & 255;
117 s[3] = u >> 8;
118 }
119
ut32_pack_big(char s[4],ut32 u)120 static inline void ut32_pack_big(char s[4], ut32 u) {
121 s[3] = u & 255;
122 u >>= 8;
123 s[2] = u & 255;
124 u >>= 8;
125 s[1] = u & 255;
126 s[0] = u >> 8;
127 }
128
ut32_unpack(char s[4],ut32 * u)129 static inline void ut32_unpack(char s[4], ut32 *u) {
130 ut32 result = 0;
131 result = (ut8) s[3];
132 result <<= 8;
133 result += (ut8) s[2];
134 result <<= 8;
135 result += (ut8) s[1];
136 result <<= 8;
137 result += (ut8) s[0];
138 *u = result;
139 }
140
141 #endif
142