Update portaudio to the latest

Previous version uses OSMemoryBarrier() on Apple but this is deprecated
in macOS 10.12.

Also, modify PRESUBMIT.py so that this patch is not blocked by
CheckLongLines.

Bug: chromium:1322548
Change-Id: I02c6b7682730abf718e88fc7f1bd4dcd7d347da6
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/256580
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Daniel.L (Byoungchan) Lee <daniel.l@hpcnt.com>
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#36873}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index cf7d261..79610366 100755
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -953,7 +953,7 @@
   # Skip long-lines check for DEPS and GN files.
   build_file_filter_list = (r'.+\.gn$', r'.+\.gni$', 'DEPS')
   # Also we will skip most checks for third_party directory.
-  third_party_filter_list = (r'^third_party[\\\/].+', )
+  third_party_filter_list = (r'(^|.*[\\\/])third_party[\\\/].+', )
   eighty_char_sources = lambda x: input_api.FilterSourceFile(
       x,
       files_to_skip=build_file_filter_list + objc_filter_list +
diff --git a/modules/audio_device/mac/audio_device_mac.cc b/modules/audio_device/mac/audio_device_mac.cc
index bf324da..2aa2841 100644
--- a/modules/audio_device/mac/audio_device_mac.cc
+++ b/modules/audio_device/mac/audio_device_mac.cc
@@ -227,7 +227,7 @@
 
   if (_paRenderBuffer == NULL) {
     _paRenderBuffer = new PaUtilRingBuffer;
-    PaRingBufferSize bufSize = -1;
+    ring_buffer_size_t bufSize = -1;
     bufSize = PaUtil_InitializeRingBuffer(
         _paRenderBuffer, sizeof(SInt16), _renderBufSizeSamples, _renderBufData);
     if (bufSize == -1) {
@@ -247,7 +247,7 @@
 
   if (_paCaptureBuffer == NULL) {
     _paCaptureBuffer = new PaUtilRingBuffer;
-    PaRingBufferSize bufSize = -1;
+    ring_buffer_size_t bufSize = -1;
     bufSize =
         PaUtil_InitializeRingBuffer(_paCaptureBuffer, sizeof(Float32),
                                     _captureBufSizeSamples, _captureBufData);
@@ -2219,7 +2219,7 @@
     }
   }
 
-  PaRingBufferSize bufSizeSamples =
+  ring_buffer_size_t bufSizeSamples =
       PaUtil_GetRingBufferReadAvailable(_paRenderBuffer);
 
   int32_t renderDelayUs =
@@ -2237,7 +2237,7 @@
 OSStatus AudioDeviceMac::implOutConverterProc(UInt32* numberDataPackets,
                                               AudioBufferList* data) {
   RTC_DCHECK(data->mNumberBuffers == 1);
-  PaRingBufferSize numSamples =
+  ring_buffer_size_t numSamples =
       *numberDataPackets * _outDesiredFormat.mChannelsPerFrame;
 
   data->mBuffers->mNumberChannels = _outDesiredFormat.mChannelsPerFrame;
@@ -2288,7 +2288,7 @@
     return 0;
   }
 
-  PaRingBufferSize bufSizeSamples =
+  ring_buffer_size_t bufSizeSamples =
       PaUtil_GetRingBufferReadAvailable(_paCaptureBuffer);
 
   int32_t captureDelayUs =
@@ -2301,9 +2301,9 @@
   _captureDelayUs = captureDelayUs;
 
   RTC_DCHECK(inputData->mNumberBuffers == 1);
-  PaRingBufferSize numSamples = inputData->mBuffers->mDataByteSize *
-                                _inStreamFormat.mChannelsPerFrame /
-                                _inStreamFormat.mBytesPerPacket;
+  ring_buffer_size_t numSamples = inputData->mBuffers->mDataByteSize *
+                                  _inStreamFormat.mChannelsPerFrame /
+                                  _inStreamFormat.mBytesPerPacket;
   PaUtil_WriteRingBuffer(_paCaptureBuffer, inputData->mBuffers->mData,
                          numSamples);
 
@@ -2318,7 +2318,7 @@
 OSStatus AudioDeviceMac::implInConverterProc(UInt32* numberDataPackets,
                                              AudioBufferList* data) {
   RTC_DCHECK(data->mNumberBuffers == 1);
-  PaRingBufferSize numSamples =
+  ring_buffer_size_t numSamples =
       *numberDataPackets * _inStreamFormat.mChannelsPerFrame;
 
   while (PaUtil_GetRingBufferReadAvailable(_paCaptureBuffer) < numSamples) {
@@ -2341,7 +2341,7 @@
 
   // Pass the read pointer directly to the converter to avoid a memcpy.
   void* dummyPtr;
-  PaRingBufferSize dummySize;
+  ring_buffer_size_t dummySize;
   PaUtil_GetRingBufferReadRegions(_paCaptureBuffer, numSamples,
                                   &data->mBuffers->mData, &numSamples,
                                   &dummyPtr, &dummySize);
@@ -2356,7 +2356,7 @@
 }
 
 bool AudioDeviceMac::RenderWorkerThread() {
-  PaRingBufferSize numSamples =
+  ring_buffer_size_t numSamples =
       ENGINE_PLAY_BUF_SIZE_IN_SAMPLES * _outDesiredFormat.mChannelsPerFrame;
   while (PaUtil_GetRingBufferWriteAvailable(_paRenderBuffer) -
              _renderDelayOffsetSamples <
diff --git a/modules/third_party/portaudio/README.chromium b/modules/third_party/portaudio/README.chromium
index 7a15b2e..722dd94 100644
--- a/modules/third_party/portaudio/README.chromium
+++ b/modules/third_party/portaudio/README.chromium
@@ -1,8 +1,8 @@
 Name: Portaudio library for mac
 Short Name: portaudio
-URL: https://app.assembla.com/spaces/portaudio/git/source/master/src/common
-Version: 0
-Date: 2018-02-01
+URL: https://github.com/PortAudio/portaudio/tree/master/src/common
+Version: 9d8563100d841300f1689b186d131347ad43a0f6
+Date: 2022-04-12
 License: Custom license
 License File: LICENSE
 Security Critical: yes
@@ -10,3 +10,5 @@
 Description:
 Part of portaudio library to operate with memory barriers and ring buffer.
 
+Local changes:
+  - Minor formatting to make 'git cl format' happy.
diff --git a/modules/third_party/portaudio/pa_memorybarrier.h b/modules/third_party/portaudio/pa_memorybarrier.h
index c1040d1..f8c1852 100644
--- a/modules/third_party/portaudio/pa_memorybarrier.h
+++ b/modules/third_party/portaudio/pa_memorybarrier.h
@@ -64,13 +64,23 @@
 #define MODULES_THIRD_PARTY_PORTAUDIO_PA_MEMORYBARRIER_H_
 
 #if defined(__APPLE__)
+/* Support for the atomic library was added in C11.
+ */
+#if (__STDC_VERSION__ < 201112L) || defined(__STDC_NO_ATOMICS__)
 #include <libkern/OSAtomic.h>
 /* Here are the memory barrier functions. Mac OS X only provides
    full memory barriers, so the three types of barriers are the same,
-   however, these barriers are superior to compiler-based ones. */
+   however, these barriers are superior to compiler-based ones.
+   These were deprecated in MacOS 10.12. */
 #define PaUtil_FullMemoryBarrier() OSMemoryBarrier()
 #define PaUtil_ReadMemoryBarrier() OSMemoryBarrier()
 #define PaUtil_WriteMemoryBarrier() OSMemoryBarrier()
+#else
+#include <stdatomic.h>
+#define PaUtil_FullMemoryBarrier() atomic_thread_fence(memory_order_seq_cst)
+#define PaUtil_ReadMemoryBarrier() atomic_thread_fence(memory_order_acquire)
+#define PaUtil_WriteMemoryBarrier() atomic_thread_fence(memory_order_release)
+#endif
 #elif defined(__GNUC__)
 /* GCC >= 4.1 has built-in intrinsics. We'll use those */
 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
@@ -98,7 +108,7 @@
 #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.
+#           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
 #elif (_MSC_VER >= 1400) && !defined(_WIN32_WCE)
@@ -106,6 +116,8 @@
 #pragma intrinsic(_ReadWriteBarrier)
 #pragma intrinsic(_ReadBarrier)
 #pragma intrinsic(_WriteBarrier)
+/* note that MSVC intrinsics _ReadWriteBarrier(), _ReadBarrier(),
+ * _WriteBarrier() are just compiler barriers *not* memory barriers */
 #define PaUtil_FullMemoryBarrier() _ReadWriteBarrier()
 #define PaUtil_ReadMemoryBarrier() _ReadBarrier()
 #define PaUtil_WriteMemoryBarrier() _WriteBarrier()
@@ -125,7 +137,7 @@
 #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.
+#       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
 
diff --git a/modules/third_party/portaudio/pa_ringbuffer.c b/modules/third_party/portaudio/pa_ringbuffer.c
index fc1053c..b978d54 100644
--- a/modules/third_party/portaudio/pa_ringbuffer.c
+++ b/modules/third_party/portaudio/pa_ringbuffer.c
@@ -1,5 +1,5 @@
 /*
- * $Id: pa_ringbuffer.c 1421 2009-11-18 16:09:05Z bjornroche $
+ * $Id$
  * Portable Audio I/O Library
  * Ring Buffer utility.
  *
@@ -7,7 +7,7 @@
  * modified for SMP safety on Mac OS X by Bjorn Roche
  * modified for SMP safety on Linux by Leland Lucius
  * also, allowed for const where possible
- * modified for multiple-byte-sized data elements by Sven Fischer 
+ * modified for multiple-byte-sized data elements by Sven Fischer
  *
  * Note that this is safe only for a single-thread reader and a
  * single-thread writer.
@@ -37,13 +37,13 @@
  */
 
 /*
- * The text above constitutes the entire PortAudio license; however, 
+ * The text above constitutes the entire PortAudio license; however,
  * the PortAudio community also makes the following non-binding requests:
  *
  * Any person wishing to distribute modifications to the Software is
  * requested to send the modifications to the original developer so that
- * they can be incorporated into the canonical version. It is also 
- * requested that these non-binding requests be included along with the 
+ * they can be incorporated into the canonical version. It is also
+ * requested that these non-binding requests be included along with the
  * license above.
  */
 
@@ -52,21 +52,19 @@
  @ingroup common_src
 */
 
-#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
+#include "pa_ringbuffer.h"
 #include <string.h>
-#include "modules/third_party/portaudio/pa_memorybarrier.h"
-#include "modules/third_party/portaudio/pa_ringbuffer.h"
+#include "pa_memorybarrier.h"
 
 /***************************************************************************
  * Initialize FIFO.
  * elementCount must be power of 2, returns -1 if not.
  */
-PaRingBufferSize PaUtil_InitializeRingBuffer(PaUtilRingBuffer* rbuf,
-                                             PaRingBufferSize elementSizeBytes,
-                                             PaRingBufferSize elementCount,
-                                             void* dataPtr) {
+ring_buffer_size_t PaUtil_InitializeRingBuffer( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementSizeBytes, ring_buffer_size_t elementCount, void *dataPtr )
+{
     if( ((elementCount-1) & elementCount) != 0) return -1; /* Not Power of two. */
     rbuf->bufferSize = elementCount;
     rbuf->buffer = (char *)dataPtr;
@@ -79,21 +77,19 @@
 
 /***************************************************************************
 ** Return number of elements available for reading. */
-PaRingBufferSize PaUtil_GetRingBufferReadAvailable( PaUtilRingBuffer *rbuf )
+ring_buffer_size_t PaUtil_GetRingBufferReadAvailable( const PaUtilRingBuffer *rbuf )
 {
-    PaUtil_ReadMemoryBarrier();
     return ( (rbuf->writeIndex - rbuf->readIndex) & rbuf->bigMask );
 }
 /***************************************************************************
 ** Return number of elements available for writing. */
-PaRingBufferSize PaUtil_GetRingBufferWriteAvailable( PaUtilRingBuffer *rbuf )
+ring_buffer_size_t PaUtil_GetRingBufferWriteAvailable( const PaUtilRingBuffer *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. */
+** Clear buffer. Should only be called when buffer is NOT being read or written. */
 void PaUtil_FlushRingBuffer( PaUtilRingBuffer *rbuf )
 {
     rbuf->writeIndex = rbuf->readIndex = 0;
@@ -105,21 +101,19 @@
 ** If non-contiguous, size2 will be the size of second region.
 ** Returns room available to be written or elementCount, whichever is smaller.
 */
-PaRingBufferSize PaUtil_GetRingBufferWriteRegions(PaUtilRingBuffer* rbuf,
-                                                  PaRingBufferSize elementCount,
-                                                  void** dataPtr1,
-                                                  PaRingBufferSize* sizePtr1,
-                                                  void** dataPtr2,
-                                                  PaRingBufferSize* sizePtr2) {
-    PaRingBufferSize   index;
-    PaRingBufferSize   available = PaUtil_GetRingBufferWriteAvailable( rbuf );
+ring_buffer_size_t PaUtil_GetRingBufferWriteRegions( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount,
+                                       void **dataPtr1, ring_buffer_size_t *sizePtr1,
+                                       void **dataPtr2, ring_buffer_size_t *sizePtr2 )
+{
+    ring_buffer_size_t   index;
+    ring_buffer_size_t   available = PaUtil_GetRingBufferWriteAvailable( rbuf );
     if( elementCount > available ) elementCount = available;
     /* Check to see if write is not contiguous. */
     index = rbuf->writeIndex & rbuf->smallMask;
     if( (index + elementCount) > rbuf->bufferSize )
     {
         /* Write data in two blocks that wrap the buffer. */
-        PaRingBufferSize   firstHalf = rbuf->bufferSize - index;
+        ring_buffer_size_t   firstHalf = rbuf->bufferSize - index;
         *dataPtr1 = &rbuf->buffer[index*rbuf->elementSizeBytes];
         *sizePtr1 = firstHalf;
         *dataPtr2 = &rbuf->buffer[0];
@@ -132,16 +126,21 @@
         *dataPtr2 = NULL;
         *sizePtr2 = 0;
     }
+
+    if( available )
+        PaUtil_FullMemoryBarrier(); /* (write-after-read) => full barrier */
+
     return elementCount;
 }
 
 
 /***************************************************************************
 */
-PaRingBufferSize PaUtil_AdvanceRingBufferWriteIndex(
-    PaUtilRingBuffer* rbuf,
-    PaRingBufferSize elementCount) {
-    /* we need to ensure that previous writes are seen before we update the write index */
+ring_buffer_size_t PaUtil_AdvanceRingBufferWriteIndex( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount )
+{
+    /* ensure that previous writes are seen before we update the write index
+       (write after write)
+    */
     PaUtil_WriteMemoryBarrier();
     return rbuf->writeIndex = (rbuf->writeIndex + elementCount) & rbuf->bigMask;
 }
@@ -150,23 +149,21 @@
 ** Get address of region(s) from which we can read data.
 ** If the region is contiguous, size2 will be zero.
 ** If non-contiguous, size2 will be the size of second region.
-** Returns room available to be written or elementCount, whichever is smaller.
+** Returns room available to be read or elementCount, whichever is smaller.
 */
-PaRingBufferSize PaUtil_GetRingBufferReadRegions(PaUtilRingBuffer* rbuf,
-                                                 PaRingBufferSize elementCount,
-                                                 void** dataPtr1,
-                                                 PaRingBufferSize* sizePtr1,
-                                                 void** dataPtr2,
-                                                 PaRingBufferSize* sizePtr2) {
-    PaRingBufferSize   index;
-    PaRingBufferSize   available = PaUtil_GetRingBufferReadAvailable( rbuf );
+ring_buffer_size_t PaUtil_GetRingBufferReadRegions( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount,
+                                void **dataPtr1, ring_buffer_size_t *sizePtr1,
+                                void **dataPtr2, ring_buffer_size_t *sizePtr2 )
+{
+    ring_buffer_size_t   index;
+    ring_buffer_size_t   available = PaUtil_GetRingBufferReadAvailable( rbuf ); /* doesn't use memory barrier */
     if( elementCount > available ) elementCount = available;
     /* Check to see if read is not contiguous. */
     index = rbuf->readIndex & rbuf->smallMask;
     if( (index + elementCount) > rbuf->bufferSize )
     {
         /* Write data in two blocks that wrap the buffer. */
-        PaRingBufferSize firstHalf = rbuf->bufferSize - index;
+        ring_buffer_size_t firstHalf = rbuf->bufferSize - index;
         *dataPtr1 = &rbuf->buffer[index*rbuf->elementSizeBytes];
         *sizePtr1 = firstHalf;
         *dataPtr2 = &rbuf->buffer[0];
@@ -179,24 +176,28 @@
         *dataPtr2 = NULL;
         *sizePtr2 = 0;
     }
+
+    if( available )
+        PaUtil_ReadMemoryBarrier(); /* (read-after-read) => read barrier */
+
     return elementCount;
 }
 /***************************************************************************
 */
-PaRingBufferSize PaUtil_AdvanceRingBufferReadIndex(
-    PaUtilRingBuffer* rbuf,
-    PaRingBufferSize elementCount) {
-    /* we need to ensure that previous writes are always seen before updating the index. */
-    PaUtil_WriteMemoryBarrier();
+ring_buffer_size_t PaUtil_AdvanceRingBufferReadIndex( PaUtilRingBuffer *rbuf, ring_buffer_size_t elementCount )
+{
+    /* ensure that previous reads (copies out of the ring buffer) are always completed before updating (writing) the read index.
+       (write-after-read) => full barrier
+    */
+    PaUtil_FullMemoryBarrier();
     return rbuf->readIndex = (rbuf->readIndex + elementCount) & rbuf->bigMask;
 }
 
 /***************************************************************************
 ** Return elements written. */
-PaRingBufferSize PaUtil_WriteRingBuffer(PaUtilRingBuffer* rbuf,
-                                        const void* data,
-                                        PaRingBufferSize elementCount) {
-    PaRingBufferSize size1, size2, numWritten;
+ring_buffer_size_t PaUtil_WriteRingBuffer( PaUtilRingBuffer *rbuf, const void *data, ring_buffer_size_t elementCount )
+{
+    ring_buffer_size_t size1, size2, numWritten;
     void *data1, *data2;
     numWritten = PaUtil_GetRingBufferWriteRegions( rbuf, elementCount, &data1, &size1, &data2, &size2 );
     if( size2 > 0 )
@@ -216,10 +217,9 @@
 
 /***************************************************************************
 ** Return elements read. */
-PaRingBufferSize PaUtil_ReadRingBuffer(PaUtilRingBuffer* rbuf,
-                                       void* data,
-                                       PaRingBufferSize elementCount) {
-    PaRingBufferSize size1, size2, numRead;
+ring_buffer_size_t PaUtil_ReadRingBuffer( PaUtilRingBuffer *rbuf, void *data, ring_buffer_size_t elementCount )
+{
+    ring_buffer_size_t size1, size2, numRead;
     void *data1, *data2;
     numRead = PaUtil_GetRingBufferReadRegions( rbuf, elementCount, &data1, &size1, &data2, &size2 );
     if( size2 > 0 )
diff --git a/modules/third_party/portaudio/pa_ringbuffer.h b/modules/third_party/portaudio/pa_ringbuffer.h
index de5722c..aa9d0aa 100644
--- a/modules/third_party/portaudio/pa_ringbuffer.h
+++ b/modules/third_party/portaudio/pa_ringbuffer.h
@@ -1,7 +1,7 @@
 #ifndef MODULES_THIRD_PARTY_PORTAUDIO_PA_RINGBUFFER_H_
 #define MODULES_THIRD_PARTY_PORTAUDIO_PA_RINGBUFFER_H_
 /*
- * $Id: pa_ringbuffer.h 1421 2009-11-18 16:09:05Z bjornroche $
+ * $Id$
  * Portable Audio I/O Library
  * Ring Buffer utility.
  *
@@ -65,19 +65,23 @@
  The memory area used to store the buffer elements must be allocated by
  the client prior to calling PaUtil_InitializeRingBuffer() and must outlive
  the use of the ring buffer.
+
+ @note The ring buffer functions are not normally exposed in the PortAudio
+ libraries. If you want to call them then you will need to add pa_ringbuffer.c
+ to your application source code.
 */
 
 #if defined(__APPLE__)
 #include <sys/types.h>
-typedef int32_t PaRingBufferSize;
+typedef int32_t ring_buffer_size_t;
 #elif defined(__GNUC__)
-typedef long PaRingBufferSize;
+typedef long ring_buffer_size_t;
 #elif (_MSC_VER >= 1400)
-typedef long PaRingBufferSize;
+typedef long ring_buffer_size_t;
 #elif defined(_MSC_VER) || defined(__BORLANDC__)
-typedef long PaRingBufferSize;
+typedef long ring_buffer_size_t;
 #else
-typedef long PaRingBufferSize;
+typedef long ring_buffer_size_t;
 #endif
 
 #ifdef __cplusplus
@@ -85,38 +89,43 @@
 #endif /* __cplusplus */
 
 typedef struct PaUtilRingBuffer {
-  PaRingBufferSize bufferSize; /**< Number of elements in FIFO. Power of 2. Set
-                                  by PaUtil_InitRingBuffer. */
-  PaRingBufferSize writeIndex; /**< Index of next writable element. Set by
-                                  PaUtil_AdvanceRingBufferWriteIndex. */
-  PaRingBufferSize readIndex;  /**< Index of next readable element. Set by
-                                  PaUtil_AdvanceRingBufferReadIndex. */
-  PaRingBufferSize bigMask;    /**< Used for wrapping indices with extra bit to
-                                  distinguish full/empty. */
-  PaRingBufferSize smallMask;  /**< Used for fitting indices to buffer. */
-  PaRingBufferSize elementSizeBytes; /**< Number of bytes per element. */
+  ring_buffer_size_t bufferSize; /**< Number of elements in FIFO. Power of 2.
+                                    Set by PaUtil_InitRingBuffer. */
+  volatile ring_buffer_size_t
+      writeIndex; /**< Index of next writable element. Set by
+                     PaUtil_AdvanceRingBufferWriteIndex. */
+  volatile ring_buffer_size_t
+      readIndex;                /**< Index of next readable element. Set by
+                                   PaUtil_AdvanceRingBufferReadIndex. */
+  ring_buffer_size_t bigMask;   /**< Used for wrapping indices with extra bit to
+                                   distinguish full/empty. */
+  ring_buffer_size_t smallMask; /**< Used for fitting indices to buffer. */
+  ring_buffer_size_t elementSizeBytes; /**< Number of bytes per element. */
   char* buffer; /**< Pointer to the buffer containing the actual data. */
 } PaUtilRingBuffer;
 
-/** Initialize Ring Buffer.
+/** Initialize Ring Buffer to empty state ready to have elements written to it.
 
  @param rbuf The ring buffer.
 
  @param elementSizeBytes The size of a single data element in bytes.
 
- @param elementCount The number of elements in the buffer (must be power of 2).
+ @param elementCount The number of elements in the buffer (must be a power of
+ 2).
 
  @param dataPtr A pointer to a previously allocated area where the data
  will be maintained.  It must be elementCount*elementSizeBytes long.
 
  @return -1 if elementCount is not a power of 2, otherwise 0.
 */
-PaRingBufferSize PaUtil_InitializeRingBuffer(PaUtilRingBuffer* rbuf,
-                                             PaRingBufferSize elementSizeBytes,
-                                             PaRingBufferSize elementCount,
-                                             void* dataPtr);
+ring_buffer_size_t PaUtil_InitializeRingBuffer(
+    PaUtilRingBuffer* rbuf,
+    ring_buffer_size_t elementSizeBytes,
+    ring_buffer_size_t elementCount,
+    void* dataPtr);
 
-/** Clear buffer. Should only be called when buffer is NOT being read.
+/** Reset buffer to empty. Should only be called when buffer is NOT being read
+ or written.
 
  @param rbuf The ring buffer.
 */
@@ -128,7 +137,8 @@
 
  @return The number of elements available for writing.
 */
-PaRingBufferSize PaUtil_GetRingBufferWriteAvailable(PaUtilRingBuffer* rbuf);
+ring_buffer_size_t PaUtil_GetRingBufferWriteAvailable(
+    const PaUtilRingBuffer* rbuf);
 
 /** Retrieve the number of elements available in the ring buffer for reading.
 
@@ -136,7 +146,8 @@
 
  @return The number of elements available for reading.
 */
-PaRingBufferSize PaUtil_GetRingBufferReadAvailable(PaUtilRingBuffer* rbuf);
+ring_buffer_size_t PaUtil_GetRingBufferReadAvailable(
+    const PaUtilRingBuffer* rbuf);
 
 /** Write data to the ring buffer.
 
@@ -148,9 +159,9 @@
 
  @return The number of elements written.
 */
-PaRingBufferSize PaUtil_WriteRingBuffer(PaUtilRingBuffer* rbuf,
-                                        const void* data,
-                                        PaRingBufferSize elementCount);
+ring_buffer_size_t PaUtil_WriteRingBuffer(PaUtilRingBuffer* rbuf,
+                                          const void* data,
+                                          ring_buffer_size_t elementCount);
 
 /** Read data from the ring buffer.
 
@@ -162,9 +173,9 @@
 
  @return The number of elements read.
 */
-PaRingBufferSize PaUtil_ReadRingBuffer(PaUtilRingBuffer* rbuf,
-                                       void* data,
-                                       PaRingBufferSize elementCount);
+ring_buffer_size_t PaUtil_ReadRingBuffer(PaUtilRingBuffer* rbuf,
+                                         void* data,
+                                         ring_buffer_size_t elementCount);
 
 /** Get address of region(s) to which we can write data.
 
@@ -186,12 +197,13 @@
 
  @return The room available to be written or elementCount, whichever is smaller.
 */
-PaRingBufferSize PaUtil_GetRingBufferWriteRegions(PaUtilRingBuffer* rbuf,
-                                                  PaRingBufferSize elementCount,
-                                                  void** dataPtr1,
-                                                  PaRingBufferSize* sizePtr1,
-                                                  void** dataPtr2,
-                                                  PaRingBufferSize* sizePtr2);
+ring_buffer_size_t PaUtil_GetRingBufferWriteRegions(
+    PaUtilRingBuffer* rbuf,
+    ring_buffer_size_t elementCount,
+    void** dataPtr1,
+    ring_buffer_size_t* sizePtr1,
+    void** dataPtr2,
+    ring_buffer_size_t* sizePtr2);
 
 /** Advance the write index to the next location to be written.
 
@@ -201,11 +213,11 @@
 
  @return The new position.
 */
-PaRingBufferSize PaUtil_AdvanceRingBufferWriteIndex(
+ring_buffer_size_t PaUtil_AdvanceRingBufferWriteIndex(
     PaUtilRingBuffer* rbuf,
-    PaRingBufferSize elementCount);
+    ring_buffer_size_t elementCount);
 
-/** Get address of region(s) from which we can write data.
+/** Get address of region(s) from which we can read data.
 
  @param rbuf The ring buffer.
 
@@ -225,12 +237,13 @@
 
  @return The number of elements available for reading.
 */
-PaRingBufferSize PaUtil_GetRingBufferReadRegions(PaUtilRingBuffer* rbuf,
-                                                 PaRingBufferSize elementCount,
-                                                 void** dataPtr1,
-                                                 PaRingBufferSize* sizePtr1,
-                                                 void** dataPtr2,
-                                                 PaRingBufferSize* sizePtr2);
+ring_buffer_size_t PaUtil_GetRingBufferReadRegions(
+    PaUtilRingBuffer* rbuf,
+    ring_buffer_size_t elementCount,
+    void** dataPtr1,
+    ring_buffer_size_t* sizePtr1,
+    void** dataPtr2,
+    ring_buffer_size_t* sizePtr2);
 
 /** Advance the read index to the next location to be read.
 
@@ -240,9 +253,9 @@
 
  @return The new position.
 */
-PaRingBufferSize PaUtil_AdvanceRingBufferReadIndex(
+ring_buffer_size_t PaUtil_AdvanceRingBufferReadIndex(
     PaUtilRingBuffer* rbuf,
-    PaRingBufferSize elementCount);
+    ring_buffer_size_t elementCount);
 
 #ifdef __cplusplus
 }