| 1 | /* |
| 2 | * $Id: pa_win_util.c,v 1.1 2006/06/10 21:30:56 dmazzoni Exp $ |
| 3 | * Portable Audio I/O Library |
| 4 | * Win32 platform-specific support functions |
| 5 | * |
| 6 | * Based on the Open Source API proposed by Ross Bencina |
| 7 | * Copyright (c) 1999-2000 Ross Bencina |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining |
| 10 | * a copy of this software and associated documentation files |
| 11 | * (the "Software"), to deal in the Software without restriction, |
| 12 | * including without limitation the rights to use, copy, modify, merge, |
| 13 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 14 | * and to permit persons to whom the Software is furnished to do so, |
| 15 | * subject to the following conditions: |
| 16 | * |
| 17 | * The above copyright notice and this permission notice shall be |
| 18 | * included in all copies or substantial portions of the Software. |
| 19 | * |
| 20 | * Any person wishing to distribute modifications to the Software is |
| 21 | * requested to send the modifications to the original developer so that |
| 22 | * they can be incorporated into the canonical version. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 25 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 26 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 27 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 28 | * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 29 | * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 30 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | /** @file |
| 34 | Win32 platform-specific support functions. |
| 35 | |
| 36 | @todo Implement workaround for QueryPerformanceCounter() skipping forward |
| 37 | bug. (see msdn kb Q274323). |
| 38 | */ |
| 39 | |
| 40 | #include "winpoop.h" |
| 41 | #include <mmsystem.h> /* for timeGetTime() */ |
| 42 | |
| 43 | #include "pa_util.h" |
| 44 | |
| 45 | |
| 46 | /* |
| 47 | Track memory allocations to avoid leaks. |
| 48 | */ |
| 49 | |
| 50 | #if PA_TRACK_MEMORY |
| 51 | static int numAllocations_ = 0; |
| 52 | #endif |
| 53 | |
| 54 | |
| 55 | void *PaUtil_AllocateMemory( long size ) |
| 56 | { |
| 57 | void *result = GlobalAlloc( GPTR, size ); |
| 58 | |
| 59 | #if PA_TRACK_MEMORY |
| 60 | if( result != NULL ) numAllocations_ += 1; |
| 61 | #endif |
| 62 | return result; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void PaUtil_FreeMemory( void *block ) |
| 67 | { |
| 68 | if( block != NULL ) |
| 69 | { |
| 70 | GlobalFree( block ); |
| 71 | #if PA_TRACK_MEMORY |
| 72 | numAllocations_ -= 1; |
| 73 | #endif |
| 74 | |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | int PaUtil_CountCurrentlyAllocatedBlocks( void ) |
| 80 | { |
| 81 | #if PA_TRACK_MEMORY |
| 82 | return numAllocations_; |
| 83 | #else |
| 84 | return 0; |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | |
| 89 | void Pa_Sleep( long msec ) |
| 90 | { |
| 91 | Sleep( msec ); |
| 92 | } |
| 93 | |
| 94 | static int usePerformanceCounter_; |
| 95 | static double secondsPerTick_; |
| 96 | |
| 97 | void PaUtil_InitializeClock( void ) |
| 98 | { |
| 99 | LARGE_INTEGER ticksPerSecond; |
| 100 | |
| 101 | if( QueryPerformanceFrequency( &ticksPerSecond ) != 0 ) |
| 102 | { |
| 103 | usePerformanceCounter_ = 1; |
| 104 | secondsPerTick_ = 1.0 / (double)ticksPerSecond.QuadPart; |
| 105 | } |
| 106 | else |
| 107 | { |
| 108 | usePerformanceCounter_ = 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | |
| 113 | double PaUtil_GetTime( void ) |
| 114 | { |
| 115 | LARGE_INTEGER time; |
| 116 | |
| 117 | if( usePerformanceCounter_ ) |
| 118 | { |
| 119 | /* FIXME: |
| 120 | according to this knowledge-base article, QueryPerformanceCounter |
| 121 | can skip forward by seconds! |
| 122 | http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q274323& |
| 123 | |
| 124 | it may be better to use the rtdsc instruction using inline asm, |
| 125 | however then a method is needed to calculate a ticks/seconds ratio. |
| 126 | */ |
| 127 | QueryPerformanceCounter( &time ); |
| 128 | return time.QuadPart * secondsPerTick_; |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | return timeGetTime() * .001; |
| 133 | } |
| 134 | } |