summaryrefslogtreecommitdiff
path: root/upb/handlers.c
blob: ea5a054a33bba961b9aadd440bc6bb02364e15b7 (plain)
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
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2011 Google Inc.  See LICENSE for details.
 * Author: Josh Haberman <jhaberman@gmail.com>
 */

#include <stdlib.h>
#include "upb/handlers.h"


/* upb_mhandlers **************************************************************/

static upb_mhandlers *upb_mhandlers_new() {
  upb_mhandlers *m = malloc(sizeof(*m));
  upb_inttable_init(&m->fieldtab);
  m->startmsg = NULL;
  m->endmsg = NULL;
  m->is_group = false;
#ifdef UPB_USE_JIT_X64
  m->tablearray = NULL;
#endif
  return m;
}

static upb_fhandlers *_upb_mhandlers_newfhandlers(upb_mhandlers *m, uint32_t n,
                                                  upb_fieldtype_t type,
                                                  bool repeated) {
  const upb_value *v = upb_inttable_lookup(&m->fieldtab, n);
  // TODO: design/refine the API for changing the set of fields or modifying
  // existing handlers.
  if (v) return NULL;
  upb_fhandlers new_f = {type, repeated, 0,
      n, -1, m, NULL, UPB_NO_VALUE, NULL, NULL, NULL, NULL, NULL,
#ifdef UPB_USE_JIT_X64
      0, 0, 0,
#endif
  };
  upb_fhandlers *ptr = malloc(sizeof(*ptr));
  memcpy(ptr, &new_f, sizeof(upb_fhandlers));
  upb_inttable_insert(&m->fieldtab, n, upb_value_ptr(ptr));
  return ptr;
}

upb_fhandlers *upb_mhandlers_newfhandlers(upb_mhandlers *m, uint32_t n,
                                          upb_fieldtype_t type, bool repeated) {
  assert(type != UPB_TYPE(MESSAGE));
  assert(type != UPB_TYPE(GROUP));
  return _upb_mhandlers_newfhandlers(m, n, type, repeated);
}

upb_fhandlers *upb_mhandlers_newfhandlers_subm(upb_mhandlers *m, uint32_t n,
                                               upb_fieldtype_t type,
                                               bool repeated,
                                               upb_mhandlers *subm) {
  assert(type == UPB_TYPE(MESSAGE) || type == UPB_TYPE(GROUP));
  assert(subm);
  upb_fhandlers *f = _upb_mhandlers_newfhandlers(m, n, type, repeated);
  if (!f) return NULL;
  f->submsg = subm;
  if (type == UPB_TYPE(GROUP))
    _upb_mhandlers_newfhandlers(subm, n, UPB_TYPE_ENDGROUP, false);
  return f;
}

upb_fhandlers *upb_mhandlers_lookup(const upb_mhandlers *m, uint32_t n) {
  const upb_value *v = upb_inttable_lookup(&m->fieldtab, n);
  return v ? upb_value_getptr(*v) : NULL;
}


/* upb_handlers ***************************************************************/

upb_handlers *upb_handlers_new() {
  upb_handlers *h = malloc(sizeof(*h));
  h->refcount = 1;
  h->msgs_len = 0;
  h->msgs_size = 4;
  h->msgs = malloc(h->msgs_size * sizeof(*h->msgs));
  h->should_jit = true;
  return h;
}

void upb_handlers_ref(upb_handlers *h) { h->refcount++; }

void upb_handlers_unref(upb_handlers *h) {
  if (--h->refcount == 0) {
    for (int i = 0; i < h->msgs_len; i++) {
      upb_mhandlers *mh = h->msgs[i];
      upb_inttable_iter j;
      upb_inttable_begin(&j, &mh->fieldtab);
      for(; !upb_inttable_done(&j); upb_inttable_next(&j)) {
        free(upb_value_getptr(upb_inttable_iter_value(&j)));
      }
      upb_inttable_uninit(&mh->fieldtab);
#ifdef UPB_USE_JIT_X64
      free(mh->tablearray);
#endif
      free(mh);
    }
    free(h->msgs);
    free(h);
  }
}

