~ubuntu-branches/ubuntu/wily/libsereal-encoder-perl/wily

« back to all changes in this revision

Viewing changes to srl_buffer.h

  • Committer: Package Import Robot
  • Author(s): Alexandre Mestiashvili, Alexandre Mestiashvili, gregor herrmann
  • Date: 2015-04-29 11:12:18 UTC
  • mfrom: (17.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20150429111218-v3ghc7ck5gcr38fu
Tags: 3.005.001-1
[ Alexandre Mestiashvili ]
* Imported Upstream version 3.005.001
* d/control: cme fix dpkg
* d/copyright: updated debian/* copyright year

[ gregor herrmann ]
* Mark package as autopkgtest-able.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
#include "srl_inline.h"
7
7
#include "srl_common.h"
8
 
#include "srl_encoder.h"
9
 
 
10
8
#include "srl_buffer_types.h"
11
9
 
12
10
#ifdef MEMDEBUG
20
18
 * For now, potentially smaller code wins. */
21
19
 
22
20
/* buffer operations */
23
 
#define BUF_POS_OFS(buf) (((buf).pos) - ((buf).start))
24
 
#define BUF_SPACE(buf) (((buf).end) - ((buf).pos))
25
 
#define BUF_SIZE(buf) (((buf).end) - ((buf).start))
 
21
#define BUF_POS_OFS(buf) (((buf)->pos) - ((buf)->start))
 
22
#define BUF_SPACE(buf) (((buf)->end) - ((buf)->pos))
 
23
#define BUF_SIZE(buf) (((buf)->end) - ((buf)->start))
26
24
#define BUF_NEED_GROW(buf, minlen) ((size_t)BUF_SPACE(buf) <= minlen)
27
25
#define BUF_NEED_GROW_TOTAL(buf, minlen) ((size_t)BUF_SIZE(buf) <= minlen)
28
 
 
 
26
#define BUF_NOT_DONE(buf) ((buf)->pos < (buf)->end)
 
27
#define BUF_DONE(buf) ((buf)->pos >= (buf)->end)
29
28
 
30
29
/* body-position/size related operations */
31
 
#define BODY_POS_OFS(buf) (((buf).pos) - ((buf).body_pos))
 
30
#define BODY_POS_OFS(buf) (((buf)->pos) - ((buf)->body_pos))
32
31
 
33
32
/* these are mostly for right between (de)serializing the header and the body */
34
 
#define SRL_SET_BODY_POS(enc, pos_ptr) ((enc)->buf.body_pos = pos_ptr)
35
 
#define SRL_UPDATE_BODY_POS(enc)                                            \
 
33
#define SRL_SET_BODY_POS(buf, pos_ptr) ((buf)->body_pos = pos_ptr)
 
34
#define SRL_UPDATE_BODY_POS(buf, protocol_version)                          \
36
35
    STMT_START {                                                            \
37
 
        if (expect_false((enc)->protocol_version == 1)) {                   \
38
 
            SRL_SET_BODY_POS(enc, (enc)->buf.start);                        \
 
36
        if (expect_false((protocol_version) == 1)) {                        \
 
37
            SRL_SET_BODY_POS((buf), (buf)->start);                          \
39
38
        } else {                                                            \
40
 
            SRL_SET_BODY_POS(enc, (enc)->buf.pos-1);                        \
 
39
            SRL_SET_BODY_POS((buf), (buf)->pos-1);                          \
41
40
        }                                                                   \
42
41
    } STMT_END
43
42
 
44
 
 
45
43
/* Internal debugging macros, used only in DEBUG mode */
46
44
#ifndef NDEBUG
47
 
#define DEBUG_ASSERT_BUF_SPACE(enc, len) STMT_START {                       \
48
 
    if((BUF_SPACE(enc->buf) < (ptrdiff_t)(len))) {                          \
 
45
#define DEBUG_ASSERT_BUF_SPACE(buf, len) STMT_START {                       \
 
46
    if((BUF_SPACE(buf) < (ptrdiff_t)(len))) {                               \
49
47
        warn("failed assertion check - pos: %ld [%p %p %p] %ld < %ld",      \
50
 
                (long)BUF_POS_OFS(enc->buf), (enc)->buf.start,              \
51
 
                (enc)->buf.pos, (enc)->buf.end,                             \
52
 
                (long)BUF_SPACE(enc->buf),(long)(len));                     \
 
48
                (long)BUF_POS_OFS(buf), (buf)->start,                       \
 
49
                (buf)->pos, (buf)->end,                                     \
 
50
                (long)BUF_SPACE(buf),(long)(len));                          \
53
51
    }                                                                       \
54
 
    assert(BUF_SPACE(enc->buf) >= (ptrdiff_t)(len));                        \
 
52
    assert(BUF_SPACE(buf) >= (ptrdiff_t)(len));                             \
55
53
} STMT_END
56
54
#else
57
 
#define DEBUG_ASSERT_BUF_SPACE(enc, len) ((void)0)
 
55
#define DEBUG_ASSERT_BUF_SPACE(buf, len) ((void)0)
58
56
#endif
59
57
 
60
58
#ifndef NDEBUG
61
 
#define DEBUG_ASSERT_BUF_SANE(enc) STMT_START {                                             \
62
 
    if(!(((enc)->buf.start <= (enc)->buf.pos) && ((enc)->buf.pos <= (enc)->buf.end))){      \
63
 
        warn("failed sanity assertion check - pos: %ld [%p %p %p] %ld",                     \
64
 
                (long)BUF_POS_OFS(enc->buf), (enc)->buf.start,                              \
65
 
                (enc)->buf.pos, (enc)->buf.end, (long)BUF_SPACE(enc->buf));                 \
66
 
    }                                                                                       \
67
 
    assert(((enc)->buf.start <= (enc)->buf.pos) && ((enc)->buf.pos <= (enc)->buf.end));     \
 
59
#define DEBUG_ASSERT_BUF_SANE(buf) STMT_START {                             \
 
60
    if(!(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end))){      \
 
61
        warn("failed sanity assertion check - pos: %ld [%p %p %p] %ld",     \
 
62
                (long)BUF_POS_OFS(buf), (buf)->start,                       \
 
63
                (buf)->pos, (buf)->end, (long)BUF_SPACE(buf));              \
 
64
    }                                                                       \
 
