lwrb.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /**
  2. * \file lwrb.h
  3. * \brief LwRB - Lightweight ring buffer
  4. */
  5. /*
  6. * Copyright (c) 2024 Tilen MAJERLE
  7. *
  8. * Permission is hereby granted, free of charge, to any person
  9. * obtaining a copy of this software and associated documentation
  10. * files (the "Software"), to deal in the Software without restriction,
  11. * including without limitation the rights to use, copy, modify, merge,
  12. * publish, distribute, sublicense, and/or sell copies of the Software,
  13. * and to permit persons to whom the Software is furnished to do so,
  14. * subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  21. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22. * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  23. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  24. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  25. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  26. * OTHER DEALINGS IN THE SOFTWARE.
  27. *
  28. * This file is part of LwRB - Lightweight ring buffer library.
  29. *
  30. * Author: Tilen MAJERLE <[email protected]>
  31. * Version: v3.2.0
  32. */
  33. #ifndef LWRB_HDR_H
  34. #define LWRB_HDR_H
  35. #include <stddef.h>
  36. #include <stdint.h>
  37. #include <string.h>
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif /* __cplusplus */
  41. /**
  42. * \defgroup LWRB Lightweight ring buffer manager
  43. * \brief Lightweight ring buffer manager
  44. * \{
  45. */
  46. #if !defined(LWRB_DISABLE_ATOMIC) || __DOXYGEN__
  47. #include <stdatomic.h>
  48. /**
  49. * \brief Atomic type for size variable.
  50. * Default value is set to be `unsigned 32-bits` type
  51. */
  52. typedef atomic_ulong lwrb_sz_atomic_t;
  53. /**
  54. * \brief Size variable for all library operations.
  55. * Default value is set to be `unsigned 32-bits` type
  56. */
  57. typedef unsigned long lwrb_sz_t;
  58. #else
  59. typedef unsigned long lwrb_sz_atomic_t;
  60. typedef unsigned long lwrb_sz_t;
  61. #endif
  62. /**
  63. * \brief Event type for buffer operations
  64. */
  65. typedef enum {
  66. LWRB_EVT_READ, /*!< Read event */
  67. LWRB_EVT_WRITE, /*!< Write event */
  68. LWRB_EVT_RESET, /*!< Reset event */
  69. } lwrb_evt_type_t;
  70. /**
  71. * \brief Buffer structure forward declaration
  72. */
  73. struct lwrb;
  74. /**
  75. * \brief Event callback function type
  76. * \param[in] buff: Buffer handle for event
  77. * \param[in] evt: Event type
  78. * \param[in] bp: Number of bytes written or read (when used), depends on event type
  79. */
  80. typedef void (*lwrb_evt_fn)(struct lwrb* buff, lwrb_evt_type_t evt, lwrb_sz_t bp);
  81. /* List of flags */
  82. #define LWRB_FLAG_READ_ALL ((uint16_t)0x0001)
  83. #define LWRB_FLAG_WRITE_ALL ((uint16_t)0x0001)
  84. /**
  85. * \brief Buffer structure
  86. */
  87. typedef struct lwrb {
  88. uint8_t* buff; /*!< Pointer to buffer data. Buffer is considered initialized when `buff != NULL` and `size > 0` */
  89. lwrb_sz_t size; /*!< Size of buffer data. Size of actual buffer is `1` byte less than value holds */
  90. lwrb_sz_atomic_t r_ptr; /*!< Next read pointer.
  91. Buffer is considered empty when `r == w` and full when `w == r - 1` */
  92. lwrb_sz_atomic_t w_ptr; /*!< Next write pointer.
  93. Buffer is considered empty when `r == w` and full when `w == r - 1` */
  94. lwrb_evt_fn evt_fn; /*!< Pointer to event callback function */
  95. void* arg; /*!< Event custom user argument */
  96. } lwrb_t;
  97. uint8_t lwrb_init(lwrb_t* buff, void* buffdata, lwrb_sz_t size);
  98. uint8_t lwrb_is_ready(lwrb_t* buff);
  99. void lwrb_free(lwrb_t* buff);
  100. void lwrb_reset(lwrb_t* buff);
  101. void lwrb_set_evt_fn(lwrb_t* buff, lwrb_evt_fn fn);
  102. void lwrb_set_arg(lwrb_t* buff, void* arg);
  103. void* lwrb_get_arg(lwrb_t* buff);
  104. /* Read/Write functions */
  105. lwrb_sz_t lwrb_write(lwrb_t* buff, const void* data, lwrb_sz_t btw);
  106. lwrb_sz_t lwrb_read(lwrb_t* buff, void* data, lwrb_sz_t btr);
  107. lwrb_sz_t lwrb_peek(const lwrb_t* buff, lwrb_sz_t skip_count, void* data, lwrb_sz_t btp);
  108. /* Extended read/write functions */
  109. uint8_t lwrb_write_ex(lwrb_t* buff, const void* data, lwrb_sz_t btw, lwrb_sz_t* bwritten, uint16_t flags);
  110. uint8_t lwrb_read_ex(lwrb_t* buff, void* data, lwrb_sz_t btr, lwrb_sz_t* bread, uint16_t flags);
  111. /* Buffer size information */
  112. lwrb_sz_t lwrb_get_free(const lwrb_t* buff);
  113. lwrb_sz_t lwrb_get_full(const lwrb_t* buff);
  114. /* Read data block management */
  115. void* lwrb_get_linear_block_read_address(const lwrb_t* buff);
  116. lwrb_sz_t lwrb_get_linear_block_read_length(const lwrb_t* buff);
  117. lwrb_sz_t lwrb_skip(lwrb_t* buff, lwrb_sz_t len);
  118. /* Write data block management */
  119. void* lwrb_get_linear_block_write_address(const lwrb_t* buff);
  120. lwrb_sz_t lwrb_get_linear_block_write_length(const lwrb_t* buff);
  121. lwrb_sz_t lwrb_advance(lwrb_t* buff, lwrb_sz_t len);
  122. /* Search in buffer */
  123. uint8_t lwrb_find(const lwrb_t* buff, const void* bts, lwrb_sz_t len, lwrb_sz_t start_offset, lwrb_sz_t* found_idx);
  124. lwrb_sz_t lwrb_overwrite(lwrb_t* buff, const void* data, lwrb_sz_t btw);
  125. lwrb_sz_t lwrb_move(lwrb_t* dest, lwrb_t* src);
  126. /**
  127. * \}
  128. */
  129. #ifdef __cplusplus
  130. }
  131. #endif /* __cplusplus */
  132. #endif /* LWRB_HDR_H */