summaryrefslogtreecommitdiff
path: root/src/upb_msg.c
blob: 9dfbea4c902490fad50126bc4bbae214bab11967 (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
/*
 * upb - a minimalist implementation of protocol buffers.
 *
 * Copyright (c) 2010 Joshua Haberman.  See LICENSE for details.
 *
 * Data structure for storing a message of protobuf data.
 */

#include "upb_msg.h"
#include "upb_decoder.h"
#include "upb_strstream.h"

static uint32_t upb_round_up_pow2(uint32_t v) {
  // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
  v--;
  v |= v >> 1;
  v |= v >> 2;
  v |= v >> 4;
  v |= v >> 8;
  v |= v >> 16;
  v++;
  return v;
}

static void upb_elem_free(upb_value v, upb_fielddef *f) {
  switch(f->type) {
    case UPB_TYPE(MESSAGE):
    case UPB_TYPE(GROUP):
      _upb_msg_free(upb_value_getmsg(v), upb_downcast_msgdef(f->def));
      break;
    case UPB_TYPE(STRING):
    case UPB_TYPE(BYTES):
      _upb_string_free(upb_value_getstr(v));
      break;
    default:
      abort();
  }
}

static void upb_elem_unref(upb_value v, upb_fielddef *f) {
  assert(upb_elem_ismm(f));
  upb_atomic_refcount_t *refcount = upb_value_getrefcount(v);
  if (refcount && upb_atomic_unref(refcount))
    upb_elem_free(v, f);
}

static void upb_field_free(upb_value v, upb_fielddef *f) {
  if (upb_isarray(f)) {
    _upb_array_free(upb_value_getarr(v), f);
  } else {
    upb_elem_free(v, f);
  }
}

static void upb_field_unref(upb_value v, upb_fielddef *f) {
  assert(upb_field_ismm(f));
  upb_atomic_refcount_t *refcount = upb_value_getrefcount(v);
  if (refcount && upb_atomic_unref(refcount))
    upb_field_free(v, f);
}


/* upb_array ******************************************************************/

upb_array *upb_array_new(void) {
  upb_array *arr = malloc(sizeof(*arr));
  upb_atomic_refcount_init(&arr->refcount, 1);
  arr->size = 0;
  arr->len = 0;
  arr->ptr = NULL;
  return arr;
}

void upb_array_recycle(upb_array **_arr, upb_fielddef *f) {
  upb_array *arr = *_arr;
  if(arr && upb_atomic_only(&arr->refcount)) {
    arr->len = 0;
  } else {
    upb_array_unref(arr, f);
    *_arr = upb_array_new();
  }
}

void _upb_array_free(upb_array *arr, upb_fielddef *f) {
  if (upb_elem_ismm(f)) {
    // Need to release refs on sub-objects.
    upb_valuetype_t type = upb_elem_valuetype(f);
    for (upb_arraylen_t i = 0; i < arr->size; i++) {
      upb_valueptr p = _upb_array_getptr(arr, f, i);
      upb_elem_unref(upb_value_read(p, type), f);
    }
  }
  free(arr->ptr);
  free(arr);
}

void upb_array_resize(upb_array *arr, upb_fielddef *f, upb_arraylen_t len) {
  size_t type_size = upb_types[f->type].size;
  upb_arraylen_t old_size = arr->size;
  if (old_size < len) {
    // Need to resize.
    size_t new_size = upb_round_up_pow2(len);
    arr->ptr = realloc(arr->ptr, new_size * type_size);
    arr->size = new_size;
    memset(arr->ptr + (old_size * type_size), 0,
           (new_size - old_size) * type_size);
  }
  arr->len = len;
}


/* upb_msg ********************************************************************/

upb_msg *upb_msg_new(upb_msgdef *md) {
  upb_msg *msg = malloc(md->size);
  // Clear all set bits and cached pointers.
  memset(msg, 0, md->size);
  upb_atomic_refcount_init(&msg->refcount, 1);
  return msg;
}

void _upb_msg_free(upb_msg *msg, upb_msgdef *md) {
  // Need to release refs on all sub-objects.
  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);
    upb_valueptr p = _upb_msg_getptr(msg, f);
    upb_valuetype_t type = upb_field_valuetype(f);
    if (upb_field_ismm(f)) upb_field_unref(upb_value_read(p, type), f);
  }
  free(msg);
}

