1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
|
/*
* upb - a minimalist implementation of protocol buffers.
*
* Copyright (c) 2010 Google Inc. See LICENSE for details.
* Author: Josh Haberman <jhaberman@gmail.com>
*
* Data structure for storing a message of protobuf data.
*/
#include "upb_msg.h"
void upb_msg_clear(void *msg, upb_msgdef *md) {
memset(msg, 0, md->hasbit_bytes);
// TODO: set primitive fields to defaults?
}
void *upb_stdarray_append(upb_stdarray *a, size_t type_size) {
assert(a->len <= a->size);
if (a->len == a->size) {
size_t old_size = a->size;
a->size = old_size == 0 ? 8 : (old_size * 2);
a->ptr = realloc(a->ptr, a->size * type_size);
memset(&a->ptr[old_size * type_size], 0, (a->size - old_size) * type_size);
}
return &a->ptr[a->len++ * type_size];
}
#if 0
static upb_flow_t upb_msg_dispatch(upb_msg *msg, upb_msgdef *md,
upb_dispatcher *d);
static upb_flow_t upb_msg_pushval(upb_value val, upb_fielddef *f,
upb_dispatcher *d, upb_fhandlers *hf) {
if (upb_issubmsg(f)) {
upb_msg *msg = upb_value_getmsg(val);
upb_dispatch_startsubmsg(d, hf);
upb_msg_dispatch(msg, upb_downcast_msgdef(f->def), d);
upb_dispatch_endsubmsg(d);
} else {
upb_dispatch_value(d, hf, val);
}
return UPB_CONTINUE;
}
static upb_flow_t upb_msg_dispatch(upb_msg *msg, upb_msgdef *md,
upb_dispatcher *d) {
upb_msg_iter i;
for(i = upb_msg_begin(md); !upb_msg_done(i); i = upb_msg_next(md, i)) {
upb_fielddef *f = upb_msg_iter_field(i);
if (!upb_msg_has(msg, f)) continue;
upb_fhandlers *hf = upb_dispatcher_lookup(d, f->number);
if (!hf) continue;
upb_value val = upb_msg_get(msg, f);
if (upb_isarray(f)) {
upb_array *arr = upb_value_getarr(val);
for (uint32_t j = 0; j < upb_array_len(arr); ++j) {
upb_msg_pushval(upb_array_get(arr, f, j), f, d, hf);
}
} else {
upb_msg_pushval(val, f, d, hf);
}
}
return UPB_CONTINUE;
}
void upb_msg_runhandlers(upb_msg *msg, upb_msgdef *md, upb_handlers *h,
void *closure, upb_status *status) {
upb_dispatcher d;
upb_dispatcher_init(&d, h, NULL, NULL, NULL);
upb_dispatcher_reset(&d, closure);
upb_dispatch_startmsg(&d);
upb_msg_dispatch(msg, md, &d);
upb_dispatch_endmsg(&d, status);
upb_dispatcher_uninit(&d);
}
#endif
/* Standard writers. **********************************************************/
void upb_stdmsg_sethas(void *_m, upb_value fval) {
char *m = _m;
upb_fielddef *f = upb_value_getfielddef(fval);
if (f->hasbit >= 0) m[f->hasbit / 8] |= (1 << (f->hasbit % 8));
}
bool upb_stdmsg_has(void *_m, upb_value fval) {
char *m = _m;
upb_fielddef *f = upb_value_getfielddef(fval);
return f->hasbit < 0 || (m[f->hasbit / 8] & (1 << (f->hasbit % 8)));
}
#define UPB_ACCESSORS(type, ctype) \
upb_flow_t upb_stdmsg_set ## type (void *_m, upb_value fval, \
upb_value val) { \
upb_fielddef *f = upb_value_getfielddef(fval); \
uint8_t *m = _m; \
upb_stdmsg_sethas(_m, fval); \
*(ctype*)&m[f->offset] = upb_value_get ## type(val); \
return UPB_CONTINUE; \
} \
\
upb_flow_t upb_stdmsg_set ## type ## _r(void *a, upb_value _fval, \
upb_value val) { \
(void)_fval; \
ctype *p = upb_stdarray_append((upb_stdarray*)a, sizeof(ctype)); \
*p = upb_value_get ## type(val); \
return UPB_CONTINUE; \
} \
\
upb_value upb_stdmsg_get ## type(void *_m, upb_value fval) { \
uint8_t *m = _m; \
upb_fielddef *f = upb_value_getfielddef(fval); \
upb_value ret; \
upb_value_set ## type(&ret, *(ctype*)&m[f->offset]); \
return ret; \
} \
upb_value upb_stdmsg_seqget ## type(void *i) { \
upb_value val; \
upb_value_set ## type(&val, *(ctype*)i); \
return val; \
}
UPB_ACCESSORS(double, double)
UPB_ACCESSORS(float, float)
UPB_ACCESSORS(int32, int32_t)
UPB_ACCESSORS(int64, int64_t)
UPB_ACCESSORS(uint32, uint32_t)
UPB_ACCESSORS(uint64, uint64_t)
UPB_ACCESSORS(bool, bool)
UPB_ACCESSORS(ptr, void*)
#undef UPB_ACCESSORS
static void _upb_stdmsg_setstr(void *_dst, upb_value _src) {
// We do:
// - upb_string_recycle(), upb_string_substr() instead of
// - upb_string_unref(), upb_string_getref()
// because we can conveniently cache these upb_string objects in the
// upb_msg, whereas the upb_src who is sending us these strings may not
// have a good way of caching them. This saves the upb_src from allocating
// new upb_strings all the time to give us.
//
// If you were using this to copy one upb_msg to another this would
// allocate string objects whereas a upb_string_getref could have avoided
// those allocations completely; if this is an issue, we could make it an
// option of the upb_msgsink which behavior is desired.
upb_string **dst = _dst;
upb_string *src = upb_value_getstr(_src);
upb_string_recycle(dst);
upb_string_substr(*dst, src, 0, upb_string_len(src));
}
upb_flow_t upb_stdmsg_setstr(void *_m, upb_value fval, upb_value val) {
char *m = _m;
upb_fielddef *f = upb_value_getfielddef(fval);
upb_stdmsg_sethas(_m, fval);
_upb_stdmsg_setstr(&m[f->offset], val);
return UPB_CONTINUE;
}
upb_flow_t upb_stdmsg_setstr_r(void *a, upb_value fval, upb_value val) {
(void)fval;
_upb_stdmsg_setstr(upb_stdarray_append((upb_stdarray*)a, sizeof(void*)), val);
return UPB_CONTINUE;
}
upb_value upb_stdmsg_getstr(void *m, upb_value fval) {
upb_value val = upb_stdmsg_getptr(m, fval);
upb_value_setstr(&val, upb_value_getptr(val));
return val;
}
upb_value upb_stdmsg_seqgetstr(void *i) {
upb_value val = upb_stdmsg_seqgetptr(i);
upb_value_setstr(&val, upb_value_getptr(val));
return val;
}
void *upb_stdmsg_new(upb_msgdef *md) {
void *m = malloc(md->size);
memset(m, 0, md->size);
upb_msg_clear(m, md);
return m;
}
void upb_stdseq_free(void *s, upb_fielddef *f) {
upb_stdarray *a = s;
if (upb_issubmsg(f) || upb_isstring(f)) {
void **p = (void**)a->ptr;
for (int i = 0; i < a->size; i++) {
if (upb_issubmsg(f)) {
upb_stdmsg_free(p[i], upb_downcast_msgdef(f->def));
} else {
upb_string_unref(p[i]);
}
}
}
free(a->ptr);
free(a);
}
void upb_stdmsg_free(void *m, upb_msgdef *md) {
if (m == NULL) return;
upb_msg_iter i;
for(i = upb_msg_begin(md); !upb_msg_done(i); i = upb_msg_next(md, i)) {
upb_fielddef *f = upb_msg_iter_field(i);
if (!upb_isseq(f) && !upb_issubmsg(f) && !upb_isstring(f)) continue;
void *subp = upb_value_getptr(upb_stdmsg_getptr(m, f->fval));
if (subp == NULL) continue;
if (upb_isseq(f)) {
upb_stdseq_free(subp, f);
} else if (upb_issubmsg(f)) {
upb_stdmsg_free(subp, upb_downcast_msgdef(f->def));
} else {
upb_string_unref(subp);
}
}
free(m);
}
upb_sflow_t upb_stdmsg_startseq(void *_m, upb_value fval) {
char *m = _m;
upb_fielddef *f = upb_value_getfielddef(fval);
upb_stdarray **arr = (void*)&m[f->offset];
if (!upb_stdmsg_has(_m, fval)) {
if (!*arr) {
*arr = malloc(sizeof(**arr));
(*arr)->size = 0;
(*arr)->ptr = NULL;
}
(*arr)->len = 0;
upb_stdmsg_sethas(m, fval);
}
return UPB_CONTINUE_WITH(*arr);
}
void upb_stdmsg_recycle(void **m, upb_msgdef *md) {
if (*m)
upb_msg_clear(*m, md);
else
*m = upb_stdmsg_new(md);
}
upb_sflow_t upb_stdmsg_startsubmsg(void *_m, upb_value fval) {
char *m = _m;
upb_fielddef *f = upb_value_getfielddef(fval);
void **subm = (void*)&m[f->offset];
if (!upb_stdmsg_has(m, fval)) {
upb_stdmsg_recycle(subm, upb_downcast_msgdef(f->def));
upb_stdmsg_sethas(m, fval);
}
return UPB_CONTINUE_WITH(*subm);
}
upb_sflow_t upb_stdmsg_startsubmsg_r(void *a, upb_value fval) {
assert(a != NULL);
upb_fielddef *f = upb_value_getfielddef(fval);
void **subm = upb_stdarray_append((upb_stdarray*)a, sizeof(void*));
upb_stdmsg_recycle(subm, upb_downcast_msgdef(f->def));
return UPB_CONTINUE_WITH(*subm);
}
void *upb_stdmsg_seqbegin(void *_a) {
upb_stdarray *a = _a;
return a->len > 0 ? a->ptr : NULL;
}
#define NEXTFUNC(size) \
void *upb_stdmsg_ ## size ## byte_seqnext(void *_a, void *iter) { \
upb_stdarray *a = _a; \
void *next = (char*)iter + size; \
return (char*)next < (char*)a->ptr + (a->len * size) ? next : NULL; \
}
NEXTFUNC(8)
NEXTFUNC(4)
NEXTFUNC(1)
#define STDMSG(type) { static upb_accessor_vtbl vtbl = {NULL, &upb_stdmsg_startsubmsg, \
&upb_stdmsg_set ## type, &upb_stdmsg_has, &upb_stdmsg_get ## type, \
NULL, NULL, NULL}; return &vtbl; }
#define STDMSG_R(type, size) { static upb_accessor_vtbl vtbl = { \
&upb_stdmsg_startseq, &upb_stdmsg_startsubmsg_r, &upb_stdmsg_set ## type ## _r, \
&upb_stdmsg_has, &upb_stdmsg_getptr, &upb_stdmsg_seqbegin, \
&upb_stdmsg_ ## size ## byte_seqnext, &upb_stdmsg_seqget ## type}; \
return &vtbl; }
upb_accessor_vtbl *upb_stdmsg_accessor(upb_fielddef *f) {
if (upb_isseq(f)) {
switch (f->type) {
case UPB_TYPE(DOUBLE): STDMSG_R(double, 8)
case UPB_TYPE(FLOAT): STDMSG_R(float, 4)
case UPB_TYPE(UINT64):
case UPB_TYPE(FIXED64): STDMSG_R(uint64, 8)
case UPB_TYPE(INT64):
case UPB_TYPE(SFIXED64):
case UPB_TYPE(SINT64): STDMSG_R(int64, 8)
case UPB_TYPE(INT32):
case UPB_TYPE(SINT32):
case UPB_TYPE(ENUM):
case UPB_TYPE(SFIXED32): STDMSG_R(int32, 4)
case UPB_TYPE(UINT32):
case UPB_TYPE(FIXED32): STDMSG_R(uint32, 4)
case UPB_TYPE(BOOL): STDMSG_R(bool, 1)
case UPB_TYPE(STRING):
case UPB_TYPE(BYTES):
case UPB_TYPE(GROUP):
case UPB_TYPE(MESSAGE): STDMSG_R(str, 8) // TODO: 32-bit
}
} else {
switch (f->type) {
case UPB_TYPE(DOUBLE): STDMSG(double)
case UPB_TYPE(FLOAT): STDMSG(float)
case UPB_TYPE(UINT64):
case UPB_TYPE(FIXED64): STDMSG(uint64)
case UPB_TYPE(INT64):
case UPB_TYPE(SFIXED64):
case UPB_TYPE(SINT64): STDMSG(int64)
case UPB_TYPE(INT32):
case UPB_TYPE(SINT32):
case UPB_TYPE(ENUM):
case UPB_TYPE(SFIXED32): STDMSG(int32)
case UPB_TYPE(UINT32):
case UPB_TYPE(FIXED32): STDMSG(uint32)
case UPB_TYPE(BOOL): STDMSG(bool)
case UPB_TYPE(STRING):
case UPB_TYPE(BYTES):
case UPB_TYPE(GROUP):
case UPB_TYPE(MESSAGE): STDMSG(str)
}
}
return NULL;
}
static void upb_accessors_onfreg(void *c, upb_fhandlers *fh, upb_fielddef *f) {
(void)c;
if (f->accessor) {
upb_fhandlers_setstartseq(fh, f->accessor->appendseq);
upb_fhandlers_setvalue(fh, f->accessor->set);
upb_fhandlers_setstartsubmsg(fh, f->accessor->appendsubmsg);
upb_fhandlers_setfval(fh, f->fval);
}
}
upb_mhandlers *upb_accessors_reghandlers(upb_handlers *h, upb_msgdef *m) {
return upb_handlers_regmsgdef(h, m, NULL, &upb_accessors_onfreg, NULL);
}
|