srl_buffer.h

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