vrefbuffer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * MessagePack for C zero-copy buffer implementation
  3. *
  4. * Copyright (C) 2008-2009 FURUHASHI Sadayuki
  5. *
  6. * Distributed under the Boost Software License, Version 1.0.
  7. * (See accompanying file LICENSE_1_0.txt or copy at
  8. * http://www.boost.org/LICENSE_1_0.txt)
  9. */
  10. #ifndef MSGPACK_VREFBUFFER_H
  11. #define MSGPACK_VREFBUFFER_H
  12. #include "zone.h"
  13. #include <stdlib.h>
  14. #if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__)
  15. #include <sys/uio.h>
  16. #else
  17. struct iovec {
  18. void *iov_base;
  19. size_t iov_len;
  20. };
  21. #endif
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. /**
  26. * @defgroup msgpack_vrefbuffer Vectored Referencing buffer
  27. * @ingroup msgpack_buffer
  28. * @{
  29. */
  30. struct msgpack_vrefbuffer_chunk;
  31. typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk;
  32. typedef struct msgpack_vrefbuffer_inner_buffer {
  33. size_t free;
  34. char* ptr;
  35. msgpack_vrefbuffer_chunk* head;
  36. } msgpack_vrefbuffer_inner_buffer;
  37. typedef struct msgpack_vrefbuffer {
  38. struct iovec* tail;
  39. struct iovec* end;
  40. struct iovec* array;
  41. size_t chunk_size;
  42. size_t ref_size;
  43. msgpack_vrefbuffer_inner_buffer inner_buffer;
  44. } msgpack_vrefbuffer;
  45. #ifndef MSGPACK_VREFBUFFER_REF_SIZE
  46. #define MSGPACK_VREFBUFFER_REF_SIZE 32
  47. #endif
  48. #ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
  49. #define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
  50. #endif
  51. MSGPACK_DLLEXPORT
  52. bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
  53. size_t ref_size, size_t chunk_size);
  54. MSGPACK_DLLEXPORT
  55. void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf);
  56. static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size);
  57. static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf);
  58. static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len);
  59. static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref);
  60. static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref);
  61. MSGPACK_DLLEXPORT
  62. int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
  63. const char* buf, size_t len);
  64. MSGPACK_DLLEXPORT
  65. int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
  66. const char* buf, size_t len);
  67. MSGPACK_DLLEXPORT
  68. int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
  69. MSGPACK_DLLEXPORT
  70. void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref);
  71. /** @} */
  72. static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
  73. {
  74. msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer));
  75. if (vbuf == NULL) return NULL;
  76. if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) {
  77. free(vbuf);
  78. return NULL;
  79. }
  80. return vbuf;
  81. }
  82. static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
  83. {
  84. if(vbuf == NULL) { return; }
  85. msgpack_vrefbuffer_destroy(vbuf);
  86. free(vbuf);
  87. }
  88. static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len)
  89. {
  90. msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
  91. if(len < vbuf->ref_size) {
  92. return msgpack_vrefbuffer_append_copy(vbuf, buf, len);
  93. } else {
  94. return msgpack_vrefbuffer_append_ref(vbuf, buf, len);
  95. }
  96. }
  97. static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref)
  98. {
  99. return vref->array;
  100. }
  101. static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref)
  102. {
  103. return (size_t)(vref->tail - vref->array);
  104. }
  105. #ifdef __cplusplus
  106. }
  107. #endif
  108. #endif /* msgpack/vrefbuffer.h */