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.

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