Subversion Repositories programming

Rev

Blame | Last modification | View Log | RSS feed

/* Copyright (c) 2005, Ira W. Snyder (devel@irasnyder.com)
 * All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */


#include "RingBuffer.h"

#include <memory.h>
#include <assert.h>
#include <stdint.h>

/* Constructor -- it should be obvious what it does */
RingBuffer::RingBuffer (int32_t buffer_size)
{
    /* Fire an assert if buffer size is too small.
     * This _is_ safe to remove, since RingBuffer will still work as expected,
     * but you probably made a mistake if you're doing this. */
    assert (buffer_size > 0);

    this->buffer = NULL;
    this->buffer_size = buffer_size;

    buffer = new int8_t[buffer_size];

    r_ptr = buffer;
    w_ptr = buffer;

    buffer_begin = buffer;
    buffer_end = buffer + buffer_size;

    available_bytes = 0;
}

/* Destructor -- cleans up a RingBuffer object.
 * NOTE: If "rb" is your RingBuffer, call "delete rb;" to free it */
RingBuffer::~RingBuffer ()
{
    delete[] buffer;
    buffer = NULL;

    buffer_size = 0;
}

/* Returns the actual number of bytes copied into dst. */
int32_t RingBuffer::read (int8_t *dst, int32_t bytes)
{
    int32_t retVal = 0;
    int32_t nbytes = 0;
    int32_t chkd_bytes = bytes;

    /* Not enough bytes */
    if (chkd_bytes > available_bytes)
    {
        retVal = available_bytes; //we can only return what we have...
        chkd_bytes = available_bytes;
    }
    else /* We've got plenty of data */
    {
        /* Check if we need to wrap */
        if ((r_ptr + chkd_bytes) > buffer_end)
        {
            nbytes = buffer_end - r_ptr; //bytes until the end of the buffer

            /* Copy the bytes from r_ptr to the end of the buffer. */
            memcpy ((void*)dst, (void*)r_ptr, nbytes);
            r_ptr = buffer_begin; //wrap

            /* Copy the remaining bytes */
            memcpy ((void*)(dst+nbytes), (void*)r_ptr, chkd_bytes-nbytes);
            r_ptr += chkd_bytes-nbytes;
        }
        else /* No wrapping needs to happen, copy normally */
        {
            memcpy ((void*)dst, (void*)r_ptr, chkd_bytes);
            r_ptr += chkd_bytes;
        }

        /* Update the number of bytes remaining */
        available_bytes -= chkd_bytes;
        retVal = chkd_bytes;
    }

    return retVal;
}

/* TRUE if we DID NOT loose data
 * FALSE if we DID loose data */
bool RingBuffer::write (int8_t *src, int32_t bytes)
{
    bool retVal = true;
    int32_t  nbytes = 0;
    int8_t *chkd_src = src;    // "checked" source pointer
    int32_t  chkd_bytes = bytes; // "checked" number of bytes

    assert (src != NULL); //make sure src is not null
    assert (chkd_bytes > 0);   //make sure that there are some bytes (safe to disable)
    
    /* Check if we are going to loose data.
     * Set the return value to FALSE if we will
     * Leave the return value alone otherwise */
    if ((bytes + available_bytes) >= buffer_size)
        retVal = false;

    /* Check if we will "wrap" more than once.
     * Stop this behaivior! */
    if (bytes > buffer_size)
    {
        chkd_src += (bytes-buffer_size); //only the non-overwritten data
        chkd_bytes = buffer_size; //the maximum number of bytes
    }

    /* Check if w_ptr will "cross over" r_ptr.
     * Move r_ptr if necessary to overwrite old bytes */
    if ((w_ptr + chkd_bytes) > r_ptr)
    {
        /* Set the new value of r_ptr */
        r_ptr += (w_ptr + chkd_bytes) - r_ptr;

        /* Check if the new value should have wrapped.
         * Wrap it if necessary. */
        if (r_ptr > buffer_end)
        {
            nbytes = r_ptr - buffer_end;
            r_ptr = buffer_begin + nbytes;
        }
    }

    /* Write data to the buffer.
     * A "wrap" will happen here, so write in two pieces. */
    if ((w_ptr + chkd_bytes) > buffer_end)
    {
        nbytes = buffer_end - w_ptr; //bytes until the end of the buffer

        /* Write the data from w_ptr to the end of the buffer */
        memcpy ((void*)w_ptr, (void*)chkd_src, nbytes);
        w_ptr = buffer_begin; //wrap the w_ptr

        /* Write the rest of the data */
        memcpy ((void*)w_ptr, (void*)(chkd_src+nbytes), chkd_bytes-nbytes);
        w_ptr += chkd_bytes-nbytes;
    }
    else /* No "wrap" will happen, so copy normally */
    {
        memcpy ((void*)w_ptr, (void*)chkd_src, chkd_bytes);
        w_ptr += chkd_bytes;
    }

    /* Add to the bytes stored in the buffer. */
    available_bytes += chkd_bytes;

    /* Since we already wrapped properly, make sure that
     * available_bytes doesn't get too large. */
    if (available_bytes > buffer_size)
        available_bytes = buffer_size;

    return retVal;
}

/* Return the number of bytes available in the buffer. */
int32_t RingBuffer::numBytes()
{
    return available_bytes;
}