Source code of Windows XP (NT5)
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.

135 lines
4.0 KiB

  1. #ifndef LSIDEFS_DEFINED
  2. #define LSIDEFS_DEFINED
  3. /*
  4. Disable non standard comment warning to allow "//" comments since such
  5. comments are in the public headers.
  6. */
  7. #pragma warning (disable : 4001)
  8. /* For ICECAP builds turn off the static directive */
  9. #ifdef ICECAP
  10. #define static
  11. #endif /* ICECAP */
  12. /* Common utility macros shared by all of Line Services
  13. */
  14. #include "lsdefs.h" /* Make external common defines visible internally */
  15. /* ****************************************************************** */
  16. /* Item #1: The Assert() family of macros.
  17. *
  18. * Assert() generates no code for non-debug builds.
  19. * AssertSz() is like Assert(), but you specify a string to print.
  20. * victork: AssertSz() is unaccessible
  21. * AssertBool() validates that its parameter is either "1" or "0".
  22. * AssertDo() checks the result of an expression that is ALWAYS
  23. * compiled, even in non-debug builds.
  24. * AssertErr asserts there is an error with displaying a string
  25. * AssertImplies(a, b) checks that a implies b
  26. */
  27. #ifdef DEBUG
  28. void (WINAPI* pfnAssertFailed)(char*, char*, int);
  29. #define AssertSz(f, sz) \
  30. (void)( (f) || (pfnAssertFailed(sz, __FILE__, __LINE__), 0) )
  31. #define AssertDo(f) Assert((f) != 0)
  32. #else /* !DEBUG */
  33. #define AssertSz(f, sz) (void)(0)
  34. #define AssertDo(f) (f)
  35. #endif /* DEBUG */
  36. #define AssertEx(f) AssertSz((f), "!(" #f ")")
  37. #define AssertBool(f) AssertSz(((f) | 1) == 1, "boolean not zero or one");
  38. /* use AssertBool(fFoo) before assuming "(fFoo == 1 || fFoo == 0)" */
  39. #define Assert(f) AssertEx(f)
  40. #define NotReached() AssertSz(fFalse, "NotReached() declaration reached")
  41. #define AssertErr(sz) (pfnAssertFailed(sz, __FILE__, __LINE__), 0)
  42. #define FImplies(a,b) (!(a)||(b))
  43. #define AssertImplies(a, b) AssertSz(FImplies(a, b), #a " => " #b)
  44. #pragma warning(disable:4705) /* Disable "Code has no effect" */
  45. /* ****************************************************************** */
  46. /* Item #2:
  47. * A macro to compute the amount of storage required for an instance
  48. * of a datatype which is defined with a repeating last element:
  49. *
  50. * struct s
  51. * {
  52. * int a,b,c;
  53. * struct q
  54. * {
  55. * long x,y,z;
  56. * } rgq[1];
  57. * };
  58. *
  59. * To determine the number of bytes required to hold a "struct s"
  60. * with "N" repeating elements of type "rgq", use "cbRep(struct s, rgq, N)";
  61. */
  62. #include <stddef.h>
  63. #define cbRep(s,r,n) (offsetof(s,r) + (n) * (sizeof(s) - offsetof(s,r)))
  64. /* ****************************************************************** */
  65. /* Item #3:
  66. * Macros which work with tags for validating the types of structures.
  67. *
  68. * tagInvalid denotes a tag which will not be used for valid object types.
  69. *
  70. * Tag('A','B','C','D') generates a DWORD which looks like "ABCD" in a
  71. * Little-Endian debugger memory dump.
  72. *
  73. * FHasTag() assumes that the structure parameter has a DWORD member
  74. * named "tag", and checks it against the tag parameter.
  75. *
  76. * To use these macros, define a unique tag for each data type and a macro
  77. * which performs typechecking for the type:
  78. * #define tagFOO tag('F','O','O','@')
  79. * #define FIsFoo(p) FHasTag(p,tagFoo)
  80. *
  81. * Next, initialize the tag when the structure is allocated:
  82. *
  83. * pfoo->tag = tagFOO;
  84. *
  85. * Then, for all APIs which manipulate items of type FOO, validate the
  86. * type of the parameter before doing any work with it.
  87. *
  88. * if (!FIsFoo(pfoo))
  89. * {
  90. * // return an error.
  91. * }
  92. */
  93. #define tagInvalid ((DWORD) 0xB4B4B4B4)
  94. #define Tag(a,b,c,d) ((DWORD)(d)<<24 | (DWORD)(c)<<16 | (DWORD)(b)<<8 | (a))
  95. #define FHasTag(p,t) ((p) != NULL && (p)->tag == (t))
  96. /* ****************************************************************** */
  97. /* Item #4:
  98. *
  99. * Clever code from Word.h meaning: "a >= b && a <= c"
  100. * (When b and c are constants, this can be done with one test and branch.)
  101. */
  102. #define FBetween(a, b, c) (Assert(b <= c), \
  103. (unsigned)((a) - (b)) <= (unsigned)((c) - (b))\
  104. )
  105. /* ****************************************************************** */
  106. /* Item #4:
  107. *
  108. * Macro meaning: I'm ignoring this parameter on purpose.
  109. */
  110. #define Unreferenced(a) ((void)a)
  111. #endif /* LSIDEFS_DEFINED */