srl_buffer.h

filter: Statementstmt 46.8% 30 / 64 Branchbran 22.7% 5 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 40.7% 35 / 86 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Encoder.xs ↑ Summary srl_siphash.h »
1 #ifndef SRL_BUFFER_H_
2 #define SRL_BUFFER_H_
3  
4 #include "assert.h"
5  
6 #include "srl_inline.h"
7 #include "srl_common.h"
8 #include "srl_buffer_types.h"
9  
10 /* The static's below plus the ifndef sort of make this header only
11 * usable in one place per compilation unit. Drop "static" when necessary.
12 * For now, potentially smaller code wins. */
13  
14 /* buffer operations */
15 #define BUF_POS_OFS(buf) (((buf)->pos) - ((buf)->start))
16 #define BUF_SPACE(buf) (((buf)->end) - ((buf)->pos))
17 #define BUF_SIZE(buf) (((buf)->end) - ((buf)->start))
18 /* a negative BUF_SPACE() (pos past end) must not wrap to "plenty of room" */
19 #define BUF_NEED_GROW(buf, minlen) (BUF_SPACE(buf) < 0 || (size_t)BUF_SPACE(buf) <= (minlen))
20 #define BUF_NEED_GROW_TOTAL(buf, minlen) ((size_t)BUF_SIZE(buf) <= minlen)
21 #define BUF_NOT_DONE(buf) ((buf)->pos < (buf)->end)
22 #define BUF_DONE(buf) ((buf)->pos >= (buf)->end)
23  
24 /* body-position/size related operations */
25 #define BODY_POS_OFS(buf) (((buf)->pos) - ((buf)->body_pos))
26  
27 /* these are mostly for right between (de)serializing the header and the body */
28 #define SRL_SET_BODY_POS(buf, pos_ptr) ((buf)->body_pos = pos_ptr)
29 #define SRL_UPDATE_BODY_POS(buf, protocol_version) \
30 STMT_START { \
31 if (expect_false((protocol_version) == 1)) { \
32 SRL_SET_BODY_POS((buf), (buf)->start); \
33 } else { \
34 SRL_SET_BODY_POS((buf), (buf)->pos-1); \
35 } \
36 } STMT_END
37  
38 /* Internal debugging macros, used only in DEBUG mode */
39 #ifndef NDEBUG
40 #define DEBUG_ASSERT_BUF_SPACE(buf, len) STMT_START { \
41 if((BUF_SPACE(buf) < (ptrdiff_t)(len))) { \
42 warn("failed assertion check - pos: %ld [%p %p %p] %ld < %ld", \
43 (long)BUF_POS_OFS(buf), (buf)->start, \
44 (buf)->pos, (buf)->end, \
45 (long)BUF_SPACE(buf),(long)(len)); \
46 } \
47 assert(BUF_SPACE(buf) >= (ptrdiff_t)(len)); \
48 } STMT_END
49 #else
50 #define DEBUG_ASSERT_BUF_SPACE(buf, len) ((void)0)
51 #endif
52  
53 #ifndef NDEBUG
54 #define DEBUG_ASSERT_BUF_SANE(buf) STMT_START { \
55 if(!(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end))){ \
56 warn("failed sanity assertion check - pos: %ld [%p %p %p] %ld", \
57 (long)BUF_POS_OFS(buf), (buf)->start, \
58 (buf)->pos, (buf)->end, (long)BUF_SPACE(buf)); \
59 } \
60 assert(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end)); \
61 } STMT_END
62 #else
63 #define DEBUG_ASSERT_BUF_SANE(buf) \
64 assert(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end))
65 #endif
66  
67 /* Allocate a virgin buffer (but not the buffer struct) */
68 SRL_STATIC_INLINE int
69 7 srl_buf_init_buffer(pTHX_ srl_buffer_t *buf, const STRLEN init_size)
70 {
71 7 Newx(buf->start, init_size, srl_buffer_char);
72 7 ▶ if (expect_false( buf->start == NULL ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( buf->start == NULL )) 0 7 7
73 0 return 1;
74 7 buf->end = buf->start + init_size - 1;
75 7 buf->pos = buf->start;
76 7 buf->body_pos = buf->start; /* SRL_SET_BODY_POS(enc, enc->buf.start) equiv */
77 7 return 0;
78 }
79  
80 /* Free a buffer (but not the buffer struct) */
81 SRL_STATIC_INLINE void
82 7 srl_buf_free_buffer(pTHX_ srl_buffer_t *buf)
83 {
84 7 Safefree(buf->start);
85 7 }
86  
87 /* Copy one buffer to another (shallowly!) */
88 SRL_STATIC_INLINE void
89 0 srl_buf_copy_buffer(pTHX_ srl_buffer_t *src, srl_buffer_t *dest)
90 {
91 0 Copy(src, dest, 1, srl_buffer_t);
92 0 }
93  
94 /* Swap two buffers */
95 SRL_STATIC_INLINE void
96 0 srl_buf_swap_buffer(pTHX_ srl_buffer_t *buf1, srl_buffer_t *buf2)
97 {
98 srl_buffer_t tmp;
99 0 Copy(buf1, &tmp, 1, srl_buffer_t);
100 0 Copy(buf2, buf1, 1, srl_buffer_t);
101 0 Copy(&tmp, buf2, 1, srl_buffer_t);
102 0 }
103  
104 /* old_size + (new_size / 4) */
105 #define OVERALLOC_FUNC(cur_size,req_size) (cur_size + (req_size >> 2))
106  
107 SRL_STATIC_INLINE void
108 0 srl_buf_grow_nocheck(pTHX_ srl_buffer_t *buf, const size_t minlen)
109 {
110 0 const size_t pos_ofs= BUF_POS_OFS(buf); /* have to store the offset of pos */
111 0 const ptrdiff_t body_ofs= buf->body_pos - buf->start; /* have to store the offset of the body */
112 #ifdef MEMDEBUG
113 const size_t new_size = minlen;
114 #else
115 0 const size_t cur_size = BUF_SIZE(buf);
116 0 const size_t tmp_size = (minlen >> 2) > (size_t)~(size_t)0 - cur_size
117 0 ▶ ? minlen : OVERALLOC_FUNC(cur_size, minlen);
Branch: 1 branch
BranchTrueFalseTotal
? minlen : OVERALLOC_FUNC(cur_size, minlen); 0 0 0
118 0 const size_t new_size = (minlen > tmp_size) ? minlen : tmp_size;
119 #endif
120  
121 DEBUG_ASSERT_BUF_SANE(buf);
122 /* assert that Renew means GROWING the buffer */
123 assert(buf->start + new_size > buf->end);
124  
125 0 Renew(buf->start, new_size, srl_buffer_char);
126 0 ▶ if (buf->start == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (buf->start == NULL) 0 0 0
127 0 croak("Out of memory!");
128  
129 0 buf->end = (srl_buffer_char*) (buf->start + new_size);
130 0 buf->pos = buf->start + pos_ofs;
131 0 SRL_SET_BODY_POS(buf, buf->start + body_ofs);
132  
133 DEBUG_ASSERT_BUF_SANE(buf);
134 assert(buf->end - buf->start > (ptrdiff_t)0);
135 assert(buf->pos - buf->start >= (ptrdiff_t)0);
136 /* The following is checking against -1 because SRL_UPDATE_BODY_POS
137 * will actually set the body_pos to pos-1, where pos can be 0.
138 * This works out fine in the end, but is admittedly a bit shady.
139 * FIXME */
140 assert(buf->body_pos - buf->start >= (ptrdiff_t)-1);
141 0 }
142  
143 #define BUF_SIZE_ASSERT(buf, minlen) \
144 STMT_START { \
145 DEBUG_ASSERT_BUF_SANE(buf); \
146 if (BUF_NEED_GROW(buf, minlen)) { \
147 if ((size_t)(minlen) > (size_t)~(size_t)0 - (size_t)BUF_SIZE(buf)) \
148 croak("Sereal output buffer is too large"); \
149 srl_buf_grow_nocheck(aTHX_ (buf), (BUF_SIZE(buf) + minlen)); \
150 } \
151 DEBUG_ASSERT_BUF_SANE(buf); \
152 } STMT_END
153  
154 #define BUF_SIZE_ASSERT_TOTAL(buf, minlen) \
155 STMT_START { \
156 DEBUG_ASSERT_BUF_SANE(buf); \
157 if (BUF_NEED_GROW_TOTAL(buf, minlen)) \
158 srl_buf_grow_nocheck(aTHX_ (buf), (minlen)); \
159 DEBUG_ASSERT_BUF_SANE(buf); \
160 } STMT_END
161  
162 SRL_STATIC_INLINE void
163 srl_buf_cat_str_int(pTHX_ srl_buffer_t *buf, const char *str, size_t len)
164 {
165 BUF_SIZE_ASSERT(buf, len);
166 Copy(str, buf->pos, len, char);
167 buf->pos += len;
168 DEBUG_ASSERT_BUF_SANE(buf);
169 }
170 #define srl_buf_cat_str(buf, str, len) srl_buf_cat_str_int(aTHX_ buf, str, len)
171 /* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
172 #define srl_buf_cat_str_s(buf, str) srl_buf_cat_str(buf, ("" str ""), sizeof(str)-1)
173  
174 SRL_STATIC_INLINE void
175 7 srl_buf_cat_str_nocheck_int(pTHX_ srl_buffer_t *buf, const char *str, size_t len)
176 {
177 DEBUG_ASSERT_BUF_SANE(buf);
178 DEBUG_ASSERT_BUF_SPACE(buf, len);
179 7 Copy(str, buf->pos, len, char);
180 7 buf->pos += len;
181 DEBUG_ASSERT_BUF_SANE(buf);
182 7 }
183 #define srl_buf_cat_str_nocheck(buf, str, len) srl_buf_cat_str_nocheck_int(aTHX_ buf, str, len)
184 /* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
185 #define srl_buf_cat_str_s_nocheck(buf, str) srl_buf_cat_str_nocheck(buf, ("" str ""), sizeof(str)-1)
186  
187 SRL_STATIC_INLINE void
188 68 srl_buf_cat_char_int(pTHX_ srl_buffer_t *buf, const char c)
189 {
190 DEBUG_ASSERT_BUF_SANE(buf);
191 68 ▶ BUF_SIZE_ASSERT(buf, 1);
Branch: 3 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(buf, 1); 68 0 68
BUF_SIZE_ASSERT(buf, 1); 0 68 68
BUF_SIZE_ASSERT(buf, 1); 0 0 0
192 DEBUG_ASSERT_BUF_SPACE(buf, 1);
193 68 *buf->pos++ = c;
194 DEBUG_ASSERT_BUF_SANE(buf);
195 68 }
196 #define srl_buf_cat_char(buf, c) srl_buf_cat_char_int(aTHX_ buf, c)
197  
198 SRL_STATIC_INLINE void
199 24 srl_buf_cat_char_nocheck_int(pTHX_ srl_buffer_t *buf, const char c)
200 {
201 DEBUG_ASSERT_BUF_SANE(buf);
202 DEBUG_ASSERT_BUF_SPACE(buf, 1);
203 24 *buf->pos++ = c;
204 DEBUG_ASSERT_BUF_SANE(buf);
205 24 }
206 #define srl_buf_cat_char_nocheck(buf, c) srl_buf_cat_char_nocheck_int(aTHX_ buf, c)
207  
208 /*
209 * This implements "varint" and "zigzag varint" types as used in protobufs, etc.
210 *
211 * varint is a variable length encoding of unsigned integers, where the low
212 * 7 bits of the input value are encoded into each byte of output, with the high bit
213 * used as a flag to indicate there is another byte worth of bits to be read.
214 *
215 * zigzag is a way of encoding signed integers as an unsigned integer in such a way
216 * that positive and negative numbers are interleaved, so that z0=0, z1=-1, z2=1,
217 * z3=-2, z4=2, etc. When the zigzag form is represented as a varint, the result is
218 * that both negative and positive number take space proportional to their distance
219 * from zero.
220 *
221 * see: https://developers.google.com/protocol-buffers/docs/encoding#types
222 *
223 */
224 /* Currently unused; retained as a reference for debugging.
225 #define srl_varint_size(x) ( \
226 z <= (1UL << 7) ? 1 : \
227 z <= (1UL << 14) ? 2 : \
228 z <= (1UL << 21) ? 3 : \
229 z <= (1UL << 28) ? 4 : \
230 z <= (1UL << 35) ? 5 : \
231 z <= (1UL << 42) ? 6 : \
232 z <= (1UL << 49) ? 7 : \
233 z <= (1UL << 56) ? 8 : \
234 z <= (1UL << 63) ? 9 : \
235 10 )
236 */
237 SRL_STATIC_INLINE void
238 13 srl_buf_cat_varint_raw_nocheck(pTHX_ srl_buffer_t *buf, UV value) {
239 DEBUG_ASSERT_BUF_SANE(buf);
240 DEBUG_ASSERT_BUF_SPACE(buf, SRL_MAX_VARINT_LENGTH);
241 13 ▶ while (value >= 0x80) { /* while we are larger than 7 bits long */
Branch: 1 branch
BranchTrueFalseTotal
while (value >= 0x80) { /* while we are larger than 7 bits long */ 0 13 13
242 0 *buf->pos++ = (value & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */
243 0 value >>= 7; /* shift off the 7 least significant bits */
244 }
245 13 *buf->pos++ = (U8)value; /* encode the last 7 bits without the high bit being set */
246 DEBUG_ASSERT_BUF_SANE(buf);
247 13 }
248  
249 SRL_STATIC_INLINE UV
250 0 srl_zigzag_iv(IV value) {
251 /* >> of a negative IV is implementation-defined; all supported compilers give all ones */
252 0 return ((UV)value << 1) ^ (UV)(value >> (sizeof(IV) * 8 - 1));
253 }
254  
255 SRL_STATIC_INLINE void
256 srl_buf_cat_zigzag_raw_nocheck(pTHX_ srl_buffer_t *buf, const IV value) {
257 srl_buf_cat_varint_raw_nocheck(aTHX_ buf, srl_zigzag_iv(value));
258 }
259  
260 SRL_STATIC_INLINE void
261 13 srl_buf_cat_varint_nocheck(pTHX_ srl_buffer_t *buf, const char tag, UV value) {
262 DEBUG_ASSERT_BUF_SPACE(buf, 1);
263 13 ▶ if (expect_true( tag ))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true( tag )) 13 0 13
264 13 *buf->pos++ = tag;
265 13 srl_buf_cat_varint_raw_nocheck(aTHX_ buf, value);
266 13 }
267  
268 SRL_STATIC_INLINE void
269 srl_buf_cat_zigzag_nocheck(pTHX_ srl_buffer_t *buf, const char tag, const IV value) {
270 srl_buf_cat_varint_nocheck(aTHX_ buf, tag, srl_zigzag_iv(value));
271 }
272  
273 SRL_STATIC_INLINE void
274 0 srl_buf_cat_varint(pTHX_ srl_buffer_t *buf, const char tag, const UV value) {
275 /* this implements "varint" from google protocol buffers */
276 0 ▶ BUF_SIZE_ASSERT(buf, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */
Branch: 3 branches
BranchTrueFalseTotal
BUF_SIZE_ASSERT(buf, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */ 0 0 0
BUF_SIZE_ASSERT(buf, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */ 0 0 0
BUF_SIZE_ASSERT(buf, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */ 0 0 0
277 0 srl_buf_cat_varint_nocheck(aTHX_ buf, tag, value);
278 0 }
279  
280 SRL_STATIC_INLINE void
281 0 srl_buf_cat_zigzag(pTHX_ srl_buffer_t *buf, const char tag, const IV value) {
282 0 srl_buf_cat_varint(aTHX_ buf, tag, srl_zigzag_iv(value));
283 0 }
284  
285 #endif
« Encoder.xs ↑ Summary srl_siphash.h »