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.

133 lines
2.5 KiB

  1. #ifndef MPACKET
  2. /* Copyright (c) 1998 Microsoft Corporation
  3. Module Name:
  4. mpacket.hxx
  5. Abstract:
  6. class and macros for managing allocations off the heap and stack
  7. Contents:
  8. MEMORYPACKET
  9. ALLOC_BYTES
  10. Author:
  11. Ahsan S. Kabir
  12. Revision History:
  13. 10Jan98 akabir
  14. Created
  15. */
  16. // ---- Memory allocation ------------------
  17. #define MP_MAX_STACK_USE 1024
  18. class MEMORYPACKET
  19. {
  20. public:
  21. DWORD dwAlloc;
  22. DWORD dwSize;
  23. LPSTR psStr;
  24. MEMORYPACKET()
  25. {
  26. psStr = NULL;
  27. dwAlloc = dwSize = 0;
  28. }
  29. ~MEMORYPACKET()
  30. {
  31. if (psStr && dwAlloc>MP_MAX_STACK_USE)
  32. {
  33. FREE_FIXED_MEMORY(psStr);
  34. }
  35. }
  36. };
  37. // -- ALLOC_BYTES -----
  38. #define ALLOC_BYTES(p, cb) \
  39. { \
  40. if ((cb)<=MP_MAX_STACK_USE) \
  41. { \
  42. __try \
  43. { \
  44. (p) = (LPSTR)_alloca(cb); \
  45. } \
  46. __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) \
  47. { \
  48. (p) = NULL; \
  49. } \
  50. } \
  51. else \
  52. { \
  53. (p) = (LPSTR)ALLOCATE_FIXED_MEMORY(cb); \
  54. } \
  55. }
  56. class MEMORYPACKETTABLE
  57. {
  58. public:
  59. WORD dwNum;
  60. LPDWORD pdwSize;
  61. LPDWORD pdwAlloc;
  62. LPSTR* ppsStr;
  63. MEMORYPACKETTABLE()
  64. {
  65. pdwAlloc = pdwSize = NULL;
  66. ppsStr = NULL;
  67. dwNum = 0;
  68. }
  69. ~MEMORYPACKETTABLE()
  70. {
  71. if (pdwSize)
  72. {
  73. for (WORD i=0; i<dwNum; i++)
  74. if (pdwAlloc[i]>MP_MAX_STACK_USE)
  75. {
  76. FREE_FIXED_MEMORY(ppsStr[i]);
  77. }
  78. FREE_FIXED_MEMORY(pdwSize);
  79. }
  80. }
  81. BOOL SetUpFor(WORD dwElem)
  82. {
  83. DWORD dwErr = ERROR_SUCCESS;
  84. BOOL fResult = FALSE;
  85. WORD wcE;
  86. dwNum = dwElem + 1;
  87. pdwSize = (LPDWORD)ALLOCATE_FIXED_MEMORY((sizeof(DWORD)*2+sizeof(LPSTR))*dwNum);
  88. if (!pdwSize)
  89. {
  90. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  91. goto cleanup;
  92. }
  93. pdwAlloc = pdwSize + dwNum;
  94. ppsStr = (LPSTR*)(pdwAlloc + dwNum);
  95. for (wcE = 0; wcE < dwNum; wcE++)
  96. {
  97. ppsStr[wcE] = NULL;
  98. pdwSize[wcE] = 0;
  99. pdwAlloc[wcE] = 0;
  100. }
  101. fResult = TRUE;
  102. cleanup:
  103. if (dwErr!=ERROR_SUCCESS)
  104. {
  105. SetLastError(dwErr);
  106. }
  107. return fResult;
  108. }
  109. };
  110. #endif MPACKET