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.

187 lines
4.6 KiB

  1. //+------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1993.
  5. //
  6. // File: bm_alloc.cxx
  7. //
  8. // Contents: IMalloc test
  9. //
  10. // Classes: COleAllocTest
  11. //
  12. // History: 29-July-93 t-martig Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include <headers.cxx>
  16. #pragma hdrstop
  17. #include <bm_alloc.hxx>
  18. TCHAR *COleAllocTest::Name ()
  19. {
  20. return TEXT("IMalloc");
  21. }
  22. SCODE COleAllocTest::Setup (CTestInput *pInput)
  23. {
  24. CTestBase::Setup(pInput);
  25. SCODE sc;
  26. // Get number of iterations
  27. m_ulIterations = pInput->GetIterations(Name());
  28. m_ulSize = 32;
  29. INIT_RESULTS (m_ulAllocTime);
  30. INIT_RESULTS (m_ulFreeTime);
  31. INIT_RESULTS (m_ulReallocTime);
  32. INIT_RESULTS (m_ulGetSizeTime);
  33. INIT_RESULTS (m_ulDidAllocTime);
  34. INIT_RESULTS (m_ulHeapMinimizeTime);
  35. sc = InitCOM();
  36. if (FAILED(sc))
  37. {
  38. Log (TEXT("Setup - CoInitialize failed."), sc);
  39. return sc;
  40. }
  41. // create an instance of the object to marshal
  42. m_pMalloc = NULL;
  43. sc = CoGetMalloc(MEMCTX_TASK, &m_pMalloc);
  44. if (FAILED(sc))
  45. {
  46. Log (TEXT("Setup - CoGetMalloc failed"), sc);
  47. return sc;
  48. }
  49. return sc;
  50. }
  51. SCODE COleAllocTest::Cleanup ()
  52. {
  53. // release objects
  54. if (m_pMalloc)
  55. m_pMalloc->Release();
  56. UninitCOM();
  57. return S_OK;
  58. }
  59. SCODE COleAllocTest::Run ()
  60. {
  61. CStopWatch sw;
  62. LPVOID FAR pv;
  63. // make the various IMalloc calls
  64. for (ULONG iIter=0; iIter<m_ulIterations; iIter++)
  65. {
  66. sw.Reset();
  67. pv = m_pMalloc->Alloc(m_ulSize);
  68. m_ulAllocTime[iIter] = sw.Read();
  69. Log(TEXT("IMalloc::Alloc"), (pv) ? S_OK : E_OUTOFMEMORY);
  70. sw.Reset();
  71. pv = m_pMalloc->Realloc(pv, m_ulSize*2);
  72. m_ulReallocTime[iIter] = sw.Read();
  73. Log(TEXT("IMalloc::Realloc"), (pv) ? S_OK : E_OUTOFMEMORY);
  74. sw.Reset();
  75. ULONG ulSize = m_pMalloc->GetSize(pv);
  76. m_ulGetSizeTime[iIter] = sw.Read();
  77. Log(TEXT("IMalloc::GetSize"), (ulSize > 0) ? S_OK : E_OUTOFMEMORY);
  78. sw.Reset();
  79. INT fRes = m_pMalloc->DidAlloc(pv);
  80. m_ulDidAllocTime[iIter] = sw.Read();
  81. Log(TEXT("IMalloc::DidAlloc"), (fRes) ? S_OK : E_OUTOFMEMORY);
  82. sw.Reset();
  83. m_pMalloc->Free(pv);
  84. m_ulFreeTime[iIter] = sw.Read();
  85. }
  86. // loop through all the sizes. starting with a size of 8,
  87. // we double it each time for MAX_SIZE_CNT times.
  88. ULONG ulSize = 8;
  89. for (ULONG iSize = 0; iSize <MAX_SIZE_CNT; iSize++)
  90. {
  91. // loop through all the number of iterations
  92. for (iIter=0; iIter<m_ulIterations; iIter++)
  93. {
  94. VOID *pv[TEST_MAX_ITERATIONS];
  95. sw.Reset();
  96. for (ULONG iCnt=0; iCnt<iIter+1; iCnt++)
  97. {
  98. pv[iCnt] = m_pMalloc->Alloc(ulSize);
  99. }
  100. m_ulAllocSizeTime[iSize][iIter] = sw.Read();
  101. sw.Reset();
  102. for (iCnt=0; iCnt<iIter+1; iCnt++)
  103. {
  104. m_pMalloc->Free(pv[iCnt]);
  105. }
  106. m_ulFreeSizeTime[iSize][iIter] = sw.Read();
  107. for (iCnt=0; iCnt<iIter+1; iCnt++)
  108. {
  109. if (pv[iCnt] == NULL)
  110. {
  111. Log(TEXT("IMalloc::Alloc failed."), E_OUTOFMEMORY);
  112. // an allocation failed, correct the times
  113. m_ulAllocSizeTime[iSize][iIter] = NOTAVAIL;
  114. m_ulFreeSizeTime[iSize][iIter] = NOTAVAIL;
  115. }
  116. }
  117. }
  118. // double the allocation size
  119. ulSize *= 2;
  120. }
  121. return S_OK;
  122. }
  123. SCODE COleAllocTest::Report (CTestOutput &output)
  124. {
  125. output.WriteSectionHeader (Name(),
  126. TEXT("OLE20 IMalloc MEMCTX_TASK"),
  127. *m_pInput);
  128. output.WriteString (TEXT("\n"));
  129. output.WriteResults (TEXT("IMalloc->Alloc 32:"), m_ulIterations, m_ulAllocTime);
  130. output.WriteResults (TEXT("IMalloc->Realloc 64:"), m_ulIterations, m_ulReallocTime);
  131. output.WriteResults (TEXT("IMalloc->GetSize "), m_ulIterations, m_ulGetSizeTime);
  132. output.WriteResults (TEXT("IMalloc->DidAlloc "), m_ulIterations, m_ulDidAllocTime);
  133. output.WriteResults (TEXT("IMalloc->Free "), m_ulIterations, m_ulFreeTime);
  134. output.WriteString (TEXT("\n"));
  135. output.WriteString (TEXT("N consecutive allocations of the same size:\n"));
  136. ULONG ulSize = 8;
  137. for (ULONG iSize = 0; iSize<MAX_SIZE_CNT; iSize++)
  138. {
  139. // format output string containing allocation size
  140. TCHAR szBuf[80];
  141. wsprintf(szBuf, TEXT("IMalloc->Alloc %6d:"), ulSize);
  142. output.WriteResults (szBuf, m_ulIterations, m_ulAllocSizeTime[iSize]);
  143. output.WriteResults (TEXT("IMalloc->Free "), m_ulIterations, m_ulFreeSizeTime[iSize]);
  144. // double the size
  145. ulSize *= 2;
  146. }
  147. return S_OK;
  148. }