65
    assert(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end));     \
68
66
} STMT_END
69
67
#else
70
 
#define DEBUG_ASSERT_BUF_SANE(enc)                                                      \
71
 
    assert(((enc)->buf.start <= (enc)->buf.pos) && ((enc)->buf.pos <= (enc)->buf.end))
 
68
#define DEBUG_ASSERT_BUF_SANE(buf)                                          \
 
69
    assert(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end))
72
70
#endif
73
71
 
74
72
/* Allocate a virgin buffer (but not the buffer struct) */
75
73
SRL_STATIC_INLINE int
76
74
srl_buf_init_buffer(pTHX_ srl_buffer_t *buf, const STRLEN init_size)
77
75
{
78
 
    Newx(buf->start, init_size, char);
 
76
    Newx(buf->start, init_size, srl_buffer_char);
79
77
    if (expect_false( buf->start == NULL ))
80
78
        return 1;
81
79
    buf->end = buf->start + init_size - 1;
108
106
    Copy(&tmp, buf2, 1, srl_buffer_t);
109
107
}
110
108
 
111
 
 
112
109
SRL_STATIC_INLINE void
113
 
srl_buf_grow_nocheck(pTHX_ srl_encoder_t *enc, size_t minlen)
 
110
srl_buf_grow_nocheck(pTHX_ srl_buffer_t *buf, size_t minlen)
114
111
{
115
 
    const size_t pos_ofs= BUF_POS_OFS(enc->buf); /* have to store the offset of pos */
116
 
    const size_t body_ofs= enc->buf.body_pos - enc->buf.start; /* have to store the offset of the body */
 
112
    const size_t pos_ofs= BUF_POS_OFS(buf); /* have to store the offset of pos */
 
113
    const size_t body_ofs= buf->body_pos - buf->start; /* have to store the offset of the body */
117
114
#ifdef MEMDEBUG
118
115
    const size_t new_size = minlen;
119
116
#else
120
 
    const size_t cur_size = BUF_SIZE(enc->buf);
 
117
    const size_t cur_size = BUF_SIZE(buf);
121
118
    const size_t grown_len = (size_t)(cur_size * BUFFER_GROWTH_FACTOR);
122
119
    const size_t new_size = 100 + (minlen > grown_len ? minlen : grown_len);
123
120
#endif
124
121
 
125
 
    DEBUG_ASSERT_BUF_SANE(enc);
 
122
    DEBUG_ASSERT_BUF_SANE(buf);
126
123
    /* assert that Renew means GROWING the buffer */
127
 
    assert(enc->buf.start + new_size > enc->buf.end);
 
124
    assert(buf->start + new_size > buf->end);
128
125
 
129
 
    Renew(enc->buf.start, new_size, char);
130
 
    if (enc->buf.start == NULL)
 
126
    Renew(buf->start, new_size, srl_buffer_char);
 
127
    if (buf->start == NULL)
131
128
        croak("Out of memory!");
132
 
    enc->buf.end = (char *)(enc->buf.start + new_size);
133
 
    enc->buf.pos= enc->buf.start + pos_ofs;
134
 
    SRL_SET_BODY_POS(enc, enc->buf.start + body_ofs);
135
 
 
136
 
    DEBUG_ASSERT_BUF_SANE(enc);
137
 
    assert(enc->buf.end - enc->buf.start > (ptrdiff_t)0);
138
 
    assert(enc->buf.pos - enc->buf.start >= (ptrdiff_t)0);
 
129
 
 
130
    buf->end = (srl_buffer_char*) (buf->start + new_size);
 
131
    buf->pos = buf->start + pos_ofs;
 
132
    SRL_SET_BODY_POS(buf, buf->start + body_ofs);
 
133
 
 
134
    DEBUG_ASSERT_BUF_SANE(buf);
 
135
    assert(buf->end - buf->start > (ptrdiff_t)0);
 
136
    assert(buf->pos - buf->start >= (ptrdiff_t)0);
139
137
    /* The following is checking against -1 because SRL_UPDATE_BODY_POS
140
138
     * will actually set the body_pos to pos-1, where pos can be 0.
141
139
     * This works out fine in the end, but is admittedly a bit shady.
142
140
     * FIXME */
143
 
    assert(enc->buf.body_pos - enc->buf.start >= (ptrdiff_t)-1);
144
 
}
145
 
 
146
 
