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.

197 lines
6.9 KiB

  1. /*================================================================================
  2. File: gmacros.h
  3. Contains: Macros used in common by both the DHCP Server and the DHCP Client.
  4. Most of them are inlines for sake of elegance and ease of usage.
  5. Author: RameshV
  6. Created: 04-Jun-97 00:01
  7. ================================================================================*/
  8. //#include <align.h>
  9. // Some block macros; usage at end
  10. // Disable the warning about unreference labels.
  11. #pragma warning(disable : 4102)
  12. #define _shorten(string) ( strrchr(string, '\\')? strrchr(string, '\\') : (string) )
  13. // print a message and the file and line # of whoever is printing this.
  14. #define _TracePrintLine(Msg) DhcpPrint((DEBUG_TRACE_CALLS, "%s:%d %s\n", _shorten(__FILE__), __LINE__, Msg))
  15. #define BlockBegin(Name) { BlockStart_ ## Name : _TracePrintLine( "Block -> " #Name );
  16. #define BlockEnd(Name) BlockEnd_ ## Name : _TracePrintLine( "Block <- " #Name ) ;}
  17. #define BlockContinue(Name) do { _TracePrintLine( "Continue to " #Name); goto BlockStart_ ## Name; } while (0)
  18. #define BlockBreak(Name) do { _TracePrintLine( "Breaking out of " #Name); goto BlockEnd_ ## Name; } while (0)
  19. #define RetFunc(F,Ret) do {_TracePrintLine( "Quitting function " #F ); return Ret ; } while (0)
  20. // The way to use the above set of simple block macros is as follows: (example usage)
  21. #if 0
  22. int
  23. DummyFunction(VOID) {
  24. BlockBegin(DummyFunctionMain) {
  25. if(GlobalCount > 0 )
  26. BlockContinue(DummyFunctionMain);
  27. else GlobalCount ++;
  28. if(GlobalCount > GlobalCountMax)
  29. BlockBreak(DummyFunctionMain);
  30. } BlockEnd(DummyFunctionMain);
  31. RetFunc(DummyFunction, RetVal);
  32. }
  33. #endif
  34. // now come some little more complicated functions..
  35. // note that these can be freely mixed with the above set of simple functions.
  36. #define BlockBeginEx(Name, String) {BlockStart_ ## Name : _TracePrintLine( #String );
  37. #define BlockEndEx(Name, String) BlockEnd_## Name : _TracePrintLine( #String );}
  38. #define BlockContinueEx(Name, String) do {_TracePrintLine( #String); goto BlockStart_ ## Name; } while (0)
  39. #define BlockBreakEx(Name, String) do {_TracePrintLine( #String); goto BlockEnd_ ## Name; } while(0)
  40. #define RetFuncEx(Name,Ret,DebMsg) do {_TracePrintLine( "QuittingFunction " #Name); DhcpPrint(DebMsg); return Ret;} while(0)
  41. // usage example:
  42. #if 0
  43. int
  44. DummyFunction(VOID) {
  45. BlockBeginEx(Main, "Entering Dummy Function" ) {
  46. if( GlobalCount > 0)
  47. BlockContinueEx(Main, GlobalCount > 0);
  48. else GlobalCount ++;
  49. if(GlobalCount > GlobalCountMax)
  50. BlockBreak(Main);
  51. } BlockEndEx(Main, "Done Dummy Function");
  52. RetFunc(DummyFunc, RetVal);
  53. // OR
  54. RetFuncEx(DummyFunc, RetVal, (DEBUG_ERRROS, "Function returning, gcount = %ld\n", GlobalCount));
  55. }
  56. #endif 0
  57. #define NOTHING
  58. // Now if a VOID function (procedure) returns, we can say RetFunc(VoidFunc, NOTHING) and things will work.
  59. //================================================================================
  60. // Now some useful inlines.
  61. //================================================================================
  62. VOID _inline
  63. FreeEx(LPVOID Ptr) {
  64. if(Ptr) DhcpFreeMemory(Ptr);
  65. }
  66. VOID _inline
  67. FreeEx2(LPVOID Ptr1, LPVOID Ptr2) {
  68. FreeEx(Ptr1); FreeEx(Ptr2);
  69. }
  70. VOID _inline
  71. FreeEx3(LPVOID Ptr1, LPVOID Ptr2, LPVOID Ptr3) {
  72. FreeEx(Ptr1); FreeEx(Ptr2); FreeEx(Ptr3);
  73. }
  74. VOID _inline
  75. FreeEx4(LPVOID Ptr1, LPVOID Ptr2, LPVOID Ptr3, LPVOID Ptr4) {
  76. FreeEx2(Ptr1, Ptr2); FreeEx2(Ptr3, Ptr4);
  77. }
  78. //--------------------------------------------------------------------------------
  79. // All the alloc functions below, allocate in one shot a few pointers,
  80. // and initialize them.. aligning them correctly.
  81. //--------------------------------------------------------------------------------
  82. LPVOID _inline
  83. AllocEx(LPVOID *Ptr1, DWORD Size1, LPVOID *Ptr2, DWORD Size2) {
  84. DWORD Size = ROUND_UP_COUNT(Size1, ALIGN_WORST) + Size2;
  85. LPBYTE Ptr = DhcpAllocateMemory(Size);
  86. if(!Ptr) return NULL;
  87. (*Ptr1) = Ptr;
  88. (*Ptr2) = Ptr + ROUND_UP_COUNT(Size1, ALIGN_WORST);
  89. return Ptr;
  90. }
  91. LPVOID _inline
  92. AllocEx2(LPVOID *Ptr1, DWORD Size1, LPVOID *Ptr2, DWORD Size2) {
  93. DWORD Size = ROUND_UP_COUNT(Size1, ALIGN_WORST) + Size2;
  94. LPBYTE Ptr = DhcpAllocateMemory(Size);
  95. if(!Ptr) return NULL;
  96. (*Ptr1) = Ptr;
  97. (*Ptr2) = Ptr + ROUND_UP_COUNT(Size1, ALIGN_WORST);
  98. return Ptr;
  99. }
  100. LPVOID _inline
  101. AllocEx3(LPVOID *Ptr1, DWORD Size1, LPVOID *Ptr2, DWORD Size2, LPVOID *Ptr3, DWORD Size3) {
  102. DWORD Size = ROUND_UP_COUNT(Size1, ALIGN_WORST) + ROUND_UP_COUNT(Size2, ALIGN_WORST) + Size3;
  103. LPBYTE Ptr = DhcpAllocateMemory(Size);
  104. if(!Ptr) return NULL;
  105. (*Ptr1) = Ptr;
  106. (*Ptr2) = Ptr + ROUND_UP_COUNT(Size1, ALIGN_WORST);
  107. (*Ptr3) = Ptr + ROUND_UP_COUNT(Size1, ALIGN_WORST) + ROUND_UP_COUNT(Size2, ALIGN_WORST);
  108. return Ptr;
  109. }
  110. LPVOID _inline
  111. AllocEx4(LPVOID *Ptr1, DWORD Size1, LPVOID *Ptr2, DWORD Size2,
  112. LPVOID *Ptr3, DWORD Size3, LPVOID *Ptr4, DWORD Size4) {
  113. DWORD Size = ROUND_UP_COUNT(Size1, ALIGN_WORST) +
  114. ROUND_UP_COUNT(Size2, ALIGN_WORST) + ROUND_UP_COUNT(Size3, ALIGN_WORST) + Size4;
  115. LPBYTE Ptr = DhcpAllocateMemory(Size);
  116. if(!Ptr) return NULL;
  117. (*Ptr1) = Ptr;
  118. (*Ptr2) = Ptr + ROUND_UP_COUNT(Size1, ALIGN_WORST);
  119. (*Ptr3) = Ptr + ROUND_UP_COUNT(Size1, ALIGN_WORST) + ROUND_UP_COUNT(Size2, ALIGN_WORST);
  120. (*Ptr4) = Ptr + ROUND_UP_COUNT(Size1, ALIGN_WORST) +
  121. ROUND_UP_COUNT(Size2, ALIGN_WORST) + ROUND_UP_COUNT(Size3, ALIGN_WORST);
  122. return Ptr;
  123. }
  124. //--------------------------------------------------------------------------------
  125. // This function takes an input string and a static buffer and if the input
  126. // string is not nul terminated, copies it to the static buffer and then null
  127. // terminates it. It also change the size to reflect the new size..
  128. //--------------------------------------------------------------------------------
  129. LPBYTE _inline
  130. AsciiNulTerminate(LPBYTE Input, DWORD *Size, LPBYTE StaticBuf, DWORD BufSize) {
  131. if( 0 == *Size) return Input; // nothing to copy
  132. if(!Input[(*Size)-1]) return Input; // Everything is fine.
  133. if(*Size >= BufSize) {
  134. // Nothing much can be done here.. this is an error.. insufficient buffer space.
  135. DhcpAssert(FALSE);
  136. *Size = BufSize - 1;
  137. }
  138. memcpy(StaticBuf, Input, (*Size));
  139. StaticBuf[*Size] = '\0';
  140. (*Size) ++;
  141. return StaticBuf;
  142. }
  143. #if DBG
  144. #define INLINE
  145. #else
  146. #define INLINE _inline
  147. #endif
  148. #define BEGIN_EXPORT
  149. #define END_EXPORT
  150. #define AssertReturn(Condition, RetVal ) do { DhcpAssert(Condition); return RetVal ;} while(0)
  151. //================================================================================
  152. // End of File.
  153. //================================================================================