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 #include <assert.h>
16 
17 #if defined(unix) || defined(__unix) || defined(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__)
18 #include <sys/uio.h>
19 typedef struct iovec msgpack_iovec;
20 #else
21 struct msgpack_iovec {
22  void *iov_base;
23  size_t iov_len;
24 };
25 typedef struct msgpack_iovec msgpack_iovec;
26 #endif
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 
41 
43  size_t free;
44  char* ptr;
47 
48 typedef struct msgpack_vrefbuffer {
52 
53  size_t chunk_size;
54  size_t ref_size;
55 
58 
59 
60 #ifndef MSGPACK_VREFBUFFER_REF_SIZE
61 #define MSGPACK_VREFBUFFER_REF_SIZE 32
62 #endif
63 
64 #ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
65 #define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
66 #endif
67 
68 MSGPACK_DLLEXPORT
70  size_t ref_size, size_t chunk_size);
71 MSGPACK_DLLEXPORT
73 
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);
76 
77 static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len);
78 
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);
81 
82 MSGPACK_DLLEXPORT
84  const char* buf, size_t len);
85 
86 MSGPACK_DLLEXPORT
88  const char* buf, size_t len);
89 
90 MSGPACK_DLLEXPORT
92 
93 MSGPACK_DLLEXPORT
95 
99 static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
100 {
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)) {
104  free(vbuf);
105  return NULL;
106  }
107  return vbuf;
108 }
109 
110 static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
111 {
112  if(vbuf == NULL) { return; }
114  free(vbuf);
115 }
116 
117 static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len)
118 {
120  assert(buf || len == 0);
121 
122  if(!buf) return 0;
123 
124  if(len < vbuf->ref_size) {
125  return msgpack_vrefbuffer_append_copy(vbuf, buf, len);
126  } else {
127  return msgpack_vrefbuffer_append_ref(vbuf, buf, len);
128  }
129 }
130 
131 static inline const msgpack_iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref)
132 {
133  return vref->array;
134 }
135 
136 static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref)
137 {
138  return (size_t)(vref->tail - vref->array);
139 }
140 
141 
142 #ifdef __cplusplus
143 }
144 #endif
145 
146 #endif /* msgpack/vrefbuffer.h */
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer *vbuf, msgpack_vrefbuffer *to)
struct msgpack_vrefbuffer msgpack_vrefbuffer
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer *vbuf, const char *buf, size_t len)
MSGPACK_DLLEXPORT void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer *vbuf)
MSGPACK_DLLEXPORT void msgpack_vrefbuffer_clear(msgpack_vrefbuffer *vref)
struct msgpack_vrefbuffer_inner_buffer msgpack_vrefbuffer_inner_buffer
struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk
Definition: vrefbuffer.h:40
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer *vbuf, const char *buf, size_t len)
MSGPACK_DLLEXPORT bool msgpack_vrefbuffer_init(msgpack_vrefbuffer *vbuf, size_t ref_size, size_t chunk_size)
Definition: vrefbuffer.h:21
size_t iov_len
Definition: vrefbuffer.h:23
void * iov_base
Definition: vrefbuffer.h:22
Definition: vrefbuffer.h:42
size_t free
Definition: vrefbuffer.h:43
char * ptr
Definition: vrefbuffer.h:44
msgpack_vrefbuffer_chunk * head
Definition: vrefbuffer.h:45
Definition: vrefbuffer.h:48
size_t chunk_size
Definition: vrefbuffer.h:53
msgpack_iovec * tail
Definition: vrefbuffer.h:49
msgpack_iovec * array
Definition: vrefbuffer.h:51
msgpack_iovec * end
Definition: vrefbuffer.h:50
msgpack_vrefbuffer_inner_buffer inner_buffer
Definition: vrefbuffer.h:56
size_t ref_size
Definition: vrefbuffer.h:54
const char * data
Definition: unpack_template.h:94
const char size_t len
Definition: unpack_template.h:94