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.

196 lines
5.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: memtask.cxx
  7. //
  8. // Contents: Memory task functions
  9. //
  10. // Functions: RunMemoryTasks
  11. //
  12. // History: 17-Aug-93 CarlH Created
  13. //
  14. //--------------------------------------------------------------------------
  15. #include "memtest.hxx"
  16. #pragma hdrstop
  17. #ifdef LINKED_COMPATIBLE
  18. #include <memalloc.h>
  19. #endif
  20. //+-------------------------------------------------------------------------
  21. //
  22. // Function: RunMemoryTasks, public
  23. //
  24. // Synopsis: Runs through a series of memory tasks
  25. //
  26. // Arguments: [pszComponent] - current test component name
  27. // [pmemtsk] - pointer to memory tasks to run
  28. // [cmemtsk] - count of memory tasks
  29. //
  30. // Returns: TRUE if successful, FALSE otherwise
  31. //
  32. // Algorithm: A buffer is first allocated and cleared to hold the
  33. // blocks returned by any allocations. The array of
  34. // tasks is then iterated through an each memory task
  35. // is performed. The given task array should be
  36. // constructed so that all allocated memory is freed.
  37. //
  38. // History: 17-Aug-93 CarlH Created
  39. //
  40. //--------------------------------------------------------------------------
  41. BOOL RunMemoryTasks(char *pszComponent, SMemTask *pmemtsk, ULONG cmemtsk)
  42. {
  43. void **ppvMemory;
  44. HRESULT hr;
  45. BOOL fPassed = TRUE;
  46. // Allocate and clear an array of void *'s to hold the pointers
  47. // returned by any allocations made during the test.
  48. //
  49. ppvMemory = new void *[cmemtsk];
  50. for (ULONG ipv = 0; ipv < cmemtsk; ipv++)
  51. {
  52. ppvMemory[ipv] = NULL;
  53. }
  54. // For each entry in the array of tasks, figure out what type
  55. // of operation is being requested and do it.
  56. //
  57. for (ULONG imemtsk = 0; (imemtsk < cmemtsk) && fPassed; imemtsk++)
  58. {
  59. switch (pmemtsk[imemtsk].memop)
  60. {
  61. #ifdef LINKED_COMPATIBLE
  62. case memopOldAlloc:
  63. PrintTrace(
  64. pszComponent,
  65. "old allocating a block of %lu bytes\n",
  66. pmemtsk[imemtsk].cb);
  67. hr = MemAlloc(pmemtsk[imemtsk].cb, ppvMemory + imemtsk);
  68. PrintTrace(
  69. pszComponent,
  70. "old allocated block at %p\n",
  71. ppvMemory[imemtsk]);
  72. break;
  73. case memopOldAllocLinked:
  74. PrintTrace(
  75. pszComponent,
  76. "old allocating a linked block of %lu bytes on %p\n",
  77. pmemtsk[imemtsk].cb,
  78. ppvMemory[pmemtsk[imemtsk].imemtsk]);
  79. hr = MemAllocLinked(
  80. ppvMemory[pmemtsk[imemtsk].imemtsk],
  81. pmemtsk[imemtsk].cb,
  82. ppvMemory + imemtsk);
  83. PrintTrace(
  84. pszComponent,
  85. "old allocated block at %p\n",
  86. ppvMemory[imemtsk]);
  87. break;
  88. case memopOldFree:
  89. PrintTrace(
  90. pszComponent,
  91. "old freeing block at %p\n",
  92. ppvMemory[pmemtsk[imemtsk].imemtsk]);
  93. hr = MemFree(ppvMemory[pmemtsk[imemtsk].imemtsk]);
  94. break;
  95. #ifdef TEST_MIDL
  96. case memopMIDLAlloc:
  97. TRY
  98. {
  99. PrintTrace(
  100. pszComponent,
  101. "MIDL allocating a block of %lu bytes\n",
  102. pmemtsk[imemtsk].cb);
  103. ppvMemory[imemtsk] = MIDL_user_allocate(pmemtsk[imemtsk].cb);
  104. PrintTrace(
  105. pszComponent,
  106. "MIDL allocated block at %p\n",
  107. ppvMemory[imemtsk]);
  108. hr = NO_ERROR;
  109. }
  110. CATCH(CException, e)
  111. {
  112. hr = e.GetErrorCode();
  113. }
  114. END_CATCH;
  115. break;
  116. case memopMIDLFree:
  117. TRY
  118. {
  119. PrintTrace(
  120. pszComponent,
  121. "MIDL freeing block at %p\n",
  122. ppvMemory[pmemtsk[imemtsk].imemtsk]);
  123. MIDL_user_free(ppvMemory[pmemtsk[imemtsk].imemtsk]);
  124. hr = NO_ERROR;
  125. }
  126. CATCH(CException, e)
  127. {
  128. hr = e.GetErrorCode();
  129. }
  130. END_CATCH;
  131. break;
  132. #endif // TEST_MIDL
  133. #endif // LINKED_COMPATIBLE
  134. case memopAlloc:
  135. // A standard memory allocation is being requested.
  136. // Allocate the number of bytes given in the task
  137. // description and store the block in our array of
  138. // void *'s.
  139. //
  140. PrintTrace(
  141. pszComponent,
  142. "allocating a block of %lu bytes\n",
  143. pmemtsk[imemtsk].cb);
  144. ppvMemory[imemtsk] = CoTaskMemAlloc(pmemtsk[imemtsk].cb);
  145. PrintTrace(
  146. pszComponent,
  147. "allocated block at %p\n",
  148. ppvMemory[imemtsk]);
  149. hr = (ppvMemory[imemtsk] != 0 ? S_OK : -1);
  150. break;
  151. case memopFree:
  152. // A standard memory free is being requested. Free
  153. // the block found in our array of void *'s specified
  154. // by the index found in the task description.
  155. //
  156. PrintTrace(
  157. pszComponent,
  158. "freeing block at %p\n",
  159. ppvMemory[pmemtsk[imemtsk].imemtsk]);
  160. CoTaskMemFree(ppvMemory[pmemtsk[imemtsk].imemtsk]);
  161. hr = S_OK;
  162. break;
  163. default:
  164. // Uh oh. We found a memory operation that we don't
  165. // understand. Set the result code to a value that
  166. // we know will cause the test to fail.
  167. //
  168. PrintError(pszComponent, "unknown memory operation\n");
  169. hr = pmemtsk[imemtsk].hr + 1;
  170. break;
  171. }
  172. // Make sure that the returned error code was what we expected
  173. // for this task.
  174. //
  175. fPassed = (hr == pmemtsk[imemtsk].hr);
  176. }
  177. delete ppvMemory;
  178. return (fPassed);
  179. }