2 * MessagePack for C simple buffer implementation
4 * Copyright (C) 2008-2009 FURUHASHI Sadayuki
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)
10 #ifndef MSGPACK_SBUFFER_H
11 #define MSGPACK_SBUFFER_H
22 * @defgroup msgpack_sbuffer Simple buffer
23 * @ingroup msgpack_buffer
27 typedef struct msgpack_sbuffer
{
33 static inline void msgpack_sbuffer_init(msgpack_sbuffer
* sbuf
)
35 memset(sbuf
, 0, sizeof(msgpack_sbuffer
));
38 static inline void msgpack_sbuffer_destroy(msgpack_sbuffer
* sbuf
)
43 static inline msgpack_sbuffer
* msgpack_sbuffer_new(void)
45 return (msgpack_sbuffer
*)calloc(1, sizeof(msgpack_sbuffer
));
48 static inline void msgpack_sbuffer_free(msgpack_sbuffer
* sbuf
)
50 if(sbuf
== NULL
) { return; }
51 msgpack_sbuffer_destroy(sbuf
);
55 #ifndef MSGPACK_SBUFFER_INIT_SIZE
56 #define MSGPACK_SBUFFER_INIT_SIZE 8192
59 static inline int msgpack_sbuffer_write(void* data
, const char* buf
, size_t len
)
61 msgpack_sbuffer
* sbuf
= (msgpack_sbuffer
*)data
;
63 if(sbuf
->alloc
- sbuf
->size
< len
) {
65 size_t nsize
= (sbuf
->alloc
) ?
66 sbuf
->alloc
* 2 : MSGPACK_SBUFFER_INIT_SIZE
;
68 while(nsize
< sbuf
->size
+ len
) {
69 size_t tmp_nsize
= nsize
* 2;
70 if (tmp_nsize
<= nsize
) {
71 nsize
= sbuf
->size
+ len
;
77 tmp
= realloc(sbuf
->data
, nsize
);
78 if(!tmp
) { return -1; }
80 sbuf
->data
= (char*)tmp
;
84 memcpy(sbuf
->data
+ sbuf
->size
, buf
, len
);
89 static inline char* msgpack_sbuffer_release(msgpack_sbuffer
* sbuf
)
91 char* tmp
= sbuf
->data
;
98 static inline void msgpack_sbuffer_clear(msgpack_sbuffer
* sbuf
)
110 #endif /* msgpack/sbuffer.h */