ringbuf.h 2.6 KB

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