123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464 |
- /*
- * RingBuffer.cpp
- * Lock-free ring buffer data structure.
- * One thread can be consumer and one can be producer
- *
- * Created by Ben Allison on 11/10/07.
- * Copyright 2007 Nullsoft, Inc. All rights reserved.
- *
- */
- #include "RingBuffer.h"
- #include "foundation/error.h"
- #include "foundation/types.h"
- #include <stdlib.h>
- #include <string.h>
- #include <stdint.h>
- #define MIN(a,b) ((a<b)?(a):(b))
- RingBuffer::RingBuffer()
- {
- ringBuffer = 0;
- ringBufferSize = 0;
- ringBufferUsed = 0;
- ringWritePosition = 0;
- ringReadPosition = 0;
- }
- RingBuffer::~RingBuffer()
- {
- if ( ringBuffer )
- free( ringBuffer );
- ringBuffer = 0;
- }
- void RingBuffer::Reset()
- {
- if ( ringBuffer )
- free( ringBuffer );
- ringBuffer = 0;
- }
- bool RingBuffer::reserve( size_t bytes )
- {
- Reset();
- ringBufferSize = bytes;
- ringBuffer = (char *)calloc( ringBufferSize, sizeof( char ) );
- if ( !ringBuffer )
- return false;
- clear();
- return true;
- }
- int RingBuffer::expand( size_t bytes )
- {
- if ( bytes > ringBufferSize )
- {
- char *new_buffer = (char *)realloc( ringBuffer, bytes );
- if ( !new_buffer )
- return NErr_OutOfMemory;
- size_t write_offset = ringReadPosition - ringBuffer;
- size_t read_offset = ringWritePosition - ringBuffer;
- /* update write pointer for the new buffer */
- ringWritePosition = new_buffer + write_offset;
- if ( write_offset > read_offset || !ringBufferUsed ) /* ringBufferUsed will resolve the ambiguity when ringWritePosition == ringReadPosition */
- {
- /* the ring buffer looks like [ RXXXW ], so we don't need to move anything.
- Just update the read pointer */
- ringReadPosition = new_buffer + write_offset;
- }
- else
- {
- /* [XXW RXX] needs to become [XXW RXX] */
- size_t end_bytes = ringBufferSize - read_offset; // number of bytes that we need to relocate (the RXX portion)
- char *new_read_pointer = &new_buffer[ bytes - end_bytes ];
- memmove( new_read_pointer, ringReadPosition, end_bytes );
- ringReadPosition = new_read_pointer; /* update read pointer */
- }
- ringBufferSize = bytes;
- ringBuffer = new_buffer;
- return NErr_Success;
- }
- else
- return NErr_NoAction;
- }
- bool RingBuffer::empty() const
- {
- return ( ringBufferUsed == 0 );
- }
- size_t RingBuffer::read( void *dest, size_t len )
- {
- int8_t *out = (int8_t *)dest; // lets us do pointer math easier
- size_t toCopy = MIN( ringBufferUsed, len );
- size_t copied = 0;
- len -= toCopy;
- // read to the end of the ring buffer
- size_t end = ringBufferSize - ( ringReadPosition - ringBuffer );
- size_t read1 = MIN( end, toCopy );
- memcpy( out, ringReadPosition, read1 );
- copied += read1;
- ringReadPosition += read1;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- // update positions
- ringBufferUsed -= read1;
- toCopy -= read1;
- out = (int8_t *)out + read1;
- // see if we still have more to read after wrapping around
- if ( toCopy )
- {
- memcpy( out, ringReadPosition, toCopy );
- copied += toCopy;
- ringReadPosition += toCopy;
- ringBufferUsed -= toCopy;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- }
- return copied;
- }
- size_t RingBuffer::at( size_t offset, void *dest, size_t len ) const
- {
- size_t toCopy = ringBufferUsed;
- // make a local copy of this so we don't blow the original
- char *ringReadPosition = this->ringReadPosition;
- /* --- do a "dummy read" to deal with the offset request --- */
- size_t dummy_end = ringBufferSize - ( ringReadPosition - ringBuffer );
- offset = MIN( toCopy, offset );
- size_t read0 = MIN( dummy_end, offset );
- ringReadPosition += read0;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- // update positions
- toCopy -= read0;
- offset -= read0;
- // do second-half read (wraparound)
- if ( offset )
- {
- ringReadPosition += offset;
- toCopy -= offset;
- }
- // dummy read done
- /* --- set up destination buffer and copy size --- */
- int8_t *out = (int8_t *)dest; // lets us do pointer math easier
- if ( toCopy > len )
- toCopy = len;
- size_t copied = 0;
- /* --- read to the end of the ring buffer --- */
- size_t end = ringBufferSize - ( ringReadPosition - ringBuffer );
- size_t read1 = MIN( end, toCopy );
- memcpy( out, ringReadPosition, read1 );
- copied += read1;
- ringReadPosition += read1;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- // update positions
- toCopy -= read1;
- out = (int8_t *)out + read1;
- /* --- see if we still have more to read after wrapping around --- */
- if ( toCopy )
- {
- memcpy( out, ringReadPosition, toCopy );
- copied += toCopy;
- ringReadPosition += toCopy;
- }
- return copied;
- }
- size_t RingBuffer::peek( void *dest, size_t len ) const
- {
- int8_t *out = (int8_t *)dest; // lets us do pointer math easier
- size_t toCopy = MIN( ringBufferUsed, len );
- size_t copied = 0;
- // make a local copy of this so we don't blow the original
- char *ringReadPosition = this->ringReadPosition;
- // read to the end of the ring buffer
- size_t end = ringBufferSize - ( ringReadPosition - ringBuffer );
- size_t read1 = MIN( end, toCopy );
- memcpy( out, ringReadPosition, read1 );
- copied += read1;
- ringReadPosition += read1;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- // update positions
- toCopy -= read1;
- out = (int8_t *)out + read1;
- // see if we still have more to read after wrapping around
- if ( toCopy )
- {
- memcpy( out, ringReadPosition, toCopy );
- copied += toCopy;
- ringReadPosition += toCopy;
- }
- return copied;
- }
- size_t RingBuffer::advance( size_t len )
- {
- size_t toCopy = MIN( ringBufferUsed, len );
- size_t copied = 0;
- len -= toCopy;
- // read to the end of the ring buffer
- size_t end = ringBufferSize - ( ringReadPosition - ringBuffer );
- size_t read1 = MIN( end, toCopy );
- copied += read1;
- ringReadPosition += read1;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- // update positions
- toCopy -= read1;
- ringBufferUsed -= read1;
- // see if we still have more to read after wrapping around
- if ( toCopy )
- {
- copied += toCopy;
- ringReadPosition += toCopy;
- ringBufferUsed -= toCopy;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- }
- return copied;
- }
- size_t RingBuffer::avail() const
- {
- return ringBufferSize - ringBufferUsed;
- }
- size_t RingBuffer::write( const void *buffer, size_t bytes )
- {
- size_t used = ringBufferUsed;
- size_t avail = ringBufferSize - used;
- bytes = MIN( avail, bytes );
- // write to the end of the ring buffer
- size_t end = ringBufferSize - ( ringWritePosition - ringBuffer );
- size_t copied = 0;
- size_t write1 = MIN( end, bytes );
- memcpy( ringWritePosition, buffer, write1 );
- copied += write1;
- ringWritePosition += write1;
- if ( ringWritePosition == ringBuffer + ringBufferSize )
- ringWritePosition = ringBuffer;
- // update positions
- ringBufferUsed += write1;
- bytes -= write1;
- buffer = (const int8_t *)buffer + write1;
- // see if we still have more to write after wrapping around
- if ( bytes )
- {
- memcpy( ringWritePosition, buffer, bytes );
- copied += bytes;
- ringWritePosition += bytes;
- ringBufferUsed += bytes;
- if ( ringWritePosition == ringBuffer + ringBufferSize )
- ringWritePosition = ringBuffer;
- }
- return copied;
- }
- size_t RingBuffer::drain( Drainer *drainer, size_t max_bytes )
- {
- // read to the end of the ring buffer
- size_t used = ringBufferUsed;
- size_t bytes = used;
- bytes = MIN( bytes, max_bytes );
- size_t copied = 0;
- size_t end = ringBufferSize - ( ringReadPosition - ringBuffer );
- size_t drain1 = MIN( end, bytes );
- if ( !drain1 )
- return 0;
- size_t read1 = drainer->Write( ringReadPosition, drain1 );
- if ( read1 == 0 )
- return 0;
- copied += read1;
- ringReadPosition += read1;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- // update positions
- ringBufferUsed -= read1;
- bytes -= read1;
- // see if we still have more to read after wrapping around
- if ( drain1 == read1 && bytes )
- {
- size_t read2 = drainer->Write( ringReadPosition, bytes );
- copied += read2;
- ringReadPosition += read2;
- ringBufferUsed -= read2;
- if ( ringReadPosition == ringBuffer + ringBufferSize )
- ringReadPosition = ringBuffer;
- }
- return copied;
- }
- size_t RingBuffer::fill( Filler *filler, size_t max_bytes )
- {
- // write to the end of the ring buffer
- size_t used = ringBufferUsed;
- size_t bytes = ringBufferSize - used;
- bytes = MIN( bytes, max_bytes );
- size_t copied = 0;
- size_t end = ringBufferSize - ( ringWritePosition - ringBuffer );
- size_t fill1 = MIN( end, bytes );
- if ( !fill1 )
- return 0;
- size_t write1 = filler->Read( ringWritePosition, fill1 );
- if ( write1 == 0 )
- return 0;
- copied += write1;
- ringWritePosition += write1;
- if ( ringWritePosition == ringBuffer + ringBufferSize )
- ringWritePosition = ringBuffer;
- // update positions
- ringBufferUsed += write1;
- bytes -= write1;
- // see if we still have more to write after wrapping around
- if ( fill1 == write1 && bytes )
- {
- size_t write2 = filler->Read( ringWritePosition, bytes );
- copied += write2;
- ringWritePosition += write2;
- ringBufferUsed += write2;
- if ( ringWritePosition == ringBuffer + ringBufferSize )
- ringWritePosition = ringBuffer;
- }
- return copied;
- }
- size_t RingBuffer::size() const
- {
- return ringBufferUsed;
- }
- void RingBuffer::clear()
- {
- ringBufferUsed = 0;
- ringWritePosition = ringBuffer;
- ringReadPosition = ringBuffer;
- }
- void *RingBuffer::LockBuffer()
- {
- return ringBuffer;
- }
- void RingBuffer::UnlockBuffer( size_t written )
- {
- ringWritePosition = ringBuffer + written;
- ringBufferUsed = written;
- }
- size_t RingBuffer::write_position() const
- {
- return (size_t)ringWritePosition;
- }
- size_t RingBuffer::read_position() const
- {
- return (size_t)ringReadPosition;
- }
- void RingBuffer::get_read_buffer( size_t bytes, const void **buffer, size_t *bytes_available ) const
- {
- size_t toCopy = MIN( ringBufferUsed, bytes );
- // read to the end of the ring buffer
- size_t end = ringBufferSize - ( ringReadPosition - ringBuffer );
- *bytes_available = MIN( end, toCopy );
- *buffer = ringReadPosition;
- }
|