[Freeswitch-svn] [commit] r5435 - freeswitch/trunk/src/mod/endpoints/mod_portaudio

Freeswitch SVN anthm at freeswitch.org
Thu Jun 21 18:48:58 EDT 2007


Author: anthm
Date: Thu Jun 21 18:48:58 2007
New Revision: 5435

Modified:
   freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.c
   freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.h

Log:
update

Modified: freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.c
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.c	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.c	Thu Jun 21 18:48:58 2007
@@ -71,16 +71,27 @@
  *
  ****************/
 
-#if defined(HAVE_LIBKERN_OSATOMIC_H) && (defined(__APPLE__) || defined(__FreeBSD__))
+#if defined(__VIA_HACK__)
+#define NO_BARRIER
+#endif
+
+#if defined(NO_BARRIER)
+#   define PaUtil_FullMemoryBarrier()
+#   define PaUtil_ReadMemoryBarrier()
+#   define PaUtil_WriteMemoryBarrier()
+#else
+
+#if defined(__APPLE__)			//|| defined(__FreeBSD__)
 #   include <libkern/OSAtomic.h>
-    /* Here are the memory barrier functions. Mac OS X and FreeBSD only provide
-       full memory barriers, so the three types of barriers are the same. */
+	/* Here are the memory barrier functions. Mac OS X and FreeBSD only provide
+	   full memory barriers, so the three types of barriers are the same. */
 #   define PaUtil_FullMemoryBarrier()  OSMemoryBarrier()
 #   define PaUtil_ReadMemoryBarrier()  OSMemoryBarrier()
 #   define PaUtil_WriteMemoryBarrier() OSMemoryBarrier()
 #elif defined(__GNUC__)
-    /* GCC understands volatile __asm__ and "memory" to mean it
-     * should not reorder memory read/writes */
+
+	/* GCC understands volatile asm and "memory" to mean it
+	 * should not reorder memory read/writes */
 #   if defined( __PPC__ )
 #      define PaUtil_FullMemoryBarrier()  __asm__ volatile("sync":::"memory")
 #      define PaUtil_ReadMemoryBarrier()  __asm__ volatile("sync":::"memory")
@@ -90,63 +101,61 @@
 #      define PaUtil_ReadMemoryBarrier()  __asm__ volatile("lfence":::"memory")
 #      define PaUtil_WriteMemoryBarrier() __asm__ volatile("sfence":::"memory")
 #   else
-#      ifdef ALLOW_SMP_DANGERS
-#         warning Memory barriers not defined on this system or system unknown
-#         warning For SMP safety, you should fix this.
-#         define PaUtil_FullMemoryBarrier()
-#         define PaUtil_ReadMemoryBarrier()
-#         define PaUtil_WriteMemoryBarrier()
-#      else
-#         error Memory barriers are not defined on this system. You can still compile by defining ALLOW_SMP_DANGERS, but SMP safety will not be guaranteed.
-#      endif
-#   endif
-#else
-#   ifdef ALLOW_SMP_DANGERS
-#      warning Memory barriers not defined on this system or system unknown
-#      warning For SMP safety, you should fix this.
 #      define PaUtil_FullMemoryBarrier()
 #      define PaUtil_ReadMemoryBarrier()
 #      define PaUtil_WriteMemoryBarrier()
-#   else
-#      error Memory barriers are not defined on this system. You can still compile by defining ALLOW_SMP_DANGERS, but SMP safety will not be guaranteed.
 #   endif
+#elif defined(_MSC_VER)
+#   include <intrin.h>
+#   pragma intrinsic(_ReadWriteBarrier)
+#   pragma intrinsic(_ReadBarrier)
+#   pragma intrinsic(_WriteBarrier)
+#   define PaUtil_FullMemoryBarrier()  _ReadWriteBarrier()
+#   define PaUtil_ReadMemoryBarrier()  _ReadBarrier()
+#   define PaUtil_WriteMemoryBarrier() _WriteBarrier()
+#else
+#   define PaUtil_FullMemoryBarrier()
+#   define PaUtil_ReadMemoryBarrier()
+#   define PaUtil_WriteMemoryBarrier()
+#endif
 #endif
