ringbuf.h 3.0 KB

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