ringbuffer.c 12.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2021, RT-Thread Development Team
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2012-09-30     Bernard      first version.
9
 * 2013-05-08     Grissiom     reimplement
B
bernard 已提交
10
 * 2016-08-18     heyuanjie    add interface
11
 * 2021-07-20     arminker     fix write_index bug in function rt_ringbuffer_put_force
12
 * 2021-08-14     Jackistang   add comments for function interface.
13 14 15 16 17 18
 */

#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>

B
bernard 已提交
19 20 21 22 23 24 25 26 27 28 29 30
rt_inline enum rt_ringbuffer_state rt_ringbuffer_status(struct rt_ringbuffer *rb)
{
    if (rb->read_index == rb->write_index)
    {
        if (rb->read_mirror == rb->write_mirror)
            return RT_RINGBUFFER_EMPTY;
        else
            return RT_RINGBUFFER_FULL;
    }
    return RT_RINGBUFFER_HALFFULL;
}

31
/**
32
 * @brief Initialize the ring buffer object.
J
Jackistang 已提交
33
 *
34
 * @param rb        A pointer to the ring buffer object.
35
 * @param pool      A pointer to the buffer.
36
 * @param size      The size of the buffer in bytes.
37
 */
38 39
void rt_ringbuffer_init(struct rt_ringbuffer *rb,
                        rt_uint8_t           *pool,
G
Grissiom 已提交
40
                        rt_int16_t            size)
41 42
{
    RT_ASSERT(rb != RT_NULL);
B
bernard 已提交
43
    RT_ASSERT(size > 0);
44 45

    /* initialize read and write index */
G
Grissiom 已提交
46 47
    rb->read_mirror = rb->read_index = 0;
    rb->write_mirror = rb->write_index = 0;
48 49 50 51 52 53 54

    /* set buffer pool and size */
    rb->buffer_ptr = pool;
    rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);
}
RTM_EXPORT(rt_ringbuffer_init);

55
/**
56
 * @brief Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will discard out-of-range data.
J
Jackistang 已提交
57
 *
58
 * @param rb            A pointer to the ring buffer object.
59 60
 * @param ptr           A pointer to the data buffer.
 * @param length        The size of data in bytes.
J
Jackistang 已提交
61
 *
62
 * @return Return the data size we put into the ring buffer.
63
 */
64 65 66 67 68
rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
                            const rt_uint8_t     *ptr,
                            rt_uint16_t           length)
{
    rt_uint16_t size;
69

70 71 72
    RT_ASSERT(rb != RT_NULL);

    /* whether has enough space */
G
Grissiom 已提交
73
    size = rt_ringbuffer_space_len(rb);
74 75 76 77

    /* no space */
    if (size == 0)
        return 0;
G
Grissiom 已提交
78

79 80 81 82
    /* drop some data */
    if (size < length)
        length = size;

G
Grissiom 已提交
83
    if (rb->buffer_size - rb->write_index > length)
84 85
    {
        /* read_index - write_index = empty space */
G
Grissiom 已提交
86 87 88 89 90
        memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
        /* this should not cause overflow because there is enough space for
         * length of data in current mirror */
        rb->write_index += length;
        return length;
91
    }
G
Grissiom 已提交
92 93 94 95 96 97 98 99 100 101 102

    memcpy(&rb->buffer_ptr[rb->write_index],
           &ptr[0],
           rb->buffer_size - rb->write_index);
    memcpy(&rb->buffer_ptr[0],
           &ptr[rb->buffer_size - rb->write_index],
           length - (rb->buffer_size - rb->write_index));

    /* we are going into the other side of the mirror */
    rb->write_mirror = ~rb->write_mirror;
    rb->write_index = length - (rb->buffer_size - rb->write_index);
103 104 105 106 107

    return length;
}
RTM_EXPORT(rt_ringbuffer_put);

108
/**
109
 * @brief Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will overwrite the existing data in the ring buffer.
J
Jackistang 已提交
110
 *
111
 * @param rb            A pointer to the ring buffer object.
112 113
 * @param ptr           A pointer to the data buffer.
 * @param length        The size of data in bytes.
114
 *
115
 * @return Return the data size we put into the ring buffer.
116 117 118 119 120
 */
rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
                            const rt_uint8_t     *ptr,
                            rt_uint16_t           length)
{
121
    rt_uint16_t space_length;
122 123 124

    RT_ASSERT(rb != RT_NULL);

125
    space_length = rt_ringbuffer_space_len(rb);
126

还_没_想_好's avatar
还_没_想_好 已提交
127 128 129
    if (length > rb->buffer_size)
    {
        ptr = &ptr[length - rb->buffer_size];
130
        length = rb->buffer_size;
还_没_想_好's avatar
还_没_想_好 已提交
131
    }
132 133 134 135 136 137 138 139 140

    if (rb->buffer_size - rb->write_index > length)
    {
        /* read_index - write_index = empty space */
        memcpy(&rb->buffer_ptr[rb->write_index], ptr, length);
        /* this should not cause overflow because there is enough space for
         * length of data in current mirror */
        rb->write_index += length;

141
        if (length > space_length)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
            rb->read_index = rb->write_index;

        return length;
    }

    memcpy(&rb->buffer_ptr[rb->write_index],
           &ptr[0],
           rb->buffer_size - rb->write_index);
    memcpy(&rb->buffer_ptr[0],
           &ptr[rb->buffer_size - rb->write_index],
           length - (rb->buffer_size - rb->write_index));

    /* we are going into the other side of the mirror */
    rb->write_mirror = ~rb->write_mirror;
    rb->write_index = length - (rb->buffer_size - rb->write_index);

158
    if (length > space_length)
159
    {
160 161
        if (rb->write_index <= rb->read_index)
            rb->read_mirror = ~rb->read_mirror;
162 163 164 165 166 167 168
        rb->read_index = rb->write_index;
    }

    return length;
}
RTM_EXPORT(rt_ringbuffer_put_force);

169
/**
170
 * @brief Get data from the ring buffer.
J
Jackistang 已提交
171
 *
172
 * @param rb            A pointer to the ring buffer.
173
 * @param ptr           A pointer to the data buffer.
174
 * @param length        The size of the data we want to read from the ring buffer.
J
Jackistang 已提交
175
 *
176
 * @return Return the data size we read from the ring buffer.
177 178 179 180 181 182 183 184
 */
rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
                            rt_uint8_t           *ptr,
                            rt_uint16_t           length)
{
    rt_size_t size;

    RT_ASSERT(rb != RT_NULL);
G
Grissiom 已提交
185

186
    /* whether has enough data  */
G
Grissiom 已提交
187
    size = rt_ringbuffer_data_len(rb);
188 189 190 191

    /* no data */
    if (size == 0)
        return 0;
G
Grissiom 已提交
192

193 194 195 196
    /* less data */
    if (size < length)
        length = size;

G
Grissiom 已提交
197
    if (rb->buffer_size - rb->read_index > length)
198 199
    {
        /* copy all of data */
G
Grissiom 已提交
200 201 202 203 204
        memcpy(ptr, &rb->buffer_ptr[rb->read_index], length);
        /* this should not cause overflow because there is enough space for
         * length of data in current mirror */
        rb->read_index += length;
        return length;
205
    }
G
Grissiom 已提交
206 207 208 209 210 211 212 213 214 215 216

    memcpy(&ptr[0],
           &rb->buffer_ptr[rb->read_index],
           rb->buffer_size - rb->read_index);
    memcpy(&ptr[rb->buffer_size - rb->read_index],
           &rb->buffer_ptr[0],
           length - (rb->buffer_size - rb->read_index));

    /* we are going into the other side of the mirror */
    rb->read_mirror = ~rb->read_mirror;
    rb->read_index = length - (rb->buffer_size - rb->read_index);
217 218 219 220 221

    return length;
}
RTM_EXPORT(rt_ringbuffer_get);

