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.

131 lines
5.0 KiB

  1. /*++ Copyright (c) 1996-1997 Microsoft Corporation --*/
  2. /*++
  3. MACROS.H
  4. useful macros I didn't want to declare in every file.
  5. first incarnation in EXTENDED: DAVIDCHR 11/4/1996
  6. next incarnation in k5 compat: DAVIDCHR 1/8/1997
  7. modified to be more portable: DAVIDCHR 4/8/1997
  8. --*/
  9. /* Note that all of these macros use "HopefullyUnusedVariableName"
  10. for a local variable to prevent the unintentional "capture" of
  11. a passed parameter by the same name. Hopefully, we'll never use
  12. a variable by that name. If so, the variable can be made even
  13. more longer and less convenient to use. :-) */
  14. #define EQUALS TRUE
  15. #define NOT_EQUALS FALSE
  16. /* BREAK_AND_LOG_IF is for use with actual test results. When you
  17. absolutely must have the results logged... */
  18. #define BREAK_AND_LOG_IF( variable, loglevel, data, message, label ) { \
  19. BOOL HopefullyUnusedVariableName; \
  20. unsigned long HopefullyUnusedVariableName_SaveData; \
  21. \
  22. HopefullyUnusedVariableName = (variable); \
  23. HopefullyUnusedVariableName_SaveData = data; \
  24. \
  25. if (HopefullyUnusedVariableName) { \
  26. if ( HopefullyUnusedVariableName_SaveData == 0 ) { \
  27. HopefullyUnusedVariableName_SaveData = \
  28. HopefullyUnusedVariableName; \
  29. } \
  30. LOGMSG( loglevel, HopefullyUnusedVariableName_SaveData, message );\
  31. goto label; \
  32. }}
  33. #define BREAK_EXPR( variable, operator, test, message, label ) BREAK_HOOK_EXPR( variable, operator, test, "%hs", message, label)
  34. #ifdef USE_NTLOG /* the other macros MAY or MAY NOT use the logger */
  35. #ifndef BREAK_LOG_LEVEL
  36. #define BREAK_LOG_LEVEL LOGLEVEL_INFO
  37. #endif
  38. #define BREAK_HOOK_EXPR( variable, operator, test, formatmessage, hook, label ) {\
  39. BOOL HopefullyUnusedVariableName;\
  40. unsigned long HopefullyUnusedVariableName_Save;\
  41. CHAR HopefullyUnusedVariableName_Buffer[1024];\
  42. /* unsigned long HopefullyUnusedVariableName_szBuffer = 1024; */\
  43. \
  44. HopefullyUnusedVariableName_Save = (ULONG) (variable);\
  45. HopefullyUnusedVariableName = (operator == EQUALS) ? \
  46. (HopefullyUnusedVariableName_Save == (ULONG) test) :\
  47. (HopefullyUnusedVariableName_Save != (ULONG) test);\
  48. \
  49. if (HopefullyUnusedVariableName) {\
  50. sprintf( HopefullyUnusedVariableName_Buffer, formatmessage, hook );\
  51. LOGMSG(BREAK_LOG_LEVEL, HopefullyUnusedVariableName_Save, HopefullyUnusedVariableName_Buffer );\
  52. goto label;\
  53. }}
  54. #else
  55. #define BREAK_HOOK_EXPR( variable, operator, test, formatmessage, hook, label ) {\
  56. BOOL HopefullyUnusedVariableName;\
  57. ULONG HopefullyUnusedVariableName_Save;\
  58. \
  59. HopefullyUnusedVariableName_Save = (ULONG) (variable);\
  60. HopefullyUnusedVariableName = (operator == EQUALS) ? \
  61. (HopefullyUnusedVariableName_Save == (ULONG) test) :\
  62. (HopefullyUnusedVariableName_Save != (ULONG) test);\
  63. \
  64. if (HopefullyUnusedVariableName) {\
  65. fprintf(stderr, "\n** 0x%x \t ", HopefullyUnusedVariableName_Save );\
  66. fprintf(stderr, formatmessage, hook);\
  67. fprintf(stderr, "\n");\
  68. goto label;\
  69. }}
  70. #endif
  71. #define BREAK_IF( variable, message, label ) BREAK_EXPR((variable), NOT_EQUALS, 0L, message, label)
  72. #define BREAK_EQ( variable, equals, message, label ) \
  73. BREAK_EXPR(variable, EQUALS, equals, message, label )
  74. #define WSA_BREAK( variable, invalidator, message, label ) \
  75. BREAK_HOOK_EXPR( variable, EQUALS, invalidator, \
  76. message "\n\tWSAGetLastError() returns (dec) %d.",\
  77. WSAGetLastError(), label )
  78. #define NT_BREAK_ON BREAK_IF
  79. /*++ MYALLOC
  80. used
  81. whom: variable to put the memory into.
  82. what: what kind of memory it points to.. the integral denomination of
  83. memory we are allocating (char for a string, int for an int *...)
  84. howmany: integral size of "what"s we are allocating (see below)
  85. withwhat: routine to use in allocation (eg malloc, LocalAlloc...).
  86. this routine must return NULL on failure to allocate.
  87. EXAMPLE: I want to allocate a string of 15 characters with malloc
  88. {
  89. PCHAR mystring;
  90. if (! MYALLOC( mystring, CHAR, 15, malloc )) {
  91. fprintf(stderr, "failed to allocate!");
  92. exit(0);
  93. }
  94. }
  95. --*/
  96. #define MYALLOC( whom, what, howmany, withwhat ) \
  97. ( ( (whom) = (what *) (withwhat)( (howmany) * sizeof(what)) ) != NULL )
  98. /* ONEALLOC is a special case of MYALLOC, where howmany is 1 */
  99. #define ONEALLOC( whom, what, withwhat ) MYALLOC( whom, what, 1, withwhat)