#define BUF_SIZE_ASSERT(enc, minlen)                                    \
147
 
  STMT_START {                                                          \
148
 
    DEBUG_ASSERT_BUF_SANE(enc);                                         \
149
 
    if (BUF_NEED_GROW(enc->buf, minlen))                                \
150
 
      srl_buf_grow_nocheck(aTHX_ (enc), (BUF_SIZE(enc->buf) + minlen)); \
151
 
    DEBUG_ASSERT_BUF_SANE(enc);                                         \
152
 
  } STMT_END
153
 
 
154
 
#define BUF_SIZE_ASSERT_TOTAL(enc, minlen)                              \
155
 
  STMT_START {                                                          \
156
 
    DEBUG_ASSERT_BUF_SANE(enc);                                         \
157
 
    if (BUF_NEED_GROW_TOTAL(enc->buf, minlen))                          \
158
 
      srl_buf_grow_nocheck(aTHX_ (enc), (minlen));                      \
159
 
    DEBUG_ASSERT_BUF_SANE(enc);                                         \
160
 
  } STMT_END
161
 
 
162
 
SRL_STATIC_INLINE void
163
 
srl_buf_cat_str_int(pTHX_ srl_encoder_t *enc, const char *str, size_t len)
164
 
{
165
 
    BUF_SIZE_ASSERT(enc, len);
166
 
    Copy(str, enc->buf.pos, len, char);
167
 
    enc->buf.pos += len;
168
 
    DEBUG_ASSERT_BUF_SANE(enc);
169
 
}
170
 
