google-protobuf 3.5.0 → 3.23.4

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


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

Files changed (47) hide show
  1. checksums.yaml +5 -5
  2. data/ext/google/protobuf_c/convert.c +361 -0
  3. data/ext/google/protobuf_c/convert.h +75 -0
  4. data/ext/google/protobuf_c/defs.c +770 -1254
  5. data/ext/google/protobuf_c/defs.h +107 -0
  6. data/ext/google/protobuf_c/extconf.rb +15 -5
  7. data/ext/google/protobuf_c/map.c +312 -474
  8. data/ext/google/protobuf_c/map.h +66 -0
  9. data/ext/google/protobuf_c/message.c +1139 -372
  10. data/ext/google/protobuf_c/message.h +104 -0
  11. data/ext/google/protobuf_c/protobuf.c +418 -51
  12. data/ext/google/protobuf_c/protobuf.h +53 -485
  13. data/ext/google/protobuf_c/repeated_field.c +319 -316
  14. data/ext/google/protobuf_c/repeated_field.h +63 -0
  15. data/ext/google/protobuf_c/ruby-upb.c +13974 -0
  16. data/ext/google/protobuf_c/ruby-upb.h +11780 -0
  17. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  18. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
  19. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
  20. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
  21. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
  22. data/ext/google/protobuf_c/wrap_memcpy.c +4 -3
  23. data/lib/google/protobuf/any_pb.rb +26 -5
  24. data/lib/google/protobuf/api_pb.rb +31 -25
  25. data/lib/google/protobuf/descriptor_dsl.rb +465 -0
  26. data/lib/google/protobuf/descriptor_pb.rb +75 -0
  27. data/lib/google/protobuf/duration_pb.rb +26 -5
  28. data/lib/google/protobuf/empty_pb.rb +26 -3
  29. data/lib/google/protobuf/field_mask_pb.rb +26 -4
  30. data/lib/google/protobuf/message_exts.rb +9 -4
  31. data/lib/google/protobuf/plugin_pb.rb +47 -0
  32. data/lib/google/protobuf/repeated_field.rb +17 -4
  33. data/lib/google/protobuf/source_context_pb.rb +26 -4
  34. data/lib/google/protobuf/struct_pb.rb +28 -22
  35. data/lib/google/protobuf/timestamp_pb.rb +26 -5
  36. data/lib/google/protobuf/type_pb.rb +37 -76
  37. data/lib/google/protobuf/well_known_types.rb +32 -4
  38. data/lib/google/protobuf/wrappers_pb.rb +35 -37
  39. data/lib/google/protobuf.rb +11 -8
  40. metadata +29 -37
  41. data/ext/google/protobuf_c/encode_decode.c +0 -1307
  42. data/ext/google/protobuf_c/storage.c +0 -904
  43. data/ext/google/protobuf_c/upb.c +0 -14913
  44. data/ext/google/protobuf_c/upb.h +0 -8969
  45. data/tests/basic.rb +0 -1403
  46. data/tests/generated_code_test.rb +0 -19
  47. data/tests/stress.rb +0 -38
