2 * MessagePack for C zero-copy buffer implementation
4 * Copyright (C) 2008-2009 FURUHASHI Sadayuki
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)
10 #ifndef MSGPACK_VREFBUFFER_H
11 #define MSGPACK_VREFBUFFER_H
17 #if defined(unix) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__)
19 typedef struct iovec msgpack_iovec
;
21 struct msgpack_iovec
{
25 typedef struct msgpack_iovec msgpack_iovec
;
34 * @defgroup msgpack_vrefbuffer Vectored Referencing buffer
35 * @ingroup msgpack_buffer
39 struct msgpack_vrefbuffer_chunk
;
40 typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk
;
42 typedef struct msgpack_vrefbuffer_inner_buffer
{
45 msgpack_vrefbuffer_chunk
* head
;
46 } msgpack_vrefbuffer_inner_buffer
;
48 typedef struct msgpack_vrefbuffer
{
56 msgpack_vrefbuffer_inner_buffer inner_buffer
;
60 #ifndef MSGPACK_VREFBUFFER_REF_SIZE
61 #define MSGPACK_VREFBUFFER_REF_SIZE 32
64 #ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
65 #define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
69 bool msgpack_vrefbuffer_init(msgpack_vrefbuffer
* vbuf
,
70 size_t ref_size
, size_t chunk_size
);
72 void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer
* vbuf
);
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
);
77 static inline int msgpack_vrefbuffer_write(void* data
, const char* buf
, size_t len
);
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
);
83 int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer
* vbuf
,
84 const char* buf
, size_t len
);
87 int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer
* vbuf
,
88 const char* buf
, size_t len
);
91 int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer
* vbuf
, msgpack_vrefbuffer
* to
);
94 void msgpack_vrefbuffer_clear(msgpack_vrefbuffer
* vref
);
99 static inline msgpack_vrefbuffer
* msgpack_vrefbuffer_new(size_t ref_size
, size_t chunk_size
)
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
)) {
110 static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer
* vbuf
)
112 if(vbuf
== NULL
) { return; }
113 msgpack_vrefbuffer_destroy(vbuf
);
117 static inline int msgpack_vrefbuffer_write(void* data
, const char* buf
, size_t len
)
119 msgpack_vrefbuffer
* vbuf
= (msgpack_vrefbuffer
*)data
;
120 assert(buf
|| len
== 0);
124 if(len
< vbuf
->ref_size
) {
125 return msgpack_vrefbuffer_append_copy(vbuf
, buf
, len
);
127 return msgpack_vrefbuffer_append_ref(vbuf
, buf
, len
);
131 static inline const msgpack_iovec
* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer
* vref
)
136 static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer
* vref
)
138 return (size_t)(vref
->tail
- vref
->array
);
146 #endif /* msgpack/vrefbuffer.h */