#define srl_buf_cat_str(enc, str, len) srl_buf_cat_str_int(aTHX_ enc, 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(enc, str) srl_buf_cat_str(enc, ("" str ""), sizeof(str)-1)
173
 
 
174
 
SRL_STATIC_INLINE void
175
 
srl_buf_cat_str_nocheck_int(pTHX_ srl_encoder_t *enc, const char *str, size_t len)
176
 
{
177
 
    DEBUG_ASSERT_BUF_SANE(enc);
178
 
    DEBUG_ASSERT_BUF_SPACE(enc, len);
179
 
    Copy(str, enc->buf.pos, len, char);
180
 
    enc->buf.pos += len;
181
 
    DEBUG_ASSERT_BUF_SANE(enc);
182
 
}
183
 
#define srl_buf_cat_str_nocheck(enc, str, len) srl_buf_cat_str_nocheck_int(aTHX_ enc, 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(enc, str) srl_buf_cat_str_nocheck(enc, ("" str ""), sizeof(str)-1)
186
 
 
187
 
SRL_STATIC_INLINE void
188
 
srl_buf_cat_char_int(pTHX_ srl_encoder_t *enc, const char c)
189
 
{
190
 
    DEBUG_ASSERT_BUF_SANE(enc);
191
 
    BUF_SIZE_ASSERT(enc, 1);
192
 
    DEBUG_ASSERT_BUF_SPACE(enc, 1);
193
 
    *enc->buf.pos++ = c;
194
 
    DEBUG_ASSERT_BUF_SANE(enc);
195
 
}
196
 
#define srl_buf_cat_char(enc, c) srl_buf_cat_char_int(aTHX_ enc, c)
197
 
 
198
 
SRL_STATIC_INLINE void
199
 
srl_buf_cat_char_nocheck_int(pTHX_ srl_encoder_t *enc, const char c)
200
 
{
201
 
    DEBUG_ASSERT_BUF_SANE(enc);
202
 
    DEBUG_ASSERT_BUF_SPACE(enc, 1);
203
 
    *enc->buf.pos++ = c;
204
 
    DEBUG_ASSERT_BUF_SANE(enc);
205
 
}
206
 
#define srl_buf_cat_char_nocheck(enc, c) srl_buf_cat_char_nocheck_int(aTHX_ enc, c)
 
141
    assert(buf->body_pos - buf->start >= (ptrdiff_t)-1);
 
142
}
 
143
 
 
144
#define BUF_SIZE_ASSERT(buf, minlen)                                    \
 
145
  STMT_START {                                                          \
 
146
    DEBUG_ASSERT_BUF_SANE(buf);                                         \
 
147
    if (BUF_NEED_GROW(buf, minlen))                                     \
 
148
      srl_buf_grow_nocheck(aTHX_ (buf), (BUF_SIZE(buf) + minlen));      \
 
149
    DEBUG_ASSERT_BUF_SANE(buf);                                         \
 
150
  } STMT_END
 
151
 
 
152
#define BUF_SIZE_ASSERT_TOTAL(buf, minlen)                              \
 
153
  STMT_START {                                                          \
 
154
    DEBUG_ASSERT_BUF_SANE(buf);                                         \
 
155
    if (BUF_NEED_GROW_TOTAL(buf, minlen))                               \
 
156
      srl_buf_grow_nocheck(aTHX_ (buf), (minlen));                      \
 
157
    DEBUG_ASSERT_BUF_SANE(buf);                                         \
 
158
  } STMT_END
 
159
 
 
160
SRL_STATIC_INLINE void
 
161
srl_buf_cat_str_int(pTHX_ srl_buffer_t *buf, const char *str, size_t len)
 
162
{
 
163
    BUF_SIZE_ASSERT(buf, len);
 
164
    Copy(str, buf->pos, len, char);
 
165
    buf->pos += len;
 
166
    DEBUG_ASSERT_BUF_SANE(buf);
 
167
}
 
168
#define srl_buf_cat_str(buf, str, len) srl_buf_cat_str_int(aTHX_ buf, str, len)
 
