Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

511 lines
16 KiB

  1. Project Coding Conventions
  2. ***** Declarations, Naming and Hungarian
  3. The following Hungarian conventions are used in the tree, as a minimum.
  4. Other conventions may be added to these, but should not change the meaning of
  5. any of these conventions. If a variable does not start with Hungarian,
  6. it should begin with a capital letter (Foo) to so indicate. Otherwise,
  7. the first letter after the Hungarian should be capitalized (pfnFoo).
  8. These prefixes are ordered in the order of their precedence (So m_ is
  9. before p is before fn, so you write m_pfnFoo).
  10. Prefix Meaning Why
  11. -------------------------------------------------------------------------------
  12. g_ Global Prefix of global variables.
  13. m_ Member of Class All methods of class will have these variables
  14. always in scope. They need to be different to
  15. protect the namespace in methods.
  16. p Pointer Always helpful to know if variable is
  17. pointer or pointer pointer (pp)
  18. h Handle Denotes a reference to memory that cannot be
  19. directly interpreted as a pointer
  20. b Bool To distinguish TRUE/FALSE containing variable
  21. from other integers
  22. c Count (signed integer) To indicate that this integer contains a count
  23. of something. Is signed so (--count < 0) tests
  24. can be used.
  25. f Floating point (single) To indicate native floating point type.
  26. d Floating point (double)
  27. fn Function Usually with pfn to indicate a pointer to a
  28. function.
  29. i Signed integer i and u are used to distinguish signed
  30. and unsigned integers
  31. o Offset Used to flag memory offsets. Useful in memory
  32. structures in DSP and Chip simulations.
  33. s String s and sz distinguish between non-zero
  34. terminated and zero terminated strings.
  35. sz Zero Terminated String
  36. u Unsigned integer
  37. 'p' should be used in conjunction with a base type tag for pointers to
  38. simple types, so a pointer to an unsigned integer is 'pu' rather than
  39. just 'p'. Complex types do not have tags so pointers to structures or
  40. objects will only have a 'p' prefix.
  41. ***** Differences From Other Hungarian Styles
  42. Many kinds of Hungarian are used around the company, ranging from very
  43. strict to very relaxed. The Hungarian described here is fairly relaxed and
  44. the following Hungarian conventions are not used:
  45. There is no distinction between far and near pointers. Pointers are
  46. always 'p'; there is no 'lp'.
  47. Arrays are not distinguished with a special prefix.
  48. int iFoo[7] instead of int aiFoo[7] or int rgiFoo[7].
  49. Functions are not tagged for return type.
  50. int Foo() instead of int iFoo().
  51. Variables holding flags are not specially distinguished.
  52. UINT32 uFlags instead of UINT32 grfFlags or UINT32 fFlags.
  53. Classes and structs do not have tags.
  54. FooClass *pFoo instead of FooClass *pfcFoo.
  55. Examples:
  56. TCHAR g_szDllName[80];
  57. PHANDLE phFile;
  58. PD3DVERTEX pVtx;
  59. C++ const's follow the Hungarian of their type. #define's are all cap's.
  60. Classes are mixed case starting with a capital. Structures are all cap's, with
  61. "tagNAME" used to name the structure, and "NAME" used to name the typedef:
  62. typedef struct tagCHUNKELEM
  63. {
  64. struct tagCHUNKELEM *pNext;
  65. EscNodeBase *pNode;
  66. EscMatrix *pMatrix;
  67. } CHUNKELEM, *PCHUNKELEM;
  68. Structure types should always have a P<struct> typedef associated with
  69. them and P<type> should be used in favor of <type> *.
  70. ***** Standard Types
  71. A set of standard types of known size has been defined. These types
  72. should be used for any data element that must be a particular size.
  73. typedef signed char INT8;
  74. typedef short int INT16;
  75. typedef long int INT32;
  76. typedef __int64 INT64;
  77. typedef unsigned char UINT8;
  78. typedef unsigned short int UINT16;
  79. typedef unsigned long int UINT32;
  80. typedef unsigned __int64 UINT64;
  81. typedef float FLOAT;
  82. typedef double DOUBLE;
  83. If a data element does not require a particular size then the standard
  84. INT and UINT types can be used. FLOAT and DOUBLE are IEEE standard formats
  85. and take 32 and 64 bits, respectively.
  86. The standard Win32 TCHAR should be used for strings where the character
  87. set can be ANSI or Unicode. __TEXT() can be used to declare TCHAR
  88. strings and the CRT file tchar.h has many standard routines #defined for
  89. Unicode and ANSI builds.
  90. ***** Comments for Files, Functions and Code
  91. Comments should use // instead of /* */ unless an in-line comment is
  92. absolutely necessary.
  93. All files must have a descriptive header comment which gives information
  94. on the contents of the file and any other file-specific information.
  95. C/C++ style:
  96. //-----------------------------------------------------------------------------
  97. //
  98. // This file contains code dealing with foo and also bar.
  99. //
  100. // Copyright (C) Microsoft Corporation, 1997.
  101. //
  102. //-----------------------------------------------------------------------------
  103. Assembly style is the same except with ; instead of //.
  104. ;------------------------------------------------------------------------------
  105. ;
  106. ; This file has optimized routines for foo.
  107. ;
  108. ; Copyright (C) Microsoft Corporation, 1997.
  109. ;
  110. ;------------------------------------------------------------------------------
  111. All functions and methods must have a header comment giving an overview
  112. of what the function does, what its inputs are and what its outputs are.
  113. C/C++ style:
  114. //-----------------------------------------------------------------------------
  115. //
  116. // CreateFoo
  117. //
  118. // Takes a bar and a baz and creates a foo from them. If the foo is
  119. // successfully created it is returned and the baz is updated to
  120. // reflect the new count of foos using it.
  121. //
  122. //-----------------------------------------------------------------------------
  123. Assembly style has ; instead of // and more information about exactly
  124. how the code is entered and left. If the function is declared with
  125. arguments then no explicit documentation is necessary in the header, but
  126. if arguments are passed in registers then the header should indicate
  127. this. Return values are assumed to be in eax unless otherwise specified.
  128. ;------------------------------------------------------------------------------
  129. ;
  130. ; CreateFoo
  131. ;
  132. ; Takes a bar and a baz and creates a foo from them. If the foo is
  133. ; successfully created it is returned and the baz is updated to
  134. ; reflect the new count of foos using it.
  135. ;
  136. ; Expects bar in eax and baz in ebx.
  137. ; Destroys ecx and edx.
  138. ;
  139. ;------------------------------------------------------------------------------
  140. All code should have at least a simple comment for each basic block and
  141. more comments as necessary. Assembly code should be heavily commented
  142. with notes on register and FP stack usage, unusual tricks, instruction
  143. pairing issues, etc.
  144. Classes should generally have a function-like comment describing the
  145. reason for their existence and an overview of their interface. Trivial
  146. classes do not need a comment. Structs generally need only a mention of
  147. their use, although if they have complicated features or methods they
  148. may need a class-like header.
  149. ***** Microsoft Internal
  150. Any code or comments which are for internal use only are bracketed as
  151. follows. The brackets and text are stripped for the file versions which
  152. are made public.
  153. //@@BEGIN_MSINTERNAL
  154. microsoft-only stuff here
  155. //@@END_MSINTERNAL
  156. ***** File Naming
  157. We'll be using a mix of C and C++ so we need to distinguish files
  158. that are C-compatible from those that are not.
  159. Use .c for plain C files.
  160. Use .h for plain C headers or headers which can be included safely in
  161. plain C files. This includes headers which have C++ constructs
  162. protected by ifdef __cplusplus. .h files that may be included in C++
  163. code should have internal extern "C" blocks.
  164. Use .cpp for C++ files.
  165. Use .hpp for C++-only headers.
  166. ***** General Code Style
  167. Lines should not exceed eighty columns. This is much more pleasant for
  168. those of us that do not use large windows.
  169. Functions should be fewer than one hundred lines long unless structure
  170. or performance requires a longer function. In general functions should
  171. be even shorter, with sub-functions created as necessary.
  172. Any commented-out code must have an explanatory comment with it
  173. describing why it is commented out but not removed entirely.
  174. Global variables should be avoided unless absolutely necessary.
  175. Use uSize members of structs that may need to be resized later with
  176. backwards compatibility. When using uSize make sure to validate sizes
  177. everywhere to force callers to use it properly.
  178. Headers should be protected with #ifndef <file> #define <file> #endif
  179. blocks for inclusion safety.
  180. ***** C++ Usage Guidelines
  181. C++ has a lot of things in it which can make code more difficult to read
  182. or slower. The following is a list of suggestions on things to
  183. avoid. These rules can be broken if necessary.
  184. Avoid overloading operators.
  185. Avoid default parameters.
  186. Avoid multiple inheritance.
  187. Avoid exceptions. This means that constructors cannot fail since they
  188. can't return anything. If an object requires initialization that can
  189. fail the constructor should put the object in an invalid state and a
  190. method called Initialize should be used to fully create the object.
  191. Avoid public data members. Use accessor methods instead.
  192. Avoid placing method bodies inline in class declarations. Bodies of
  193. inline methods should follow the class declaration in the header file.
  194. Avoid naming conflicts with functions so that :: is not needed.
  195. ***** C/C++ Code Style
  196. Indentation and spacing should be done entirely with spaces; no tabs
  197. should be used. One indentation level is four spaces.
  198. Braces should be on separate lines and aligned with each other. All
  199. statements must be bracketed even if they are only a single line.
  200. Case statements should align with the brackets of the switch. break
  201. statements in case statements should be indented once from the case.
  202. Functions should be declared so that their name begins on the first
  203. character of a line. Return types, calling conventions and other
  204. decorations should be on a preceeding line. Arguments can immediately
  205. follow the name.
  206. Functions should always declare a return type and use (void) if they
  207. have no arguments.
  208. Complete Example:
  209. foo.hpp:
  210. //-----------------------------------------------------------------------------
  211. //
  212. // This file contains the declaration of the Foo class.
  213. //
  214. // Copyright (C) Microsoft Corporation, 1997.
  215. //
  216. //-----------------------------------------------------------------------------
  217. #ifndef _FOO_HPP_
  218. #define _FOO_HPP_
  219. // Flags for the Foo class.
  220. #define FOO_INVALID 0x00000001
  221. #define FOO_MATCH 0x00000002
  222. // Special numbers for checking.
  223. #define FOO_RETURN_NEGONE 0xfefefefe
  224. #define FOO_DO_NOTHING 0xefefefef
  225. //-----------------------------------------------------------------------------
  226. //
  227. // class Foo
  228. //
  229. // The Foo class is responsible for keeping a buffer of things to check.
  230. //
  231. //-----------------------------------------------------------------------------
  232. class Foo
  233. {
  234. public:
  235. Foo(void);
  236. BOOL Initialize(int cElts);
  237. ~Foo(void);
  238. int Check(int iCheck);
  239. inline UINT32 GetFlags(void);
  240. private:
  241. INT m_cElts;
  242. INT m_cUsed;
  243. PINT m_piBuffer;
  244. UINT32 m_uFlags;
  245. };
  246. //-----------------------------------------------------------------------------
  247. //
  248. // Foo::GetFlags
  249. //
  250. // Accessor method for a Foo's flags.
  251. //
  252. //-----------------------------------------------------------------------------
  253. inline UINT32
  254. GetFlags(void)
  255. {
  256. return m_uFlags;
  257. }
  258. #endif // _FOO_HPP_
  259. foo.cpp:
  260. //-----------------------------------------------------------------------------
  261. //
  262. // This file contains the implementation of the Foo class.
  263. //
  264. // Copyright (C) Microsoft Corporation, 1997.
  265. //
  266. //-----------------------------------------------------------------------------
  267. #include "foo.hpp"
  268. //-----------------------------------------------------------------------------
  269. //
  270. // Foo::Foo
  271. //
  272. // Initializes a Foo to an invalid state.
  273. //
  274. //-----------------------------------------------------------------------------
  275. Foo::Foo(void)
  276. {
  277. m_piBuffer = NULL;
  278. m_uFlags = FOO_INVALID;
  279. m_cElts = 0;
  280. m_cUsed = 0;
  281. }
  282. //-----------------------------------------------------------------------------
  283. //
  284. // Foo::Initialize
  285. //
  286. // Constructs a Foo.
  287. //
  288. //-----------------------------------------------------------------------------
  289. BOOL
  290. Foo::Initialize(int cElts)
  291. {
  292. // Allocate a buffer.
  293. m_piBuffer = new int[cElts];
  294. if (m_piBuffer == NULL)
  295. {
  296. return FALSE;
  297. }
  298. m_cElts = cElts;
  299. m_uFlags = 0;
  300. return TRUE;
  301. }
  302. //-----------------------------------------------------------------------------
  303. //
  304. // Foo::~Foo
  305. //
  306. // Cleans up a Foo.
  307. //
  308. //-----------------------------------------------------------------------------
  309. Foo::~Foo(void)
  310. {
  311. delete m_piBuffer;
  312. }
  313. //-----------------------------------------------------------------------------
  314. //
  315. // Foo::Check
  316. //
  317. // Checks for the given number in the buffer. If a match is found,
  318. // return the index of the match. If no match is found, add the number
  319. // to the buffer and return its index. If the buffer overflows, -1 is
  320. // returned.
  321. //
  322. // If the number is one of the special numbers, return its special code.
  323. //
  324. //-----------------------------------------------------------------------------
  325. int
  326. Foo::Check(int iCheck)
  327. {
  328. int i;
  329. // Check for initialization.
  330. if (m_uFlags & FOO_INVALID)
  331. {
  332. return -1;
  333. }
  334. // Check for a special number.
  335. switch(iCheck)
  336. {
  337. case FOO_RETURN_NEGONE:
  338. return -1;
  339. case FOO_DO_NOTHING:
  340. break;
  341. default:
  342. break;
  343. }
  344. // Look for an existing match.
  345. for (i = 0; i < m_cUsed; i++)
  346. {
  347. if (m_piBuffer[i] == iCheck)
  348. {
  349. m_uFlags |= FOO_MATCH;
  350. return i;
  351. }
  352. }
  353. // No match was found, so add the new number if there's space.
  354. if (m_cUsed == m_cElts)
  355. {
  356. return -1;
  357. }
  358. m_piBuffer[m_cUsed] = iCheck;
  359. return m_cUsed++;
  360. }
  361. ***** Assembly Style
  362. All assembly ops and registers should be in lower case. Each line is
  363. indented eight spaces from the left margin. Assembler directives and
  364. macros are always in uppercase.
  365. Complete Example:
  366. ;------------------------------------------------------------------------------
  367. ;
  368. ; This file has optimized routines for foo.
  369. ;
  370. ; Copyright (C) Microsoft Corporation, 1997.
  371. ;
  372. ;------------------------------------------------------------------------------
  373. .386p
  374. .MODEL FLAT
  375. .CODE
  376. ;------------------------------------------------------------------------------
  377. ;
  378. ; Foo
  379. ;
  380. ; Takes a pointer to a bar and returns the total bit count for it and its
  381. ; successor.
  382. ;
  383. ;------------------------------------------------------------------------------
  384. Foo PROC PUBLIC,
  385. pBar:PBAR
  386. LOCAL cBits:DWORD
  387. ; Get bit count for current bar.
  388. mov ecx, pBar
  389. mov eax, [ecx+BAR_cBits]
  390. mov cBits, eax
  391. ; Get successor bar in ecx.
  392. push ecx
  393. call UpdateBar
  394. mov ecx, eax
  395. ; Sum bit counts for return.
  396. mov eax, cBits
  397. add eax, [ecx+BAR_cBits]
  398. ret
  399. Foo ENDP
  400. END
  401. ***** Asserts, Debug Output and Other Debugging Aids
  402. Assert checks should be placed where appropriate. These should be used
  403. where unexpected but still possible bad things can happen.
  404. Note that parameter validation will occur in the D3D runtime, so checking
  405. of state and items like vertex type is not necessary.
  406. We will use the new DX DPF support defined in newdpf.h.