sessiond: Add msgpack-c 3.3.0 to the tree
[lttng-tools.git] / src / vendor / msgpack / vrefbuffer.h
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(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__)
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
30 /**
31 * @defgroup msgpack_vrefbuffer Vectored Referencing buffer
32 * @ingroup msgpack_buffer
33 * @{
34 */
35
36 struct msgpack_vrefbuffer_chunk;
37 typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk;
38
39 typedef struct msgpack_vrefbuffer_inner_buffer {
40 size_t free;
41 char* ptr;
42 msgpack_vrefbuffer_chunk* head;
43 } msgpack_vrefbuffer_inner_buffer;
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
53 msgpack_vrefbuffer_inner_buffer inner_buffer;
54 } msgpack_vrefbuffer;
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
65 MSGPACK_DLLEXPORT
66 bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
67 size_t ref_size, size_t chunk_size);
68 MSGPACK_DLLEXPORT
69 void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf);
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
79 MSGPACK_DLLEXPORT
80 int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
81 const char* buf, size_t len);
82
83 MSGPACK_DLLEXPORT
84 int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
85 const char* buf, size_t len);
86
87 MSGPACK_DLLEXPORT
88 int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
89
90 MSGPACK_DLLEXPORT
91 void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref);
92
93 /** @} */
94
95
96 static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
97 {
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)) {
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; }
110 msgpack_vrefbuffer_destroy(vbuf);
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 */
This page took 0.045208 seconds and 4 git commands to generate.