1 | /** @file
|
---|
2 | *
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #ifndef __RUNTIME__H
|
---|
18 | #define __RUNTIME__H
|
---|
19 |
|
---|
20 | #include <endian.h>
|
---|
21 | #include <byteswap.h>
|
---|
22 | #include <stdint.h>
|
---|
23 | #include <ctype.h>
|
---|
24 | #include <errno.h>
|
---|
25 |
|
---|
26 | #ifndef cpu_to_le16
|
---|
27 | #if __BYTE_ORDER == __LITTLE_ENDIAN
|
---|
28 | #define cpu_to_le16(_le16) (_le16)
|
---|
29 | #elif __BYTE_ORDER == __BIG_ENDIAN
|
---|
30 | #define cpu_to_le16(_le16) bswap_16(_le16)
|
---|
31 | #else
|
---|
32 | #error Unsupported host byte order!
|
---|
33 | #endif
|
---|
34 | #endif /* cpu_to_le16 */
|
---|
35 |
|
---|
36 | //#include <asm/byteorder.h>
|
---|
37 | //#define cpu_to_le16 __cpu_to_le16
|
---|
38 |
|
---|
39 | #if 0
|
---|
40 | #define uint8_t uint8
|
---|
41 | #define uint16_t uint16
|
---|
42 | #define uint32_t uint32
|
---|
43 | #define uint64_t long long
|
---|
44 | #endif
|
---|
45 | #define bool int
|
---|
46 | #define false 0
|
---|
47 | #define true 1
|
---|
48 |
|
---|
49 | #define OPSTATIC
|
---|
50 | // #define RDPUSB_DEBUG
|
---|
51 | #ifdef RDPUSB_DEBUG
|
---|
52 | #define DoLog(a) do { \
|
---|
53 | printf("Time %llu: ", (long long unsigned) RTTimeMilliTS()); \
|
---|
54 | printf a; \
|
---|
55 | } while(0)
|
---|
56 |
|
---|
57 | #define LogFlow(a) DoLog(a)
|
---|
58 | #define Log(a) DoLog(a)
|
---|
59 | #define Log2(a) DoLog(a)
|
---|
60 | #else
|
---|
61 | #define LogFlow(a) do {} while (0)
|
---|
62 | #define Log(a) do {} while (0)
|
---|
63 | #define Log2(a) do {} while (0)
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #define LogRel(a) printf a
|
---|
67 |
|
---|
68 | /* Runtime wrappers. */
|
---|
69 | #define RTMemAlloc xmalloc
|
---|
70 | #define RTMemRealloc xrealloc
|
---|
71 | #define RTMemFree xfree
|
---|
72 |
|
---|
73 | #define _1K 1024
|
---|
74 |
|
---|
75 | #ifdef RT_BIG_ENDIAN
|
---|
76 | # define RT_H2LE_U16(u16) RT_BSWAP_U16(u16)
|
---|
77 | # define RT_LE2H_U16(u16) RT_BSWAP_U16(u16)
|
---|
78 | #else
|
---|
79 | # define RT_H2LE_U16(u16) (u16)
|
---|
80 | # define RT_LE2H_U16(u16) (u16)
|
---|
81 | #endif
|
---|
82 | #define RT_BSWAP_U16(u16) RT_MAKE_U16(RT_HIBYTE(u16), RT_LOBYTE(u16))
|
---|
83 | #define RT_MAKE_U16(Lo, Hi) \
|
---|
84 | ((uint16_t)( (uint16_t)((uint8_t)(Hi)) << 8 \
|
---|
85 | | (uint8_t)(Lo) ))
|
---|
86 | #define RT_LOBYTE(a) ( (a) & 0xff )
|
---|
87 | #define RT_HIBYTE(a) ( (a) >> 8 )
|
---|
88 |
|
---|
89 | #define VINF_SUCCESS 0
|
---|
90 | #define VERR_NO_MEMORY (-8)
|
---|
91 | #define VERR_INVALID_POINTER (-6)
|
---|
92 | #define VERR_UNRESOLVED_ERROR (-35)
|
---|
93 | #define VERR_NOT_SUPPORTED (-37)
|
---|
94 | #define VERR_ACCESS_DENIED (-38)
|
---|
95 | #define VERR_VUSB_USBFS_PERMISSION (-2005)
|
---|
96 |
|
---|
97 | static inline const char *RTErrGetShort(int iErr)
|
---|
98 | {
|
---|
99 | return iErr == VINF_SUCCESS ? "VINF_SUCCESS"
|
---|
100 | : iErr == VERR_NO_MEMORY ? "VERR_NO_MEMORY"
|
---|
101 | : iErr == VERR_INVALID_POINTER ? "VERR_INVALID_POINTER"
|
---|
102 | : iErr == VERR_UNRESOLVED_ERROR ? "VERR_UNRESOLVED_ERROR"
|
---|
103 | : iErr == VERR_NOT_SUPPORTED ? "VERR_NOT_SUPPORTED"
|
---|
104 | : iErr == VERR_ACCESS_DENIED ? "VERR_ACCESS_DENIED"
|
---|
105 | : iErr == VERR_VUSB_USBFS_PERMISSION ? "VERR_VUSB_USBFS_PERMISSION"
|
---|
106 | : "Unknown error";
|
---|
107 | }
|
---|
108 |
|
---|
109 | static inline int RTErrConvertFromErrno(int iErrno)
|
---|
110 | {
|
---|
111 | return iErrno == 0 ? VINF_SUCCESS
|
---|
112 | : iErrno == ENOMEM ? VERR_NO_MEMORY
|
---|
113 | : iErrno == ENODEV ? VERR_NOT_SUPPORTED
|
---|
114 | : iErrno == ENOSYS ? VERR_NOT_SUPPORTED
|
---|
115 | : iErrno == EPERM ? VERR_ACCESS_DENIED
|
---|
116 | : iErrno == EACCES ? VERR_ACCESS_DENIED
|
---|
117 | : VERR_UNRESOLVED_ERROR;
|
---|
118 | }
|
---|
119 |
|
---|
120 | #define RT_SUCCESS(_rc) ((_rc) >= 0)
|
---|
121 |
|
---|
122 | #define RTFILE int
|
---|
123 | #define RTCRITSECT void *
|
---|
124 |
|
---|
125 | #define Assert(_expr) do { \
|
---|
126 | if (!(_expr)) \
|
---|
127 | { \
|
---|
128 | Log(("Assertion failed: {%s}!!!\n", #_expr)); \
|
---|
129 | } \
|
---|
130 | } while (0)
|
---|
131 |
|
---|
132 | #define AssertMsgFailed(_msg) do { \
|
---|
133 | Log(("Assertion failed msg:!!!\n")); \
|
---|
134 | Log(_msg); \
|
---|
135 | } while (0)
|
---|
136 |
|
---|
137 | #define AssertReturn(_expr, _retval) do { \
|
---|
138 | if (!(_expr)) \
|
---|
139 | { \
|
---|
140 | Log(("Assertion failed: {%s}, returning 0x%08X!!!\n", #_expr, _retval)); \
|
---|
141 | return (_retval); \
|
---|
142 | } \
|
---|
143 | } while (0)
|
---|
144 |
|
---|
145 | #define AssertRC(_rc) Assert(RT_SUCCESS(_rc))
|
---|
146 |
|
---|
147 | #define RT_FAILURE(_rc) (!RT_SUCCESS(_rc))
|
---|
148 |
|
---|
149 | #define NOREF(_a) ((void)_a)
|
---|
150 |
|
---|
151 | #define VALID_PTR(ptr) ((ptr) != NULL)
|
---|
152 |
|
---|
153 | #define RT_C_DECLS_BEGIN
|
---|
154 | #define RT_C_DECLS_END
|
---|
155 |
|
---|
156 | #define RTCALL
|
---|
157 | #define DECLR3CALLBACKMEMBER(type, name, args) type (RTCALL * name) args
|
---|
158 | #define DECLINLINE(type) static __inline__ type
|
---|
159 | #define DECLCALLBACK(type) type RTCALL
|
---|
160 | #define DECLCALLBACKMEMBER(type, name) type (RTCALL * name)
|
---|
161 | #define RTDECL(type) RTCALL type
|
---|
162 |
|
---|
163 | #define RT_BIT(bit) ( 1U << (bit) )
|
---|
164 | #define RT_MAX(Value1, Value2) ( (Value1) >= (Value2) ? (Value1) : (Value2) )
|
---|
165 | #define RT_MIN(Value1, Value2) ( (Value1) <= (Value2) ? (Value1) : (Value2) )
|
---|
166 | typedef uint32_t RTGCPHYS32;
|
---|
167 | typedef uint32_t RTMSINTERVAL;
|
---|
168 |
|
---|
169 | static inline uint64_t RTTimeMilliTS (void)
|
---|
170 | {
|
---|
171 | struct timeval tv;
|
---|
172 | gettimeofday (&tv, NULL);
|
---|
173 | return (uint64_t)tv.tv_sec * (uint64_t)(1000)
|
---|
174 | + (uint64_t)(tv.tv_usec / 1000);
|
---|
175 | }
|
---|
176 |
|
---|
177 | static inline int RTCritSectInit (RTCRITSECT *pCritSect)
|
---|
178 | {
|
---|
179 | return VINF_SUCCESS;
|
---|
180 | }
|
---|
181 |
|
---|
182 | static inline int RTCritSectDelete (RTCRITSECT *pCritSect)
|
---|
183 | {
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | }
|
---|
186 |
|
---|
187 | static inline int RTCritSectEnter (RTCRITSECT *pCritSect)
|
---|
188 | {
|
---|
189 | return VINF_SUCCESS;
|
---|
190 | }
|
---|
191 |
|
---|
192 | static inline int RTCritSectLeave (RTCRITSECT *pCritSect)
|
---|
193 | {
|
---|
194 | return VINF_SUCCESS;
|
---|
195 | }
|
---|
196 |
|
---|
197 | static inline void *RTMemDupEx (const void *pvSrc, size_t cbSrc, size_t cbExtra)
|
---|
198 | {
|
---|
199 | void *p = RTMemAlloc (cbSrc + cbExtra);
|
---|
200 |
|
---|
201 | if (p)
|
---|
202 | {
|
---|
203 | memcpy (p, pvSrc, cbSrc);
|
---|
204 | memset ((char *)p + cbSrc, 0, cbExtra);
|
---|
205 | }
|
---|
206 |
|
---|
207 | return p;
|
---|
208 | }
|
---|
209 |
|
---|
210 | static inline void *RTMemAllocZ (size_t cb)
|
---|
211 | {
|
---|
212 | void *p = RTMemAlloc (cb);
|
---|
213 |
|
---|
214 | if (p)
|
---|
215 | {
|
---|
216 | memset (p, 0, cb);
|
---|
217 | }
|
---|
218 |
|
---|
219 | return p;
|
---|
220 | }
|
---|
221 |
|
---|
222 | static inline void *RTMemAllocZVar (size_t cbUnaligned)
|
---|
223 | {
|
---|
224 | return RTMemAllocZ(cbUnaligned);
|
---|
225 | }
|
---|
226 |
|
---|
227 | static inline int RTStrToUInt32Ex (const char *pszValue, char **ppszNext, unsigned uBase, uint32_t *pu32)
|
---|
228 | {
|
---|
229 | *pu32 = strtoul (pszValue, ppszNext, uBase);
|
---|
230 | return VINF_SUCCESS;
|
---|
231 | }
|
---|
232 |
|
---|
233 | #define PRTSTREAM FILE *
|
---|
234 |
|
---|
235 | static inline int RTStrmOpen (const char *pszFileName, const char *pszMode, PRTSTREAM *ppStream)
|
---|
236 | {
|
---|
237 | *ppStream = fopen (pszFileName, pszMode);
|
---|
238 |
|
---|
239 | if (*ppStream)
|
---|
240 | {
|
---|
241 | return VINF_SUCCESS;
|
---|
242 | }
|
---|
243 |
|
---|
244 | return VERR_NOT_SUPPORTED;
|
---|
245 | }
|
---|
246 |
|
---|
247 | static inline int RTStrmClose (PRTSTREAM pStream)
|
---|
248 | {
|
---|
249 | fclose (pStream);
|
---|
250 | return VINF_SUCCESS;
|
---|
251 | }
|
---|
252 |
|
---|
253 | static inline int RTStrmGetLine (PRTSTREAM pStream, char *pszString, size_t cchString)
|
---|
254 | {
|
---|
255 | if (fgets (pszString, cchString, pStream))
|
---|
256 | {
|
---|
257 | return VINF_SUCCESS;
|
---|
258 | }
|
---|
259 |
|
---|
260 | return VERR_NOT_SUPPORTED;
|
---|
261 | }
|
---|
262 |
|
---|
263 | static inline char *RTStrStripL (const char *psz)
|
---|
264 | {
|
---|
265 | while (isspace (*psz))
|
---|
266 | psz++;
|
---|
267 | return (char *)psz;
|
---|
268 | }
|
---|
269 |
|
---|
270 | #define NIL_RTFILE -1
|
---|
271 |
|
---|
272 | #define RTFILE_O_READWRITE 0x00000003
|
---|
273 | #define RTFILE_O_OPEN 0x00000000
|
---|
274 | #define RTFILE_O_DENY_NONE 0x00000000
|
---|
275 |
|
---|
276 | static inline int RTFileOpen (RTFILE *pFile, const char *pszFileName, unsigned fOpen)
|
---|
277 | {
|
---|
278 | Assert (fOpen == RTFILE_O_READWRITE);
|
---|
279 |
|
---|
280 | *pFile = open (pszFileName, O_RDWR, 00600);
|
---|
281 |
|
---|
282 | if (*pFile != -1)
|
---|
283 | {
|
---|
284 | return VINF_SUCCESS;
|
---|
285 | }
|
---|
286 |
|
---|
287 | return VERR_ACCESS_DENIED;
|
---|
288 | }
|
---|
289 |
|
---|
290 | static inline int RTFileClose (RTFILE file)
|
---|
291 | {
|
---|
292 | close (file);
|
---|
293 | return VINF_SUCCESS;
|
---|
294 | }
|
---|
295 | #endif /* __RUNTIME__H */
|
---|