| 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 | |
| 13 | #include "zone.h" |
| 14 | #include <stdlib.h> |
| 15 | #include <assert.h> |
| 16 | |
| 17 | #if defined(unix) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__) |
| 18 | #include <sys/uio.h> |
| 19 | typedef struct iovec msgpack_iovec; |
| 20 | #else |
| 21 | struct msgpack_iovec { |
| 22 | void *iov_base; |
| 23 | size_t iov_len; |
| 24 | }; |
| 25 | typedef struct msgpack_iovec msgpack_iovec; |
| 26 | #endif |
| 27 | |
| 28 | #ifdef __cplusplus |
| 29 | extern "C" { |
| 30 | #endif |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * @defgroup msgpack_vrefbuffer Vectored Referencing buffer |
| 35 | * @ingroup msgpack_buffer |
| 36 | * @{ |
| 37 | */ |
| 38 | |
| 39 | struct msgpack_vrefbuffer_chunk; |
| 40 | typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk; |
| 41 | |
| 42 | typedef struct msgpack_vrefbuffer_inner_buffer { |
| 43 | size_t free; |
| 44 | char* ptr; |
| 45 | msgpack_vrefbuffer_chunk* head; |
| 46 | } msgpack_vrefbuffer_inner_buffer; |
| 47 | |
| 48 | typedef struct msgpack_vrefbuffer { |
| 49 | msgpack_iovec* tail; |
| 50 | msgpack_iovec* end; |
| 51 | msgpack_iovec* array; |
| 52 | |
| 53 | size_t chunk_size; |
| 54 | size_t ref_size; |
| 55 | |
| 56 | msgpack_vrefbuffer_inner_buffer inner_buffer; |
| 57 | } msgpack_vrefbuffer; |
| 58 | |
| 59 | |
| 60 | #ifndef MSGPACK_VREFBUFFER_REF_SIZE |
| 61 | #define MSGPACK_VREFBUFFER_REF_SIZE 32 |
| 62 | #endif |
| 63 | |
| 64 | #ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE |
| 65 | #define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192 |
| 66 | #endif |
| 67 | |
| 68 | MSGPACK_DLLEXPORT |
| 69 | bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf, |
| 70 | size_t ref_size, size_t chunk_size); |
| 71 | MSGPACK_DLLEXPORT |
| 72 | void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf); |
| 73 | |
| 74 | static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size); |
| 75 | static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf); |
| 76 | |
| 77 | static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len); |
| 78 | |
| 79 | static inline const msgpack_iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref); |
| 80 | static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref); |
| 81 | |
| 82 | MSGPACK_DLLEXPORT |
| 83 | int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf, |
| 84 | const char* buf, size_t len); |
| 85 | |
| 86 | MSGPACK_DLLEXPORT |
| 87 | int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf, |
| 88 | const char* buf, size_t len); |
| 89 | |
| 90 | MSGPACK_DLLEXPORT |
| 91 | int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to); |
| 92 | |
| 93 | MSGPACK_DLLEXPORT |
| 94 | void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref); |
| 95 | |
| 96 | /** @} */ |
| 97 | |
| 98 | |
| 99 | static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size) |
| 100 | { |
| 101 | msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer)); |
| 102 | if (vbuf == NULL) return NULL; |
| 103 | if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) { |
| 104 | free(vbuf); |
| 105 | return NULL; |
| 106 | } |
| 107 | return vbuf; |
| 108 | } |
| 109 | |
| 110 | static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf) |
| 111 | { |
| 112 | if(vbuf == NULL) { return; } |
| 113 | msgpack_vrefbuffer_destroy(vbuf); |
| 114 | free(vbuf); |
| 115 | } |
| 116 | |
| 117 | static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len) |
| 118 | { |
| 119 | msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data; |
| 120 | assert(buf || len == 0); |
| 121 | |
| 122 | if(!buf) return 0; |
| 123 | |
| 124 | if(len < vbuf->ref_size) { |
| 125 | return msgpack_vrefbuffer_append_copy(vbuf, buf, len); |
| 126 | } else { |
| 127 | return msgpack_vrefbuffer_append_ref(vbuf, buf, len); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | static inline const msgpack_iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref) |
| 132 | { |
| 133 | return vref->array; |
| 134 | } |
| 135 | |
| 136 | static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref) |
| 137 | { |
| 138 | return (size_t)(vref->tail - vref->array); |
| 139 | } |
| 140 | |
| 141 | |
| 142 | #ifdef __cplusplus |
| 143 | } |
| 144 | #endif |
| 145 | |
| 146 | #endif /* msgpack/vrefbuffer.h */ |