-
 /***************************************************************************
  * Initialize FIFO.
  * numBytes must be power of 2, returns -1 if not.
  */
-long PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, long numBytes, void *dataPtr )
+long PaUtil_InitializeRingBuffer(PaUtilRingBuffer * rbuf, long numBytes, void *dataPtr)
 {
-    if( ((numBytes-1) & numBytes) != 0) return -1; /* Not Power of two. */
-    rbuf->bufferSize = numBytes;
-    rbuf->buffer = (char *)dataPtr;
-    PaUtil_FlushRingBuffer( rbuf );
-    rbuf->bigMask = (numBytes*2)-1;
-    rbuf->smallMask = (numBytes)-1;
-    return 0;
+	if (((numBytes - 1) & numBytes) != 0)
+		return -1;				/* Not Power of two. */
+	rbuf->bufferSize = numBytes;
+	rbuf->buffer = (char *) dataPtr;
+	PaUtil_FlushRingBuffer(rbuf);
+	rbuf->bigMask = (numBytes * 2) - 1;
+	rbuf->smallMask = (numBytes) - 1;
+	return 0;
 }
 
 /***************************************************************************
 ** Return number of bytes available for reading. */
-long PaUtil_GetRingBufferReadAvailable( PaUtilRingBuffer *rbuf )
+long PaUtil_GetRingBufferReadAvailable(PaUtilRingBuffer * rbuf)
 {
-    PaUtil_ReadMemoryBarrier();
-    return ( (rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask );
+	PaUtil_ReadMemoryBarrier();
+	return ((rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask);
 }
+
 /***************************************************************************
 ** Return number of bytes available for writing. */
-long PaUtil_GetRingBufferWriteAvailable( PaUtilRingBuffer *rbuf )
+long PaUtil_GetRingBufferWriteAvailable(PaUtilRingBuffer * rbuf)
 {
-    /* Since we are calling PaUtil_GetRingBufferReadAvailable, we don't need an aditional MB */
-    return ( rbuf->bufferSize - PaUtil_GetRingBufferReadAvailable(rbuf));
+	/* Since we are calling PaUtil_GetRingBufferReadAvailable, we don't need an aditional MB */
+	return (rbuf->bufferSize - PaUtil_GetRingBufferReadAvailable(rbuf));
 }
 
 /***************************************************************************
 ** Clear buffer. Should only be called when buffer is NOT being read. */
-void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf )
+void PaUtil_FlushRingBuffer(PaUtilRingBuffer * rbuf)
 {
-    rbuf->writeIndex = rbuf->readIndex = 0;
+	rbuf->writeIndex = rbuf->readIndex = 0;
 }
 
 /***************************************************************************
@@ -155,42 +164,38 @@
 ** If non-contiguous, size2 will be the size of second region.
 ** Returns room available to be written or numBytes, whichever is smaller.
 */
-long PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, long numBytes,
-                                       void **dataPtr1, long *sizePtr1,
-                                       void **dataPtr2, long *sizePtr2 )
-{
-    long   index;
-    long   available = PaUtil_GetRingBufferWriteAvailable( rbuf );
-    if( numBytes > available ) numBytes = available;
-    /* Check to see if write is not contiguous. */
-    index = rbuf->writeIndex & rbuf->smallMask;
-    if( (index + numBytes) > rbuf->bufferSize )
-    {
-        /* Write data in two blocks that wrap the buffer. */
-        long   firstHalf = rbuf->bufferSize - index;
-        *dataPtr1 = &rbuf->buffer[index];
-        *sizePtr1 = firstHalf;
-        *dataPtr2 = &rbuf->buffer[0];
-        *sizePtr2 = numBytes - firstHalf;
-    }
-    else
-    {
-        *dataPtr1 = &rbuf->buffer[index];
-        *sizePtr1 = numBytes;
-        *dataPtr2 = NULL;
-        *sizePtr2 = 0;
-    }
-    return numBytes;
+long PaUtil_GetRingBufferWriteRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2)
+{
+	long index;
+	long available = PaUtil_GetRingBufferWriteAvailable(rbuf);
+	if (numBytes > available)
+		numBytes = available;
+	/* Check to see if write is not contiguous. */
+	index = rbuf->writeIndex & rbuf->smallMask;
+	if ((index + numBytes) > rbuf->bufferSize) {
+		/* Write data in two blocks that wrap the buffer. */
+		long firstHalf = rbuf->bufferSize - index;
+		*dataPtr1 = &rbuf->buffer[index];
+		*sizePtr1 = firstHalf;
+		*dataPtr2 = &rbuf->buffer[0];
+		*sizePtr2 = numBytes - firstHalf;
+	} else {
+		*dataPtr1 = &rbuf->buffer[index];
+		*sizePtr1 = numBytes;
+		*dataPtr2 = NULL;
+		*sizePtr2 = 0;
+	}
+	return numBytes;
 }
 
 
 /***************************************************************************
 */
