MessagePack for C
Loading...
Searching...
No Matches
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(__linux__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__QNX__) || defined(__QNXTO__) || defined(__HAIKU__)
17#include <sys/uio.h>
18#else
19struct iovec {
20 void *iov_base;
21 size_t iov_len;
22};
23#endif
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
29
38
40 size_t free;
41 char* ptr;
44
45typedef 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
71static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size);
72static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf);
73
74static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len);
75
76static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref);
77static 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
96static 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
107static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
108{
109 if(vbuf == NULL) { return; }
111 free(vbuf);
112}
113
114static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len)
115{
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
125static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref)
126{
127 return vref->array;
128}
129
130static 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 */
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer *vbuf, msgpack_vrefbuffer *to)
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_chunk msgpack_vrefbuffer_chunk
Definition vrefbuffer.h:37
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:19
void * iov_base
Definition vrefbuffer.h:20
size_t iov_len
Definition vrefbuffer.h:21
Definition vrefbuffer.h:39
size_t free
Definition vrefbuffer.h:40
char * ptr
Definition vrefbuffer.h:41
msgpack_vrefbuffer_chunk * head
Definition vrefbuffer.h:42
Definition vrefbuffer.h:45
size_t chunk_size
Definition vrefbuffer.h:50
struct iovec * end
Definition vrefbuffer.h:47
struct iovec * tail
Definition vrefbuffer.h:46
struct iovec * array
Definition vrefbuffer.h:48
msgpack_vrefbuffer_inner_buffer inner_buffer
Definition vrefbuffer.h:53
size_t ref_size
Definition vrefbuffer.h:51
#define MSGPACK_DLLEXPORT
Definition sysdep.h:40
const char * data
Definition unpack_template.h:94
const char size_t len
Definition unpack_template.h:94