a1c70a47961a69baac2f0bfe906c15870c88677d
[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.  If your
43 code is too ugly, it will cost you points.
44
45 Please limit C source file lines to at most 79 characters long.
46
47 Pintos comments sometimes refer to external standards or
48 specifications by writing a name inside square brackets, like this:
49 @code{[IA32-v3]}.  These names refer to the reference names used in
50 this documentation (@pxref{References}).
51
52 If you remove existing Pintos code, please delete it from your source
53 file entirely.  Don't just put it into a comment or a conditional
54 compilation directive, because that makes the resulting code hard to
55 read.  We're only going to do a compile in the directory for the current
56 project, so you don't need to make sure that the previous projects also
57 compile.
58
59 @node Conditional Compilation
60 @section Conditional Compilation
61
62 Given the scope and complexity of your assignments this quarter, you
63 may find it convenient while coding and debugging (and we will find it
64 convenient while grading) to be able to independently turn different
65 parts of the assignments on and off.  To do this, choose a macro name
66 and use it in conditional
67 compilation directives, e.g.:
68
69 @example
70 #ifdef @var{NAME}
71 @dots{}your code@dots{}
72 #endif
73 @end example
74
75 In general, the code that you turn in must not depend on conditional
76 compilation directives.  Project code should be written so that all of
77 the subproblems for the project function together, and it should
78 compile properly without the need for any new macros to be defined.
79 There are a few exceptions:
80
81 @itemize @bullet
82 @item
83 Problem 1-3, the advanced scheduler.  We must be able to turn this on
84 and off with a compile-time directive.  You must use the macro name we
85 specify for that part.  @xref{Problem 1-3 Advanced Scheduler}, for
86 details.
87
88 @item
89 Problem 3-2, paging to and from disk.  Your page replacement policy must
90 default to LRU-like replacement, but we must be able to choose a random
91 replacement policy with a compile-time directive.  You must use the
92 macro name we specify for that part.  @xref{Problem 3-2 Paging To and
93 From Disk}, for details.
94
95 @item
96 Code written for extra credit may be included conditionally.  If the
97 extra credit code changes the normally expected functionality of the
98 code, then it @emph{must} be included conditionally, and it must not
99 be enabled by default.
100 @end itemize
101
102 You can use @file{constants.h} in @file{pintos/src} to define macros
103 for conditional compilation.  We will replace the @file{constants.h}
104 that you supply with one of our own when we test your code, so do not
105 define anything important in it.
106
107 @node C99
108 @section C99
109
110 The Pintos source code uses a few features of the ``C99'' standard
111 library that were not in the original 1989 standard for C.  Because
112 they are so new, most classes do not cover these features, so this
113 section will describe them.  The new features used in Pintos are
114 mostly in new headers:
115
116 @table @file
117 @item <stdbool.h>
118 Defines macros @code{bool}, a 1-bit type that takes on only the values
119 0 and 1, @code{true}, which expands to 1, and @code{false}, which
120 expands to 0.
121
122 @item <stdint.h>
123 On systems that support them, this header defines types
124 @code{int@var{n}_t} and @code{uint@var{n}_t} for @var{n} = 8, 16, 32,
125 64, and possibly others.  These are 2's complement signed and unsigned
126 types, respectively, with the given number of bits.  
127
128 On systems where it is possible, this header also defines types
129 @code{intptr_t} and @code{uintptr_t}, which are integer types big
130 enough to hold a pointer.
131
132 On all systems, this header defines types @code{intmax_t} and
133 @code{uintmax_t}, which are the system's signed and unsigned integer
134 types with the widest ranges.
135
136 For every signed integer type @code{@var{type}_t} it defines, as well
137 as for @code{ptrdiff_t} defined in @file{<stddef.h>}, this header also
138 defines macros @code{@var{type}_MAX} and @code{@var{type}_MIN} that
139 give the type's range.  Similarly, for every unsigned integer type
140 @code{@var{type}_t} defined here, as well as for @code{size_t} defined
141 in @file{<stddef.h>}, this header defines a @code{@var{type}_MAX}
142 macro giving its maximum value.
143
144 @item <inttypes.h>
145 @file{<stdint.h>} is useful on its own, but it provides no way to pass
146 the types it defines to @func{printf} and related functions.  This
147 header provides macros to help with that.  For every
148 @code{int@var{n}_t} defined by @file{<stdint.h>}, it provides macros
149 @code{PRId@var{n}} and @code{PRIi@var{n}} for formatting values of
150 that type with @code{"%d"} and @code{"%i"}.  Similarly, for every
151 @code{uint@var{n}_t}, it provides @code{PRIo@var{n}},
152 @code{PRIu@var{n}}, @code{PRIu@var{x}}, and @code{PRIu@var{X}}.
153
154 You use these something like this, taking advantage of the fact that
155 the C compiler concatenates adjacent string literals:
156 @example
157 #include <inttypes.h>
158 @dots{}
159 int32_t value = @dots{};
160 printf ("value=%08"PRId32"\n", value);
161 @end example
162 @noindent
163 The @samp{%} is not supplied by the @code{PRI} macros.  As shown
164 above, you supply it yourself and follow it by any flags, field
165 widths, etc.
166
167 @item <stdio.h>
168 The @func{printf} function has some new type modifiers for printing
169 standard types:
170
171 @table @samp
172 @item j
173 For @code{intmax_t} (e.g.@: @samp{%jd}) or @code{uintmax_t} (e.g.@:
174 @samp{%ju}).
175
176 @item z
177 For @code{size_t} (e.g.@: @samp{%zu}).
178
179 @item t
180 For @code{ptrdiff_t} (e.g.@: @samp{%td}).
181 @end table
182
183 Pintos @func{printf} also implements a nonstandard @samp{'} flag that
184 group large numbers with commas to make them easier to read.
185 @end table
186
187 @node Unsafe String Functions
188 @section Unsafe String Functions
189
190 A few of the string functions declared in the standard
191 @file{<string.h>} and @file{<stdio.h>} headers are notoriously unsafe.
192 The worst offenders are intentionally not included in the Pintos C
193 library:
194
195 @table @func
196 @item strcpy
197 When used carelessly this function can overflow the buffer reserved
198 for its output string.  Use @func{strlcpy} instead.  Refer to
199 comments in its source code in @code{lib/string.c} for documentation.
200
201 @item strncpy
202 This function can leave its destination buffer without a null string
203 terminator and it has performance problems besides.  Again, use
204 @func{strlcpy}.
205
206 @item strcat
207 Same issue as @func{strcpy}.  Use @func{strlcat} instead.
208 Again, refer to comments in its source code in @code{lib/string.c} for
209 documentation.
210
211 @item strncat
212 The meaning of its buffer size argument often leads to problems.
213 Again, use @func{strlcat}.
214
215 @item strtok
216 Uses global data, so it is unsafe in threaded programs such as
217 kernels.  Use @func{strtok_r} instead, and see its source code in
218 @code{lib/string.c} for documentation and an example.
219
220 @item sprintf
221 Same issue as @func{strcpy}.  Use @func{snprintf} instead.  Refer
222 to comments in @code{lib/stdio.h} for documentation.
223
224 @item vsprintf
225 Same issue as @func{strcpy}.  Use @func{vsnprintf} instead.
226 @end table
227
228 If you try to use any of these functions, you should get a hint from
229 the error message, which will refer to an identifier like
230 @code{dont_use_sprintf_use_snprintf}.