-long PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, long numBytes )
+long PaUtil_AdvanceRingBufferWriteIndex(PaUtilRingBuffer * rbuf, long numBytes)
 {
-    /* we need to ensure that previous writes are seen before we update the write index */
-    PaUtil_WriteMemoryBarrier();
-    return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask;
+	/* we need to ensure that previous writes are seen before we update the write index */
+	PaUtil_WriteMemoryBarrier();
+	return rbuf->writeIndex = (rbuf->writeIndex + numBytes) & rbuf->bigMask;
 }
 
 /***************************************************************************
@@ -199,81 +204,72 @@
 ** If non-contiguous, size2 will be the size of second region.
 ** Returns room available to be written or numBytes, whichever is smaller.
 */
-long PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, long numBytes,
-                                void **dataPtr1, long *sizePtr1,
-                                void **dataPtr2, long *sizePtr2 )
-{
-    long   index;
-    long   available = PaUtil_GetRingBufferReadAvailable( rbuf );
-    if( numBytes > available ) numBytes = available;
-    /* Check to see if read is not contiguous. */
-    index = rbuf->readIndex & rbuf->smallMask;
-    if( (index + numBytes) > rbuf->bufferSize )
-    {
-        /* Write data in two blocks that wrap the buffer. */
-        long firstHalf = rbuf->bufferSize - index;
-        *dataPtr1 = &rbuf->buffer[index];
-        *sizePtr1 = firstHalf;
-        *dataPtr2 = &rbuf->buffer[0];
-        *sizePtr2 = numBytes - firstHalf;
-    }
-    else
-    {
-        *dataPtr1 = &rbuf->buffer[index];
-        *sizePtr1 = numBytes;
-        *dataPtr2 = NULL;
-        *sizePtr2 = 0;
-    }
-    return numBytes;
+long PaUtil_GetRingBufferReadRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2)
+{
+	long index;
+	long available = PaUtil_GetRingBufferReadAvailable(rbuf);
+	if (numBytes > available)
+		numBytes = available;
+	/* Check to see if read is not contiguous. */
+	index = rbuf->readIndex & rbuf->smallMask;
+	if ((index + numBytes) > rbuf->bufferSize) {
+		/* Write data in two blocks that wrap the buffer. */
+		long firstHalf = rbuf->bufferSize - index;
+		*dataPtr1 = &rbuf->buffer[index];
+		*sizePtr1 = firstHalf;
+		*dataPtr2 = &rbuf->buffer[0];
+		*sizePtr2 = numBytes - firstHalf;
+	} else {
+		*dataPtr1 = &rbuf->buffer[index];
+		*sizePtr1 = numBytes;
+		*dataPtr2 = NULL;
+		*sizePtr2 = 0;
+	}
+	return numBytes;
 }
+
 /***************************************************************************
 */
