title: How can I improve the performance of my ring buffer code?
body: <p>I am using a ringbuffer to hold samples for a streaming audio application. I copied the ringbuffer implementation from  Ken Greenebaum's Audio Anecdotes 2 book. </p>
  
  <p>After running Intel's Vtune analyzer on my code, it tells me that most of the time is being spent in the functions <code>getSamplesAvailable()</code> and <code>getSpaceAvailable()</code>. </p>
  
  <p>Can anyone advise as to how I might optimise these functions?</p>
  
  <pre><code>RingBuffer::getSamplesAvailable(void)
  {
     int count = (mTail - mHead + mSize) % mSize;
     return(count);
  }
  
  unsigned int RingBuffer::getSpaceAvailable(void)
  {
     int free = (mHead - mTail + mSize - 1)%mSize;
     int underMark = mHighWaterMark - getSamplesAvailable();
     int spaceAvailable = min(underMark, free);
     return(spaceAvailable);
  }
  
  int RingBuffer::push(int value)
  {
     int status = 1;
     if(getSpaceAvailable()) {
        // next two operations do NOT have to be atomic!
        // do NOT have to worry about collision with _tail
        mBuffer[mTail] = value;   // store value
        mTail = ++mTail % mSize;  // increment tail
    } else {
       status = 0;
    }
    return(status);
  }
  
  int RingBuffer::pop(int *value)
  {
     int status = 1;
     if(getSamplesAvailable()) {
         *value = mBuffer[mHead];
         mHead = ++mHead % mSize;  // increment head
     } else {
        status = 0;
     }
     return(status);
  }
  </code></pre>