@@ -0,0 +1,104 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 Google Inc. All rights reserved.
3
+ // https://developers.google.com/protocol-buffers/
4
+ //
5
+ // Redistribution and use in source and binary forms, with or without
6
+ // modification, are permitted provided that the following conditions are
7
+ // met:
8
+ //
9
+ // * Redistributions of source code must retain the above copyright
10
+ // notice, this list of conditions and the following disclaimer.
11
+ // * Redistributions in binary form must reproduce the above
12
+ // copyright notice, this list of conditions and the following disclaimer
13
+ // in the documentation and/or other materials provided with the
14
+ // distribution.
15
+ // * Neither the name of Google Inc. nor the names of its
16
+ // contributors may be used to endorse or promote products derived from
17
+ // this software without specific prior written permission.
18
+ //
19
+ // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23
+ // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24
+ // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25
+ // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26
+ // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27
+ // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28
+ // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29
+ // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+
31
+ #ifndef RUBY_PROTOBUF_MESSAGE_H_
32
+ #define RUBY_PROTOBUF_MESSAGE_H_
33
+
34
+ #include <ruby/ruby.h>
35
+
36
+ #include "protobuf.h"
37
+ #include "ruby-upb.h"
38
+
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
+
43
+ // Like Message_Get(), but checks that the object is not frozen and returns a
44
+ // mutable pointer.
45
+ upb_Message* Message_GetMutable(VALUE value, const upb_MessageDef** m);
46
+
47
+ // Returns the Arena object for this message.
48
+ VALUE Message_GetArena(VALUE value);
49
+
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
+ //
55
+ // This will perform automatic conversions in some cases (for example, Time ->
56
+ // Google::Protobuf::Timestamp). If any new message is created, it will be
57
+ // created on |arena|, and any existing message will have its arena fused with
58
+ // |arena|.
59
+ const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
60
+ const char* name, upb_Arena* arena);
61
+
62
+ // Gets or constructs a Ruby wrapper object for the given message. The wrapper
63
+ // object will reference |arena| and ensure that it outlives this object.
64
+ VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m,
65
+ VALUE arena);
66
+
67
+ // Gets the given field from this message.
68
+ VALUE Message_getfield(VALUE _self, const upb_FieldDef* f);
69
+
70
+ // Implements #inspect for this message, printing the text to |b|.
71
+ void Message_PrintMessage(StringBuilder* b, const upb_Message* msg,
72
+ const upb_MessageDef* m);
73
+
74
+ // Returns a hash value for the given message.
75
+ uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
76
+ uint64_t seed);
77
+
78
+ // Returns a deep copy of the given message.
79
+ upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
80
+ upb_Arena* arena);
81
+
82
+ // Returns true if these two messages are equal.
83
+ bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
84
+ const upb_MessageDef* m);
85
+
86
+ // Checks that this Ruby object is a message, and raises an exception if not.
87
+ void Message_CheckClass(VALUE klass);
88
+
89
+ // Returns a new Hash object containing the contents of this message.
90
+ VALUE Scalar_CreateHash(upb_MessageValue val, TypeInfo type_info);
91
+
92
+ // Creates a message class or enum module for this descriptor, respectively.
93
+ VALUE build_class_from_descriptor(VALUE descriptor);
94
+ VALUE build_module_from_enumdesc(VALUE _enumdesc);
95
+
96
+ // Returns the Descriptor/EnumDescriptor for the given message class or enum
97
+ // module, respectively. Returns nil if this is not a message class or enum
98
+ // module.
99
+ VALUE MessageOrEnum_GetDescriptor(VALUE klass);
100
+
101
+ // Call at startup to register all types in this module.
102
+ void Message_register(VALUE protobuf);
103
+
104
+ #endif // RUBY_PROTOBUF_MESSAGE_H_
@@ -30,51 +30,427 @@
30
30
 
31
31
  #include "protobuf.h"
32
32
 
33
+ #include <ruby/version.h>
34
+
35
+ #include "defs.h"
36
+ #include "map.h"
37
+ #include "message.h"
38
+ #include "repeated_field.h"
39
+
40
+ VALUE cParseError;
41
+ VALUE cTypeError;
42
+
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
+ }
47
+
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
+ }
52
+
33
53
  // -----------------------------------------------------------------------------
34
- // Global map from upb {msg,enum}defs to wrapper Descriptor/EnumDescriptor
35
- // instances.
54
+ // StringBuilder, for inspect
36
55
  // -----------------------------------------------------------------------------
37
56
 
38
- // This is a hash table from def objects (encoded by converting pointers to
39
- // Ruby integers) to MessageDef/EnumDef instances (as Ruby values).
40
- VALUE upb_def_to_ruby_obj_map;
57
+ struct StringBuilder {
58
+ size_t size;
59
+ size_t cap;
60
+ char *data;
61
+ };
41
62
 
