summaryrefslogtreecommitdiff
path: root/core/upb_msg.c
blob: 83191d2f6ffc2c38a620e9056ab4424f871b0edf (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
/*
 * 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"

void _upb_elem_free(upb_value v, upb_fielddef *f) {
  switch(f->type) {
    case UPB_TYPE(MESSAGE):
    case UPB_TYPE(GROUP):
      _upb_msg_free(v.msg, upb_downcast_msgdef(f->def));
      break;
    case UPB_TYPE(STRING):
    case UPB_TYPE(BYTES):
      _upb_string_free(v.str);
      break;
    default:
      abort();
  }
}

void _upb_field_free(upb_value v, upb_fielddef *f) {
  if (upb_isarray(f)) {
    _upb_array_free(v.arr, f);
  } else {
    _upb_elem_free(v, f);
  }
}

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 *md);
  upb_msg *msg = *_msg;
  if(msg && upb_atomic_only(&msg->refcount)) {
    upb_msg_clear(msg);
  } else {
    upb_msg_unref(msg);
    *_msg = upb_msg_new();
  }
}

void upb_msg_appendval(upb_msg *msg, upb_fielddef *f, upb_value val) {
  upb_valueptr ptr;
  if (upb_isarray(f)) {
  }
}

INLINE upb_value upb_msg_getmutable(upb_msg *msg, upb_fielddef *f);
  assert(upb_field_ismm(f));
  upb_valueptr p = _upb_msg_getptr(msg, f);
  upb_valuetype_t type = upb_field_valuetype(f);
  upb_value val = upb_value_read(p, type);
  if (!upb_msg_has(msg, f)) {
    upb_msg_sethas(msg, f);
    val = upb_field_tryrecycle(p, val, f, type);
  }
  return val;
}

INLINE void upb_msg_set(upb_msg *msg, upb_fielddef *f, upb_value val) {
  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);
    _upb_value_ref(val);
  }
  upb_msg_sethas(msg, f);
  upb_value_write(p, val, upb_field_valuetype(f));
}

INLINE void upb_msg_sethas(upb_msg *msg, upb_fielddef *f) {
  msg->data[f->field_index/8] |= (1 << (f->field_index % 8));
}


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->elements._void = NULL;
  return arr;
}

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);
    }
  }
  if (arr->elements._void) free(arr->elements._void);
  free(arr);
}

upb_value upb_field_new(upb_fielddef *f, upb_valuetype_t type) {
  upb_value v;
  switch(type) {
    case UPB_TYPE(MESSAGE):
    case UPB_TYPE(GROUP):
      v.msg = upb_msg_new(upb_downcast_msgdef(f->def));
    case UPB_TYPE(STRING):
    case UPB_TYPE(BYTES):
      v.str = upb_string_new();
    case UPB_VALUETYPE_ARRAY:
      v.arr = upb_array_new();
    default:
      abort();
  }
  return v;
}

static void upb_field_recycle(upb_value val) {
  (void)val;
}

upb_value upb_field_tryrecycle(upb_valueptr p, upb_value val, upb_fielddef *f,
                               upb_valuetype_t type) {
  if (val._void == NULL || !upb_atomic_only(val.refcount)) {
    if (val._void != NULL) upb_atomic_unref(val.refcount);
    val = upb_field_new(f, type);
    upb_value_write(p, val, type);
  } else {
    upb_field_recycle(val);
  }
  return val;
}

void upb_msg_decodestr(upb_msg *msg, upb_msgdef *md, upb_string *str,
                       upb_status *status) {
  upb_stringsrc *ssrc = upb_stringsrc_new();
  upb_stringsrc_reset(ssrc, str);
  upb_decoder *d = upb_decoder_new(md);
  upb_decoder_reset(d, upb_stringsrc_bytesrc(ssrc));

  upb_decoder_free(d);
  upb_stringsrc_free(ssrc);
}

void upb_msg_encodestr(upb_msg *msg, upb_msgdef *md, upb_string *str,
                       upb_status *status) {
  (void)msg;
  (void)md;
  (void)str;
  (void)status;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback