da_buf.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef DA_BUF_H_
  17. #define DA_BUF_H_
  18. #include "da_queue.h"
  19. #include "stdbool.h"
  20. struct instance;
  21. struct mbuf;
  22. typedef void (*mbuf_copy_t)(struct mbuf *, void *);
  23. struct mbuf {
  24. uint32_t magic; /* mbuf magic (const) */
  25. STAILQ_ENTRY(mbuf) next; /* next mbuf */
  26. uint8_t *pos; /* read marker */
  27. uint8_t *last; /* write marker */
  28. uint8_t *start; /* start of buffer (const) */
  29. uint8_t *end; /* end of buffer (const) */
  30. };
  31. STAILQ_HEAD(buf_stqh, mbuf);
  32. #define MBUF_MAGIC 0xdeadbeef
  33. /*
  34. * 最小的长度应该把msg头接收下来
  35. */
  36. #define MBUF_MIN_SIZE 256
  37. #define MBUF_MAX_SIZE 16777216
  38. #define DEF_MBUF_SIZE 16384
  39. static inline bool mbuf_empty(struct mbuf *mbuf) {
  40. return mbuf->pos == mbuf->last ? true : false;
  41. }
  42. static inline bool mbuf_full(struct mbuf *mbuf) {
  43. return mbuf->last == mbuf->end ? true : false;
  44. }
  45. int mbuf_init(struct instance *ins);
  46. int mbuf_deinit(void);
  47. struct mbuf *mbuf_get();
  48. void mbuf_put(struct mbuf *mbuf);
  49. void mbuf_rewind(struct mbuf *mbuf);
  50. uint32_t mbuf_length(struct mbuf *mbuf);
  51. uint32_t mbuf_size(struct mbuf *mbuf);
  52. size_t mbuf_data_size(void);
  53. void mbuf_insert(struct buf_stqh *mhdr, struct mbuf *mbuf);
  54. void mbuf_remove(struct buf_stqh *mhdr, struct mbuf *mbuf);
  55. void mbuf_copy(struct mbuf *mbuf, uint8_t *pos, size_t n);
  56. struct mbuf *mbuf_split(struct mbuf *mbuf, uint8_t *pos, mbuf_copy_t cb,
  57. void *cbarg);
  58. struct mbuf *_mbuf_split(struct mbuf *mbuf, uint8_t *pos, mbuf_copy_t cb,
  59. void *cbarg);
  60. #endif /* DA_BUF_H_ */