Commit | Line | Data |
---|---|---|
116a02e3 JG |
1 | /* |
2 | * MessagePack for C simple 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_SBUFFER_H | |
11 | #define MSGPACK_SBUFFER_H | |
12 | ||
13 | #include <stdlib.h> | |
14 | #include <string.h> | |
0c07860d | 15 | #include <assert.h> |
116a02e3 JG |
16 | |
17 | #ifdef __cplusplus | |
18 | extern "C" { | |
19 | #endif | |
20 | ||
21 | ||
22 | /** | |
23 | * @defgroup msgpack_sbuffer Simple buffer | |
24 | * @ingroup msgpack_buffer | |
25 | * @{ | |
26 | */ | |
27 | ||
28 | typedef struct msgpack_sbuffer { | |
29 | size_t size; | |
30 | char* data; | |
31 | size_t alloc; | |
32 | } msgpack_sbuffer; | |
33 | ||
34 | static inline void msgpack_sbuffer_init(msgpack_sbuffer* sbuf) | |
35 | { | |
36 | memset(sbuf, 0, sizeof(msgpack_sbuffer)); | |
37 | } | |
38 | ||
39 | static inline void msgpack_sbuffer_destroy(msgpack_sbuffer* sbuf) | |
40 | { | |
41 | free(sbuf->data); | |
42 | } | |
43 | ||
44 | static inline msgpack_sbuffer* msgpack_sbuffer_new(void) | |
45 | { | |
46 | return (msgpack_sbuffer*)calloc(1, sizeof(msgpack_sbuffer)); | |
47 | } | |
48 | ||
49 | static inline void msgpack_sbuffer_free(msgpack_sbuffer* sbuf) | |
50 | { | |
51 | if(sbuf == NULL) { return; } | |
52 | msgpack_sbuffer_destroy(sbuf); | |
53 | free(sbuf); | |
54 | } | |
55 | ||
56 | #ifndef MSGPACK_SBUFFER_INIT_SIZE | |
57 | #define MSGPACK_SBUFFER_INIT_SIZE 8192 | |
58 | #endif | |
59 | ||
60 | static inline int msgpack_sbuffer_write(void* data, const char* buf, size_t len) | |
61 | { | |
62 | msgpack_sbuffer* sbuf = (msgpack_sbuffer*)data; | |
63 | ||
0c07860d MJ |
64 | assert(buf || len == 0); |
65 | if(!buf) return 0; | |
66 | ||
116a02e3 JG |
67 | if(sbuf->alloc - sbuf->size < len) { |
68 | void* tmp; | |
69 | size_t nsize = (sbuf->alloc) ? | |
70 | sbuf->alloc * 2 : MSGPACK_SBUFFER_INIT_SIZE; | |
71 | ||
72 | while(nsize < sbuf->size + len) { | |
73 | size_t tmp_nsize = nsize * 2; | |
74 | if (tmp_nsize <= nsize) { | |
75 | nsize = sbuf->size + len; | |
76 | break; | |
77 | } | |
78 | nsize = tmp_nsize; | |
79 | } | |
80 | ||
81 | tmp = realloc(sbuf->data, nsize); | |
82 | if(!tmp) { return -1; } | |
83 | ||
84 | sbuf->data = (char*)tmp; | |
85 | sbuf->alloc = nsize; | |
86 | } | |
87 | ||
88 | memcpy(sbuf->data + sbuf->size, buf, len); | |
89 | sbuf->size += len; | |
0c07860d | 90 | |
116a02e3 JG |
91 | return 0; |
92 | } | |
93 | ||
94 | static inline char* msgpack_sbuffer_release(msgpack_sbuffer* sbuf) | |
95 | { | |
96 | char* tmp = sbuf->data; | |
97 | sbuf->size = 0; | |
98 | sbuf->data = NULL; | |
99 | sbuf->alloc = 0; | |
100 | return tmp; | |
101 | } | |
102 | ||
103 | static inline void msgpack_sbuffer_clear(msgpack_sbuffer* sbuf) | |
104 | { | |
105 | sbuf->size = 0; | |
106 | } | |
107 | ||
108 | /** @} */ | |
109 | ||
110 | ||
111 | #ifdef __cplusplus | |
112 | } | |
113 | #endif | |
114 | ||
115 | #endif /* msgpack/sbuffer.h */ |