upb_mhandlers *upb_handlers_newmhandlers(upb_handlers *h) {
  if (h->msgs_len == h->msgs_size) {
    h->msgs_size *= 2;
    h->msgs = realloc(h->msgs, h->msgs_size * sizeof(*h->msgs));
  }
  upb_mhandlers *mh = upb_mhandlers_new();
  h->msgs[h->msgs_len++] = mh;
  return mh;
}

static upb_mhandlers *upb_regmsg_dfs(upb_handlers *h, const upb_msgdef *m,
                                     upb_onmsgreg *msgreg_cb,
                                     upb_onfieldreg *fieldreg_cb,
                                     void *closure, upb_strtable *mtab) {
  upb_mhandlers *mh = upb_handlers_newmhandlers(h);
  upb_strtable_insert(mtab, upb_def_fullname(UPB_UPCAST(m)), upb_value_ptr(mh));
  if (msgreg_cb) msgreg_cb(closure, mh, m);
  upb_msg_iter i;
  for(upb_msg_begin(&i, m); !upb_msg_done(&i); upb_msg_next(&i)) {
    upb_fielddef *f = upb_msg_iter_field(&i);
    upb_fhandlers *fh;
    if (upb_issubmsg(f)) {
      upb_mhandlers *sub_mh;
      const upb_value *subm_ent;
      // The table lookup is necessary to break the DFS for type cycles.
      const char *subname = upb_def_fullname(upb_fielddef_subdef(f));
      if ((subm_ent = upb_strtable_lookup(mtab, subname)) != NULL) {
        sub_mh = upb_value_getptr(*subm_ent);
      } else {
        sub_mh = upb_regmsg_dfs(
            h, upb_downcast_msgdef_const(upb_fielddef_subdef(f)),
            msgreg_cb, fieldreg_cb, closure, mtab);
      }
      fh = upb_mhandlers_newfhandlers_subm(
          mh, f->number, f->type, upb_isseq(f), sub_mh);
    } else {
      fh = upb_mhandlers_newfhandlers(mh, f->number, f->type, upb_isseq(f));
    }
    if (fieldreg_cb) fieldreg_cb(closure, fh, f);
  }
  return mh;
}

upb_mhandlers *upb_handlers_regmsgdef(upb_handlers *h, const upb_msgdef *m,
                                      upb_onmsgreg *msgreg_cb,
                                      upb_onfieldreg *fieldreg_cb,
                                      void *closure) {
  upb_strtable mtab;
  upb_strtable_init(&mtab);
  upb_mhandlers *ret =
      upb_regmsg_dfs(h, m, msgreg_cb, fieldreg_cb, closure, &mtab);
  upb_strtable_uninit(&mtab);
  return ret;
}


/* upb_dispatcher *************************************************************/

void upb_dispatcher_init(upb_dispatcher *d, upb_status *status,
                         upb_exit_handler UPB_NORETURN *exit,
                         void *srcclosure) {
  d->stack[0].f = NULL;  // Should never be read.
  d->limit = &d->stack[UPB_MAX_NESTING];
  d->exitjmp = exit;
  d->srcclosure = srcclosure;
  d->top_is_implicit = false;
  d->msgent = NULL;
  d->top = NULL;
  d->toplevel_msgent = NULL;
  d->status = status;
}

upb_dispatcher_frame *upb_dispatcher_reset(upb_dispatcher *d, void *closure,
                                           upb_mhandlers *top) {
  d->msgent = top;
  d->toplevel_msgent = top;
  d->top = d->stack;
  d->top->closure = closure;
  d->top->is_sequence = false;
  d->top->is_packed = false;
  return d->top;
}

void upb_dispatcher_uninit(upb_dispatcher *d) {
  (void)d;
}

void upb_dispatch_startmsg(upb_dispatcher *d) {
  upb_flow_t flow = UPB_CONTINUE;
  if (d->msgent->startmsg) d->msgent->startmsg(d->top->closure);
  if (flow != UPB_CONTINUE) _upb_dispatcher_abortjmp(d);
}

