MessagePack for C
vrefbuffer.h
Go to the documentation of this file.
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 #if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__)
17 #include <sys/uio.h>
18 #else
19 struct iovec {
20  void *iov_base;
21  size_t iov_len;
22 };
23 #endif
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 
38 
40  size_t free;
41  char* ptr;
44 
45 typedef 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 
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 
67  size_t ref_size, size_t chunk_size);
70 
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);
73 
74 static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len);
75 
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);
78 
81  const char* buf, size_t len);
82 
85  const char* buf, size_t len);
86 
89 
92 
96 static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
97 {
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 
107 static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
108 {
109  if(vbuf == NULL) { return; }
111  free(vbuf);
112 }
113 
114 static 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 
125 static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref)
126 {
127  return vref->array;
128 }
129 
130 static 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 
size_t iov_len
Definition: vrefbuffer.h:21
struct iovec * tail
Definition: vrefbuffer.h:46
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer *vbuf, const char *buf, size_t len)
void * iov_base
Definition: vrefbuffer.h:20
struct msgpack_vrefbuffer msgpack_vrefbuffer
Definition: vrefbuffer.h:39
MSGPACK_DLLEXPORT void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer *vbuf)
#define MSGPACK_DLLEXPORT
Definition: sysdep.h:42
msgpack_vrefbuffer_inner_buffer inner_buffer
Definition: vrefbuffer.h:53
char * ptr
Definition: vrefbuffer.h:41
struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk
Definition: vrefbuffer.h:37
Definition: vrefbuffer.h:45
struct iovec * array
Definition: vrefbuffer.h:48
size_t free
Definition: vrefbuffer.h:40
Definition: vrefbuffer.h:19
MSGPACK_DLLEXPORT bool msgpack_vrefbuffer_init(msgpack_vrefbuffer *vbuf, size_t ref_size, size_t chunk_size)
msgpack_vrefbuffer_chunk * head
Definition: vrefbuffer.h:42
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer *vbuf, msgpack_vrefbuffer *to)
const char * data
Definition: unpack_template.h:89
size_t chunk_size
Definition: vrefbuffer.h:50
const char size_t len
Definition: unpack_template.h:89
size_t ref_size
Definition: vrefbuffer.h:51
struct msgpack_vrefbuffer_inner_buffer msgpack_vrefbuffer_inner_buffer
struct iovec * end
Definition: vrefbuffer.h:47
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer *vbuf, const char *buf, size_t len)
MSGPACK_DLLEXPORT void msgpack_vrefbuffer_clear(msgpack_vrefbuffer *vref)