pa_win_util.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #ifdef _WIN32
  2. /*
  3. * $Id: pa_win_util.c 1584 2011-02-02 18:58:17Z rossb $
  4. * Portable Audio I/O Library
  5. * Win32 platform-specific support functions
  6. *
  7. * Based on the Open Source API proposed by Ross Bencina
  8. * Copyright (c) 1999-2008 Ross Bencina
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining
  11. * a copy of this software and associated documentation files
  12. * (the "Software"), to deal in the Software without restriction,
  13. * including without limitation the rights to use, copy, modify, merge,
  14. * publish, distribute, sublicense, and/or sell copies of the Software,
  15. * and to permit persons to whom the Software is furnished to do so,
  16. * subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be
  19. * included in all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  24. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  25. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  26. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. */
  29. /*
  30. * The text above constitutes the entire PortAudio license; however,
  31. * the PortAudio community also makes the following non-binding requests:
  32. *
  33. * Any person wishing to distribute modifications to the Software is
  34. * requested to send the modifications to the original developer so that
  35. * they can be incorporated into the canonical version. It is also
  36. * requested that these non-binding requests be included along with the
  37. * license above.
  38. */
  39. /** @file
  40. @ingroup win_src
  41. @brief Win32 implementation of platform-specific PaUtil support functions.
  42. */
  43. #include <windows.h>
  44. #include <mmsystem.h> /* for timeGetTime() */
  45. #include "pa_util.h"
  46. #if (defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) && !defined(_WIN32_WCE) /* MSC version 6 and above */
  47. #pragma comment( lib, "winmm.lib" )
  48. #endif
  49. /*
  50. Track memory allocations to avoid leaks.
  51. */
  52. #if PA_TRACK_MEMORY
  53. static int numAllocations_ = 0;
  54. #endif
  55. void *PaUtil_AllocateMemory( long size )
  56. {
  57. void *result = GlobalAlloc( GPTR, size );
  58. #if PA_TRACK_MEMORY
  59. if( result != NULL ) numAllocations_ += 1;
  60. #endif
  61. return result;
  62. }
  63. void PaUtil_FreeMemory( void *block )
  64. {
  65. if( block != NULL )
  66. {
  67. GlobalFree( block );
  68. #if PA_TRACK_MEMORY
  69. numAllocations_ -= 1;
  70. #endif
  71. }
  72. }
  73. int PaUtil_CountCurrentlyAllocatedBlocks( void )
  74. {
  75. #if PA_TRACK_MEMORY
  76. return numAllocations_;
  77. #else
  78. return 0;
  79. #endif
  80. }
  81. void Pa_Sleep( long msec )
  82. {
  83. Sleep( msec );
  84. }
  85. static int usePerformanceCounter_;
  86. static double secondsPerTick_;
  87. void PaUtil_InitializeClock( void )
  88. {
  89. LARGE_INTEGER ticksPerSecond;
  90. if( QueryPerformanceFrequency( &ticksPerSecond ) != 0 )
  91. {
  92. usePerformanceCounter_ = 1;
  93. secondsPerTick_ = 1.0 / (double)ticksPerSecond.QuadPart;
  94. }
  95. else
  96. {
  97. usePerformanceCounter_ = 0;
  98. }
  99. }
  100. double PaUtil_GetTime( void )
  101. {
  102. LARGE_INTEGER time;
  103. if( usePerformanceCounter_ )
  104. {
  105. /*
  106. Note: QueryPerformanceCounter has a known issue where it can skip forward
  107. by a few seconds (!) due to a hardware bug on some PCI-ISA bridge hardware.
  108. This is documented here:
  109. http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q274323&
  110. The work-arounds are not very paletable and involve querying GetTickCount
  111. at every time step.
  112. Using rdtsc is not a good option on multi-core systems.
  113. For now we just use QueryPerformanceCounter(). It's good, most of the time.
  114. */
  115. QueryPerformanceCounter( &time );
  116. return time.QuadPart * secondsPerTick_;
  117. }
  118. else
  119. {
  120. #ifndef UNDER_CE
  121. return timeGetTime() * .001;
  122. #else
  123. return GetTickCount() * .001;
  124. #endif
  125. }
  126. }
  127. #endif