void upb_dispatch_endmsg(upb_dispatcher *d, upb_status *status) {
  assert(d->top == d->stack);
  if (d->msgent->endmsg) d->msgent->endmsg(d->top->closure, d->status);
  // TODO: should we avoid this copy by passing client's status obj to cbs?
  upb_status_copy(status, d->status);
}

upb_dispatcher_frame *upb_dispatch_startseq(upb_dispatcher *d,
                                            upb_fhandlers *f) {
  if (d->top + 1 >= d->limit) {
    upb_status_seterrliteral(d->status, "Nesting too deep.");
    _upb_dispatcher_abortjmp(d);
  }

  upb_sflow_t sflow = UPB_CONTINUE_WITH(d->top->closure);
  if (f->startseq) sflow = f->startseq(d->top->closure, f->fval);
  _upb_dispatcher_sethas(d->top->closure, f->hasbit);
  if (sflow.flow != UPB_CONTINUE) {
    _upb_dispatcher_abortjmp(d);
  }

  ++d->top;
  d->top->f = f;
  d->top->is_sequence = true;
  d->top->is_packed = false;
  d->top->closure = sflow.closure;
  return d->top;
}

upb_dispatcher_frame *upb_dispatch_endseq(upb_dispatcher *d) {
  assert(d->top > d->stack);
  assert(d->top->is_sequence);
  upb_fhandlers *f = d->top->f;
  --d->top;
  upb_flow_t flow = UPB_CONTINUE;
  if (f->endseq) flow = f->endseq(d->top->closure, f->fval);
  if (flow != UPB_CONTINUE) {
    _upb_dispatcher_abortjmp(d);
  }
  d->msgent = d->top->f ? d->top->f->submsg : d->toplevel_msgent;
  return d->top;
}

upb_dispatcher_frame *upb_dispatch_startsubmsg(upb_dispatcher *d,
                                               upb_fhandlers *f) {
  if (d->top + 1 >= d->limit) {
    upb_status_seterrliteral(d->status, "Nesting too deep.");
    _upb_dispatcher_abortjmp(d);
  }

  upb_sflow_t sflow = UPB_CONTINUE_WITH(d->top->closure);
  if (f->startsubmsg) sflow = f->startsubmsg(d->top->closure, f->fval);
  _upb_dispatcher_sethas(d->top->closure, f->hasbit);
  if (sflow.flow != UPB_CONTINUE) {
    _upb_dispatcher_abortjmp(d);
  }

  ++d->top;
  d->top->f = f;
  d->top->is_sequence = false;
  d->top->is_packed = false;
  d->top->closure = sflow.closure;
  d->msgent = f->submsg;
  upb_dispatch_startmsg(d);
  return d->top;
}

upb_dispatcher_frame *upb_dispatch_endsubmsg(upb_dispatcher *d) {
  assert(d->top > d->stack);
  assert(!d->top->is_sequence);
  upb_fhandlers *f = d->top->f;
  if (d->msgent->endmsg) d->msgent->endmsg(d->top->closure, d->status);
  d->msgent = d->top->f->msg;
  --d->top;
  upb_flow_t flow = UPB_CONTINUE;
  if (f->endsubmsg) f->endsubmsg(d->top->closure, f->fval);
  if (flow != UPB_CONTINUE) _upb_dispatcher_abortjmp(d);
  return d->top;
}

bool upb_dispatcher_stackempty(upb_dispatcher *d) {
  return d->top == d->stack;
}
bool upb_dispatcher_islegalend(upb_dispatcher *d) {
  if (d->top == d->stack) return true;
  if (d->top - 1 == d->stack &&
      d->top->is_sequence && !d->top->is_packed) return true;
  return false;
}

void _upb_dispatcher_abortjmp(upb_dispatcher *d) {
  d->exitjmp(d->srcclosure);
  assert(false);  // Never returns.
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback