| 1 |
#ifdef _WIN32 |
| 2 |
/* |
| 3 |
* $Id$ |
| 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 |
/* |
| 31 |
* The text above constitutes the entire PortAudio license; however, |
| 32 |
* the PortAudio community also makes the following non-binding requests: |
| 33 |
* |
| 34 |
* Any person wishing to distribute modifications to the Software is |
| 35 |
* requested to send the modifications to the original developer so that |
| 36 |
* they can be incorporated into the canonical version. It is also |
| 37 |
* requested that these non-binding requests be included along with the |
| 38 |
* license above. |
| 39 |
*/ |
| 40 |
|
| 41 |
/** @file |
| 42 |
@ingroup win_src |
| 43 |
|
| 44 |
@brief Win32 implementation of platform-specific PaUtil support functions. |
| 45 |
*/ |
| 46 |
|
| 47 |
#include <windows.h> |
| 48 |
|
| 49 |
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) |
| 50 |
#include <sys/timeb.h> /* for _ftime_s() */ |
| 51 |
#else |
| 52 |
#include <mmsystem.h> /* for timeGetTime() */ |
| 53 |
#if (defined(WIN32) && (defined(_MSC_VER) && (_MSC_VER >= 1200))) && !defined(_WIN32_WCE) /* MSC version 6 and above */ |
| 54 |
#pragma comment( lib, "winmm.lib" ) |
| 55 |
#endif |
| 56 |
#endif |
| 57 |
|
| 58 |
#include "pa_util.h" |
| 59 |
|
| 60 |
/* |
| 61 |
Track memory allocations to avoid leaks. |
| 62 |
*/ |
| 63 |
|
| 64 |
#if PA_TRACK_MEMORY |
| 65 |
static int numAllocations_ = 0; |
| 66 |
#endif |
| 67 |
|
| 68 |
|
| 69 |
void *PaUtil_AllocateMemory( long size ) |
| 70 |
{ |
| 71 |
void *result = GlobalAlloc( GPTR, size ); |
| 72 |
|
| 73 |
#if PA_TRACK_MEMORY |
| 74 |
if( result != NULL ) numAllocations_ += 1; |
| 75 |
#endif |
| 76 |
return result; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
void PaUtil_FreeMemory( void *block ) |
| 81 |
{ |
| 82 |
if( block != NULL ) |
| 83 |
{ |
| 84 |
GlobalFree( block ); |
| 85 |
#if PA_TRACK_MEMORY |
| 86 |
numAllocations_ -= 1; |
| 87 |
#endif |
| 88 |
|
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
int PaUtil_CountCurrentlyAllocatedBlocks( void ) |
| 94 |
{ |
| 95 |
#if PA_TRACK_MEMORY |
| 96 |
return numAllocations_; |
| 97 |
#else |
| 98 |
return 0; |
| 99 |
#endif |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
void Pa_Sleep( long msec ) |
| 104 |
{ |
| 105 |
Sleep( msec ); |
| 106 |
} |
| 107 |
|
| 108 |
static int usePerformanceCounter_; |
| 109 |
static double secondsPerTick_; |
| 110 |
|
| 111 |
void PaUtil_InitializeClock( void ) |
| 112 |
{ |
| 113 |
LARGE_INTEGER ticksPerSecond; |
| 114 |
|
| 115 |
if( QueryPerformanceFrequency( &ticksPerSecond ) != 0 ) |
| 116 |
{ |
| 117 |
usePerformanceCounter_ = 1; |
| 118 |
secondsPerTick_ = 1.0 / (double)ticksPerSecond.QuadPart; |
| 119 |
} |
| 120 |
else |
| 121 |
{ |
| 122 |
usePerformanceCounter_ = 0; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
double PaUtil_GetTime( void ) |
| 128 |
{ |
| 129 |
LARGE_INTEGER time; |
| 130 |
|
| 131 |
if( usePerformanceCounter_ ) |
| 132 |
{ |
| 133 |
/* |
| 134 |
Note: QueryPerformanceCounter has a known issue where it can skip forward |
| 135 |
by a few seconds (!) due to a hardware bug on some PCI-ISA bridge hardware. |
| 136 |
This is documented here: |
| 137 |
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q274323& |
| 138 |
|
| 139 |
The work-arounds are not very paletable and involve querying GetTickCount |
| 140 |
at every time step. |
| 141 |
|
| 142 |
Using rdtsc is not a good option on multi-core systems. |
| 143 |
|
| 144 |
For now we just use QueryPerformanceCounter(). It's good, most of the time. |
| 145 |
*/ |
| 146 |
QueryPerformanceCounter( &time ); |
| 147 |
return time.QuadPart * secondsPerTick_; |
| 148 |
} |
| 149 |
else |
| 150 |
{ |
| 151 |
#ifndef UNDER_CE |
| 152 |
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP) |
| 153 |
return GetTickCount64() * .001; |
| 154 |
#else |
| 155 |
return timeGetTime() * .001; |
| 156 |
#endif |
| 157 |
#else |
| 158 |
return GetTickCount() * .001; |
| 159 |
#endif |
| 160 |
} |
| 161 |
} |
| 162 |
#endif |