vrefbuffer.h
Go to the documentation of this file.
/*
* MessagePack for C zero-copy buffer implementation
*
* Copyright (C) 2008-2009 FURUHASHI Sadayuki
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef MSGPACK_VREFBUFFER_H
#define MSGPACK_VREFBUFFER_H
#include "zone.h"
#include <stdlib.h>
#if defined(unix) || defined(__unix) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__QNX__) || defined(__QNXTO__)
#include <sys/uio.h>
#else
struct iovec {
void *iov_base;
size_t iov_len;
};
#endif
#ifdef __cplusplus
extern "C" {
#endif
struct msgpack_vrefbuffer_chunk;
typedef struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk;
typedef struct msgpack_vrefbuffer_inner_buffer {
size_t free;
char* ptr;
msgpack_vrefbuffer_chunk* head;
} msgpack_vrefbuffer_inner_buffer;
typedef struct msgpack_vrefbuffer {
struct iovec* tail;
struct iovec* end;
struct iovec* array;
size_t chunk_size;
size_t ref_size;
msgpack_vrefbuffer_inner_buffer inner_buffer;
} msgpack_vrefbuffer;
#ifndef MSGPACK_VREFBUFFER_REF_SIZE
#define MSGPACK_VREFBUFFER_REF_SIZE 32
#endif
#ifndef MSGPACK_VREFBUFFER_CHUNK_SIZE
#define MSGPACK_VREFBUFFER_CHUNK_SIZE 8192
#endif
MSGPACK_DLLEXPORT
bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
size_t ref_size, size_t chunk_size);
MSGPACK_DLLEXPORT
void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer* vbuf);
static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size);
static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf);
static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len);
static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref);
static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref);
MSGPACK_DLLEXPORT
int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer* vbuf,
const char* buf, size_t len);
MSGPACK_DLLEXPORT
int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer* vbuf,
const char* buf, size_t len);
MSGPACK_DLLEXPORT
int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to);
MSGPACK_DLLEXPORT
void msgpack_vrefbuffer_clear(msgpack_vrefbuffer* vref);
static inline msgpack_vrefbuffer* msgpack_vrefbuffer_new(size_t ref_size, size_t chunk_size)
{
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)malloc(sizeof(msgpack_vrefbuffer));
if (vbuf == NULL) return NULL;
if(!msgpack_vrefbuffer_init(vbuf, ref_size, chunk_size)) {
free(vbuf);
return NULL;
}
return vbuf;
}
static inline void msgpack_vrefbuffer_free(msgpack_vrefbuffer* vbuf)
{
if(vbuf == NULL) { return; }
msgpack_vrefbuffer_destroy(vbuf);
free(vbuf);
}
static inline int msgpack_vrefbuffer_write(void* data, const char* buf, size_t len)
{
msgpack_vrefbuffer* vbuf = (msgpack_vrefbuffer*)data;
if(len < vbuf->ref_size) {
return msgpack_vrefbuffer_append_copy(vbuf, buf, len);
} else {
return msgpack_vrefbuffer_append_ref(vbuf, buf, len);
}
}
static inline const struct iovec* msgpack_vrefbuffer_vec(const msgpack_vrefbuffer* vref)
{
return vref->array;
}
static inline size_t msgpack_vrefbuffer_veclen(const msgpack_vrefbuffer* vref)
{
return (size_t)(vref->tail - vref->array);
}
#ifdef __cplusplus
}
#endif
#endif /* msgpack/vrefbuffer.h */
iovec::iov_len
size_t iov_len
Definition: vrefbuffer.h:21
msgpack_vrefbuffer::tail
struct iovec * tail
Definition: vrefbuffer.h:46
msgpack_vrefbuffer_append_ref
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_append_ref(msgpack_vrefbuffer *vbuf, const char *buf, size_t len)
iovec::iov_base
void * iov_base
Definition: vrefbuffer.h:20
msgpack_vrefbuffer
struct msgpack_vrefbuffer msgpack_vrefbuffer
msgpack_vrefbuffer_inner_buffer
Definition: vrefbuffer.h:39
msgpack_vrefbuffer_destroy
MSGPACK_DLLEXPORT void msgpack_vrefbuffer_destroy(msgpack_vrefbuffer *vbuf)
MSGPACK_DLLEXPORT
#define MSGPACK_DLLEXPORT
Definition: sysdep.h:42
msgpack_vrefbuffer::inner_buffer
msgpack_vrefbuffer_inner_buffer inner_buffer
Definition: vrefbuffer.h:53
msgpack_vrefbuffer_inner_buffer::ptr
char * ptr
Definition: vrefbuffer.h:41
msgpack_vrefbuffer_chunk
struct msgpack_vrefbuffer_chunk msgpack_vrefbuffer_chunk
Definition: vrefbuffer.h:37
msgpack_vrefbuffer
Definition: vrefbuffer.h:45
msgpack_vrefbuffer::array
struct iovec * array
Definition: vrefbuffer.h:48
msgpack_vrefbuffer_inner_buffer::free
size_t free
Definition: vrefbuffer.h:40
iovec
Definition: vrefbuffer.h:19
msgpack_vrefbuffer_init
MSGPACK_DLLEXPORT bool msgpack_vrefbuffer_init(msgpack_vrefbuffer *vbuf, size_t ref_size, size_t chunk_size)
msgpack_vrefbuffer_inner_buffer::head
msgpack_vrefbuffer_chunk * head
Definition: vrefbuffer.h:42
zone.h
msgpack_vrefbuffer_migrate
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer *vbuf, msgpack_vrefbuffer *to)
data
const char * data
Definition: unpack_template.h:89
msgpack_vrefbuffer::chunk_size
size_t chunk_size
Definition: vrefbuffer.h:50
len
const char size_t len
Definition: unpack_template.h:89
msgpack_vrefbuffer::ref_size
size_t ref_size
Definition: vrefbuffer.h:51
msgpack_vrefbuffer_inner_buffer
struct msgpack_vrefbuffer_inner_buffer msgpack_vrefbuffer_inner_buffer
msgpack_vrefbuffer::end
struct iovec * end
Definition: vrefbuffer.h:47
msgpack_vrefbuffer_append_copy
MSGPACK_DLLEXPORT int msgpack_vrefbuffer_append_copy(msgpack_vrefbuffer *vbuf, const char *buf, size_t len)
msgpack_vrefbuffer_clear
MSGPACK_DLLEXPORT void msgpack_vrefbuffer_clear(msgpack_vrefbuffer *vref)