Statistics
| Branch: | Revision:

root / HACKING @ feature-archipelago

History | View | Annotate | Download (7 kB)

1 45fad878 Blue Swirl
1. Preprocessor
2 45fad878 Blue Swirl
3 45fad878 Blue Swirl
For variadic macros, stick with this C99-like syntax:
4 45fad878 Blue Swirl
5 45fad878 Blue Swirl
#define DPRINTF(fmt, ...)                                       \
6 45fad878 Blue Swirl
    do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)
7 84174436 Blue Swirl
8 84174436 Blue Swirl
2. C types
9 84174436 Blue Swirl
10 84174436 Blue Swirl
It should be common sense to use the right type, but we have collected
11 84174436 Blue Swirl
a few useful guidelines here.
12 84174436 Blue Swirl
13 84174436 Blue Swirl
2.1. Scalars
14 84174436 Blue Swirl
15 84174436 Blue Swirl
If you're using "int" or "long", odds are good that there's a better type.
16 84174436 Blue Swirl
If a variable is counting something, it should be declared with an
17 84174436 Blue Swirl
unsigned type.
18 84174436 Blue Swirl
19 84174436 Blue Swirl
If it's host memory-size related, size_t should be a good choice (use
20 84174436 Blue Swirl
ssize_t only if required). Guest RAM memory offsets must use ram_addr_t,
21 84174436 Blue Swirl
but only for RAM, it may not cover whole guest address space.
22 84174436 Blue Swirl
23 84174436 Blue Swirl
If it's file-size related, use off_t.
24 84174436 Blue Swirl
If it's file-offset related (i.e., signed), use off_t.
25 84174436 Blue Swirl
If it's just counting small numbers use "unsigned int";
26 84174436 Blue Swirl
(on all but oddball embedded systems, you can assume that that
27 84174436 Blue Swirl
type is at least four bytes wide).
28 84174436 Blue Swirl
29 84174436 Blue Swirl
In the event that you require a specific width, use a standard type
30 84174436 Blue Swirl
like int32_t, uint32_t, uint64_t, etc.  The specific types are
31 84174436 Blue Swirl
mandatory for VMState fields.
32 84174436 Blue Swirl
33 84174436 Blue Swirl
Don't use Linux kernel internal types like u32, __u32 or __le32.
34 84174436 Blue Swirl
35 a8170e5e Avi Kivity
Use hwaddr for guest physical addresses except pcibus_t
36 84174436 Blue Swirl
for PCI addresses.  In addition, ram_addr_t is a QEMU internal address
37 84174436 Blue Swirl
space that maps guest RAM physical addresses into an intermediate
38 84174436 Blue Swirl
address space that can map to host virtual address spaces.  Generally
39 84174436 Blue Swirl
speaking, the size of guest memory can always fit into ram_addr_t but
40 84174436 Blue Swirl
it would not be correct to store an actual guest physical address in a
41 84174436 Blue Swirl
ram_addr_t.
42 84174436 Blue Swirl
43 2be8d450 Peter Maydell
For CPU virtual addresses there are several possible types.
44 2be8d450 Peter Maydell
vaddr is the best type to use to hold a CPU virtual address in
45 2be8d450 Peter Maydell
target-independent code. It is guaranteed to be large enough to hold a
46 2be8d450 Peter Maydell
virtual address for any target, and it does not change size from target
47 2be8d450 Peter Maydell
to target. It is always unsigned.
48 2be8d450 Peter Maydell
target_ulong is a type the size of a virtual address on the CPU; this means
49 2be8d450 Peter Maydell
it may be 32 or 64 bits depending on which target is being built. It should
50 2be8d450 Peter Maydell
therefore be used only in target-specific code, and in some
51 2be8d450 Peter Maydell
performance-critical built-per-target core code such as the TLB code.
52 2be8d450 Peter Maydell
There is also a signed version, target_long.
53 2be8d450 Peter Maydell
abi_ulong is for the *-user targets, and represents a type the size of
54 2be8d450 Peter Maydell
'void *' in that target's ABI. (This may not be the same as the size of a
55 2be8d450 Peter Maydell
full CPU virtual address in the case of target ABIs which use 32 bit pointers
56 2be8d450 Peter Maydell
on 64 bit CPUs, like sparc32plus.) Definitions of structures that must match
57 2be8d450 Peter Maydell
the target's ABI must use this type for anything that on the target is defined
58 2be8d450 Peter Maydell
to be an 'unsigned long' or a pointer type.
59 2be8d450 Peter Maydell
There is also a signed version, abi_long.
60 84174436 Blue Swirl
61 84174436 Blue Swirl
Of course, take all of the above with a grain of salt.  If you're about
62 84174436 Blue Swirl
to use some system interface that requires a type like size_t, pid_t or
63 84174436 Blue Swirl
off_t, use matching types for any corresponding variables.
64 84174436 Blue Swirl
65 84174436 Blue Swirl
Also, if you try to use e.g., "unsigned int" as a type, and that
66 84174436 Blue Swirl
conflicts with the signedness of a related variable, sometimes
67 84174436 Blue Swirl
it's best just to use the *wrong* type, if "pulling the thread"
68 84174436 Blue Swirl
and fixing all related variables would be too invasive.
69 84174436 Blue Swirl
70 84174436 Blue Swirl
Finally, while using descriptive types is important, be careful not to
71 84174436 Blue Swirl
go overboard.  If whatever you're doing causes warnings, or requires
72 84174436 Blue Swirl
casts, then reconsider or ask for help.
73 84174436 Blue Swirl
74 84174436 Blue Swirl
2.2. Pointers
75 84174436 Blue Swirl
76 84174436 Blue Swirl
Ensure that all of your pointers are "const-correct".
77 84174436 Blue Swirl
Unless a pointer is used to modify the pointed-to storage,
78 84174436 Blue Swirl
give it the "const" attribute.  That way, the reader knows
79 84174436 Blue Swirl
up-front that this is a read-only pointer.  Perhaps more
80 84174436 Blue Swirl
importantly, if we're diligent about this, when you see a non-const
81 84174436 Blue Swirl
pointer, you're guaranteed that it is used to modify the storage
82 84174436 Blue Swirl
it points to, or it is aliased to another pointer that is.
83 84174436 Blue Swirl
84 84174436 Blue Swirl
2.3. Typedefs
85 84174436 Blue Swirl
Typedefs are used to eliminate the redundant 'struct' keyword.
86 84174436 Blue Swirl
87 84174436 Blue Swirl
2.4. Reserved namespaces in C and POSIX
88 84174436 Blue Swirl
Underscore capital, double underscore, and underscore 't' suffixes should be
89 84174436 Blue Swirl
avoided.
90 54b2cc50 Blue Swirl
91 54b2cc50 Blue Swirl
3. Low level memory management
92 54b2cc50 Blue Swirl
93 54b2cc50 Blue Swirl
Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
94 54b2cc50 Blue Swirl
APIs is not allowed in the QEMU codebase. Instead of these routines,
95 f603a687 Peter Maydell
use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
96 6eebf958 Paolo Bonzini
g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
97 f603a687 Peter Maydell
APIs.
98 54b2cc50 Blue Swirl
99 f603a687 Peter Maydell
Please note that g_malloc will exit on allocation failure, so there
100 f603a687 Peter Maydell
is no need to test for failure (as you would have to with malloc).
101 f603a687 Peter Maydell
Calling g_malloc with a zero size is valid and will return NULL.
102 54b2cc50 Blue Swirl
103 6eebf958 Paolo Bonzini
Memory allocated by qemu_memalign or qemu_blockalign must be freed with
104 6eebf958 Paolo Bonzini
qemu_vfree, since breaking this will cause problems on Win32.
105 d241f143 Blue Swirl
106 d241f143 Blue Swirl
4. String manipulation
107 d241f143 Blue Swirl
108 9b9e3ec1 Jim Meyering
Do not use the strncpy function.  As mentioned in the man page, it does *not*
109 9b9e3ec1 Jim Meyering
guarantee a NULL-terminated buffer, which makes it extremely dangerous to use.
110 9b9e3ec1 Jim Meyering
It also zeros trailing destination bytes out to the specified length.  Instead,
111 9b9e3ec1 Jim Meyering
use this similar function when possible, but note its different signature:
112 9b9e3ec1 Jim Meyering
void pstrcpy(char *dest, int dest_buf_size, const char *src)
113 d241f143 Blue Swirl
114 d241f143 Blue Swirl
Don't use strcat because it can't check for buffer overflows, but:
115 d241f143 Blue Swirl
char *pstrcat(char *buf, int buf_size, const char *s)
116 d241f143 Blue Swirl
117 d241f143 Blue Swirl
The same limitation exists with sprintf and vsprintf, so use snprintf and
118 d241f143 Blue Swirl
vsnprintf.
119 d241f143 Blue Swirl
120 d241f143 Blue Swirl
QEMU provides other useful string functions:
121 d241f143 Blue Swirl
int strstart(const char *str, const char *val, const char **ptr)
122 d241f143 Blue Swirl
int stristart(const char *str, const char *val, const char **ptr)
123 d241f143 Blue Swirl
int qemu_strnlen(const char *s, int max_len)
124 d241f143 Blue Swirl
125 d241f143 Blue Swirl
There are also replacement character processing macros for isxyz and toxyz,
126 d241f143 Blue Swirl
so instead of e.g. isalnum you should use qemu_isalnum.
127 d241f143 Blue Swirl
128 145e21db Anthony Liguori
Because of the memory management rules, you must use g_strdup/g_strndup
129 d241f143 Blue Swirl
instead of plain strdup/strndup.
130 876f256b Blue Swirl
131 876f256b Blue Swirl
5. Printf-style functions
132 876f256b Blue Swirl
133 876f256b Blue Swirl
Whenever you add a new printf-style function, i.e., one with a format
134 876f256b Blue Swirl
string argument and following "..." in its prototype, be sure to use
135 876f256b Blue Swirl
gcc's printf attribute directive in the prototype.
136 876f256b Blue Swirl
137 876f256b Blue Swirl
This makes it so gcc's -Wformat and -Wformat-security options can do
138 876f256b Blue Swirl
their jobs and cross-check format strings with the number and types
139 876f256b Blue Swirl
of arguments.
140 47536317 Peter Maydell
141 47536317 Peter Maydell
6. C standard, implementation defined and undefined behaviors
142 47536317 Peter Maydell
143 47536317 Peter Maydell
C code in QEMU should be written to the C99 language specification. A copy
144 47536317 Peter Maydell
of the final version of the C99 standard with corrigenda TC1, TC2, and TC3
145 47536317 Peter Maydell
included, formatted as a draft, can be downloaded from:
146 47536317 Peter Maydell
 http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
147 47536317 Peter Maydell
148 47536317 Peter Maydell
The C language specification defines regions of undefined behavior and
149 47536317 Peter Maydell
implementation defined behavior (to give compiler authors enough leeway to
150 47536317 Peter Maydell
produce better code).  In general, code in QEMU should follow the language
151 47536317 Peter Maydell
specification and avoid both undefined and implementation defined
152 47536317 Peter Maydell
constructs. ("It works fine on the gcc I tested it with" is not a valid
153 47536317 Peter Maydell
argument...) However there are a few areas where we allow ourselves to
154 47536317 Peter Maydell
assume certain behaviors because in practice all the platforms we care about
155 47536317 Peter Maydell
behave in the same way and writing strictly conformant code would be
156 47536317 Peter Maydell
painful. These are:
157 47536317 Peter Maydell
 * you may assume that integers are 2s complement representation
158 47536317 Peter Maydell
 * you may assume that right shift of a signed integer duplicates
159 47536317 Peter Maydell
   the sign bit (ie it is an arithmetic shift, not a logical shift)