Source code of Windows XP (NT5)
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.

179 lines
4.3 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1994 - 1999
  3. Module Name:
  4. system.c
  5. Abstract:
  6. System functionality used by the RPC development performance tests.
  7. Author:
  8. Mario Goertzel (mariogo) 29-Mar-1994
  9. Revision History:
  10. --*/
  11. #include<rpcperf.h>
  12. #ifndef WIN32
  13. #include<time.h>
  14. #include<malloc.h>
  15. #endif
  16. #ifdef WIN32
  17. void FlushProcessWorkingSet()
  18. {
  19. SetProcessWorkingSetSize(GetCurrentProcess(), ~0UL, ~0UL);
  20. return;
  21. }
  22. #endif
  23. #ifdef WIN32
  24. LARGE_INTEGER _StartTime;
  25. #else
  26. clock_t _StartTime;
  27. #endif
  28. void StartTime(void)
  29. {
  30. #ifdef WIN32
  31. QueryPerformanceCounter(&_StartTime);
  32. #else
  33. _StartTime = clock();
  34. #endif
  35. return;
  36. }
  37. void EndTime(char *string)
  38. {
  39. unsigned long mseconds;
  40. mseconds = FinishTiming();
  41. printf("Time %s: %d.%03d\n",
  42. string,
  43. mseconds / 1000,
  44. mseconds % 1000);
  45. return;
  46. }
  47. // Returns milliseconds since last call to StartTime();
  48. unsigned long FinishTiming()
  49. {
  50. #ifdef WIN32
  51. LARGE_INTEGER liDiff;
  52. LARGE_INTEGER liFreq;
  53. QueryPerformanceCounter(&liDiff);
  54. liDiff.QuadPart -= _StartTime.QuadPart;
  55. (void)QueryPerformanceFrequency(&liFreq);
  56. return (ULONG)(liDiff.QuadPart / (liFreq.QuadPart / 1000));
  57. #else
  58. unsigned long Diff = clock() - _StartTime;
  59. if (Diff)
  60. return( ( (Diff / CLOCKS_PER_SEC) * 1000 ) +
  61. ( ( (Diff % CLOCKS_PER_SEC) * 1000) / CLOCKS_PER_SEC) );
  62. else
  63. return(0);
  64. #endif
  65. }
  66. #ifndef MAC
  67. int TlsIndex = 0;
  68. HANDLE ProcessHeap = 0;
  69. typedef struct tagThreadAllocatorStorage
  70. {
  71. size_t CachedSize;
  72. PVOID CachedBlock;
  73. BOOLEAN BlockAvailable;
  74. } ThreadAllocatorStorage;
  75. void __RPC_FAR * __RPC_USER MIDL_user_allocate(size_t size)
  76. {
  77. ThreadAllocatorStorage *ThreadLocalStorage = (ThreadAllocatorStorage *) TlsGetValue(TlsIndex);
  78. if (ThreadLocalStorage != NULL)
  79. {
  80. if (ThreadLocalStorage->BlockAvailable)
  81. {
  82. if (size <= ThreadLocalStorage->CachedSize)
  83. {
  84. // found a block in the cache - use it
  85. ThreadLocalStorage->BlockAvailable = FALSE;
  86. return ThreadLocalStorage->CachedBlock;
  87. }
  88. else
  89. {
  90. // free the old block, and fall through to allocate the new block
  91. RtlFreeHeap(ProcessHeap, 0, ThreadLocalStorage->CachedBlock);
  92. memset(ThreadLocalStorage, 0, sizeof(ThreadAllocatorStorage));
  93. }
  94. }
  95. }
  96. else
  97. {
  98. ThreadLocalStorage = (ThreadAllocatorStorage *) HeapAlloc(ProcessHeap, 0, sizeof(ThreadAllocatorStorage));
  99. memset(ThreadLocalStorage, 0, sizeof(ThreadAllocatorStorage));
  100. TlsSetValue(TlsIndex, ThreadLocalStorage);
  101. }
  102. ThreadLocalStorage->CachedBlock = HeapAlloc(ProcessHeap, 0, size);
  103. ThreadLocalStorage->CachedSize = size;
  104. ThreadLocalStorage->BlockAvailable = FALSE;
  105. return ThreadLocalStorage->CachedBlock;
  106. }
  107. void __RPC_USER MIDL_user_free(void __RPC_FAR * p)
  108. {
  109. ThreadAllocatorStorage *ThreadLocalStorage = (ThreadAllocatorStorage *) TlsGetValue(TlsIndex);
  110. if (ThreadLocalStorage->BlockAvailable)
  111. {
  112. // free the old block, and store the new one
  113. RtlFreeHeap(ProcessHeap, 0, ThreadLocalStorage->CachedBlock);
  114. ThreadLocalStorage->CachedBlock = p;
  115. ThreadLocalStorage->CachedSize = RtlSizeHeap(ProcessHeap, 0, p);
  116. ThreadLocalStorage->BlockAvailable = TRUE;
  117. return;
  118. }
  119. else if (ThreadLocalStorage->CachedBlock != p)
  120. {
  121. // the block is not available, but it is different
  122. // don't free the old block, but replace it in the cache with the new
  123. ThreadLocalStorage->CachedBlock = p;
  124. ThreadLocalStorage->CachedSize = RtlSizeHeap(ProcessHeap, 0, p);
  125. ThreadLocalStorage->BlockAvailable = TRUE;
  126. return;
  127. }
  128. else
  129. {
  130. // the block in the cache is not available, and it is the same as this one
  131. // just update the cache
  132. ThreadLocalStorage->BlockAvailable = TRUE;
  133. return;
  134. }
  135. }
  136. #endif
  137. void ApiError(char *string, unsigned long status)
  138. {
  139. printf("%s failed - %lu (%08lX)\n", string, status, status);
  140. exit((int)status);
  141. }
  142. void InitAllocator(void)
  143. {
  144. ProcessHeap = GetProcessHeap();
  145. TlsIndex = TlsAlloc();
  146. }