Update docs.
[pintos-anon] / doc / standards.texi
1 @node Coding Standards, Project Documentation, Multilevel Feedback Scheduling, Top
2 @appendix Coding Standards
3
4 All of you should have taken a class like CS 107, so we expect you to
5 be familiar with some set of coding standards such as
6 @uref{http://www.stanford.edu/class/cs140/projects/misc/CodingStandards.pdf,
7 , CS 107 Coding Standards}. Even if you've taken 107, we recommend
8 reviewing that document.  We expect code at the "Peer-Review Quality"
9 level as described there.
10
11 Our standards for coding are mostly important in grading.  More
12 information on our grading methodology can be found on the Course Info
13 page and the Grading page.  We also want to stress that aside from the
14 fact that we are explicitly basing part of your grade on these things,
15 good coding practices will improve the quality of your code.  This
16 makes it easier for your partners to interact with it, and ultimately,
17 will improve your chances of having a good working program.  That said
18 once, the rest of this document will discuss only the ways in which
19 our coding standards will affect our grading.
20
21 @menu
22 * Coding Style::                
23 * Conditional Compilation::     
24 * C99::                         
25 * Unsafe String Functions::     
26 @end menu
27
28 @node Coding Style
29 @section Style
30
31 Style, for the purposes of our grading, refers to how readable your
32 code is.  At minimum, this means that your code is well formatted, your
33 variable names are descriptive and your functions are decomposed and
34 well commented.  Any other factors which make it hard (or easy) for us
35 to read or use your code will be reflected in your style grade.
36
37 The existing Pintos code is written in the GNU style and largely
38 follows the @uref{http://www.gnu.org/prep/standards_toc.html, , GNU
39 Coding Standards}.  We encourage you to follow the applicable parts of
40 them too, especially chapter 5, ``Making the Best Use of C.''  Using a
41 different style won't cause actual problems, but it's ugly to see
42 gratuitous differences in style from one function to another.
43
44 @node Conditional Compilation
45 @section Conditional Compilation
46
47 Given the scope and complexity of your assignments this quarter, you
48 may find it convenient while coding and debugging (and we will find it
49 convenient while grading) to be able to independently turn different
50 parts of the assignments on and off.  To do this, choose a macro name
51 and use it in conditional
52 compilation directives, e.g.:
53
54 @example
55 #ifdef @var{NAME}
56 @dots{}your code@dots{}
57 #endif
58 @end example
59
60 In general, the code that you turn in must not depend on conditional
61 compilation directives.  Project code should be written so that all of
62 the subproblems for the project function together, and it should
63 compile properly without the need for any new macros to be defined.
64 There are a few exceptions:
65
66 @itemize @bullet
67 @item
68 Problem 1-4, the advanced scheduler.  We must be able to turn this on
69 and off with a compile time directive.  You must use the macro name we
70 specify for that part.  @xref{Problem 1-4 Advanced Scheduler}, for
71 details.
72
73 @item
74 Code written for extra credit may be included conditionally.  If the
75 extra credit code changes the normally expected functionality of the
76 code, then it @emph{must} be included conditionally, and it must not
77 be enabled by default.
78 @end itemize
79
80 You can use @file{constants.h} in @file{pintos/src} to define macros
81 for conditional compilation.  We will replace the @file{constants.h}
82 that you supply with one of our own when we test your code, so do not
83 define anything important in it.
84
85 @node C99
86 @section C99
87
88 The Pintos source code uses a few features of the ``C99'' standard
89 library that were not in the original 1989 standard for C.  Because
90 they are so new, most classes do not cover these features, so this
91 section will describe them.  The new features used in Pintos are
92 mostly in new headers:
93
94 @table @file
95 @item <stdbool.h>
96 Defines macros @code{bool}, a 1-bit type that takes on only the values
97 0 and 1, @code{true}, which expands to 1, and @code{false}, which
98 expands to 0.
99
100 @item <stdint.h>
101 On systems that support them, this header defines types
102 @code{int@var{n}_t} and @code{uint@var{n}_t} for @var{n} = 8, 16, 32,
103 64, and possibly others.  These are 2's complement signed and unsigned
104 types, respectively, with the given number of bits.  
105
106 On systems where it is possible, this header also defines types
107 @code{intptr_t} and @code{uintptr_t}, which are integer types big
108 enough to hold a pointer.
109
110 On all systems, this header defines types @code{intmax_t} and
111 @code{uintmax_t}, which are the system's signed and unsigned integer
112 types with the widest ranges.
113
114 For every signed integer type @code{@var{type}_t} it defines, as well
115 as for @code{ptrdiff_t} defined in @file{<stddef.h>}, this header also
116 defines macros @code{@var{type}_MAX} and @code{@var{type}_MIN} that
117 give the type's range.  Similarly, for every unsigned integer type
118 @code{@var{type}_t} defined here, as well as for @code{size_t} defined
119 in @file{<stddef.h>}, this header defines a @code{@var{type}_MAX}
120 macro giving its maximum value.
121
122 @item <inttypes.h>
123 @file{<stdint.h>} is useful on its own, but it provides no way to pass
124 the types it defines to @code{printf()} and related functions.  This
125 header provides macros to help with that.  For every
126 @code{int@var{n}_t} defined by @file{<stdint.h>}, it provides macros
127 @code{PRId@var{n}} and @code{PRIi@var{n}} for formatting values of
128 that type with @code{"%d"} and @code{"%i"}.  Similarly, for every
129 @code{uint@var{n}_t}, it provides @code{PRIo@var{n}},
130 @code{PRIu@var{n}}, @code{PRIu@var{x}}, and @code{PRIu@var{X}}.
131
132 You use these something like this, taking advantage of the fact that
133 the C compiler concatenates adjacent string literals:
134 @example
135 #include <inttypes.h>
136 @dots{}
137 int32_t value = @dots{};
138 printf ("value=%08"PRId32"\n");
139 @end example
140 @noindent
141 The @samp{%} is not supplied by the @code{PRI} macros.  As shown
142 above, you supply it yourself and follow it by any flags, field
143 widths, etc.
144
145 @item <stdio.h>
146 The @file{printf()} function has some new type modifiers for printing
147 standard types:
148
149 @table @samp
150 @item j
151 For @code{intmax_t} (e.g.@: @samp{%jd}) or @code{uintmax_t} (e.g.@:
152 @samp{%ju}).
153
154 @item z
155 For @code{size_t} (e.g.@: @samp{%zu}).
156
157 @item t
158 For @code{ptrdiff_t} (e.g.@: @samp{%td}).
159 @end table
160 @end table
161
162 @node Unsafe String Functions
163 @section Unsafe String Functions
164
165 A few of the string functions declared in the standard
166 @file{<string.h>} and @file{<stdio.h>} headers are notoriously unsafe.
167 The worst offenders are intentionally not included in the Pintos C
168 library:
169
170 @table @code
171 @item strcpy()
172 When used carelessly this function can overflow the buffer reserved
173 for its output string.  Use @code{strlcpy()} instead.  Refer to
174 comments in its source code in @code{lib/string.c} for documentation.
175
176 @item strncpy()
177 This function can leave its destination buffer without a null string
178 terminator and it has performance problems besides.  Again, use
179 @code{strlcpy()}.
180
181 @item strcat()
182 Same issue as @code{strcpy()}, but substitute @code{strlcat()}.
183 Again, refer to comments in its source code in @code{lib/string.c} for
184 documentation.
185
186 @item strncat()
187 The meaning of its buffer size argument often leads to problems.
188 Again, use @code{strlcat()}.
189
190 @item strtok()
191 Uses global data, so it is unsafe in threaded programs such as
192 kernels.  Use @code{strtok_r()} instead, and see its source code in
193 @code{lib/string.c} for documentation and an example.
194
195 @item sprintf()
196 Same issue as @code{strcpy()}.  Use @code{snprintf()} instead.  Refer
197 to comments in @code{lib/stdio.h} for documentation.
198
199 @item vsprintf()
200 Same issue as @code{strcpy()}.  Use @code{vsnprintf()} instead.
201 @end table
202
203 If you try to use any of these functions, you should get a hint from
204 the error message, which will refer to an identifier like
205 @code{dont_use_sprintf_use_snprintf}.