-long PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, long numBytes )
+long PaUtil_AdvanceRingBufferReadIndex(PaUtilRingBuffer * rbuf, long numBytes)
 {
-    /* we need to ensure that previous writes are always seen before updating the index. */
-    PaUtil_WriteMemoryBarrier();
-    return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask;
+	/* we need to ensure that previous writes are always seen before updating the index. */
+	PaUtil_WriteMemoryBarrier();
+	return rbuf->readIndex = (rbuf->readIndex + numBytes) & rbuf->bigMask;
 }
 
 /***************************************************************************
 ** Return bytes written. */
-long PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, long numBytes )
+long PaUtil_WriteRingBuffer(PaUtilRingBuffer * rbuf, const void *data, long numBytes)
 {
-    long size1, size2, numWritten;
-    void *data1, *data2;
-    numWritten = PaUtil_GetRingBufferWriteRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
-    if( size2 > 0 )
-    {
-
-        memcpy( data1, data, size1 );
-        data = ((char *)data) + size1;
-        memcpy( data2, data, size2 );
-    }
-    else
-    {
-        memcpy( data1, data, size1 );
-    }
-    PaUtil_AdvanceRingBufferWriteIndex( rbuf, numWritten );
-    return numWritten;
+	long size1, size2, numWritten;
+	void *data1, *data2;
+	numWritten = PaUtil_GetRingBufferWriteRegions(rbuf, numBytes, &data1, &size1, &data2, &size2);
+	if (size2 > 0) {
+
+		memcpy(data1, data, size1);
+		data = ((char *) data) + size1;
+		memcpy(data2, data, size2);
+	} else {
+		memcpy(data1, data, size1);
+	}
+	PaUtil_AdvanceRingBufferWriteIndex(rbuf, numWritten);
+	return numWritten;
 }
 
 /***************************************************************************
 ** Return bytes read. */
-long PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, long numBytes )
+long PaUtil_ReadRingBuffer(PaUtilRingBuffer * rbuf, void *data, long numBytes)
 {
-    long size1, size2, numRead;
-    void *data1, *data2;
-    numRead = PaUtil_GetRingBufferReadRegions( rbuf, numBytes, &data1, &size1, &data2, &size2 );
-    if( size2 > 0 )
-    {
-        memcpy( data, data1, size1 );
-        data = ((char *)data) + size1;
-        memcpy( data, data2, size2 );
-    }
-    else
-    {
-        memcpy( data, data1, size1 );
-    }
-    PaUtil_AdvanceRingBufferReadIndex( rbuf, numRead );
-    return numRead;
+	long size1, size2, numRead;
+	void *data1, *data2;
+	numRead = PaUtil_GetRingBufferReadRegions(rbuf, numBytes, &data1, &size1, &data2, &size2);
+	if (size2 > 0) {
+		memcpy(data, data1, size1);
+		data = ((char *) data) + size1;
+		memcpy(data, data2, size2);
+	} else {
+		memcpy(data, data1, size1);
+	}
+	PaUtil_AdvanceRingBufferReadIndex(rbuf, numRead);
+	return numRead;
 }

Modified: freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.h
==============================================================================
--- freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.h	(original)
+++ freeswitch/trunk/src/mod/endpoints/mod_portaudio/pa_ringbuffer.h	Thu Jun 21 18:48:58 2007
@@ -51,19 +51,17 @@
 */
 
 #ifdef __cplusplus