42
- VALUE cError;
43
- VALUE cParseError;
63
+ typedef struct StringBuilder StringBuilder;
64
+
65
+ static size_t StringBuilder_SizeOf(size_t cap) {
66
+ return sizeof(StringBuilder) + cap;
67
+ }
68
+
69
+ StringBuilder *StringBuilder_New() {
70
+ const size_t cap = 128;
71
+ StringBuilder *builder = malloc(sizeof(*builder));
72
+ builder->size = 0;
73
+ builder->cap = cap;
74
+ builder->data = malloc(builder->cap);
75
+ return builder;
76
+ }
77
+
78
+ void StringBuilder_Free(StringBuilder *b) {
79
+ free(b->data);
80
+ free(b);
81
+ }
82
+
83
+ void StringBuilder_Printf(StringBuilder *b, const char *fmt, ...) {
84
+ size_t have = b->cap - b->size;
85
+ size_t n;
86
+ va_list args;
87
+
88
+ va_start(args, fmt);
89
+ n = vsnprintf(&b->data[b->size], have, fmt, args);
90
+ va_end(args);
91
+
92
+ if (have <= n) {
93
+ while (have <= n) {
94
+ b->cap *= 2;
95
+ have = b->cap - b->size;
96
+ }
97
+ b->data = realloc(b->data, StringBuilder_SizeOf(b->cap));
98
+ va_start(args, fmt);
99
+ n = vsnprintf(&b->data[b->size], have, fmt, args);
100
+ va_end(args);
101
+ PBRUBY_ASSERT(n < have);
102
+ }
103
+
104
+ b->size += n;
105
+ }
106
+
107
+ VALUE StringBuilder_ToRubyString(StringBuilder *b) {
108
+ VALUE ret = rb_str_new(b->data, b->size);
109
+ rb_enc_associate(ret, rb_utf8_encoding());
110
+ return ret;
111
+ }
44
112
 
45
- void add_def_obj(const void* def, VALUE value) {
46
- rb_hash_aset(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def), value);
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
+ } else {
119
+ StringBuilder_Printf(b, "%" PRId32, val);
120
+ }
47
121
  }
48
122
 
49
- VALUE get_def_obj(const void* def) {
50
- return rb_hash_aref(upb_def_to_ruby_obj_map, ULL2NUM((intptr_t)def));
123
+ void StringBuilder_PrintMsgval(StringBuilder *b, upb_MessageValue val,
124
+ TypeInfo info) {
125
+ switch (info.type) {
126
+ case kUpb_CType_Bool:
127
+ StringBuilder_Printf(b, "%s", val.bool_val ? "true" : "false");
128
+ break;
129
+ case kUpb_CType_Float: {
130
+ VALUE str = rb_inspect(DBL2NUM(val.float_val));
131
+ StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
132
+ break;
133
+ }
134
+ case kUpb_CType_Double: {
135
+ VALUE str = rb_inspect(DBL2NUM(val.double_val));
136
+ StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
137
+ break;
138
+ }
139
+ case kUpb_CType_Int32:
140
+ StringBuilder_Printf(b, "%" PRId32, val.int32_val);
141
+ break;
142
+ case kUpb_CType_UInt32:
143
+ StringBuilder_Printf(b, "%" PRIu32, val.uint32_val);
144
+ break;
145
+ case kUpb_CType_Int64:
146
+ StringBuilder_Printf(b, "%" PRId64, val.int64_val);
147
+ break;
148
+ case kUpb_CType_UInt64:
149
+ StringBuilder_Printf(b, "%" PRIu64, val.uint64_val);
150
+ break;
151
+ case kUpb_CType_String:
152
+ StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
153
+ val.str_val.data);
154
+ break;
155
+ case kUpb_CType_Bytes:
156
+ StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
157
+ val.str_val.data);
158
+ break;
159
+ case kUpb_CType_Enum:
160
+ StringBuilder_PrintEnum(b, val.int32_val, info.def.enumdef);
161
+ break;
162
+ case kUpb_CType_Message:
163
+ Message_PrintMessage(b, val.msg_val, info.def.msgdef);
164
+ break;
165
+ }
51
166
  }
52
167
 
53
168
  // -----------------------------------------------------------------------------
54
- // Utilities.
169
+ // Arena
55
170
  // -----------------------------------------------------------------------------
56
171
 
