Add qemu_strndup: qemu_strdup with length limit.
[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 <inttypes.h>
11 #include <limits.h>
12 #include <time.h>
13 #include <ctype.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <sys/stat.h>
18
19 #ifndef O_LARGEFILE
20 #define O_LARGEFILE 0
21 #endif
22 #ifndef O_BINARY
23 #define O_BINARY 0
24 #endif
25
26 #ifndef ENOMEDIUM
27 #define ENOMEDIUM ENODEV
28 #endif
29
30 #ifdef _WIN32
31 #define WIN32_LEAN_AND_MEAN
32 #include <windows.h>
33 #define fsync _commit
34 #define lseek _lseeki64
35 #define ENOTSUP 4096
36 extern int qemu_ftruncate64(int, int64_t);
37 #define ftruncate qemu_ftruncate64
38
39
40 static inline char *realpath(const char *path, char *resolved_path)
41 {
42     _fullpath(resolved_path, path, _MAX_PATH);
43     return resolved_path;
44 }
45
46 #define PRId64 "I64d"
47 #define PRIx64 "I64x"
48 #define PRIu64 "I64u"
49 #define PRIo64 "I64o"
50 #endif
51
52 /* FIXME: Remove NEED_CPU_H.  */
53 #ifndef NEED_CPU_H
54
55 #include "config-host.h"
56 #include <setjmp.h>
57 #include "osdep.h"
58 #include "bswap.h"
59
60 #else
61
62 #include "cpu.h"
63
64 #endif /* !defined(NEED_CPU_H) */
65
66 /* bottom halves */
67 typedef struct QEMUBH QEMUBH;
68
69 typedef void QEMUBHFunc(void *opaque);
70
71 QEMUBH *qemu_bh_new(QEMUBHFunc *cb, void *opaque);
72 void qemu_bh_schedule(QEMUBH *bh);
73 /* Bottom halfs that are scheduled from a bottom half handler are instantly
74  * invoked.  This can create an infinite loop if a bottom half handler
75  * schedules itself.  qemu_bh_schedule_idle() avoids this infinite loop by
76  * ensuring that the bottom half isn't executed until the next main loop
77  * iteration.
78  */
79 void qemu_bh_schedule_idle(QEMUBH *bh);
80 void qemu_bh_cancel(QEMUBH *bh);
81 void qemu_bh_delete(QEMUBH *bh);
82 int qemu_bh_poll(void);
83
84 uint64_t muldiv64(uint64_t a, uint32_t b, uint32_t c);
85
86 void qemu_get_timedate(struct tm *tm, int offset);
87 int qemu_timedate_diff(struct tm *tm);
88
89 /* cutils.c */
90 void pstrcpy(char *buf, int buf_size, const char *str);
91 char *pstrcat(char *buf, int buf_size, const char *s);
92 int strstart(const char *str, const char *val, const char **ptr);
93 int stristart(const char *str, const char *val, const char **ptr);
94 time_t mktimegm(struct tm *tm);
95
96 void *qemu_malloc(size_t size);
97 void *qemu_realloc(void *ptr, size_t size);
98 void *qemu_mallocz(size_t size);
99 void qemu_free(void *ptr);
100 char *qemu_strdup(const char *str);
101 char *qemu_strndup(const char *str, size_t size);
102
103 void *get_mmap_addr(unsigned long size);
104
105
106 /* Error handling.  */
107
108 void hw_error(const char *fmt, ...)
109     __attribute__ ((__format__ (__printf__, 1, 2)))
110     __attribute__ ((__noreturn__));
111
112 /* IO callbacks.  */
113 typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
114 typedef int IOCanRWHandler(void *opaque);
115 typedef void IOHandler(void *opaque);
116
117 struct ParallelIOArg {
118     void *buffer;
119     int count;
120 };
121
122 typedef int (*DMA_transfer_handler) (void *opaque, int nchan, int pos, int size);
123
124 /* A load of opaque types so that device init declarations don't have to
125    pull in all the real definitions.  */
126 typedef struct NICInfo NICInfo;
127 typedef struct HCIInfo HCIInfo;
128 typedef struct AudioState AudioState;
129 typedef struct BlockDriverState BlockDriverState;
130 typedef struct DisplayState DisplayState;
131 typedef struct TextConsole TextConsole;
132 typedef TextConsole QEMUConsole;
133 typedef struct CharDriverState CharDriverState;
134 typedef struct VLANState VLANState;
135 typedef struct QEMUFile QEMUFile;
136 typedef struct i2c_bus i2c_bus;
137 typedef struct i2c_slave i2c_slave;
138 typedef struct SMBusDevice SMBusDevice;
139 typedef struct QEMUTimer QEMUTimer;
140 typedef struct PCIBus PCIBus;
141 typedef struct PCIDevice PCIDevice;
142 typedef struct SerialState SerialState;
143 typedef struct IRQState *qemu_irq;
144 struct pcmcia_card_s;
145
146 /* CPU save/load.  */
147 void cpu_save(QEMUFile *f, void *opaque);
148 int cpu_load(QEMUFile *f, void *opaque, int version_id);
149
150 /* Force QEMU to stop what it's doing and service IO */
151 void qemu_service_io(void);
152
153 #endif