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
16 #if defined(unix) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__)
31 * @defgroup msgpack_vrefbuffer Vectored Referencing buffer
32 * @ingroup msgpack_buffer
36 struct msgpack_vrefbuffer_chunk
;
37 typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk
;
39 typedef struct msgpack_vrefbuffer_inner_buffer
{
42 msgpack_vrefbuffer_chunk
* head
;
43 } msgpack_vrefbuffer_inner_buffer
;
45 typedef struct msgpack_vrefbuffer
{
53 msgpack_vrefbuffer_inner_buffer inner_buffer
;
57 #ifndef MSGPACK_VREFBUFFER_REF_SIZE
58 #define MSGPACK_VREFBUFFER_REF_SIZE 32
61 #ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
62 #define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
66 bool msgpack_vrefbuffer_init(msgpack_vrefbuffer
* vbuf
,
67 size_t ref_size
, size_t chunk_size
);
69 void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer
* vbuf
);
71 static inline msgpack_vrefbuffer
* msgpack_vrefbuffer_new(size_t ref_size
, size_t chunk_size
);
72 static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer
* vbuf
);
74 static inline int msgpack_vrefbuffer_write(void* data
, const char* buf
, size_t len
);
76 static inline const struct iovec
* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer
* vref
);
77 static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer
* vref
);
80 int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer
* vbuf
,
81 const char* buf
, size_t len
);
84 int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer
* vbuf
,
85 const char* buf
, size_t len
);
88 int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer
* vbuf
, msgpack_vrefbuffer
* to
);
91 void msgpack_vrefbuffer_clear(msgpack_vrefbuffer
* vref
);
96 static inline msgpack_vrefbuffer
* msgpack_vrefbuffer_new(size_t ref_size
, size_t chunk_size
)
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
)) {
107 static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer
* vbuf
)
109 if(vbuf
== NULL
) { return; }
110 msgpack_vrefbuffer_destroy(vbuf
);
114 static inline int msgpack_vrefbuffer_write(void* data
, const char* buf
, size_t len
)
116 msgpack_vrefbuffer
* vbuf
= (msgpack_vrefbuffer
*)data
;
118 if(len
< vbuf
->ref_size
) {
119 return msgpack_vrefbuffer_append_copy(vbuf
, buf
, len
);
121 return msgpack_vrefbuffer_append_ref(vbuf
, buf
, len
);
125 static inline const struct iovec
* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer
* vref
)
130 static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer
* vref
)
132 return (size_t)(vref
->tail
- vref
->array
);
140 #endif /* msgpack/vrefbuffer.h */