57
- // Raises a Ruby error if |status| is not OK, using its error message.
58
- void check_upb_status(const upb_status* status, const char* msg) {
59
- if (!upb_ok(status)) {
60
- rb_raise(rb_eRuntimeError, "%s: %s\n", msg, upb_status_errmsg(status));
172
+ typedef struct {
173
+ upb_Arena *arena;
174
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
175
+ // macro to update VALUE references, as to trigger write barriers.
176
+ VALUE pinned_objs;
177
+ } Arena;
178
+
179
+ static void Arena_mark(void *data) {
180
+ Arena *arena = data;
181
+ rb_gc_mark(arena->pinned_objs);
182
+ }
183
+
184
+ static void Arena_free(void *data) {
185
+ Arena *arena = data;
186
+ upb_Arena_Free(arena->arena);
187
+ xfree(arena);
188
+ }
189
+
190
+ static VALUE cArena;
191
+
192
+ const rb_data_type_t Arena_type = {
193
+ "Google::Protobuf::Internal::Arena",
194
+ {Arena_mark, Arena_free, NULL},
195
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
196
+ };
197
+
198
+ static void* ruby_upb_allocfunc(upb_alloc* alloc, void* ptr, size_t oldsize, size_t size) {
199
+ if (size == 0) {
200
+ xfree(ptr);
201
+ return NULL;
202
+ } else {
203
+ return xrealloc(ptr, size);
204
+ }
205
+ }
206
+
207
+ upb_alloc ruby_upb_alloc = {&ruby_upb_allocfunc};
208
+
209
+ static VALUE Arena_alloc(VALUE klass) {
210
+ Arena *arena = ALLOC(Arena);
211
+ arena->arena = upb_Arena_Init(NULL, 0, &ruby_upb_alloc);
212
+ arena->pinned_objs = Qnil;
213
+ return TypedData_Wrap_Struct(klass, &Arena_type, arena);
214
+ }
215
+
216
+ upb_Arena *Arena_get(VALUE _arena) {
217
+ Arena *arena;
218
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
219
+ return arena->arena;
220
+ }
221
+
222
+ void Arena_fuse(VALUE _arena, upb_Arena *other) {
223
+ Arena *arena;
224
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
225
+ if (!upb_Arena_Fuse(arena->arena, other)) {
226
+ rb_raise(rb_eRuntimeError,
227
+ "Unable to fuse arenas. This should never happen since Ruby does "
228
+ "not use initial blocks");
229
+ }
230
+ }
231
+
232
+ VALUE Arena_new() { return Arena_alloc(cArena); }
233
+
234
+ void Arena_Pin(VALUE _arena, VALUE obj) {
235
+ Arena *arena;
236
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
237
+ if (arena->pinned_objs == Qnil) {
238
+ RB_OBJ_WRITE(_arena, &arena->pinned_objs, rb_ary_new());
61
239
  }
240
+ rb_ary_push(arena->pinned_objs, obj);
62
241
  }
63
242
 
64
- // String encodings: we look these up once, at load time, and then cache them
65
- // here.
66
- rb_encoding* kRubyStringUtf8Encoding;
67
- rb_encoding* kRubyStringASCIIEncoding;
68
- rb_encoding* kRubyString8bitEncoding;
243
+ void Arena_register(VALUE module) {
244
+ VALUE internal = rb_define_module_under(module, "Internal");
245
+ VALUE klass = rb_define_class_under(internal, "Arena", rb_cObject);
246
+ rb_define_alloc_func(klass, Arena_alloc);
247
+ rb_gc_register_address(&cArena);
248
+ cArena = klass;
249
+ }
250
+
251
+ // -----------------------------------------------------------------------------
252
+ // Object Cache
253
+ // -----------------------------------------------------------------------------
254
+
255
+ // A pointer -> Ruby Object cache that keeps references to Ruby wrapper
256
+ // objects. This allows us to look up any Ruby wrapper object by the address
257
+ // of the object it is wrapping. That way we can avoid ever creating two
258
+ // different wrapper objects for the same C object, which saves memory and
259
+ // preserves object identity.
260
+ //
261
+ // We use WeakMap for the cache. For Ruby <2.7 we also need a secondary Hash
262
+ // to store WeakMap keys because Ruby <2.7 WeakMap doesn't allow non-finalizable
263
+ // keys.
264
+ //
265
+ // We also need the secondary Hash if sizeof(long) < sizeof(VALUE), because this
266
+ // means it may not be possible to fit a pointer into a Fixnum. Keys are
267
+ // pointers, and if they fit into a Fixnum, Ruby doesn't collect them, but if
268
+ // they overflow and require allocating a Bignum, they could get collected
269
+ // prematurely, thus removing the cache entry. This happens on 64-bit Windows,
270
+ // on which pointers are 64 bits but longs are 32 bits. In this case, we enable
271
+ // the secondary Hash to hold the keys and prevent them from being collected.
272
+
273
+ #if RUBY_API_VERSION_CODE >= 20700 && SIZEOF_LONG >= SIZEOF_VALUE
274
+ #define USE_SECONDARY_MAP 0
275
+ #else
276
+ #define USE_SECONDARY_MAP 1
277
+ #endif
278
+
279
+ #if USE_SECONDARY_MAP
280
+
281
+ // Maps Numeric -> Object. The object is then used as a key into the WeakMap.
282
+ // This is needed for Ruby <2.7 where a number cannot be a key to WeakMap.
283
+ // The object is used only for its identity; it does not contain any data.
284
+ VALUE secondary_map = Qnil;
285
+
286
+ // Mutations to the map are under a mutex, because SeconaryMap_MaybeGC()
287
+ // iterates over the map which cannot happen in parallel with insertions, or
288
+ // Ruby will throw:
289
+ // can't add a new key into hash during iteration (RuntimeError)
290
+ VALUE secondary_map_mutex = Qnil;
69
291
 
70
- // Ruby-interned string: "descriptor". We use this identifier to store an
71
- // instance variable on message classes we create in order to link them back to
72
- // their descriptors.
292
+ // Lambda that will GC entries from the secondary map that are no longer present
293
+ // in the primary map.
294
+ VALUE gc_secondary_map_lambda = Qnil;
295
+ ID length;
296
+
297
+ extern VALUE weak_obj_cache;
298
+
299
+ static void SecondaryMap_Init() {
300
+ rb_gc_register_address(&secondary_map);
301
+ rb_gc_register_address(&gc_secondary_map_lambda);
302
+ rb_gc_register_address(&secondary_map_mutex);
303
+ secondary_map = rb_hash_new();
304
+ gc_secondary_map_lambda = rb_eval_string(
305
+ "->(secondary, weak) {\n"
306
+ " secondary.delete_if { |k, v| !weak.key?(v) }\n"
307
+ "}\n");
308
+ secondary_map_mutex = rb_mutex_new();
309
+ length = rb_intern("length");
310
+ }
311
+
312
+ // The secondary map is a regular Hash, and will never shrink on its own.
313
+ // The main object cache is a WeakMap that will automatically remove entries
314
+ // when the target object is no longer reachable, but unless we manually
315
+ // remove the corresponding entries from the secondary map, it will grow
316
+ // without bound.
317
+ //
318
+ // To avoid this unbounded growth we periodically remove entries from the
319
+ // secondary map that are no longer present in the WeakMap. The logic of
320
+ // how often to perform this GC is an artbirary tuning parameter that
321
+ // represents a straightforward CPU/memory tradeoff.
73
322
  //
74
- // We intern this once at module load time then use the interned identifier at
75
- // runtime in order to avoid the cost of repeatedly interning in hot paths.
76
- const char* kDescriptorInstanceVar = "descriptor";
77
- ID descriptor_instancevar_interned;
323
+ // Requires: secondary_map_mutex is held.
324
+ static void SecondaryMap_MaybeGC() {
325
+ PBRUBY_ASSERT(rb_mutex_locked_p(secondary_map_mutex) == Qtrue);
326
+ size_t weak_len = NUM2ULL(rb_funcall(weak_obj_cache, length, 0));
327
+ size_t secondary_len = RHASH_SIZE(secondary_map);
328
+ if (secondary_len < weak_len) {
329
+ // Logically this case should not be possible: a valid entry cannot exist in
330
+ // the weak table unless there is a corresponding entry in the secondary
331
+ // table. It should *always* be the case that secondary_len >= weak_len.
332
+ //
333
+ // However ObjectSpace::WeakMap#length (and therefore weak_len) is
334
+ // unreliable: it overreports its true length by including non-live objects.
335
+ // However these non-live objects are not yielded in iteration, so we may
336
+ // have previously deleted them from the secondary map in a previous
337
+ // invocation of SecondaryMap_MaybeGC().
338
+ //
339
+ // In this case, we can't measure any waste, so we just return.
340
+ return;
341
+ }
342
+ size_t waste = secondary_len - weak_len;
343
+ // GC if we could remove at least 2000 entries or 20% of the table size
344
+ // (whichever is greater). Since the cost of the GC pass is O(N), we
345
+ // want to make sure that we condition this on overall table size, to
346
+ // avoid O(N^2) CPU costs.
347
+ size_t threshold = PBRUBY_MAX(secondary_len * 0.2, 2000);
348
+ if (waste > threshold) {
349
+ rb_funcall(gc_secondary_map_lambda, rb_intern("call"), 2, secondary_map,
350
+ weak_obj_cache);
351
+ }
352
+ }
353
+
354
+ // Requires: secondary_map_mutex is held by this thread iff create == true.
355
+ static VALUE SecondaryMap_Get(VALUE key, bool create) {
356
+ PBRUBY_ASSERT(!create || rb_mutex_locked_p(secondary_map_mutex) == Qtrue);
357
+ VALUE ret = rb_hash_lookup(secondary_map, key);
358
+ if (ret == Qnil && create) {
359
+ SecondaryMap_MaybeGC();
360
+ ret = rb_class_new_instance(0, NULL, rb_cObject);
361
+ rb_hash_aset(secondary_map, key, ret);
362
+ }
363
+ return ret;
364
+ }
365
+
366
+ #endif
367
+
368
+ // Requires: secondary_map_mutex is held by this thread iff create == true.
369
+ static VALUE ObjectCache_GetKey(const void *key, bool create) {
370
+ VALUE key_val = (VALUE)key;
371
+ PBRUBY_ASSERT((key_val & 3) == 0);
372
+ VALUE ret = LL2NUM(key_val >> 2);
373
+ #if USE_SECONDARY_MAP
374
+ ret = SecondaryMap_Get(ret, create);
375
+ #endif
376
+ return ret;
377
+ }
378
+
379
+ // Public ObjectCache API.
380
+
381
+ VALUE weak_obj_cache = Qnil;
382
+ ID item_get;
383
+ ID item_set;
384
+
385
+ static void ObjectCache_Init() {
386
+ rb_gc_register_address(&weak_obj_cache);
387
+ VALUE klass = rb_eval_string("ObjectSpace::WeakMap");
388
+ weak_obj_cache = rb_class_new_instance(0, NULL, klass);
389
+ item_get = rb_intern("[]");
390
+ item_set = rb_intern("[]=");
391
+ #if USE_SECONDARY_MAP
392
+ SecondaryMap_Init();
393
+ #endif
394
+ }
395
+
396
+ void ObjectCache_Add(const void *key, VALUE val) {
397
+ PBRUBY_ASSERT(ObjectCache_Get(key) == Qnil);
398
+ #if USE_SECONDARY_MAP
399
+ rb_mutex_lock(secondary_map_mutex);
400
+ #endif
401
+ VALUE key_rb = ObjectCache_GetKey(key, true);
402
+ rb_funcall(weak_obj_cache, item_set, 2, key_rb, val);
403
+ #if USE_SECONDARY_MAP
404
+ rb_mutex_unlock(secondary_map_mutex);
405
+ #endif
406
+ PBRUBY_ASSERT(ObjectCache_Get(key) == val);
407
+ }
408
+
409
+ // Returns the cached object for this key, if any. Otherwise returns Qnil.
410
+ VALUE ObjectCache_Get(const void *key) {
411
+ VALUE key_rb = ObjectCache_GetKey(key, false);
412
+ return rb_funcall(weak_obj_cache, item_get, 1, key_rb);
413
+ }
414
+
415
+ /*
416
+ * call-seq:
417
+ * Google::Protobuf.discard_unknown(msg)
418
+ *
419
+ * Discard unknown fields in the given message object and recursively discard
420
+ * unknown fields in submessages.
421
+ */
422
+ static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
423
+ const upb_MessageDef *m;
424
+ upb_Message *msg = Message_GetMutable(msg_rb, &m);
425
+ if (!upb_Message_DiscardUnknown(msg, m, 128)) {
426
+ rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
427
+ }
428
+
429
+ return Qnil;
430
+ }
431
+
432
+ /*
433
+ * call-seq:
434
+ * Google::Protobuf.deep_copy(obj) => copy_of_obj
435
+ *
436
+ * Performs a deep copy of a RepeatedField instance, a Map instance, or a
437
+ * message object, recursively copying its members.
438
+ */
439
+ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
440
+ VALUE klass = CLASS_OF(obj);
441
+ if (klass == cRepeatedField) {
442
+ return RepeatedField_deep_copy(obj);
443
+ } else if (klass == cMap) {
444
+ return Map_deep_copy(obj);
445
+ } else {
446
+ VALUE new_arena_rb = Arena_new();
447
+ upb_Arena *new_arena = Arena_get(new_arena_rb);
448
+ const upb_MessageDef *m;
449
+ const upb_Message *msg = Message_Get(obj, &m);
450
+ upb_Message *new_msg = Message_deep_copy(msg, m, new_arena);
451
+ return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
452
+ }
453
+ }
78
454
 