169
/* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
 
170
#define srl_buf_cat_str_s(buf, str) srl_buf_cat_str(buf, ("" str ""), sizeof(str)-1)
 
171
 
 
172
SRL_STATIC_INLINE void
 
173
srl_buf_cat_str_nocheck_int(pTHX_ srl_buffer_t *buf, const char *str, size_t len)
 
174
{
 
175
    DEBUG_ASSERT_BUF_SANE(buf);
 
176
    DEBUG_ASSERT_BUF_SPACE(buf, len);
 
177
    Copy(str, buf->pos, len, char);
 
178
    buf->pos += len;
 
179
    DEBUG_ASSERT_BUF_SANE(buf);
 
180
}
 
181
#define srl_buf_cat_str_nocheck(buf, str, len) srl_buf_cat_str_nocheck_int(aTHX_ buf, str, len)
 
182
/* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
 
183
#define srl_buf_cat_str_s_nocheck(buf, str) srl_buf_cat_str_nocheck(buf, ("" str ""), sizeof(str)-1)
 
184
 
 
185
SRL_STATIC_INLINE void
 
186
srl_buf_cat_char_int(pTHX_ srl_buffer_t *buf, const char c)
 
187
{
 
188
    DEBUG_ASSERT_BUF_SANE(buf);
 
189
    BUF_SIZE_ASSERT(buf, 1);
 
190
    DEBUG_ASSERT_BUF_SPACE(buf, 1);
 
191
    *buf->pos++ = c;
 
192
    DEBUG_ASSERT_BUF_SANE(buf);
 
193
}
 
194
#define srl_buf_cat_char(buf, c) srl_buf_cat_char_int(aTHX_ buf, c)
 
195
 
 
196
SRL_STATIC_INLINE void
 
197
srl_buf_cat_char_nocheck_int(pTHX_ srl_buffer_t *buf, const char c)
 
198
{
 
199
    DEBUG_ASSERT_BUF_SANE(buf);
 
200
    DEBUG_ASSERT_BUF_SPACE(buf, 1);
 
201
    *buf->pos++ = c;
 
202
    DEBUG_ASSERT_BUF_SANE(buf);
 
203
}
 
204
#define srl_buf_cat_char_nocheck(buf, c) srl_buf_cat_char_nocheck_int(aTHX_ buf, c)
207
205
 
208
206
/* define constant for other code to use in preallocations */
209
207
#define SRL_MAX_VARINT_LENGTH 11
210
 
 
211
 
SRL_STATIC_INLINE void
212
 
srl_buf_cat_varint_nocheck(pTHX_ srl_encoder_t *enc, const char tag, UV n) {
213
 
    DEBUG_ASSERT_BUF_SANE(enc);
214
 
    DEBUG_ASSERT_BUF_SPACE(enc, (tag==0 ? 0 : 1) + SRL_MAX_VARINT_LENGTH);
 
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
#define srl_varint_size(x) (    \
 
225
    z <= (1UL << 7)  ? 1 :    \
 
226
    z <= (1UL << 14) ? 2 :    \
 
227
    z <= (1UL << 21) ? 3 :    \
 
228
    z <= (1UL << 28) ? 4 :    \
 
229
    z <= (1UL << 35) ? 5 :    \
 
230
    z <= (1UL << 42) ? 6 :    \
 
231
    z <= (1UL << 49) ? 7 :    \
 
232
    z <= (1UL << 56) ? 8 :    \
 
233
    z <= (1UL << 63) ? 9 :    \
 
234
                     10 )
 
235
 
 
236
 
 
237
SRL_STATIC_INLINE void
 
238
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
    while (value >= 0x80) {                     /* while we are larger than 7 bits long */
 
242
        *buf->pos++ = (value & 0x7f) | 0x80;    /* write out the least significant 7 bits, set the high bit */
 
243
        value >>= 7;                            /* shift off the 7 least significant bits */
 
244
    }
 
245
    *buf->pos++ = (U8)value;                    /* encode the last 7 bits without the high bit being set */
 
246
    DEBUG_ASSERT_BUF_SANE(buf);
 
247
}
 
248
 
 
249
SRL_STATIC_INLINE UV
 
