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.

198 lines
6.8 KiB

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