1 @node Coding Standards, Project Documentation, 4.4BSD Scheduler, Top
2 @appendix Coding Standards
4 All of you should have taken a class like CS 107, so we expect you to be
5 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''
11 Our standards for coding are most important for grading. We want to
12 stress that aside from the fact that we are explicitly basing part of
13 your grade on these things, good coding practices will improve the
14 quality of your code. This makes it easier for your partners to
15 interact with it, and ultimately, will improve your chances of having a
16 good working program. That said once, the rest of this document will
17 discuss only the ways in which our coding standards will affect our
23 * Unsafe String Functions::
29 Style, for the purposes of our grading, refers to how readable your
30 code is. At minimum, this means that your code is well formatted, your
31 variable names are descriptive and your functions are decomposed and
32 well commented. Any other factors which make it hard (or easy) for us
33 to read or use your code will be reflected in your style grade.
35 The existing Pintos code is written in the GNU style and largely
36 follows the @uref{http://www.gnu.org/prep/standards_toc.html, , GNU
37 Coding Standards}. We encourage you to follow the applicable parts of
38 them too, especially chapter 5, ``Making the Best Use of C.'' Using a
39 different style won't cause actual problems, but it's ugly to see
40 gratuitous differences in style from one function to another. If your
41 code is too ugly, it will cost you points.
43 Please limit C source file lines to at most 79 characters long.
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
55 We're only going to do a compile in the directory for the project being
56 submitted. You don't need to make sure that the previous projects also
59 Project code should be written so that all of the subproblems for the
60 project function together, that is, without the need to rebuild with
61 different macros defined, etc. If you do extra credit work that
62 changes normal Pintos behavior so as to interfere with grading, then
63 you must implement it so that it only acts that way when given a
64 special command-line option of the form @option{-@var{name}}, where
65 @var{name} is a name of your choice. You can add such an option by
66 modifying @func{parse_options} in @file{threads/init.c}.
68 The introduction describes additional coding style requirements
74 The Pintos source code uses a few features of the ``C99'' standard
75 library that were not in the original 1989 standard for C. Many
76 programmers are unaware of these feature, so we will describe them. The
77 new features used in Pintos are
78 mostly in new headers:
82 Defines macros @code{bool}, a 1-bit type that takes on only the values
83 0 and 1, @code{true}, which expands to 1, and @code{false}, which
87 On systems that support them, this header defines types
88 @code{int@var{n}_t} and @code{uint@var{n}_t} for @var{n} = 8, 16, 32,
89 64, and possibly other values. These are 2's complement signed and unsigned
90 types, respectively, with the given number of bits.
92 On systems where it is possible, this header also defines types
93 @code{intptr_t} and @code{uintptr_t}, which are integer types big
94 enough to hold a pointer.
96 On all systems, this header defines types @code{intmax_t} and
97 @code{uintmax_t}, which are the system's signed and unsigned integer
98 types with the widest ranges.
100 For every signed integer type @code{@var{type}_t} defined here, as well
101 as for @code{ptrdiff_t} defined in @file{<stddef.h>}, this header also
102 defines macros @code{@var{TYPE}_MAX} and @code{@var{TYPE}_MIN} that
103 give the type's range. Similarly, for every unsigned integer type
104 @code{@var{type}_t} defined here, as well as for @code{size_t} defined
105 in @file{<stddef.h>}, this header defines a @code{@var{TYPE}_MAX}
106 macro giving its maximum value.
109 @file{<stdint.h>} provides no straightforward way to format
110 the types it defines with @func{printf} and related functions. This
111 header provides macros to help with that. For every
112 @code{int@var{n}_t} defined by @file{<stdint.h>}, it provides macros
113 @code{PRId@var{n}} and @code{PRIi@var{n}} for formatting values of
114 that type with @code{"%d"} and @code{"%i"}. Similarly, for every
115 @code{uint@var{n}_t}, it provides @code{PRIo@var{n}},
116 @code{PRIu@var{n}}, @code{PRIu@var{x}}, and @code{PRIu@var{X}}.
118 You use these something like this, taking advantage of the fact that
119 the C compiler concatenates adjacent string literals:
121 #include <inttypes.h>
123 int32_t value = @dots{};
124 printf ("value=%08"PRId32"\n", value);
127 The @samp{%} is not supplied by the @code{PRI} macros. As shown
128 above, you supply it yourself and follow it by any flags, field
132 The @func{printf} function has some new type modifiers for printing
137 For @code{intmax_t} (e.g.@: @samp{%jd}) or @code{uintmax_t} (e.g.@:
141 For @code{size_t} (e.g.@: @samp{%zu}).
144 For @code{ptrdiff_t} (e.g.@: @samp{%td}).
147 Pintos @func{printf} also implements a nonstandard @samp{'} flag that
148 groups large numbers with commas to make them easier to read.
151 @node Unsafe String Functions
152 @section Unsafe String Functions
154 A few of the string functions declared in the standard
155 @file{<string.h>} and @file{<stdio.h>} headers are notoriously unsafe.
156 The worst offenders are intentionally not included in the Pintos C
161 When used carelessly this function can overflow the buffer reserved
162 for its output string. Use @func{strlcpy} instead. Refer to
163 comments in its source code in @code{lib/string.c} for documentation.
166 This function can leave its destination buffer without a null string
167 terminator. It also has performance problems. Again, use
171 Same issue as @func{strcpy}. Use @func{strlcat} instead.
172 Again, refer to comments in its source code in @code{lib/string.c} for
176 The meaning of its buffer size argument is surprising.
177 Again, use @func{strlcat}.
180 Uses global data, so it is unsafe in threaded programs such as
181 kernels. Use @func{strtok_r} instead, and see its source code in
182 @code{lib/string.c} for documentation and an example.
185 Same issue as @func{strcpy}. Use @func{snprintf} instead. Refer
186 to comments in @code{lib/stdio.h} for documentation.
189 Same issue as @func{strcpy}. Use @func{vsnprintf} instead.
192 If you try to use any of these functions, the error message will give
193 you a hint by referring to an identifier like
194 @code{dont_use_sprintf_use_snprintf}.