pa_mac_util.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * $Id: pa_unix_util.c 1097 2006-08-26 08:27:53Z rossb $
  3. * Portable Audio I/O Library
  4. * UNIX 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. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  23. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  24. * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  25. * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. /*
  29. * The text above constitutes the entire PortAudio license; however,
  30. * the PortAudio community also makes the following non-binding requests:
  31. *
  32. * Any person wishing to distribute modifications to the Software is
  33. * requested to send the modifications to the original developer so that
  34. * they can be incorporated into the canonical version. It is also
  35. * requested that these non-binding requests be included along with the
  36. * license above.
  37. */
  38. /** @file
  39. @ingroup unix_src
  40. */
  41. #include <pthread.h>
  42. #include <unistd.h>
  43. #include <stdlib.h>
  44. #include <time.h>
  45. #include <sys/time.h>
  46. #include <assert.h>
  47. #include <string.h> /* For memset */
  48. #include <math.h>
  49. #include <errno.h>
  50. #include "pa_util.h"
  51. //#include "pa_mac_util.h"
  52. /*
  53. Track memory allocations to avoid leaks.
  54. */
  55. #if PA_TRACK_MEMORY
  56. static int numAllocations_ = 0;
  57. #endif
  58. void *PaUtil_AllocateMemory( long size )
  59. {
  60. void *result = malloc( size );
  61. #if PA_TRACK_MEMORY
  62. if( result != NULL ) numAllocations_ += 1;
  63. #endif
  64. return result;
  65. }
  66. void PaUtil_FreeMemory( void *block )
  67. {
  68. if( block != NULL )
  69. {
  70. free( block );
  71. #if PA_TRACK_MEMORY
  72. numAllocations_ -= 1;
  73. #endif
  74. }
  75. }
  76. int PaUtil_CountCurrentlyAllocatedBlocks( void )
  77. {
  78. #if PA_TRACK_MEMORY
  79. return numAllocations_;
  80. #else
  81. return 0;
  82. #endif
  83. }
  84. void Pa_Sleep( long msec )
  85. {
  86. #ifdef HAVE_NANOSLEEP
  87. struct timespec req = {0}, rem = {0};
  88. PaTime time = msec / 1.e3;
  89. req.tv_sec = (time_t)time;
  90. assert(time - req.tv_sec < 1.0);
  91. req.tv_nsec = (long)((time - req.tv_sec) * 1.e9);
  92. nanosleep(&req, &rem);
  93. /* XXX: Try sleeping the remaining time (contained in rem) if interrupted by a signal? */
  94. #else
  95. while( msec > 999 ) /* For OpenBSD and IRIX, argument */
  96. { /* to usleep must be < 1000000. */
  97. usleep( 999000 );
  98. msec -= 999;
  99. }
  100. usleep( msec * 1000 );
  101. #endif
  102. }
  103. /* *** NOT USED YET: ***
  104. static int usePerformanceCounter_;
  105. static double microsecondsPerTick_;
  106. */
  107. void PaUtil_InitializeClock( void )
  108. {
  109. /* TODO */
  110. }
  111. PaTime PaUtil_GetTime( void )
  112. {
  113. #ifdef HAVE_CLOCK_GETTIME
  114. struct timespec tp;
  115. clock_gettime(CLOCK_REALTIME, &tp);
  116. return (PaTime)(tp.tv_sec + tp.tv_nsec / 1.e9);
  117. #else
  118. struct timeval tv;
  119. gettimeofday( &tv, NULL );
  120. return (PaTime) tv.tv_usec / 1000000. + tv.tv_sec;
  121. #endif
  122. }