Branch data Line data Source code
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 : : #ifndef MICROPY_INCLUDED_PY_RINGBUF_H 27 : : #define MICROPY_INCLUDED_PY_RINGBUF_H 28 : : 29 : : #include <stddef.h> 30 : : #include <stdint.h> 31 : : 32 : : #ifdef _MSC_VER 33 : : #include "py/mpconfig.h" // For inline. 34 : : #endif 35 : : 36 : : typedef struct _ringbuf_t { 37 : : uint8_t *buf; 38 : : uint16_t size; 39 : : uint16_t iget; 40 : : uint16_t iput; 41 : : } ringbuf_t; 42 : : 43 : : // Static initialization: 44 : : // byte buf_array[N]; 45 : : // ringbuf_t buf = {buf_array, sizeof(buf_array)}; 46 : : 47 : : // Dynamic initialization. This needs to become findable as a root pointer! 48 : : #define ringbuf_alloc(r, sz) \ 49 : : { \ 50 : : (r)->buf = m_new(uint8_t, sz); \ 51 : : (r)->size = sz; \ 52 : : (r)->iget = (r)->iput = 0; \ 53 : : } 54 : : 55 : 594 : static inline int ringbuf_get(ringbuf_t *r) { 56 [ + - ]: 594 : if (r->iget == r->iput) { 57 : : return -1; 58 : : } 59 : 594 : uint8_t v = r->buf[r->iget++]; 60 [ + + ]: 594 : if (r->iget >= r->size) { 61 : 2 : r->iget = 0; 62 : : } 63 : 594 : return v; 64 : : } 65 : : 66 : : static inline int ringbuf_peek(ringbuf_t *r) { 67 : : if (r->iget == r->iput) { 68 : : return -1; 69 : : } 70 : : return r->buf[r->iget]; 71 : : } 72 : : 73 : 596 : static inline int ringbuf_put(ringbuf_t *r, uint8_t v) { 74 : 596 : uint32_t iput_new = r->iput + 1; 75 [ + + ]: 596 : if (iput_new >= r->size) { 76 : 2 : iput_new = 0; 77 : : } 78 [ + - ]: 596 : if (iput_new == r->iget) { 79 : : return -1; 80 : : } 81 : 596 : r->buf[r->iput] = v; 82 : 596 : r->iput = iput_new; 83 : 596 : return 0; 84 : : } 85 : : 86 : 18 : static inline size_t ringbuf_free(ringbuf_t *r) { 87 : 18 : return (r->size + r->iget - r->iput - 1) % r->size; 88 : : } 89 : : 90 : 18 : static inline size_t ringbuf_avail(ringbuf_t *r) { 91 : 18 : return (r->size + r->iput - r->iget) % r->size; 92 : : } 93 : : 94 : : // Note: big-endian. No-op if not enough room available for both bytes. 95 : : int ringbuf_get16(ringbuf_t *r); 96 : : int ringbuf_peek16(ringbuf_t *r); 97 : : int ringbuf_put16(ringbuf_t *r, uint16_t v); 98 : : 99 : : #endif // MICROPY_INCLUDED_PY_RINGBUF_H