-extern "C"
-{
-#endif /* __cplusplus */
-
-typedef struct PaUtilRingBuffer
-{
-    long   bufferSize; /* Number of bytes in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */
-    long   writeIndex; /* Index of next writable byte. Set by PaUtil_AdvanceRingBufferWriteIndex. */
-    long   readIndex;  /* Index of next readable byte. Set by PaUtil_AdvanceRingBufferReadIndex. */
-    long   bigMask;    /* Used for wrapping indices with extra bit to distinguish full/empty. */
-    long   smallMask;  /* Used for fitting indices to buffer. */
-    char  *buffer;
-}PaUtilRingBuffer;
+extern "C" {
+#endif							/* __cplusplus */
+
+	typedef struct PaUtilRingBuffer {
+		long bufferSize;		/* Number of bytes in FIFO. Power of 2. Set by PaUtil_InitRingBuffer. */
+		long writeIndex;		/* Index of next writable byte. Set by PaUtil_AdvanceRingBufferWriteIndex. */
+		long readIndex;			/* Index of next readable byte. Set by PaUtil_AdvanceRingBufferReadIndex. */
+		long bigMask;			/* Used for wrapping indices with extra bit to distinguish full/empty. */
+		long smallMask;			/* Used for fitting indices to buffer. */
+		char *buffer;
+	} PaUtilRingBuffer;
 
 /** Initialize Ring Buffer.
 
@@ -76,13 +74,13 @@
 
  @return -1 if numBytes is not a power of 2, otherwise 0.
 */
-long PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, long numBytes, void *dataPtr );
+	long PaUtil_InitializeRingBuffer(PaUtilRingBuffer * rbuf, long numBytes, void *dataPtr);
 
 /** Clear buffer. Should only be called when buffer is NOT being read.
 
  @param rbuf The ring buffer.
 */
-void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf );
+	void PaUtil_FlushRingBuffer(PaUtilRingBuffer * rbuf);
 
 /** Retrieve the number of bytes available in the ring buffer for writing.
 
@@ -90,7 +88,7 @@
 
  @return The number of bytes available for writing.
 */
-long PaUtil_GetRingBufferWriteAvailable( PaUtilRingBuffer *rbuf );
+	long PaUtil_GetRingBufferWriteAvailable(PaUtilRingBuffer * rbuf);
 
 /** Retrieve the number of bytes available in the ring buffer for reading.
 
@@ -98,7 +96,7 @@
 
  @return The number of bytes available for reading.
 */
-long PaUtil_GetRingBufferReadAvailable( PaUtilRingBuffer *rbuf );
+	long PaUtil_GetRingBufferReadAvailable(PaUtilRingBuffer * rbuf);
 
 /** Write data to the ring buffer.
 
@@ -110,7 +108,7 @@
 
  @return The number of bytes written.
 */
-long PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, long numBytes );
+	long PaUtil_WriteRingBuffer(PaUtilRingBuffer * rbuf, const void *data, long numBytes);
 
 /** Read data from the ring buffer.
 
@@ -122,7 +120,7 @@
 
  @return The number of bytes read.
 */
-long PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, long numBytes );
+	long PaUtil_ReadRingBuffer(PaUtilRingBuffer * rbuf, void *data, long numBytes);
 
 /** Get address of region(s) to which we can write data.
 
@@ -144,9 +142,7 @@
 
  @return The room available to be written or numBytes, whichever is smaller.
 */
-long PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, long numBytes,
-                                       void **dataPtr1, long *sizePtr1,
-                                       void **dataPtr2, long *sizePtr2 );
+	long PaUtil_GetRingBufferWriteRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2);
 
 /** Advance the write index to the next location to be written.
 
@@ -156,7 +152,7 @@
 
  @return The new position.
 */
-long PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, long numBytes );
+	long PaUtil_AdvanceRingBufferWriteIndex(PaUtilRingBuffer * rbuf, long numBytes);
 
 /** Get address of region(s) from which we can write data.
 
@@ -178,9 +174,7 @@
 
  @return The number of bytes available for reading.
 */
-long PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, long numBytes,
-                                      void **dataPtr1, long *sizePtr1,
-                                      void **dataPtr2, long *sizePtr2 );
+	long PaUtil_GetRingBufferReadRegions(PaUtilRingBuffer * rbuf, long numBytes, void **dataPtr1, long *sizePtr1, void **dataPtr2, long *sizePtr2);
 
 /** Advance the read index to the next location to be read.
 
@@ -190,9 +184,9 @@
 
  @return The new position.
 */
-long PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, long numBytes );
+	long PaUtil_AdvanceRingBufferReadIndex(PaUtilRingBuffer * rbuf, long numBytes);
 
 #ifdef __cplusplus
 }
-#endif /* __cplusplus */
-#endif /* PA_RINGBUFFER_H */
+#endif							/* __cplusplus */
+#endif							/* PA_RINGBUFFER_H */



More information about the Freeswitch-svn mailing list