Comeon_fly's avatar
Comeon_fly 已提交
222
/**
223
 * @brief Get the first readable byte of the ring buffer.
J
Jackistang 已提交
224
 *
225
 * @param rb        A pointer to the ringbuffer.
226
 * @param ptr       When this function return, *ptr is a pointer to the first readable byte of the ring buffer.
J
Jackistang 已提交
227
 *
228 229 230
 * @note It is recommended to read only one byte, otherwise it may cause buffer overflow.
 *
 * @return Return the size of the ring buffer.
Comeon_fly's avatar
Comeon_fly 已提交
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
 */
rt_size_t rt_ringbuffer_peak(struct rt_ringbuffer *rb, rt_uint8_t **ptr)
{
    RT_ASSERT(rb != RT_NULL);

    *ptr = RT_NULL;

    /* whether has enough data  */
    rt_size_t size = rt_ringbuffer_data_len(rb);

    /* no data */
    if (size == 0)
        return 0;

    *ptr = &rb->buffer_ptr[rb->read_index];

    if(rb->buffer_size - rb->read_index > size)
    {
        rb->read_index += size;
        return size;
    }

    size = rb->buffer_size - rb->read_index;

    /* we are going into the other side of the mirror */
    rb->read_mirror = ~rb->read_mirror;
    rb->read_index = 0;

    return size;
}
RTM_EXPORT(rt_ringbuffer_peak);

263
/**
264
 * @brief Put a byte into the ring buffer. If ring buffer is full, this operation will fail.
J
Jackistang 已提交
265
 *
266
 * @param rb        A pointer to the ring buffer object.
267
 * @param ch        A byte put into the ring buffer.
J
Jackistang 已提交
268
 *
269
 * @return Return the data size we put into the ring buffer. The ring buffer is full if returns 0. Otherwise, it will return 1.
270 271 272 273 274 275
 */
rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb, const rt_uint8_t ch)
{
    RT_ASSERT(rb != RT_NULL);

    /* whether has enough space */
G
Grissiom 已提交
276
    if (!rt_ringbuffer_space_len(rb))
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        return 0;

    rb->buffer_ptr[rb->write_index] = ch;

    /* flip mirror */
    if (rb->write_index == rb->buffer_size-1)
    {
        rb->write_mirror = ~rb->write_mirror;
        rb->write_index = 0;
    }
    else
    {
        rb->write_index++;
    }

    return 1;
}
RTM_EXPORT(rt_ringbuffer_putchar);

296
/**
297
 * @brief Put a byte into the ring buffer. If ring buffer is full, it will discard an old data and put into a new data.
298
 *
299
 * @param rb        A pointer to the ring buffer object.
300
 * @param ch        A byte put into the ring buffer.
J
Jackistang 已提交
301
 *
302
 * @return Return the data size we put into the ring buffer. Always return 1.
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
 */
rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb, const rt_uint8_t ch)
{
    enum rt_ringbuffer_state old_state;

    RT_ASSERT(rb != RT_NULL);

    old_state = rt_ringbuffer_status(rb);

    rb->buffer_ptr[rb->write_index] = ch;

    /* flip mirror */
    if (rb->write_index == rb->buffer_size-1)
    {
        rb->write_mirror = ~rb->write_mirror;
        rb->write_index = 0;
        if (old_state == RT_RINGBUFFER_FULL)
        {
            rb->read_mirror = ~rb->read_mirror;
            rb->read_index = rb->write_index;
        }
    }
    else
    {
        rb->write_index++;
        if (old_state == RT_RINGBUFFER_FULL)
            rb->read_index = rb->write_index;
    }

    return 1;
}
RTM_EXPORT(rt_ringbuffer_putchar_force);

336
/**
337
 * @brief Get a byte from the ring buffer.
J
Jackistang 已提交
338
 *
339 340
 * @param rb        The pointer to the ring buffer object.
 * @param ch        A pointer to the buffer, used to store one byte.
J
Jackistang 已提交
341
 *
342
 * @return 0    The ring buffer is empty.
343
 * @return 1    Success
344 345 346 347
 */
rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch)
{
    RT_ASSERT(rb != RT_NULL);
G
Grissiom 已提交
348

349
    /* ringbuffer is empty */
G
Grissiom 已提交
350
    if (!rt_ringbuffer_data_len(rb))
351 352
        return 0;

353
    /* put byte */
G
Grissiom 已提交
354 355 356 357 358 359 360 361 362 363 364
    *ch = rb->buffer_ptr[rb->read_index];

    if (rb->read_index == rb->buffer_size-1)
    {
        rb->read_mirror = ~rb->read_mirror;
        rb->read_index = 0;
    }
    else
    {
        rb->read_index++;
    }
365 366 367 368

    return 1;
}
RTM_EXPORT(rt_ringbuffer_getchar);
369

370
/**
371
 * @brief Get the size of data in the ring buffer in bytes.
J
Jackistang 已提交
372
 *
373
 * @param rb        The pointer to the ring buffer object.
J
Jackistang 已提交
374
 *
375
 * @return Return the size of data in the ring buffer in bytes.
B
bernard 已提交
376 377 378 379 380 381 382 383 384 385 386
 */
rt_size_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
{
    switch (rt_ringbuffer_status(rb))
    {
    case RT_RINGBUFFER_EMPTY:
        return 0;
    case RT_RINGBUFFER_FULL:
        return rb->buffer_size;
    case RT_RINGBUFFER_HALFFULL:
    default:
387 388 389 390 391
    {
        rt_size_t wi = rb->write_index, ri = rb->read_index;

        if (wi > ri)
            return wi - ri;
B
bernard 已提交
392
        else
393 394 395
            return rb->buffer_size - (ri - wi);
    }
    }
B
bernard 已提交
396 397 398
}
RTM_EXPORT(rt_ringbuffer_data_len);

399
/**
400
 * @brief Reset the ring buffer object, and clear all contents in the buffer.
J
Jackistang 已提交
401
 *
402
 * @param rb        A pointer to the ring buffer object.
B
bernard 已提交
403 404 405 406 407 408 409 410 411 412 413 414 415 416
 */
void rt_ringbuffer_reset(struct rt_ringbuffer *rb)
{
    RT_ASSERT(rb != RT_NULL);

    rb->read_mirror = 0;
    rb->read_index = 0;
    rb->write_mirror = 0;
    rb->write_index = 0;
}
RTM_EXPORT(rt_ringbuffer_reset);

#ifdef RT_USING_HEAP

417
/**
418
 * @brief Create a ring buffer object with a given size.
J
Jackistang 已提交
419
 *
420
 * @param size      The size of the buffer in bytes.
J
Jackistang 已提交
421
 *
422
 * @return Return a pointer to ring buffer object. When the return value is RT_NULL, it means this creation failed.
423
 */
J
Jackistang 已提交
424
struct rt_ringbuffer *rt_ringbuffer_create(rt_uint16_t size)
B
bernard 已提交
425 426 427 428
{
    struct rt_ringbuffer *rb;
    rt_uint8_t *pool;

429
    RT_ASSERT(size > 0);
B
bernard 已提交
430 431 432

    size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE);

433
    rb = (struct rt_ringbuffer *)rt_malloc(sizeof(struct rt_ringbuffer));
B
bernard 已提交
434 435 436
    if (rb == RT_NULL)
        goto exit;

437
    pool = (rt_uint8_t *)rt_malloc(size);
B
bernard 已提交
438 439 440
    if (pool == RT_NULL)
    {
        rt_free(rb);
441
        rb = RT_NULL;
B
bernard 已提交
442 443 444 445 446 447 448 449 450
        goto exit;
    }
    rt_ringbuffer_init(rb, pool, size);

exit:
    return rb;
}
RTM_EXPORT(rt_ringbuffer_create);

451
/**
452
 * @brief Destroy the ring buffer object, which is created by rt_ringbuffer_create() .
J
Jackistang 已提交
453
 *
454
 * @param rb        A pointer to the ring buffer object.
455
 */
B
bernard 已提交
456 457 458 459 460 461 462 463 464 465
void rt_ringbuffer_destroy(struct rt_ringbuffer *rb)
{
    RT_ASSERT(rb != RT_NULL);

    rt_free(rb->buffer_ptr);
    rt_free(rb);
}
RTM_EXPORT(rt_ringbuffer_destroy);

#endif