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.

126 lines
4.6 KiB

  1. /*--
  2. Copyright (c) 1995-1999 Microsoft Corporation
  3. Module Name: servutil.h
  4. Authors: Arul Menezes
  5. John Spaith
  6. Abstract: Common macros for servers project
  7. --*/
  8. #ifndef UNDER_CE
  9. #ifdef DEBUG
  10. # define DEBUGMSG(x, y) wprintf y
  11. # define DEBUGCHK(exp) Assert(exp)
  12. # define RETAILMSG(x,y) wprintf y
  13. #else
  14. # define DEBUGMSG(x, y)
  15. # define DEBUGCHK(exp)
  16. # define RETAILMSG(x,y)
  17. #endif // DEBUG
  18. #endif // UNDER_CE
  19. // Debug Macros
  20. // Some functions use a local variable err to help with debugging messages,
  21. // -- if err != 0 then there's been an error, which will be print out.
  22. // However we don't want this extra variable and checks in retail mode.
  23. // ISSUE-2000/11/7-danielwe: Need to remove this eventually
  24. #define DEBUG_CODE_INIT int err = 0;
  25. #ifdef DEBUG
  26. #define DEBUGMSG_ERR(x,y) { if (err) { DEBUGMSG(x,y); } }
  27. #define myretleave(r,e) { ret=r; err=e; goto done; }
  28. #define myleave(e) { err=e; goto done; }
  29. #else
  30. #define DEBUGMSG_ERR(x,y)
  31. #define myretleave(r,e) { ret=r; goto done; }
  32. #define myleave(e) { goto done; }
  33. #endif
  34. #define ARRAYSIZEOF(x) (sizeof(x) / sizeof((x)[0]))
  35. #define CCHSIZEOF ARRAYSIZEOF
  36. #define ZEROMEM(p) memset(p, 0, sizeof(*(p)))
  37. #define CELOADSZ(ids) ((LPCTSTR)LoadString(g_hInst, ids, NULL, 0) )
  38. inline void *svsutil_AllocZ (DWORD dwSize, void *pvAllocData) {
  39. void *pvRes = svsutil_Alloc (dwSize, pvAllocData);
  40. if (pvRes)
  41. memset (pvRes, 0, dwSize);
  42. return pvRes;
  43. }
  44. inline void *svsutil_ReAlloc(DWORD dwSizeOld, DWORD dwSizeNew, BYTE *pvDataOld, void *pvAllocData)
  45. {
  46. DEBUGCHK(dwSizeOld < dwSizeNew);
  47. BYTE *pvRes = (BYTE *) svsutil_Alloc(dwSizeNew, pvAllocData);
  48. if (pvRes)
  49. {
  50. memcpy(pvRes,pvDataOld,dwSizeOld);
  51. memset(pvRes + dwSizeOld,0,dwSizeNew - dwSizeOld);
  52. g_funcFree(pvDataOld,g_pvFreeData);
  53. }
  54. return pvRes;
  55. }
  56. #define MyAllocZ(typ) ((typ*)svsutil_AllocZ(sizeof(typ), g_pvAllocData))
  57. #define MyAllocNZ(typ) ((typ*)g_funcAlloc(sizeof(typ), g_pvAllocData))
  58. #define MyRgAllocZ(typ, n) ((typ*)svsutil_AllocZ((n)*sizeof(typ), g_pvAllocData))
  59. #define MyRgAllocNZ(typ, n) ((typ*)g_funcAlloc((n)*sizeof(typ), g_pvAllocData))
  60. #define MyRgReAlloc(typ, p, nOld, nNew) ((typ*) svsutil_ReAlloc(sizeof(typ)*(nOld), sizeof(typ)*(nNew), (BYTE*) p, g_pvAllocData))
  61. #define MyFree(p) { if (p) { g_funcFree ((void *) p, g_pvFreeData); (p)=0;} }
  62. #define MyFreeNZ(p) { if (p) { g_funcFree ((void *) p, g_pvFreeData);} }
  63. #define MySzAllocA(n) MyRgAllocNZ(CHAR, (1+(n)))
  64. #define MySzAllocW(n) MyRgAllocNZ(WCHAR, (1+(n)))
  65. #define MySzReAllocA(p, nOld, nNew) MyRgReAlloc(CHAR, p, nOld, (1+(n)))
  66. #define ResetString(oldStr, newStr) { MyFree(oldStr); oldStr = MySzDupA(newStr); }
  67. #define Nstrcpy(szDest, szSrc, nLen) { memcpy((szDest), (szSrc), (nLen)); \
  68. (szDest)[(nLen)] = 0; }
  69. // Copy from pszDest to pszDest, and move
  70. #define CONSTSIZEOF(x) (sizeof(x)-1)
  71. #define NTFAILED(x) (INVALID_HANDLE_VALUE == (x))
  72. #define MyFreeLib(h) { if(h) FreeLibrary(h); }
  73. #define MyCloseHandle(h) { if(INVALID_HANDLE_VALUE != h) CloseHandle(h); }
  74. #define MyCreateProcess(app, args) CreateProcess(app, args, NULL,NULL,FALSE,0,NULL,NULL,NULL,NULL)
  75. #define MyCreateThread(fn, arg) CreateThread(NULL, 0, fn, (LPVOID)arg, 0, NULL)
  76. #define MyOpenReadFile(path) CreateFile(path, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  77. #define MyOpenAppendFile(path) CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0)
  78. #define MyOpenQueryFile(path) CreateFile(path, 0, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  79. #define abs(x) ( (x) < 0 ? -(x) : (x) )
  80. #define MyStrlenA(str) ( str ? strlen(str) : 0 )
  81. #define MyStrlenW(str) ( str ? wcslen(str) : 0 )
  82. //------------- Error handling macros ------------------------
  83. #define GLE(e) (e ? GetLastError() : 0)
  84. /////////////////////////////////////////////////////////////////////////////
  85. // Misc string handling helpers
  86. /////////////////////////////////////////////////////////////////////////////
  87. #define MyA2W(psz, wsz, iOutLen) MultiByteToWideChar(CP_ACP, 0, psz, -1, wsz, iOutLen)
  88. #define MyW2A(wsz, psz, iOutLen) WideCharToMultiByte(CP_ACP, 0, wsz, -1, psz, iOutLen, 0, 0)
  89. #define MyW2ACP(wsz, psz, iOutLen, lCodePage) WideCharToMultiByte(lCodePage, 0, wsz, -1, psz, iOutLen, 0, 0)
  90. #define _stricmp(sz1, sz2) lstrcmpiA(sz1, sz2)
  91. #define strcmpi _stricmp
  92. // max # of times we try to get our server going in device.exe
  93. #define MAX_SERVER_STARTUP_TRIES 60