1 @node Coding Standards, Project Documentation, Multilevel Feedback Scheduling, Top
2 @appendix Coding Standards
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.
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.
23 * Conditional Compilation::
25 * Unsafe String Functions::
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.
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.
45 Pintos comments sometimes refer to external standards or
46 specifications by writing a name inside square brackets, like this:
47 @code{[IA32-v3]}. These names refer to the reference names used in
48 this documentation (@pxref{References}).
50 If you remove existing Pintos code, please delete it from your source
51 file entirely. Don't just put it into a comment or a conditional
52 compilation directive, because that makes the resulting code hard to
53 read. If you're worried about
55 @node Conditional Compilation
56 @section Conditional Compilation
58 Given the scope and complexity of your assignments this quarter, you
59 may find it convenient while coding and debugging (and we will find it
60 convenient while grading) to be able to independently turn different
61 parts of the assignments on and off. To do this, choose a macro name
62 and use it in conditional
63 compilation directives, e.g.:
67 @dots{}your code@dots{}
71 In general, the code that you turn in must not depend on conditional
72 compilation directives. Project code should be written so that all of
73 the subproblems for the project function together, and it should
74 compile properly without the need for any new macros to be defined.
75 There are a few exceptions:
79 Problem 1-2, @func{thread_join}. Some other code expects
80 @code{THREAD_JOIN_IMPLEMENTED} to be defined once you've implemented
84 Problem 1-4, the advanced scheduler. We must be able to turn this on
85 and off with a compile-time directive. You must use the macro name we
86 specify for that part. @xref{Problem 1-4 Advanced Scheduler}, for
90 Problem 3-2, paging to and from disk. Your page replacement policy must
91 default to LRU-like replacement, but we must be able to choose a random
92 replacement policy with a compile-time directive. You must use the
93 macro name we specify for that part. @xref{Problem 3-2 Paging To and
94 From Disk}, for details.
97 Code written for extra credit may be included conditionally. If the
98 extra credit code changes the normally expected functionality of the
99 code, then it @emph{must} be included conditionally, and it must not
100 be enabled by default.
103 You can use @file{constants.h} in @file{pintos/src} to define macros
104 for conditional compilation. We will replace the @file{constants.h}
105 that you supply with one of our own when we test your code, so do not
106 define anything important in it.
111 The Pintos source code uses a few features of the ``C99'' standard
112 library that were not in the original 1989 standard for C. Because
113 they are so new, most classes do not cover these features, so this
114 section will describe them. The new features used in Pintos are
115 mostly in new headers:
119 Defines macros @code{bool}, a 1-bit type that takes on only the values
120 0 and 1, @code{true}, which expands to 1, and @code{false}, which
124 On systems that support them, this header defines types
125 @code{int@var{n}_t} and @code{uint@var{n}_t} for @var{n} = 8, 16, 32,
126 64, and possibly others. These are 2's complement signed and unsigned
127 types, respectively, with the given number of bits.
129 On systems where it is possible, this header also defines types
130 @code{intptr_t} and @code{uintptr_t}, which are integer types big
131 enough to hold a pointer.
133 On all systems, this header defines types @code{intmax_t} and
134 @code{uintmax_t}, which are the system's signed and unsigned integer
135 types with the widest ranges.
137 For every signed integer type @code{@var{type}_t} it defines, as well
138 as for @code{ptrdiff_t} defined in @file{<stddef.h>}, this header also
139 defines macros @code{@var{type}_MAX} and @code{@var{type}_MIN} that
140 give the type's range. Similarly, for every unsigned integer type
141 @code{@var{type}_t} defined here, as well as for @code{size_t} defined
142 in @file{<stddef.h>}, this header defines a @code{@var{type}_MAX}
143 macro giving its maximum value.
146 @file{<stdint.h>} is useful on its own, but it provides no way to pass
147 the types it defines to @func{printf} and related functions. This
148 header provides macros to help with that. For every
149 @code{int@var{n}_t} defined by @file{<stdint.h>}, it provides macros
150 @code{PRId@var{n}} and @code{PRIi@var{n}} for formatting values of
151 that type with @code{"%d"} and @code{"%i"}. Similarly, for every
152 @code{uint@var{n}_t}, it provides @code{PRIo@var{n}},
153 @code{PRIu@var{n}}, @code{PRIu@var{x}}, and @code{PRIu@var{X}}.
155 You use these something like this, taking advantage of the fact that
156 the C compiler concatenates adjacent string literals:
158 #include <inttypes.h>
160 int32_t value = @dots{};
161 printf ("value=%08"PRId32"\n", value);
164 The @samp{%} is not supplied by the @code{PRI} macros. As shown
165 above, you supply it yourself and follow it by any flags, field
169 The @func{printf} function has some new type modifiers for printing
174 For @code{intmax_t} (e.g.@: @samp{%jd}) or @code{uintmax_t} (e.g.@:
178 For @code{size_t} (e.g.@: @samp{%zu}).
181 For @code{ptrdiff_t} (e.g.@: @samp{%td}).
184 Pintos @func{printf} also implements a nonstandard @samp{'} flag that
185 group large numbers with commas to make them easier to read.
188 @node Unsafe String Functions
189 @section Unsafe String Functions
191 A few of the string functions declared in the standard
192 @file{<string.h>} and @file{<stdio.h>} headers are notoriously unsafe.
193 The worst offenders are intentionally not included in the Pintos C
198 When used carelessly this function can overflow the buffer reserved
199 for its output string. Use @func{strlcpy} instead. Refer to
200 comments in its source code in @code{lib/string.c} for documentation.
203 This function can leave its destination buffer without a null string
204 terminator and it has performance problems besides. Again, use
208 Same issue as @func{strcpy}, but substitute @func{strlcat}.
209 Again, refer to comments in its source code in @code{lib/string.c} for
213 The meaning of its buffer size argument often leads to problems.
214 Again, use @func{strlcat}.
217 Uses global data, so it is unsafe in threaded programs such as
218 kernels. Use @func{strtok_r} instead, and see its source code in
219 @code{lib/string.c} for documentation and an example.
222 Same issue as @func{strcpy}. Use @func{snprintf} instead. Refer
223 to comments in @code{lib/stdio.h} for documentation.
226 Same issue as @func{strcpy}. Use @func{vsnprintf} instead.
229 If you try to use any of these functions, you should get a hint from
230 the error message, which will refer to an identifier like
231 @code{dont_use_sprintf_use_snprintf}.