|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2016 Paul Sokolovsky |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "ringbuf.h" |
| 28 | + |
| 29 | +// Dynamic initialization. This should be accessible from a root pointer. |
| 30 | +// capacity is the number of bytes the ring buffer can hold. The actual |
| 31 | +// size of the buffer is one greater than that, due to how the buffer |
| 32 | +// handles empty and full statuses. |
| 33 | +bool ringbuf_alloc(ringbuf_t *r, size_t capacity, bool long_lived) { |
| 34 | + r->buf = gc_alloc(capacity + 1, false, long_lived); |
| 35 | + r->size = capacity + 1; |
| 36 | + r->iget = r->iput = 0; |
| 37 | + return r->buf != NULL; |
| 38 | +} |
| 39 | + |
| 40 | +size_t ringbuf_capacity(ringbuf_t *r) { |
| 41 | + return r->size - 1; |
| 42 | +} |
| 43 | + |
| 44 | +// Returns -1 if buffer is empty, else returns byte fetched. |
| 45 | +int ringbuf_get(ringbuf_t *r) { |
| 46 | + if (r->iget == r->iput) { |
| 47 | + return -1; |
| 48 | + } |
| 49 | + uint8_t v = r->buf[r->iget++]; |
| 50 | + if (r->iget >= r->size) { |
| 51 | + r->iget = 0; |
| 52 | + } |
| 53 | + return v; |
| 54 | +} |
| 55 | + |
| 56 | +// Returns -1 if no room in buffer, else returns 0. |
| 57 | +int ringbuf_put(ringbuf_t *r, uint8_t v) { |
| 58 | + uint32_t iput_new = r->iput + 1; |
| 59 | + if (iput_new >= r->size) { |
| 60 | + iput_new = 0; |
| 61 | + } |
| 62 | + if (iput_new == r->iget) { |
| 63 | + return -1; |
| 64 | + } |
| 65 | + r->buf[r->iput] = v; |
| 66 | + r->iput = iput_new; |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +void ringbuf_clear(ringbuf_t *r) { |
| 71 | + r->iput = r->iget = 0; |
| 72 | +} |
| 73 | + |
| 74 | +// Number of free slots that can be written. |
| 75 | +size_t ringbuf_free(ringbuf_t *r) { |
| 76 | + return (r->size + r->iget - r->iput - 1) % r->size; |
| 77 | +} |
| 78 | + |
| 79 | +// Number of bytes available to read. |
| 80 | +size_t ringbuf_avail(ringbuf_t *r) { |
| 81 | + return (r->size + r->iput - r->iget) % r->size; |
| 82 | +} |
| 83 | + |
| 84 | +// If the ring buffer fills up, not all bytes will be written. |
| 85 | +// Returns how many bytes were successfully written. |
| 86 | +size_t ringbuf_put_n(ringbuf_t* r, uint8_t* buf, size_t bufsize) |
| 87 | +{ |
| 88 | + for(size_t i=0; i < bufsize; i++) { |
| 89 | + if ( ringbuf_put(r, buf[i]) < 0 ) { |
| 90 | + // If ringbuf is full, give up and return how many bytes |
| 91 | + // we wrote so far. |
| 92 | + return i; |
| 93 | + } |
| 94 | + } |
| 95 | + return bufsize; |
| 96 | +} |
| 97 | + |
| 98 | +// Returns how many bytes were fetched. |
| 99 | +size_t ringbuf_get_n(ringbuf_t* r, uint8_t* buf, size_t bufsize) |
| 100 | +{ |
| 101 | + for(size_t i=0; i < bufsize; i++) { |
| 102 | + int b = ringbuf_get(r); |
| 103 | + if (b < 0) { |
| 104 | + return i; |
| 105 | + } |
| 106 | + buf[i] = b; |
| 107 | + } |
| 108 | + return bufsize; |
| 109 | +} |
0 commit comments