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