void upb_msg_recycle(upb_msg **_msg, upb_msgdef *msgdef) {
  upb_msg *msg = *_msg;
  if(msg && upb_atomic_only(&msg->refcount)) {
    upb_msg_clear(msg, msgdef);
  } else {
    upb_msg_unref(msg, msgdef);
    *_msg = upb_msg_new(msgdef);
  }
}

INLINE void upb_msg_sethas(upb_msg *msg, upb_fielddef *f) {
  msg->data[f->set_bit_offset] |= f->set_bit_mask;
}

static upb_valueptr upb_msg_getappendptr(upb_msg *msg, upb_fielddef *f) {
  upb_valueptr p = _upb_msg_getptr(msg, f);
  if (upb_isarray(f)) {
    // Create/recycle/resize the array if necessary, and find a pointer to
    // a newly-appended element.
    if (!upb_msg_has(msg, f)) {
      upb_array_recycle(p.arr, f);
      upb_msg_sethas(msg, f);
    }
    assert(*p.arr != NULL);
    upb_arraylen_t oldlen = upb_array_len(*p.arr);
    upb_array_resize(*p.arr, f, oldlen + 1);
    p = _upb_array_getptr(*p.arr, f, oldlen);
  }
  return p;
}

static void upb_msg_appendval(upb_msg *msg, upb_fielddef *f, upb_value val) {
  upb_valueptr p = upb_msg_getappendptr(msg, f);
  if (upb_isstring(f)) {
    // 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_msgpopulator which behavior is desired.
    upb_string *src = upb_value_getstr(val);
    upb_string_recycle(p.str);
    upb_string_substr(*p.str, src, 0, upb_string_len(src));
  } else {
    upb_value_write(p, val, f->type);
  }
  upb_msg_sethas(msg, f);
}

upb_msg *upb_msg_appendmsg(upb_msg *msg, upb_fielddef *f, upb_msgdef *msgdef) {
  upb_valueptr p = upb_msg_getappendptr(msg, f);
  if (upb_isarray(f) || !upb_msg_has(msg, f)) {
    upb_msg_recycle(p.msg, msgdef);
    upb_msg_sethas(msg, f);
  }
  return *p.msg;
}


/* upb_msgpopulator ***********************************************************/

void upb_msgpopulator_init(upb_msgpopulator *p) {
  upb_status_init(&p->status);
}

void upb_msgpopulator_reset(upb_msgpopulator *p, upb_msg *m, upb_msgdef *md) {
  p->top = p->stack;
  p->limit = p->stack + sizeof(p->stack);
  p->top->msg = m;
  p->top->msgdef = md;
}

void upb_msgpopulator_uninit(upb_msgpopulator *p) {
  upb_status_uninit(&p->status);
}

static upb_flow_t upb_msgpopulator_value(void *_p, upb_fielddef *f, upb_value val) {
  upb_msgpopulator *p = _p;
  upb_msg_appendval(p->top->msg, f, val);
  return UPB_CONTINUE;
}

static upb_flow_t upb_msgpopulator_startsubmsg(void *_p, upb_fielddef *f,
                                               upb_handlers *delegate_to) {
  upb_msgpopulator *p = _p;
  (void)delegate_to;
  upb_msg *oldmsg = p->top->msg;
  if (++p->top == p->limit) {
    upb_seterr(&p->status, UPB_ERROR, "Exceeded maximum nesting");
    return UPB_BREAK;
  }
  upb_msgdef *msgdef = upb_downcast_msgdef(f->def);
  p->top->msgdef = msgdef;
  p->top->msg = upb_msg_appendmsg(oldmsg, f, msgdef);
  return UPB_CONTINUE;
}

static upb_flow_t upb_msgpopulator_endsubmsg(void *_p) {
  upb_msgpopulator *p = _p;
  --p->top;
  return UPB_CONTINUE;
}

void upb_msgpopulator_register_handlers(upb_msgpopulator *p, upb_handlers *h) {
  static upb_handlerset handlerset = {
    NULL,   // startmsg
    NULL,   // endmsg
    &upb_msgpopulator_value,
    &upb_msgpopulator_startsubmsg,
    &upb_msgpopulator_endsubmsg,
  };
  upb_register_handlerset(h, &handlerset);
  upb_set_handler_closure(h, p, &p->status);
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback