1 /*
2 * MessagePack for C zero-copy buffer implementation
3 *
4 * Copyright (C) 2008-2009 FURUHASHI Sadayuki
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 #ifndef MSGPACK_VREFBUFFER_H
19 #define MSGPACK_VREFBUFFER_H
20
21 #include "zone.h"
22 #include <stdlib.h>
23
24 #ifndef _WIN32
25 #include <sys/uio.h>
26 #else
27 struct iovec {
28 void *iov_base;
29 size_t iov_len;
30 };
31 #endif
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37
38 /**
39 * @defgroup msgpack_vrefbuffer Vectored Referencing buffer
40 * @ingroup msgpack_buffer
41 * @{
42 */
43
44 struct msgpack_vrefbuffer_chunk;
45 typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk;
46
47 typedef struct msgpack_vrefbuffer_inner_buffer {
48 size_t free;
49 char* ptr;
50 msgpack_vrefbuffer_chunk* head;
51 } msgpack_vrefbuffer_inner_buffer;
52
53 typedef struct msgpack_vrefbuffer {
54 struct iovec* tail;
55 struct iovec* end;
56 struct iovec* array;
57
58 size_t chunk_size;
59 size_t ref_size;
60
61 msgpack_vrefbuffer_inner_buffer inner_buffer;
62 } msgpack_vrefbuffer;
63
64
65 #ifndef MSGPACK_VREFBUFFER_REF_SIZE
66 #define MSGPACK_VREFBUFFER_REF_SIZE 32
67 #endif
68
69 #ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
70 #define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
71 #endif
72
73 MSGPACK_DLLEXPORT
74 bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
75 size_t ref_size, size_t chunk_size);
76 MSGPACK_DLLEXPORT
77 void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf);
78
79 static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size);
80 static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf);
81
82 static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len);
83
84 static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref);
85 static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref);
86
87 MSGPACK_DLLEXPORT
88 int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
89 const char* buf, size_t len);
90
91 MSGPACK_DLLEXPORT
92 int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
93 const char* buf, size_t len);
94
95 MSGPACK_DLLEXPORT
96 int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
97
98 MSGPACK_DLLEXPORT
99 void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref);
100
101 /** @} */
102
103
msgpack_vrefbuffer_new(size_t ref_size,size_t chunk_size)104 static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
105 {
106 msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer));
107 if (vbuf == NULL) return NULL;
108 if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) {
109 free(vbuf);
110 return NULL;
111 }
112 return vbuf;
113 }
114
msgpack_vrefbuffer_free(msgpack_vrefbuffer * vbuf)115 static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
116 {
117 if(vbuf == NULL) { return; }
118 msgpack_vrefbuffer_destroy(vbuf);
119 free(vbuf);
120 }
121
msgpack_vrefbuffer_write(void * data,const char * buf,size_t len)122 static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len)
123 {
124 msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
125
126 if(len < vbuf->ref_size) {
127 return msgpack_vrefbuffer_append_copy(vbuf, buf, len);
128 } else {
129 return msgpack_vrefbuffer_append_ref(vbuf, buf, len);
130 }
131 }
132
msgpack_vrefbuffer_vec(const msgpack_vrefbuffer * vref)133 static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref)
134 {
135 return vref->array;
136 }
137
msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer * vref)138 static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref)
139 {
140 return (size_t)(vref->tail - vref->array);
141 }
142
143
144 #ifdef __cplusplus
145 }
146 #endif
147
148 #endif /* msgpack/vrefbuffer.h */
149
150