250
srl_zigzag_iv(IV value) {
 
251
    return (UV)((value << 1) ^ (value >> (sizeof(IV) * 8 - 1)));
 
252
}
 
253
 
 
254
SRL_STATIC_INLINE void
 
255
srl_buf_cat_zigzag_raw_nocheck(pTHX_ srl_buffer_t *buf, const IV value) {
 
256
    srl_buf_cat_varint_raw_nocheck(aTHX_ buf, srl_zigzag_iv(value));
 
257
}
 
258
 
 
259
SRL_STATIC_INLINE void
 
260
srl_buf_cat_varint_nocheck(pTHX_ srl_buffer_t *buf, const char tag, UV value) {
 
261
    DEBUG_ASSERT_BUF_SPACE(buf, 1);
215
262
    if (expect_true( tag ))
216
 
        *enc->buf.pos++ = tag;
217
 
    while (n >= 0x80) {                      /* while we are larger than 7 bits long */
218
 
        *enc->buf.pos++ = (n & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */
219
 
        n = n >> 7;                          /* shift off the 7 least significant bits */
220
 
    }
221
 
    *enc->buf.pos++ = n;                     /* encode the last 7 bits without the high bit being set */
222
 
    DEBUG_ASSERT_BUF_SANE(enc);
223
 
}
224
 
 
225
 
SRL_STATIC_INLINE void
226
 
srl_buf_cat_varint(pTHX_ srl_encoder_t *enc, const char tag, const UV n) {
 
263
        *buf->pos++ = tag;
 
264
    srl_buf_cat_varint_raw_nocheck(aTHX_ buf, value);
 
265
}
 
266
 
 
267
SRL_STATIC_INLINE void
 
268
srl_buf_cat_zigzag_nocheck(pTHX_ srl_buffer_t *buf, const char tag, const IV value) {
 
269
    srl_buf_cat_varint_nocheck(aTHX_ buf, tag, srl_zigzag_iv(value));
 
270
}
 
271
 
 
272
SRL_STATIC_INLINE void
 
273
srl_buf_cat_varint(pTHX_ srl_buffer_t *buf, const char tag, const UV value) {
227
274
    /* this implements "varint" from google protocol buffers */
228
 
    DEBUG_ASSERT_BUF_SANE(enc);
229
 
    BUF_SIZE_ASSERT(enc, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */
230
 
    srl_buf_cat_varint_nocheck(aTHX_ enc, tag, n);
231
 
}
232
 
 
233
 
SRL_STATIC_INLINE void
234
 
srl_buf_cat_zigzag_nocheck(pTHX_ srl_encoder_t *enc, const char tag, const IV n) {
235
 
    const UV z= (n << 1) ^ (n >> (sizeof(IV) * 8 - 1));
236
 
    srl_buf_cat_varint_nocheck(aTHX_ enc, tag, z);
237
 
}
238
 
 
239
 
SRL_STATIC_INLINE void
240
 
srl_buf_cat_zigzag(pTHX_ srl_encoder_t *enc, const char tag, const IV n) {
241
 
    /*
242
 
     * This implements googles "zigzag varints" which effectively interleave negative
243
 
     * and positive numbers.
244
 
     *
245
 
     * see: https://developers.google.com/protocol-buffers/docs/encoding#types
246
 
     *
247
 
     * Note: maybe for negative numbers we should just invert and then treat as a positive?
248
 
     *
249
 
     */
250
 
    const UV z= (n << 1) ^ (n >> (sizeof(IV) * 8 - 1));
251
 
    srl_buf_cat_varint(aTHX_ enc, tag, z);
 
275
    BUF_SIZE_ASSERT(buf, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */
 
276
    srl_buf_cat_varint_nocheck(aTHX_ buf, tag, value);
 
277
}
 
278
 
 
279
SRL_STATIC_INLINE void
 
280
srl_buf_cat_zigzag(pTHX_ srl_buffer_t *buf, const char tag, const IV value) {
 
281
    srl_buf_cat_varint(aTHX_ buf, tag, srl_zigzag_iv(value));
252
282
}
253
283
 
254
284
#endif