Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
115 ira 1
/* Copyright (c) 2005, Ira W. Snyder (devel@irasnyder.com)
2
 * All rights reserved.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy
5
 * of this software and associated documentation files (the "Software"), to
6
 * deal in the Software without restriction, including without limitation the
7
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8
 * sell copies of the Software, and to permit persons to whom the Software is
9
 * furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in
12
 * all copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20
 * IN THE SOFTWARE.
21
 */
22
 
23
 
24
#include "RingBuffer.h"
25
 
26
#include <memory.h>
27
#include <assert.h>
28
#include <stdint.h>
29
 
30
/* Constructor -- it should be obvious what it does */
31
RingBuffer::RingBuffer (int32_t buffer_size)
32
{
33
    /* Fire an assert if buffer size is too small.
34
     * This _is_ safe to remove, since RingBuffer will still work as expected,
35
     * but you probably made a mistake if you're doing this. */
36
    assert (buffer_size > 0);
37
 
38
    this->buffer = NULL;
39
    this->buffer_size = buffer_size;
40
 
41
    buffer = new int8_t[buffer_size];
42
 
43
    r_ptr = buffer;
44
    w_ptr = buffer;
45
 
46
    buffer_begin = buffer;
47
    buffer_end = buffer + buffer_size;
48
 
49
    available_bytes = 0;
50
}
51
 
52
/* Destructor -- cleans up a RingBuffer object.
53
 * NOTE: If "rb" is your RingBuffer, call "delete rb;" to free it */
54
RingBuffer::~RingBuffer ()
55
{
56
    delete[] buffer;
57
    buffer = NULL;
58
 
59
    buffer_size = 0;
60
}
61
 
62
/* Returns the actual number of bytes copied into dst. */
63
int32_t RingBuffer::read (int8_t *dst, int32_t bytes)
64
{
65
    int32_t retVal = 0;
66
    int32_t nbytes = 0;
67
    int32_t chkd_bytes = bytes;
68
 
69
    /* Not enough bytes */
70
    if (chkd_bytes > available_bytes)
71
    {
72
        retVal = available_bytes; //we can only return what we have...
73
        chkd_bytes = available_bytes;
74
    }
75
    else /* We've got plenty of data */
76
    {
77
        /* Check if we need to wrap */
78
        if ((r_ptr + chkd_bytes) > buffer_end)
79
        {
80
            nbytes = buffer_end - r_ptr; //bytes until the end of the buffer
81
 
82
            /* Copy the bytes from r_ptr to the end of the buffer. */
83
            memcpy ((void*)dst, (void*)r_ptr, nbytes);
84
            r_ptr = buffer_begin; //wrap
85
 
86
            /* Copy the remaining bytes */
87
            memcpy ((void*)(dst+nbytes), (void*)r_ptr, chkd_bytes-nbytes);
88
            r_ptr += chkd_bytes-nbytes;
89
        }
90
        else /* No wrapping needs to happen, copy normally */
91
        {
92
            memcpy ((void*)dst, (void*)r_ptr, chkd_bytes);
93
            r_ptr += chkd_bytes;
94
        }
95
 
96
        /* Update the number of bytes remaining */
97
        available_bytes -= chkd_bytes;
98
        retVal = chkd_bytes;
99
    }
100
 
101
    return retVal;
102
}
103
 
104
/* TRUE if we DID NOT loose data
105
 * FALSE if we DID loose data */
106
bool RingBuffer::write (int8_t *src, int32_t bytes)
107
{
108
    bool retVal = true;
109
    int32_t  nbytes = 0;
110
    int8_t *chkd_src = src;    // "checked" source pointer
111
    int32_t  chkd_bytes = bytes; // "checked" number of bytes
112
 
113
    assert (src != NULL); //make sure src is not null
114
    assert (chkd_bytes > 0);   //make sure that there are some bytes (safe to disable)
115
 
116
    /* Check if we are going to loose data.
117
     * Set the return value to FALSE if we will
118
     * Leave the return value alone otherwise */
119
    if ((bytes + available_bytes) >= buffer_size)
120
        retVal = false;
121
 
122
    /* Check if we will "wrap" more than once.
123
     * Stop this behaivior! */
124
    if (bytes > buffer_size)
125
    {
126
        chkd_src += (bytes-buffer_size); //only the non-overwritten data
127
        chkd_bytes = buffer_size; //the maximum number of bytes
128
    }
129
 
130
    /* Check if w_ptr will "cross over" r_ptr.
131
     * Move r_ptr if necessary to overwrite old bytes */
132
    if ((w_ptr + chkd_bytes) > r_ptr)
133
    {
134
        /* Set the new value of r_ptr */
135
        r_ptr += (w_ptr + chkd_bytes) - r_ptr;
136
 
137
        /* Check if the new value should have wrapped.
138
         * Wrap it if necessary. */
139
        if (r_ptr > buffer_end)
140
        {
141
            nbytes = r_ptr - buffer_end;
142
            r_ptr = buffer_begin + nbytes;
143
        }
144
    }
145
 
146
    /* Write data to the buffer.
147
     * A "wrap" will happen here, so write in two pieces. */
148
    if ((w_ptr + chkd_bytes) > buffer_end)
149
    {
150
        nbytes = buffer_end - w_ptr; //bytes until the end of the buffer
151
 
152
        /* Write the data from w_ptr to the end of the buffer */
153
        memcpy ((void*)w_ptr, (void*)chkd_src, nbytes);
154
        w_ptr = buffer_begin; //wrap the w_ptr
155
 
156
        /* Write the rest of the data */
157
        memcpy ((void*)w_ptr, (void*)(chkd_src+nbytes), chkd_bytes-nbytes);
158
        w_ptr += chkd_bytes-nbytes;
159
    }
160
    else /* No "wrap" will happen, so copy normally */
161
    {
162
        memcpy ((void*)w_ptr, (void*)chkd_src, chkd_bytes);
163
        w_ptr += chkd_bytes;
164
    }
165
 
166
    /* Add to the bytes stored in the buffer. */
167
    available_bytes += chkd_bytes;
168
 
169
    /* Since we already wrapped properly, make sure that
170
     * available_bytes doesn't get too large. */
171
    if (available_bytes > buffer_size)
172
        available_bytes = buffer_size;
173
 
174
    return retVal;
175
}
176
 
177
/* Return the number of bytes available in the buffer. */
178
int32_t RingBuffer::numBytes()
179
{
180
    return available_bytes;
181
}
182