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.

135 lines
3.6 KiB

  1. /***************************************************************************\
  2. *
  3. * File: TempHelp.cpp
  4. *
  5. * Description:
  6. * TempHelp.h implements a "lightweight heap", designed to continuously grow
  7. * until all memory is freed. This is valuable as a temporary heap that can
  8. * be used to "collect" data and processed slightly later.
  9. *
  10. *
  11. * History:
  12. * 3/30/2000: JStall: Created
  13. *
  14. * Copyright (C) 2000 by Microsoft Corporation. All rights reserved.
  15. *
  16. \***************************************************************************/
  17. #include "stdafx.h"
  18. #include "Base.h"
  19. #include "TempHeap.h"
  20. #include "SimpleHeap.h"
  21. /***************************************************************************\
  22. *****************************************************************************
  23. *
  24. * class TempHeap
  25. *
  26. *****************************************************************************
  27. \***************************************************************************/
  28. //------------------------------------------------------------------------------
  29. TempHeap::TempHeap(int cbPageAlloc, int cbLargeThreshold)
  30. {
  31. m_ppageCur = NULL;
  32. m_ppageLarge = NULL;
  33. m_pbFree = NULL;
  34. m_cbFree = 0;
  35. m_cbPageAlloc = cbPageAlloc;
  36. m_cbLargeThreshold = cbLargeThreshold;
  37. }
  38. //------------------------------------------------------------------------------
  39. void *
  40. TempHeap::Alloc(int cbAlloc)
  41. {
  42. AssertMsg(cbAlloc > 0, "Must specify a valid allocation size");
  43. if (cbAlloc > m_cbLargeThreshold) {
  44. //
  45. // Allocating a very large block, so allocate it directly.
  46. //
  47. Page * pageNew = (Page *) ClientAlloc(sizeof(Page) + cbAlloc);
  48. if (pageNew == NULL) {
  49. return NULL;
  50. }
  51. pageNew->pNext = m_ppageLarge;
  52. m_ppageLarge = pageNew;
  53. return pageNew->GetData();
  54. }
  55. if ((m_ppageCur == NULL) || (cbAlloc > m_cbFree)) {
  56. Page * pageNew = (Page *) ClientAlloc(sizeof(Page) + m_cbPageAlloc);
  57. if (pageNew == NULL) {
  58. return NULL;
  59. }
  60. pageNew->pNext = m_ppageCur;
  61. m_ppageCur = pageNew;
  62. m_cbFree = m_cbPageAlloc;
  63. m_pbFree = pageNew->GetData();
  64. }
  65. AssertMsg(m_cbFree >= cbAlloc, "Should have enough space to allocate by now");
  66. void * pvNew = m_pbFree;
  67. m_cbFree -= cbAlloc;
  68. m_pbFree += cbAlloc;
  69. return pvNew;
  70. }
  71. //------------------------------------------------------------------------------
  72. void
  73. TempHeap::FreeAll(BOOL fComplete)
  74. {
  75. Page * pageNext;
  76. Page * pageTemp;
  77. //
  78. // Free large-block allocations
  79. //
  80. pageTemp = m_ppageLarge;
  81. while (pageTemp != NULL) {
  82. pageNext = pageTemp->pNext;
  83. ClientFree(pageTemp);
  84. pageTemp = pageNext;
  85. }
  86. m_ppageLarge = NULL;
  87. //
  88. // Free small-block allocations
  89. //
  90. pageTemp = m_ppageCur;
  91. while (pageTemp != NULL) {
  92. pageNext = pageTemp->pNext;
  93. if ((pageNext == NULL) && (!fComplete)) {
  94. //
  95. // Don't free the first block, since we will immediately turn around
  96. // and allocate it again. Instead, renew it.
  97. //
  98. m_ppageCur = pageTemp;
  99. m_cbFree = m_cbPageAlloc;
  100. m_pbFree = pageTemp->GetData();
  101. break;
  102. }
  103. ClientFree(pageTemp);
  104. pageTemp = pageNext;
  105. }
  106. if (fComplete) {
  107. m_ppageCur = NULL;
  108. m_pbFree = NULL;
  109. m_cbFree = 0;
  110. }
  111. }