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.

119 lines
2.2 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(cb) ((cb<=MP_MAX_STACK_USE) ? \
  39. _alloca(cb) : \
  40. ALLOCATE_FIXED_MEMORY(cb))
  41. class MEMORYPACKETTABLE
  42. {
  43. public:
  44. WORD dwNum;
  45. LPDWORD pdwSize;
  46. LPDWORD pdwAlloc;
  47. LPSTR* ppsStr;
  48. MEMORYPACKETTABLE()
  49. {
  50. pdwAlloc = pdwSize = NULL;
  51. ppsStr = NULL;
  52. dwNum = 0;
  53. }
  54. ~MEMORYPACKETTABLE()
  55. {
  56. if (pdwSize)
  57. {
  58. for (WORD i=0; i<dwNum; i++)
  59. if (pdwAlloc[i]>MP_MAX_STACK_USE)
  60. {
  61. FREE_FIXED_MEMORY(ppsStr[i]);
  62. }
  63. FREE_FIXED_MEMORY(pdwSize);
  64. }
  65. }
  66. BOOL SetUpFor(WORD dwElem)
  67. {
  68. DWORD dwErr = ERROR_SUCCESS;
  69. BOOL fResult = FALSE;
  70. WORD wcE;
  71. dwNum = dwElem + 1;
  72. pdwSize = (LPDWORD)ALLOCATE_FIXED_MEMORY((sizeof(DWORD)*2+sizeof(LPSTR))*dwNum);
  73. if (!pdwSize)
  74. {
  75. dwErr = ERROR_NOT_ENOUGH_MEMORY;
  76. goto cleanup;
  77. }
  78. pdwAlloc = pdwSize + dwNum;
  79. ppsStr = (LPSTR*)(pdwAlloc + dwNum);
  80. for (wcE = 0; wcE < dwNum; wcE++)
  81. {
  82. ppsStr[wcE] = NULL;
  83. pdwSize[wcE] = 0;
  84. pdwAlloc[wcE] = 0;
  85. }
  86. fResult = TRUE;
  87. cleanup:
  88. if (dwErr!=ERROR_SUCCESS)
  89. {
  90. SetLastError(dwErr);
  91. }
  92. return fResult;
  93. }
  94. };
  95. #endif MPACKET