79
455
  // -----------------------------------------------------------------------------
80
456
  // Initialization/entry point.
@@ -82,34 +458,25 @@ ID descriptor_instancevar_interned;
82
458
 
83
459
  // This must be named "Init_protobuf_c" because the Ruby module is named
84
460
  // "protobuf_c" -- the VM looks for this symbol in our .so.
85
- void Init_protobuf_c() {
461
+ __attribute__((visibility("default"))) void Init_protobuf_c() {
462
+ ObjectCache_Init();
463
+
86
464
  VALUE google = rb_define_module("Google");
87
465
  VALUE protobuf = rb_define_module_under(google, "Protobuf");
88
- VALUE internal = rb_define_module_under(protobuf, "Internal");
89
-
90
- descriptor_instancevar_interned = rb_intern(kDescriptorInstanceVar);
91
- DescriptorPool_register(protobuf);
92
- Descriptor_register(protobuf);
93
- FieldDescriptor_register(protobuf);
94
- OneofDescriptor_register(protobuf);
95
- EnumDescriptor_register(protobuf);
96
- MessageBuilderContext_register(internal);
97
- OneofBuilderContext_register(internal);
98
- EnumBuilderContext_register(internal);
99
- Builder_register(internal);
466
+
467
+ Arena_register(protobuf);
468
+ Defs_register(protobuf);
100
469
  RepeatedField_register(protobuf);
101
470
  Map_register(protobuf);
471
+ Message_register(protobuf);
102
472
 
103
- cError = rb_const_get(protobuf, rb_intern("Error"));
104
473
  cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
474
+ rb_gc_register_mark_object(cParseError);
475
+ cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
476
+ rb_gc_register_mark_object(cTypeError);
105
477
 
106
- rb_define_singleton_method(protobuf, "deep_copy",
107
- Google_Protobuf_deep_copy, 1);
108
-
109
- kRubyStringUtf8Encoding = rb_utf8_encoding();
110
- kRubyStringASCIIEncoding = rb_usascii_encoding();
111
- kRubyString8bitEncoding = rb_ascii8bit_encoding();
112
-
113
- rb_gc_register_address(&upb_def_to_ruby_obj_map);
114
- upb_def_to_ruby_obj_map = rb_hash_new();
478
+ rb_define_singleton_method(protobuf, "discard_unknown",
479
+ Google_Protobuf_discard_unknown, 1);
480
+ rb_define_singleton_method(protobuf, "deep_copy", Google_Protobuf_deep_copy,
481
+ 1);
115
482
  }