google-protobuf 3.19.2 → 3.21.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of google-protobuf might be problematic. Click here for more details.

@@ -36,55 +36,58 @@
36
36
  #include "protobuf.h"
37
37
  #include "ruby-upb.h"
38
38
 
39
- // Gets the underlying upb_msg* and upb_msgdef for the given Ruby message
40
- // wrapper. Requires that |value| is indeed a message object.
41
- const upb_msg *Message_Get(VALUE value, const upb_msgdef **m);
39
+ // Gets the underlying upb_Message* and upb_MessageDef for the given Ruby
40
+ // message wrapper. Requires that |value| is indeed a message object.
41
+ const upb_Message* Message_Get(VALUE value, const upb_MessageDef** m);
42
42
 
43
43
  // Like Message_Get(), but checks that the object is not frozen and returns a
44
44
  // mutable pointer.
45
- upb_msg *Message_GetMutable(VALUE value, const upb_msgdef **m);
45
+ upb_Message* Message_GetMutable(VALUE value, const upb_MessageDef** m);
46
46
 
47
47
  // Returns the Arena object for this message.
48
48
  VALUE Message_GetArena(VALUE value);
49
49
 
50
- // Converts |value| into a upb_msg value of the expected upb_msgdef type,
51
- // raising an error if this is not possible. Used when assigning |value| to a
52
- // field of another message, which means the message must be of a particular
53
- // type.
50
+ // Converts |value| into a upb_Message value of the expected upb_MessageDef
51
+ // type, raising an error if this is not possible. Used when assigning |value|
52
+ // to a field of another message, which means the message must be of a
53
+ // particular type.
54
54
  //
55
55
  // This will perform automatic conversions in some cases (for example, Time ->
56
56
  // Google::Protobuf::Timestamp). If any new message is created, it will be
57
57
  // created on |arena|, and any existing message will have its arena fused with
58
58
  // |arena|.
59
- const upb_msg* Message_GetUpbMessage(VALUE value, const upb_msgdef* m,
60
- const char* name, upb_arena* arena);
59
+ const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
60
+ const char* name, upb_Arena* arena);
61
61
 
62
62
  // Gets or constructs a Ruby wrapper object for the given message. The wrapper
63
63
  // object will reference |arena| and ensure that it outlives this object.
64
- VALUE Message_GetRubyWrapper(upb_msg* msg, const upb_msgdef* m, VALUE arena);
64
+ VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m,
65
+ VALUE arena);
65
66
 
66
67
  // Gets the given field from this message.
67
- VALUE Message_getfield(VALUE _self, const upb_fielddef* f);
68
+ VALUE Message_getfield(VALUE _self, const upb_FieldDef* f);
68
69
 
69
70
  // Implements #inspect for this message, printing the text to |b|.
70
- void Message_PrintMessage(StringBuilder* b, const upb_msg* msg,
71
- const upb_msgdef* m);
71
+ void Message_PrintMessage(StringBuilder* b, const upb_Message* msg,
72
+ const upb_MessageDef* m);
72
73
 
73
74
  // Returns a hash value for the given message.
74
- uint64_t Message_Hash(const upb_msg *msg, const upb_msgdef *m, uint64_t seed);
75
+ uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
76
+ uint64_t seed);
75
77
 
76
78
  // Returns a deep copy of the given message.
77
- upb_msg* Message_deep_copy(const upb_msg* msg, const upb_msgdef* m,
78
- upb_arena *arena);
79
+ upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
80
+ upb_Arena* arena);
79
81
 
80
82
  // Returns true if these two messages are equal.
81
- bool Message_Equal(const upb_msg *m1, const upb_msg *m2, const upb_msgdef *m);
83
+ bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
84
+ const upb_MessageDef* m);
82
85
 
83
86
  // Checks that this Ruby object is a message, and raises an exception if not.
84
87
  void Message_CheckClass(VALUE klass);
85
88
 
86
89
  // Returns a new Hash object containing the contents of this message.
87
- VALUE Scalar_CreateHash(upb_msgval val, TypeInfo type_info);
90
+ VALUE Scalar_CreateHash(upb_MessageValue val, TypeInfo type_info);
88
91
 
89
92
  // Creates a message class or enum module for this descriptor, respectively.
