diff options
Diffstat (limited to 'doc/pintos_6.html')
| -rw-r--r-- | doc/pintos_6.html | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/doc/pintos_6.html b/doc/pintos_6.html new file mode 100644 index 0000000..afe7d8a --- /dev/null +++ b/doc/pintos_6.html | |||
| @@ -0,0 +1,314 @@ | |||
| 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | ||
| 2 | "http://www.w3.org/TR/html40/loose.dtd"> | ||
| 3 | <HTML> | ||
| 4 | <!-- Created on March, 6 2012 by texi2html 1.66 --> | ||
| 5 | <!-- | ||
| 6 | Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) | ||
| 7 | Karl Berry <karl@freefriends.org> | ||
| 8 | Olaf Bachmann <obachman@mathematik.uni-kl.de> | ||
| 9 | and many others. | ||
| 10 | Maintained by: Many creative people <dev@texi2html.cvshome.org> | ||
| 11 | Send bugs and suggestions to <users@texi2html.cvshome.org> | ||
| 12 | |||
| 13 | --> | ||
| 14 | <HEAD> | ||
| 15 | <TITLE>Pintos Projects: Coding Standards</TITLE> | ||
| 16 | |||
| 17 | <META NAME="description" CONTENT="Pintos Projects: Coding Standards"> | ||
| 18 | <META NAME="keywords" CONTENT="Pintos Projects: Coding Standards"> | ||
| 19 | <META NAME="resource-type" CONTENT="document"> | ||
| 20 | <META NAME="distribution" CONTENT="global"> | ||
| 21 | <META NAME="Generator" CONTENT="texi2html 1.66"> | ||
| 22 | <LINK REL="stylesheet" HREF="pintos.css"> | ||
| 23 | </HEAD> | ||
| 24 | |||
| 25 | <BODY > | ||
| 26 | |||
| 27 | <A NAME="SEC89"></A> | ||
| 28 | <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> | ||
| 29 | <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos_5.html#SEC48"> << </A>]</TD> | ||
| 30 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos_7.html#SEC93"> >> </A>]</TD> | ||
| 31 | <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos.html#SEC_Top">Top</A>]</TD> | ||
| 32 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos.html#SEC_Contents">Contents</A>]</TD> | ||
| 33 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD> | ||
| 34 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos_abt.html#SEC_About"> ? </A>]</TD> | ||
| 35 | </TR></TABLE> | ||
| 36 | |||
| 37 | <HR SIZE=2> | ||
| 38 | <H1> B. Coding Standards </H1> | ||
| 39 | <!--docid::SEC89::--> | ||
| 40 | <P> | ||
| 41 | |||
| 42 | TODO: TUW coding standards | ||
| 43 | We expect you to be | ||
| 44 | familiar with some set of coding standards such as | ||
| 45 | <A HREF="http://www.stanford.edu/class/cs140/projects/misc/CodingStandards.pdf">CS 107 Coding Standards</A>. Even if you've taken 107, we recommend | ||
| 46 | reviewing that document. We expect code at the "Peer-Review Quality" | ||
| 47 | level described there. | ||
| 48 | </P> | ||
| 49 | <P> | ||
| 50 | |||
| 51 | Our standards for coding are most important for grading. We want to | ||
| 52 | stress that aside from the fact that we are explicitly basing part of | ||
| 53 | your grade on these things, good coding practices will improve the | ||
| 54 | quality of your code. This makes it easier for your partners to | ||
| 55 | interact with it, and ultimately, will improve your chances of having a | ||
| 56 | good working program. That said once, the rest of this document will | ||
| 57 | discuss only the ways in which our coding standards will affect our | ||
| 58 | grading. | ||
| 59 | </P> | ||
| 60 | <P> | ||
| 61 | |||
| 62 | <A NAME="Coding Style"></A> | ||
| 63 | <HR SIZE="6"> | ||
| 64 | <A NAME="SEC90"></A> | ||
| 65 | <H2> B.1 Style </H2> | ||
| 66 | <!--docid::SEC90::--> | ||
| 67 | <P> | ||
| 68 | |||
| 69 | Style, for the purposes of our grading, refers to how readable your | ||
| 70 | code is. At minimum, this means that your code is well formatted, your | ||
| 71 | variable names are descriptive and your functions are decomposed and | ||
| 72 | well commented. Any other factors which make it hard (or easy) for us | ||
| 73 | to read or use your code will be reflected in your style grade. | ||
| 74 | </P> | ||
| 75 | <P> | ||
| 76 | |||
| 77 | The existing Pintos code is written in the GNU style and largely | ||
| 78 | follows the <A HREF="http://www.gnu.org/prep/standards_toc.html">GNU | ||
| 79 | Coding Standards</A>. We encourage you to follow the applicable parts of | ||
| 80 | them too, especially chapter 5, "Making the Best Use of C." Using a | ||
| 81 | different style won't cause actual problems, but it's ugly to see | ||
| 82 | gratuitous differences in style from one function to another. If your | ||
| 83 | code is too ugly, it will cost you points. | ||
| 84 | </P> | ||
| 85 | <P> | ||
| 86 | |||
| 87 | Please limit C source file lines to at most 79 characters long. | ||
| 88 | </P> | ||
| 89 | <P> | ||
| 90 | |||
| 91 | Pintos comments sometimes refer to external standards or | ||
| 92 | specifications by writing a name inside square brackets, like this: | ||
| 93 | <CODE>[IA32-v3a]</CODE>. These names refer to the reference names used in | ||
| 94 | this documentation (see section <A HREF="pintos_10.html#SEC113">Bibliography</A>). | ||
| 95 | </P> | ||
| 96 | <P> | ||
| 97 | |||
| 98 | If you remove existing Pintos code, please delete it from your source | ||
| 99 | file entirely. Don't just put it into a comment or a conditional | ||
| 100 | compilation directive, because that makes the resulting code hard to | ||
| 101 | read. | ||
| 102 | </P> | ||
| 103 | <P> | ||
| 104 | |||
| 105 | We're only going to do a compile in the directory for the project being | ||
| 106 | submitted. You don't need to make sure that the previous projects also | ||
| 107 | compile. | ||
| 108 | </P> | ||
| 109 | <P> | ||
| 110 | |||
| 111 | Project code should be written so that all of the subproblems for the | ||
| 112 | project function together, that is, without the need to rebuild with | ||
| 113 | different macros defined, etc. If you do extra credit work that | ||
| 114 | changes normal Pintos behavior so as to interfere with grading, then | ||
| 115 | you must implement it so that it only acts that way when given a | ||
| 116 | special command-line option of the form <Q><SAMP>-<VAR>name</VAR></SAMP></Q>, where | ||
| 117 | <VAR>name</VAR> is a name of your choice. You can add such an option by | ||
| 118 | modifying <CODE>parse_options()</CODE> in <Q><TT>threads/init.c</TT></Q>. | ||
| 119 | </P> | ||
| 120 | <P> | ||
| 121 | |||
| 122 | The introduction describes additional coding style requirements | ||
| 123 | (see section <A HREF="pintos_1.html#SEC9">1.2.2 Design</A>). | ||
| 124 | </P> | ||
| 125 | <P> | ||
| 126 | |||
| 127 | <A NAME="C99"></A> | ||
| 128 | <HR SIZE="6"> | ||
| 129 | <A NAME="SEC91"></A> | ||
| 130 | <H2> B.2 C99 </H2> | ||
| 131 | <!--docid::SEC91::--> | ||
| 132 | <P> | ||
| 133 | |||
| 134 | The Pintos source code uses a few features of the "C99" standard | ||
| 135 | library that were not in the original 1989 standard for C. Many | ||
| 136 | programmers are unaware of these feature, so we will describe them. The | ||
| 137 | new features used in Pintos are | ||
| 138 | mostly in new headers: | ||
| 139 | </P> | ||
| 140 | <P> | ||
| 141 | |||
| 142 | </P> | ||
| 143 | <DL COMPACT> | ||
| 144 | <DT><Q><TT><stdbool.h></TT></Q> | ||
| 145 | <DD>Defines macros <CODE>bool</CODE>, a 1-bit type that takes on only the values | ||
| 146 | 0 and 1, <CODE>true</CODE>, which expands to 1, and <CODE>false</CODE>, which | ||
| 147 | expands to 0. | ||
| 148 | <P> | ||
| 149 | |||
| 150 | </P> | ||
| 151 | <DT><Q><TT><stdint.h></TT></Q> | ||
| 152 | <DD>On systems that support them, this header defines types | ||
| 153 | <CODE>int<VAR>n</VAR>_t</CODE> and <CODE>uint<VAR>n</VAR>_t</CODE> for <VAR>n</VAR> = 8, 16, 32, | ||
| 154 | 64, and possibly other values. These are 2's complement signed and unsigned | ||
| 155 | types, respectively, with the given number of bits. | ||
| 156 | <P> | ||
| 157 | |||
| 158 | On systems where it is possible, this header also defines types | ||
| 159 | <CODE>intptr_t</CODE> and <CODE>uintptr_t</CODE>, which are integer types big | ||
| 160 | enough to hold a pointer. | ||
| 161 | </P> | ||
| 162 | <P> | ||
| 163 | |||
| 164 | On all systems, this header defines types <CODE>intmax_t</CODE> and | ||
| 165 | <CODE>uintmax_t</CODE>, which are the system's signed and unsigned integer | ||
| 166 | types with the widest ranges. | ||
| 167 | </P> | ||
| 168 | <P> | ||
| 169 | |||
| 170 | For every signed integer type <CODE><VAR>type</VAR>_t</CODE> defined here, as well | ||
| 171 | as for <CODE>ptrdiff_t</CODE> defined in <Q><TT><stddef.h></TT></Q>, this header also | ||
| 172 | defines macros <CODE><VAR>TYPE</VAR>_MAX</CODE> and <CODE><VAR>TYPE</VAR>_MIN</CODE> that | ||
| 173 | give the type's range. Similarly, for every unsigned integer type | ||
| 174 | <CODE><VAR>type</VAR>_t</CODE> defined here, as well as for <CODE>size_t</CODE> defined | ||
| 175 | in <Q><TT><stddef.h></TT></Q>, this header defines a <CODE><VAR>TYPE</VAR>_MAX</CODE> | ||
| 176 | macro giving its maximum value. | ||
| 177 | </P> | ||
| 178 | <P> | ||
| 179 | |||
| 180 | </P> | ||
| 181 | <DT><Q><TT><inttypes.h></TT></Q> | ||
| 182 | <DD><Q><TT><stdint.h></TT></Q> provides no straightforward way to format | ||
| 183 | the types it defines with <CODE>printf()</CODE> and related functions. This | ||
| 184 | header provides macros to help with that. For every | ||
| 185 | <CODE>int<VAR>n</VAR>_t</CODE> defined by <Q><TT><stdint.h></TT></Q>, it provides macros | ||
| 186 | <CODE>PRId<VAR>n</VAR></CODE> and <CODE>PRIi<VAR>n</VAR></CODE> for formatting values of | ||
| 187 | that type with <CODE>"%d"</CODE> and <CODE>"%i"</CODE>. Similarly, for every | ||
| 188 | <CODE>uint<VAR>n</VAR>_t</CODE>, it provides <CODE>PRIo<VAR>n</VAR></CODE>, | ||
| 189 | <CODE>PRIu<VAR>n</VAR></CODE>, <CODE>PRIu<VAR>x</VAR></CODE>, and <CODE>PRIu<VAR>X</VAR></CODE>. | ||
| 190 | <P> | ||
| 191 | |||
| 192 | You use these something like this, taking advantage of the fact that | ||
| 193 | the C compiler concatenates adjacent string literals: | ||
| 194 | <TABLE><tr><td> </td><td class=example><pre>#include <inttypes.h> | ||
| 195 | <small>...</small> | ||
| 196 | int32_t value = <small>...</small>; | ||
| 197 | printf ("value=%08"PRId32"\n", value); | ||
| 198 | </pre></td></tr></table>The <Q><SAMP>%</SAMP></Q> is not supplied by the <CODE>PRI</CODE> macros. As shown | ||
| 199 | above, you supply it yourself and follow it by any flags, field | ||
| 200 | width, etc. | ||
| 201 | <P> | ||
| 202 | |||
| 203 | </P> | ||
| 204 | <DT><Q><TT><stdio.h></TT></Q> | ||
| 205 | <DD>The <CODE>printf()</CODE> function has some new type modifiers for printing | ||
| 206 | standard types: | ||
| 207 | <P> | ||
| 208 | |||
| 209 | </P> | ||
| 210 | <DL COMPACT> | ||
| 211 | <DT><Q><SAMP>j</SAMP></Q> | ||
| 212 | <DD>For <CODE>intmax_t</CODE> (e.g. <Q><SAMP>%jd</SAMP></Q>) or <CODE>uintmax_t</CODE> (e.g. | ||
| 213 | <Q><SAMP>%ju</SAMP></Q>). | ||
| 214 | <P> | ||
| 215 | |||
| 216 | </P> | ||
| 217 | <DT><Q><SAMP>z</SAMP></Q> | ||
| 218 | <DD>For <CODE>size_t</CODE> (e.g. <Q><SAMP>%zu</SAMP></Q>). | ||
| 219 | <P> | ||
| 220 | |||
| 221 | </P> | ||
| 222 | <DT><Q><SAMP>t</SAMP></Q> | ||
| 223 | <DD>For <CODE>ptrdiff_t</CODE> (e.g. <Q><SAMP>%td</SAMP></Q>). | ||
| 224 | </DL> | ||
| 225 | <P> | ||
| 226 | |||
| 227 | Pintos <CODE>printf()</CODE> also implements a nonstandard <Q><SAMP>'</SAMP></Q> flag that | ||
| 228 | groups large numbers with commas to make them easier to read. | ||
| 229 | </DL> | ||
| 230 | <P> | ||
| 231 | |||
| 232 | <A NAME="Unsafe String Functions"></A> | ||
| 233 | <HR SIZE="6"> | ||
| 234 | <A NAME="SEC92"></A> | ||
| 235 | <H2> B.3 Unsafe String Functions </H2> | ||
| 236 | <!--docid::SEC92::--> | ||
| 237 | <P> | ||
| 238 | |||
| 239 | A few of the string functions declared in the standard | ||
| 240 | <Q><TT><string.h></TT></Q> and <Q><TT><stdio.h></TT></Q> headers are notoriously unsafe. | ||
| 241 | The worst offenders are intentionally not included in the Pintos C | ||
| 242 | library: | ||
| 243 | </P> | ||
| 244 | <P> | ||
| 245 | |||
| 246 | </P> | ||
| 247 | <DL COMPACT> | ||
| 248 | <DT><CODE>strcpy</CODE> | ||
| 249 | <DD>When used carelessly this function can overflow the buffer reserved | ||
| 250 | for its output string. Use <CODE>strlcpy()</CODE> instead. Refer to | ||
| 251 | comments in its source code in <CODE>lib/string.c</CODE> for documentation. | ||
| 252 | <P> | ||
| 253 | |||
| 254 | </P> | ||
| 255 | <DT><CODE>strncpy</CODE> | ||
| 256 | <DD>This function can leave its destination buffer without a null string | ||
| 257 | terminator. It also has performance problems. Again, use | ||
| 258 | <CODE>strlcpy()</CODE>. | ||
| 259 | <P> | ||
| 260 | |||
| 261 | </P> | ||
| 262 | <DT><CODE>strcat</CODE> | ||
| 263 | <DD>Same issue as <CODE>strcpy()</CODE>. Use <CODE>strlcat()</CODE> instead. | ||
| 264 | Again, refer to comments in its source code in <CODE>lib/string.c</CODE> for | ||
| 265 | documentation. | ||
| 266 | <P> | ||
| 267 | |||
| 268 | </P> | ||
| 269 | <DT><CODE>strncat</CODE> | ||
| 270 | <DD>The meaning of its buffer size argument is surprising. | ||
| 271 | Again, use <CODE>strlcat()</CODE>. | ||
| 272 | <P> | ||
| 273 | |||
| 274 | </P> | ||
| 275 | <DT><CODE>strtok</CODE> | ||
| 276 | <DD>Uses global data, so it is unsafe in threaded programs such as | ||
| 277 | kernels. Use <CODE>strtok_r()</CODE> instead, and see its source code in | ||
| 278 | <CODE>lib/string.c</CODE> for documentation and an example. | ||
| 279 | <P> | ||
| 280 | |||
| 281 | </P> | ||
| 282 | <DT><CODE>sprintf</CODE> | ||
| 283 | <DD>Same issue as <CODE>strcpy()</CODE>. Use <CODE>snprintf()</CODE> instead. Refer | ||
| 284 | to comments in <CODE>lib/stdio.h</CODE> for documentation. | ||
| 285 | <P> | ||
| 286 | |||
| 287 | </P> | ||
| 288 | <DT><CODE>vsprintf</CODE> | ||
| 289 | <DD>Same issue as <CODE>strcpy()</CODE>. Use <CODE>vsnprintf()</CODE> instead. | ||
| 290 | </DL> | ||
| 291 | <P> | ||
| 292 | |||
| 293 | If you try to use any of these functions, the error message will give | ||
| 294 | you a hint by referring to an identifier like | ||
| 295 | <CODE>dont_use_sprintf_use_snprintf</CODE>. | ||
| 296 | <A NAME="Project Documentation"></A> | ||
| 297 | <HR SIZE="6"> | ||
| 298 | <TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0> | ||
| 299 | <TR><TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos_6.html#SEC89"> << </A>]</TD> | ||
| 300 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos_7.html#SEC93"> >> </A>]</TD> | ||
| 301 | <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT"> <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos.html#SEC_Top">Top</A>]</TD> | ||
| 302 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos.html#SEC_Contents">Contents</A>]</TD> | ||
| 303 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[Index]</TD> | ||
| 304 | <TD VALIGN="MIDDLE" ALIGN="LEFT">[<A HREF="pintos_abt.html#SEC_About"> ? </A>]</TD> | ||
| 305 | </TR></TABLE> | ||
| 306 | <BR> | ||
| 307 | <FONT SIZE="-1"> | ||
| 308 | This document was generated | ||
| 309 | by on <I>March, 6 2012</I> | ||
| 310 | using <A HREF="http://texi2html.cvshome.org"><I>texi2html</I></A> | ||
| 311 | </FONT> | ||
| 312 | |||
| 313 | </BODY> | ||
| 314 | </HTML> | ||
