Introduce fls() helper
[qemu] / qemu-common.h
1 /* Common header file that is included by all of qemu.  */
2 #ifndef QEMU_COMMON_H
3 #define QEMU_COMMON_H
4
5 /* we put basic includes here to avoid repeating them in device drivers */
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <stdarg.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <inttypes.h>
12 #include <limits.h>
13 #include <time.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <sys/stat.h>
19
20 #ifndef O_LARGEFILE
21 #define O_LARGEFILE 0
22 #endif
23 #ifndef O_BINARY
24 #define O_BINARY 0
25 #endif
26
27 #ifndef ENOMEDIUM
28 #define ENOMEDIUM ENODEV
29 #endif
30
31 #ifdef _WIN32
32 #define WIN32_LEAN_AND_MEAN
33 #define WINVER 0x0501  /* needed for ipv6 bits */
34 #include <windows.h>
35 #define fsync _commit
36 #define lseek _lseeki64
37 #define ENOTSUP 4096
38 extern int qemu_ftruncate64(int, int64_t);
39 #define ftruncate qemu_ftruncate64
40
41
42 static inline char *realpath(const char *path, char *resolved_path)
43 {
44     _fullpath(resolved_path, path, _MAX_PATH);
45     return resolved_path;
46 }
47
48 #define PRId64 "I64d"
49 #define PRIx64 "I64x"
50 #define PRIu64 "I64u"
51 #define PRIo64 "I64o"
52 #endif
53
54 /* FIXME: Remove NEED_CPU_H.  */
55 #ifndef NEED_CPU_H
56
57 #include "config-host.h"
58 #include <setjmp.h>
59 #include "osdep.h"
60 #include "bswap.h"
61
62 #else
63
64 #include "cpu.h"
65
66 #endif /* !defined(NEED_CPU_H) */
67
68 /* bottom halves */
69 typedef struct QEMUBH QEMUBH;
70
71 typedef void QEMUBHFunc(void *opaque);
72
73 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
74 void qemu_bh_schedule(QEMUBH *bh);
75 /* Bottom halfs that are scheduled from a bottom half handler are instantly
76  * invoked.  This can create an infinite loop if a bottom half handler
77  * schedules itself.  qemu_bh_schedule_idle() avoids this infinite loop by
78  * ensuring that the bottom half isn't executed until the next main loop
79  * iteration.
80  */
81 void qemu_bh_schedule_idle(QEMUBH *bh);
82 void qemu_bh_cancel(QEMUBH *bh);
83 void qemu_bh_delete(QEMUBH *bh);
84 int qemu_bh_poll(void);
85
86 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
87
88 void qemu_get_timedate(struct tm *tm, int offset);
89 int qemu_timedate_diff(struct tm *tm);
90
91 /* cutils.c */
92 void pstrcpy(char *buf, int buf_size, const char *str);
93 char *pstrcat(char *buf, int buf_size, const char *s);
94 int strstart(const char *str, const char *val, const char **ptr);
95 int stristart(const char *str, const char *val, const char **ptr);
96 time_t mktimegm(struct tm *tm);
97 int fls(int i);
98
99 #define qemu_isalnum(c)         isalnum((unsigned char)(c))
100 #define qemu_isalpha(c)         isalpha((unsigned char)(c))
101 #define qemu_iscntrl(c)         iscntrl((unsigned char)(c))
102 #define qemu_isdigit(c)         isdigit((unsigned char)(c))
103 #define qemu_isgraph(c)         isgraph((unsigned char)(c))
104 #define qemu_islower(c)         islower((unsigned char)(c))
105 #define qemu_isprint(c)         isprint((unsigned char)(c))
106 #define qemu_ispunct(c)         ispunct((unsigned char)(c))
107 #define qemu_isspace(c)         isspace((unsigned char)(c))
108 #define qemu_isupper(c)         isupper((unsigned char)(c))
109 #define qemu_isxdigit(c)        isxdigit((unsigned char)(c))
110 #define qemu_tolower(c)         tolower((unsigned char)(c))
111 #define qemu_toupper(c)         toupper((unsigned char)(c))
112 #define qemu_isascii(c)         isascii((unsigned char)(c))
113 #define qemu_toascii(c)         toascii((unsigned char)(c))
114
115 void *qemu_malloc(size_t size);
116 void *qemu_realloc(void *ptr, size_t size);
117 void *qemu_mallocz(size_t size);
118 void qemu_free(void *ptr);
119 char *qemu_strdup(const char *str);
120 char *qemu_strndup(const char *str, size_t size);
121
122 void *get_mmap_addr(unsigned long size);
123
124
125 /* Error handling.  */
126
127 void hw_error(const char *fmt, ...)
128     __attribute__ ((__format__ (__printf__, 1, 2)))
129     __attribute__ ((__noreturn__));
130
131 /* IO callbacks.  */
132 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
133 typedef int IOCanRWHandler(void *opaque);
134 typedef void IOHandler(void *opaque);
135
136 struct ParallelIOArg {
137     void *buffer;
138     int count;
139 };
140
141 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
142
143 /* A load of opaque types so that device init declarations don't have to
144    pull in all the real definitions.  */
145 typedef struct NICInfo NICInfo;
146 typedef struct HCIInfo HCIInfo;
147 typedef struct AudioState AudioState;
148 typedef struct BlockDriverState BlockDriverState;
149 typedef struct DisplayState DisplayState;
150 typedef struct TextConsole TextConsole;
151 typedef TextConsole QEMUConsole;
152 typedef struct CharDriverState CharDriverState;
153 typedef struct VLANState VLANState;
154 typedef struct QEMUFile QEMUFile;
155 typedef struct i2c_bus i2c_bus;
156 typedef struct i2c_slave i2c_slave;
157 typedef struct SMBusDevice SMBusDevice;
158 typedef struct QEMUTimer QEMUTimer;
159 typedef struct PCIBus PCIBus;
160 typedef struct PCIDevice PCIDevice;
161 typedef struct SerialState SerialState;
162 typedef struct IRQState *qemu_irq;
163 struct pcmcia_card_s;
164
165 /* CPU save/load.  */
166 void cpu_save(QEMUFile *f, void *opaque);
167 int cpu_load(QEMUFile *f, void *opaque, int version_id);
168
169 /* Force QEMU to stop what it's doing and service IO */
170 void qemu_service_io(void);
171
172 #endif