90
93
  VALUE build_class_from_descriptor(VALUE descriptor);
@@ -40,14 +40,14 @@
40
40
  VALUE cParseError;
41
41
  VALUE cTypeError;
42
42
 
43
- const upb_fielddef* map_field_key(const upb_fielddef* field) {
44
- const upb_msgdef *entry = upb_fielddef_msgsubdef(field);
45
- return upb_msgdef_itof(entry, 1);
43
+ const upb_FieldDef *map_field_key(const upb_FieldDef *field) {
44
+ const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
45
+ return upb_MessageDef_FindFieldByNumber(entry, 1);
46
46
  }
47
47
 
48
- const upb_fielddef* map_field_value(const upb_fielddef* field) {
49
- const upb_msgdef *entry = upb_fielddef_msgsubdef(field);
50
- return upb_msgdef_itof(entry, 2);
48
+ const upb_FieldDef *map_field_value(const upb_FieldDef *field) {
49
+ const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
50
+ return upb_MessageDef_FindFieldByNumber(entry, 2);
51
51
  }
52
52
 
53
53
  // -----------------------------------------------------------------------------
@@ -66,21 +66,21 @@ static size_t StringBuilder_SizeOf(size_t cap) {
66
66
  return sizeof(StringBuilder) + cap;
67
67
  }
68
68
 
69
- StringBuilder* StringBuilder_New() {
69
+ StringBuilder *StringBuilder_New() {
70
70
  const size_t cap = 128;
71
- StringBuilder* builder = malloc(sizeof(*builder));
71
+ StringBuilder *builder = malloc(sizeof(*builder));
72
72
  builder->size = 0;
73
73
  builder->cap = cap;
74
74
  builder->data = malloc(builder->cap);
75
75
  return builder;
76
76
  }
77
77
 
78
- void StringBuilder_Free(StringBuilder* b) {
78
+ void StringBuilder_Free(StringBuilder *b) {
79
79
  free(b->data);
80
80
  free(b);
81
81
  }
82
82
 
83
- void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...) {
83
+ void StringBuilder_Printf(StringBuilder *b, const char *fmt, ...) {
84
84
  size_t have = b->cap - b->size;
85
85
  size_t n;
86
86
  va_list args;
@@ -104,60 +104,62 @@ void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...) {
104
104
  b->size += n;
105
105
  }
106
106
 
107
- VALUE StringBuilder_ToRubyString(StringBuilder* b) {
107
+ VALUE StringBuilder_ToRubyString(StringBuilder *b) {
108
108
  VALUE ret = rb_str_new(b->data, b->size);
109
109
  rb_enc_associate(ret, rb_utf8_encoding());
110
110
  return ret;
111
111
  }
112
112
 
113
- static void StringBuilder_PrintEnum(StringBuilder* b, int32_t val,
114
- const upb_enumdef* e) {
115
- const char *name = upb_enumdef_iton(e, val);
116
- if (name) {
117
- StringBuilder_Printf(b, ":%s", name);
113
+ static void StringBuilder_PrintEnum(StringBuilder *b, int32_t val,
114
+ const upb_EnumDef *e) {
115
+ const upb_EnumValueDef *ev = upb_EnumDef_FindValueByNumber(e, val);
116
+ if (ev) {
117
+ StringBuilder_Printf(b, ":%s", upb_EnumValueDef_Name(ev));
118
118
  } else {
119
119
  StringBuilder_Printf(b, "%" PRId32, val);
120
120
  }
121
121
  }
122
122
 
123
- void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val,
123
+ void StringBuilder_PrintMsgval(StringBuilder *b, upb_MessageValue val,
124
124
  TypeInfo info) {
125
125
  switch (info.type) {
126
- case UPB_TYPE_BOOL:
126
+ case kUpb_CType_Bool:
127
127
  StringBuilder_Printf(b, "%s", val.bool_val ? "true" : "false");
128
128
  break;
129
- case UPB_TYPE_FLOAT: {
129
+ case kUpb_CType_Float: {
130
130
  VALUE str = rb_inspect(DBL2NUM(val.float_val));
131
131
  StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
132
132
  break;
133
133
  }
134
- case UPB_TYPE_DOUBLE: {
134
+ case kUpb_CType_Double: {
135
135
  VALUE str = rb_inspect(DBL2NUM(val.double_val));
136
136
  StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
137
137
  break;
138
138
  }
139
- case UPB_TYPE_INT32:
139
+ case kUpb_CType_Int32:
140
140
  StringBuilder_Printf(b, "%" PRId32, val.int32_val);
141
141
  break;
142
- case UPB_TYPE_UINT32:
142
+ case kUpb_CType_UInt32:
143
143
  StringBuilder_Printf(b, "%" PRIu32, val.uint32_val);
144
144
  break;
145
- case UPB_TYPE_INT64:
145
+ case kUpb_CType_Int64:
146
146
  StringBuilder_Printf(b, "%" PRId64, val.int64_val);
147
147
  break;
148
- case UPB_TYPE_UINT64:
148
+ case kUpb_CType_UInt64:
149
149
  StringBuilder_Printf(b, "%" PRIu64, val.uint64_val);
150
150
  break;
151
- case UPB_TYPE_STRING:
152
- StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size, val.str_val.data);
151
+ case kUpb_CType_String:
152
+ StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
153
+ val.str_val.data);
153
154
  break;
154
- case UPB_TYPE_BYTES:
155
- StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size, val.str_val.data);
155
+ case kUpb_CType_Bytes:
156
+ StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
157
+ val.str_val.data);
156
158
  break;
157
- case UPB_TYPE_ENUM:
159
+ case kUpb_CType_Enum:
158
160
  StringBuilder_PrintEnum(b, val.int32_val, info.def.enumdef);
159
161
  break;
160
- case UPB_TYPE_MESSAGE:
162
+ case kUpb_CType_Message:
161
163
  Message_PrintMessage(b, val.msg_val, info.def.msgdef);
162
164
  break;
163
165
  }
@@ -168,7 +170,7 @@ void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val,
168
170
  // -----------------------------------------------------------------------------
169
171
 
170
172
  typedef struct {
171
- upb_arena *arena;
173
+ upb_Arena *arena;
172
174
  VALUE pinned_objs;
173
175
  } Arena;
174
176
 
@@ -179,44 +181,53 @@ static void Arena_mark(void *data) {
179
181
 
180
182
  static void Arena_free(void *data) {
181
183
  Arena *arena = data;
182
- upb_arena_free(arena->arena);
184
+ upb_Arena_Free(arena->arena);
183
185
  xfree(arena);
184
186
  }
185
187
 
186
188
  static VALUE cArena;
187
189
 
188
190
  const rb_data_type_t Arena_type = {
189
- "Google::Protobuf::Internal::Arena",
190
- { Arena_mark, Arena_free, NULL },
191
- .flags = RUBY_TYPED_FREE_IMMEDIATELY,
191
+ "Google::Protobuf::Internal::Arena",
192
+ {Arena_mark, Arena_free, NULL},
193
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
192
194
  };
193
195
 
196
+ static void* ruby_upb_allocfunc(upb_alloc* alloc, void* ptr, size_t oldsize, size_t size) {
197
+ if (size == 0) {
198
+ xfree(ptr);
199
+ return NULL;
200
+ } else {
201
+ return xrealloc(ptr, size);
202
+ }
203
+ }
204
+
205
+ upb_alloc ruby_upb_alloc = {&ruby_upb_allocfunc};
206
+
194
207
  static VALUE Arena_alloc(VALUE klass) {
195
208
  Arena *arena = ALLOC(Arena);
196
- arena->arena = upb_arena_new();
209
+ arena->arena = upb_Arena_Init(NULL, 0, &ruby_upb_alloc);
197
210
  arena->pinned_objs = Qnil;
198
211
  return TypedData_Wrap_Struct(klass, &Arena_type, arena);
199
212
  }
200
213
 
201
- upb_arena *Arena_get(VALUE _arena) {
214
+ upb_Arena *Arena_get(VALUE _arena) {
202
215
  Arena *arena;
203
216
  TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
204
217
  return arena->arena;
205
218
  }
206
219
 
207
- void Arena_fuse(VALUE _arena, upb_arena *other) {
220
+ void Arena_fuse(VALUE _arena, upb_Arena *other) {
208
221
  Arena *arena;
209
222
  TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
210
- if (!upb_arena_fuse(arena->arena, other)) {
223
+ if (!upb_Arena_Fuse(arena->arena, other)) {
211
224
  rb_raise(rb_eRuntimeError,
212
225
  "Unable to fuse arenas. This should never happen since Ruby does "
213
226
  "not use initial blocks");
214
227
  }
215
228
  }
216
229
 
217
- VALUE Arena_new() {
218
- return Arena_alloc(cArena);
219
- }
230
+ VALUE Arena_new() { return Arena_alloc(cArena); }
220
231
 
221
232
  void Arena_Pin(VALUE _arena, VALUE obj) {
222
233
  Arena *arena;
@@ -333,8 +344,8 @@ static void SecondaryMap_MaybeGC() {
333
344
  // avoid O(N^2) CPU costs.
334
345
  size_t threshold = PBRUBY_MAX(secondary_len * 0.2, 2000);
335
346
  if (waste > threshold) {
336
- rb_funcall(gc_secondary_map_lambda, rb_intern("call"), 2,
337
- secondary_map, weak_obj_cache);
347
+ rb_funcall(gc_secondary_map_lambda, rb_intern("call"), 2, secondary_map,
348
+ weak_obj_cache);
338
349
  }
339
350
  }
340
351
 
@@ -353,7 +364,7 @@ static VALUE SecondaryMap_Get(VALUE key, bool create) {
353
364
  #endif
354
365
 
355
366
  // Requires: secondary_map_mutex is held by this thread iff create == true.
356
- static VALUE ObjectCache_GetKey(const void* key, bool create) {
367
+ static VALUE ObjectCache_GetKey(const void *key, bool create) {
357
368
  VALUE key_val = (VALUE)key;
358
369
  PBRUBY_ASSERT((key_val & 3) == 0);
359
370
  VALUE ret = LL2NUM(key_val >> 2);
@@ -380,7 +391,7 @@ static void ObjectCache_Init() {
380
391
  #endif
381
392
  }
382
393
 
383
- void ObjectCache_Add(const void* key, VALUE val) {
394
+ void ObjectCache_Add(const void *key, VALUE val) {
384
395
  PBRUBY_ASSERT(ObjectCache_Get(key) == Qnil);
385
396
  #if USE_SECONDARY_MAP
386
397
  rb_mutex_lock(secondary_map_mutex);
@@ -394,7 +405,7 @@ void ObjectCache_Add(const void* key, VALUE val) {
394
405
  }
395
406
 
396
407
  // Returns the cached object for this key, if any. Otherwise returns Qnil.
397
- VALUE ObjectCache_Get(const void* key) {
408
+ VALUE ObjectCache_Get(const void *key) {
398
409
  VALUE key_rb = ObjectCache_GetKey(key, false);
399
410
  return rb_funcall(weak_obj_cache, item_get, 1, key_rb);
400
411
  }
@@ -407,9 +418,9 @@ VALUE ObjectCache_Get(const void* key) {
407
418
  * unknown fields in submessages.
408
419
  */
409
420
  static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
410
- const upb_msgdef *m;
411
- upb_msg *msg = Message_GetMutable(msg_rb, &m);
412
- if (!upb_msg_discardunknown(msg, m, 128)) {
421
+ const upb_MessageDef *m;
422
+ upb_Message *msg = Message_GetMutable(msg_rb, &m);
423
+ if (!upb_Message_DiscardUnknown(msg, m, 128)) {
413
424
  rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
414
425
  }
415
426
 
@@ -431,10 +442,10 @@ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
431
442
  return Map_deep_copy(obj);
432
443
  } else {
433
444
  VALUE new_arena_rb = Arena_new();
434
- upb_arena *new_arena = Arena_get(new_arena_rb);
435
- const upb_msgdef *m;
436
- const upb_msg *msg = Message_Get(obj, &m);
437
- upb_msg* new_msg = Message_deep_copy(msg, m, new_arena);
445
+ upb_Arena *new_arena = Arena_get(new_arena_rb);
446
+ const upb_MessageDef *m;
447
+ const upb_Message *msg = Message_Get(obj, &m);
448
+ upb_Message *new_msg = Message_deep_copy(msg, m, new_arena);
438
449
  return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
439
450
  }
440
451
  }
@@ -445,8 +456,7 @@ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
445
456
 
446
457
  // This must be named "Init_protobuf_c" because the Ruby module is named
447
458
  // "protobuf_c" -- the VM looks for this symbol in our .so.
448
- __attribute__ ((visibility ("default")))
449
- void Init_protobuf_c() {
459
+ __attribute__((visibility("default"))) void Init_protobuf_c() {
450
460
  ObjectCache_Init();
451
461
 
452
462
  VALUE google = rb_define_module("Google");
@@ -465,6 +475,6 @@ void Init_protobuf_c() {
465
475
 
466
476
  rb_define_singleton_method(protobuf, "discard_unknown",
467
477
  Google_Protobuf_discard_unknown, 1);
468
- rb_define_singleton_method(protobuf, "deep_copy",
469
- Google_Protobuf_deep_copy, 1);
478
+ rb_define_singleton_method(protobuf, "deep_copy", Google_Protobuf_deep_copy,
479
+ 1);
470
480
  }
@@ -31,33 +31,33 @@
31
31
  #ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
32
32
  #define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
33
33
 
34
+ #include <ruby/encoding.h>
34
35
  #include <ruby/ruby.h>
35
36
  #include <ruby/vm.h>
36
- #include <ruby/encoding.h>
37
37
 
38
- #include "ruby-upb.h"
39
38
  #include "defs.h"
39
+ #include "ruby-upb.h"
40
40
 
41
41
  // These operate on a map field (i.e., a repeated field of submessages whose
42
42
  // submessage type is a map-entry msgdef).
43
- const upb_fielddef* map_field_key(const upb_fielddef* field);
44
- const upb_fielddef* map_field_value(const upb_fielddef* field);
43
+ const upb_FieldDef* map_field_key(const upb_FieldDef* field);
44
+ const upb_FieldDef* map_field_value(const upb_FieldDef* field);
45
45
 
46
46
  // -----------------------------------------------------------------------------
47
47
  // Arena
48
48
  // -----------------------------------------------------------------------------
49
49
 
50
- // A Ruby object that wraps an underlying upb_arena. Any objects that are
50
+ // A Ruby object that wraps an underlying upb_Arena. Any objects that are
51
51
  // allocated from this arena should reference the Arena in rb_gc_mark(), to
52
52
  // ensure that the object's underlying memory outlives any Ruby object that can
53
53
  // reach it.
54
54
 
55
55
  VALUE Arena_new();
56
- upb_arena *Arena_get(VALUE arena);
56
+ upb_Arena* Arena_get(VALUE arena);
57
57
 
58
58
  // Fuses this arena to another, throwing a Ruby exception if this is not
59
59
  // possible.
60
- void Arena_fuse(VALUE arena, upb_arena *other);
60
+ void Arena_fuse(VALUE arena, upb_Arena* other);
61
61
 
62
62
  // Pins this Ruby object to the lifetime of this arena, so that as long as the
63
63
  // arena is alive this object will not be collected.
@@ -93,10 +93,11 @@ typedef struct StringBuilder StringBuilder;
93
93
 
94
94
  StringBuilder* StringBuilder_New();
95
95
  void StringBuilder_Free(StringBuilder* b);
96
- void StringBuilder_Printf(StringBuilder* b, const char *fmt, ...);
96
+ void StringBuilder_Printf(StringBuilder* b, const char* fmt, ...);
97
97
  VALUE StringBuilder_ToRubyString(StringBuilder* b);
98
98
 
99
- void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val, TypeInfo info);
99
+ void StringBuilder_PrintMsgval(StringBuilder* b, upb_MessageValue val,
100
+ TypeInfo info);
100
101
 
101
102
  // -----------------------------------------------------------------------------
102
103
  // Utilities.
@@ -105,7 +106,9 @@ void StringBuilder_PrintMsgval(StringBuilder* b, upb_msgval val, TypeInfo info);
105
106
  extern VALUE cTypeError;
106
107
 
107
108
  #ifdef NDEBUG
108
- #define PBRUBY_ASSERT(expr) do {} while (false && (expr))
109
+ #define PBRUBY_ASSERT(expr) \
110
+ do { \
111
+ } while (false && (expr))
109
112
  #else
110
113
  #define PBRUBY_ASSERT(expr) assert(expr)
111
114
  #endif