google-protobuf 3.24.4-java → 3.25.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/Rakefile +3 -0
  3. data/ext/google/protobuf_c/glue.c +21 -0
  4. data/ext/google/protobuf_c/message.c +1388 -0
  5. data/ext/google/protobuf_c/message.h +79 -0
  6. data/ext/google/protobuf_c/protobuf.c +343 -0
  7. data/ext/google/protobuf_c/protobuf.h +112 -0
  8. data/ext/google/protobuf_c/ruby-upb.c +14414 -0
  9. data/ext/google/protobuf_c/ruby-upb.h +13044 -0
  10. data/ext/google/protobuf_c/shared_convert.c +64 -0
  11. data/ext/google/protobuf_c/shared_convert.h +26 -0
  12. data/ext/google/protobuf_c/shared_message.c +65 -0
  13. data/ext/google/protobuf_c/shared_message.h +25 -0
  14. data/ext/google/protobuf_c/third_party/utf8_range/LICENSE +22 -0
  15. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +92 -0
  16. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +157 -0
  17. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +170 -0
  18. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +21 -0
  19. data/lib/google/protobuf/any_pb.rb +1 -1
  20. data/lib/google/protobuf/api_pb.rb +1 -1
  21. data/lib/google/protobuf/descriptor_pb.rb +6 -3
  22. data/lib/google/protobuf/duration_pb.rb +1 -1
  23. data/lib/google/protobuf/empty_pb.rb +1 -1
  24. data/lib/google/protobuf/ffi/descriptor.rb +154 -0
  25. data/lib/google/protobuf/ffi/descriptor_pool.rb +70 -0
  26. data/lib/google/protobuf/ffi/enum_descriptor.rb +161 -0
  27. data/lib/google/protobuf/ffi/ffi.rb +213 -0
  28. data/lib/google/protobuf/ffi/field_descriptor.rb +309 -0
  29. data/lib/google/protobuf/ffi/file_descriptor.rb +48 -0
  30. data/lib/google/protobuf/ffi/internal/arena.rb +66 -0
  31. data/lib/google/protobuf/ffi/internal/convert.rb +305 -0
  32. data/lib/google/protobuf/ffi/internal/pointer_helper.rb +35 -0
  33. data/lib/google/protobuf/ffi/internal/type_safety.rb +25 -0
  34. data/lib/google/protobuf/ffi/map.rb +396 -0
  35. data/lib/google/protobuf/ffi/message.rb +641 -0
  36. data/lib/google/protobuf/ffi/object_cache.rb +30 -0
  37. data/lib/google/protobuf/ffi/oneof_descriptor.rb +88 -0
  38. data/lib/google/protobuf/ffi/repeated_field.rb +503 -0
  39. data/lib/google/protobuf/field_mask_pb.rb +1 -1
  40. data/lib/google/protobuf/message_exts.rb +3 -26
  41. data/lib/google/protobuf/object_cache.rb +3 -26
  42. data/lib/google/protobuf/plugin_pb.rb +1 -1
  43. data/lib/google/protobuf/repeated_field.rb +3 -26
  44. data/lib/google/protobuf/source_context_pb.rb +1 -1
  45. data/lib/google/protobuf/struct_pb.rb +1 -1
  46. data/lib/google/protobuf/timestamp_pb.rb +1 -1
  47. data/lib/google/protobuf/type_pb.rb +1 -1
  48. data/lib/google/protobuf/well_known_types.rb +3 -26
  49. data/lib/google/protobuf/wrappers_pb.rb +1 -1
  50. data/lib/google/protobuf.rb +26 -45
  51. data/lib/google/protobuf_ffi.rb +50 -0
  52. data/lib/google/protobuf_java.jar +0 -0
  53. data/lib/google/protobuf_native.rb +20 -0
  54. data/lib/google/tasks/ffi.rake +102 -0
  55. metadata +110 -4
@@ -0,0 +1,79 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2008 Google Inc. All rights reserved.
3
+ //
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
7
+
8
+ #ifndef RUBY_PROTOBUF_MESSAGE_H_
9
+ #define RUBY_PROTOBUF_MESSAGE_H_
10
+
11
+ #include "protobuf.h"
12
+ #include "ruby-upb.h"
13
+
14
+ // Gets the underlying upb_Message* and upb_MessageDef for the given Ruby
15
+ // message wrapper. Requires that |value| is indeed a message object.
16
+ const upb_Message* Message_Get(VALUE value, const upb_MessageDef** m);
17
+
18
+ // Like Message_Get(), but checks that the object is not frozen and returns a
19
+ // mutable pointer.
20
+ upb_Message* Message_GetMutable(VALUE value, const upb_MessageDef** m);
21
+
22
+ // Returns the Arena object for this message.
23
+ VALUE Message_GetArena(VALUE value);
24
+
25
+ // Converts |value| into a upb_Message value of the expected upb_MessageDef
26
+ // type, raising an error if this is not possible. Used when assigning |value|
27
+ // to a field of another message, which means the message must be of a
28
+ // particular type.
29
+ //
30
+ // This will perform automatic conversions in some cases (for example, Time ->
31
+ // Google::Protobuf::Timestamp). If any new message is created, it will be
32
+ // created on |arena|, and any existing message will have its arena fused with
33
+ // |arena|.
34
+ const upb_Message* Message_GetUpbMessage(VALUE value, const upb_MessageDef* m,
35
+ const char* name, upb_Arena* arena);
36
+
37
+ // Gets or constructs a Ruby wrapper object for the given message. The wrapper
38
+ // object will reference |arena| and ensure that it outlives this object.
39
+ VALUE Message_GetRubyWrapper(upb_Message* msg, const upb_MessageDef* m,
40
+ VALUE arena);
41
+
42
+ // Gets the given field from this message.
43
+ VALUE Message_getfield(VALUE _self, const upb_FieldDef* f);
44
+
45
+ // Implements #inspect for this message, printing the text to |b|.
46
+ void Message_PrintMessage(StringBuilder* b, const upb_Message* msg,
47
+ const upb_MessageDef* m);
48
+
49
+ // Returns a hash value for the given message.
50
+ uint64_t Message_Hash(const upb_Message* msg, const upb_MessageDef* m,
51
+ uint64_t seed);
52
+
53
+ // Returns a deep copy of the given message.
54
+ upb_Message* Message_deep_copy(const upb_Message* msg, const upb_MessageDef* m,
55
+ upb_Arena* arena);
56
+
57
+ // Returns true if these two messages are equal.
58
+ bool Message_Equal(const upb_Message* m1, const upb_Message* m2,
59
+ const upb_MessageDef* m);
60
+
61
+ // Checks that this Ruby object is a message, and raises an exception if not.
62
+ void Message_CheckClass(VALUE klass);
63
+
64
+ // Returns a new Hash object containing the contents of this message.
65
+ VALUE Scalar_CreateHash(upb_MessageValue val, TypeInfo type_info);
66
+
67
+ // Creates a message class or enum module for this descriptor, respectively.
68
+ VALUE build_class_from_descriptor(VALUE descriptor);
69
+ VALUE build_module_from_enumdesc(VALUE _enumdesc);
70
+
71
+ // Returns the Descriptor/EnumDescriptor for the given message class or enum
72
+ // module, respectively. Returns nil if this is not a message class or enum
73
+ // module.
74
+ VALUE MessageOrEnum_GetDescriptor(VALUE klass);
75
+
76
+ // Call at startup to register all types in this module.
77
+ void Message_register(VALUE protobuf);
78
+
79
+ #endif // RUBY_PROTOBUF_MESSAGE_H_
@@ -0,0 +1,343 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2014 Google Inc. All rights reserved.
3
+ //
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
7
+
8
+ #include "protobuf.h"
9
+
10
+ #include <ruby/version.h>
11
+
12
+ #include "defs.h"
13
+ #include "map.h"
14
+ #include "message.h"
15
+ #include "repeated_field.h"
16
+
17
+ VALUE cParseError;
18
+ VALUE cTypeError;
19
+
20
+ const upb_FieldDef *map_field_key(const upb_FieldDef *field) {
21
+ const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
22
+ return upb_MessageDef_FindFieldByNumber(entry, 1);
23
+ }
24
+
25
+ const upb_FieldDef *map_field_value(const upb_FieldDef *field) {
26
+ const upb_MessageDef *entry = upb_FieldDef_MessageSubDef(field);
27
+ return upb_MessageDef_FindFieldByNumber(entry, 2);
28
+ }
29
+
30
+ // -----------------------------------------------------------------------------
31
+ // StringBuilder, for inspect
32
+ // -----------------------------------------------------------------------------
33
+
34
+ struct StringBuilder {
35
+ size_t size;
36
+ size_t cap;
37
+ char *data;
38
+ };
39
+
40
+ typedef struct StringBuilder StringBuilder;
41
+
42
+ static size_t StringBuilder_SizeOf(size_t cap) {
43
+ return sizeof(StringBuilder) + cap;
44
+ }
45
+
46
+ StringBuilder *StringBuilder_New() {
47
+ const size_t cap = 128;
48
+ StringBuilder *builder = malloc(sizeof(*builder));
49
+ builder->size = 0;
50
+ builder->cap = cap;
51
+ builder->data = malloc(builder->cap);
52
+ return builder;
53
+ }
54
+
55
+ void StringBuilder_Free(StringBuilder *b) {
56
+ free(b->data);
57
+ free(b);
58
+ }
59
+
60
+ void StringBuilder_Printf(StringBuilder *b, const char *fmt, ...) {
61
+ size_t have = b->cap - b->size;
62
+ size_t n;
63
+ va_list args;
64
+
65
+ va_start(args, fmt);
66
+ n = vsnprintf(&b->data[b->size], have, fmt, args);
67
+ va_end(args);
68
+
69
+ if (have <= n) {
70
+ while (have <= n) {
71
+ b->cap *= 2;
72
+ have = b->cap - b->size;
73
+ }
74
+ b->data = realloc(b->data, StringBuilder_SizeOf(b->cap));
75
+ va_start(args, fmt);
76
+ n = vsnprintf(&b->data[b->size], have, fmt, args);
77
+ va_end(args);
78
+ PBRUBY_ASSERT(n < have);
79
+ }
80
+
81
+ b->size += n;
82
+ }
83
+
84
+ VALUE StringBuilder_ToRubyString(StringBuilder *b) {
85
+ VALUE ret = rb_str_new(b->data, b->size);
86
+ rb_enc_associate(ret, rb_utf8_encoding());
87
+ return ret;
88
+ }
89
+
90
+ static void StringBuilder_PrintEnum(StringBuilder *b, int32_t val,
91
+ const upb_EnumDef *e) {
92
+ const upb_EnumValueDef *ev = upb_EnumDef_FindValueByNumber(e, val);
93
+ if (ev) {
94
+ StringBuilder_Printf(b, ":%s", upb_EnumValueDef_Name(ev));
95
+ } else {
96
+ StringBuilder_Printf(b, "%" PRId32, val);
97
+ }
98
+ }
99
+
100
+ void StringBuilder_PrintMsgval(StringBuilder *b, upb_MessageValue val,
101
+ TypeInfo info) {
102
+ switch (info.type) {
103
+ case kUpb_CType_Bool:
104
+ StringBuilder_Printf(b, "%s", val.bool_val ? "true" : "false");
105
+ break;
106
+ case kUpb_CType_Float: {
107
+ VALUE str = rb_inspect(DBL2NUM(val.float_val));
108
+ StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
109
+ break;
110
+ }
111
+ case kUpb_CType_Double: {
112
+ VALUE str = rb_inspect(DBL2NUM(val.double_val));
113
+ StringBuilder_Printf(b, "%s", RSTRING_PTR(str));
114
+ break;
115
+ }
116
+ case kUpb_CType_Int32:
117
+ StringBuilder_Printf(b, "%" PRId32, val.int32_val);
118
+ break;
119
+ case kUpb_CType_UInt32:
120
+ StringBuilder_Printf(b, "%" PRIu32, val.uint32_val);
121
+ break;
122
+ case kUpb_CType_Int64:
123
+ StringBuilder_Printf(b, "%" PRId64, val.int64_val);
124
+ break;
125
+ case kUpb_CType_UInt64:
126
+ StringBuilder_Printf(b, "%" PRIu64, val.uint64_val);
127
+ break;
128
+ case kUpb_CType_String:
129
+ StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
130
+ val.str_val.data);
131
+ break;
132
+ case kUpb_CType_Bytes:
133
+ StringBuilder_Printf(b, "\"%.*s\"", (int)val.str_val.size,
134
+ val.str_val.data);
135
+ break;
136
+ case kUpb_CType_Enum:
137
+ StringBuilder_PrintEnum(b, val.int32_val, info.def.enumdef);
138
+ break;
139
+ case kUpb_CType_Message:
140
+ Message_PrintMessage(b, val.msg_val, info.def.msgdef);
141
+ break;
142
+ }
143
+ }
144
+
145
+ // -----------------------------------------------------------------------------
146
+ // Arena
147
+ // -----------------------------------------------------------------------------
148
+
149
+ typedef struct {
150
+ upb_Arena *arena;
151
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
152
+ // macro to update VALUE references, as to trigger write barriers.
153
+ VALUE pinned_objs;
154
+ } Arena;
155
+
156
+ static void Arena_mark(void *data) {
157
+ Arena *arena = data;
158
+ rb_gc_mark(arena->pinned_objs);
159
+ }
160
+
161
+ static void Arena_free(void *data) {
162
+ Arena *arena = data;
163
+ upb_Arena_Free(arena->arena);
164
+ xfree(arena);
165
+ }
166
+
167
+ static VALUE cArena;
168
+
169
+ const rb_data_type_t Arena_type = {
170
+ "Google::Protobuf::Internal::Arena",
171
+ {Arena_mark, Arena_free, NULL},
172
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
173
+ };
174
+
175
+ static void *ruby_upb_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
176
+ size_t size) {
177
+ if (size == 0) {
178
+ xfree(ptr);
179
+ return NULL;
180
+ } else {
181
+ return xrealloc(ptr, size);
182
+ }
183
+ }
184
+
185
+ upb_alloc ruby_upb_alloc = {&ruby_upb_allocfunc};
186
+
187
+ static VALUE Arena_alloc(VALUE klass) {
188
+ Arena *arena = ALLOC(Arena);
189
+ arena->arena = upb_Arena_Init(NULL, 0, &ruby_upb_alloc);
190
+ arena->pinned_objs = Qnil;
191
+ return TypedData_Wrap_Struct(klass, &Arena_type, arena);
192
+ }
193
+
194
+ upb_Arena *Arena_get(VALUE _arena) {
195
+ Arena *arena;
196
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
197
+ return arena->arena;
198
+ }
199
+
200
+ void Arena_fuse(VALUE _arena, upb_Arena *other) {
201
+ Arena *arena;
202
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
203
+ if (!upb_Arena_Fuse(arena->arena, other)) {
204
+ rb_raise(rb_eRuntimeError,
205
+ "Unable to fuse arenas. This should never happen since Ruby does "
206
+ "not use initial blocks");
207
+ }
208
+ }
209
+
210
+ VALUE Arena_new() { return Arena_alloc(cArena); }
211
+
212
+ void Arena_Pin(VALUE _arena, VALUE obj) {
213
+ Arena *arena;
214
+ TypedData_Get_Struct(_arena, Arena, &Arena_type, arena);
215
+ if (arena->pinned_objs == Qnil) {
216
+ RB_OBJ_WRITE(_arena, &arena->pinned_objs, rb_ary_new());
217
+ }
218
+ rb_ary_push(arena->pinned_objs, obj);
219
+ }
220
+
221
+ void Arena_register(VALUE module) {
222
+ VALUE internal = rb_define_module_under(module, "Internal");
223
+ VALUE klass = rb_define_class_under(internal, "Arena", rb_cObject);
224
+ rb_define_alloc_func(klass, Arena_alloc);
225
+ rb_gc_register_address(&cArena);
226
+ cArena = klass;
227
+ }
228
+
229
+ // -----------------------------------------------------------------------------
230
+ // Object Cache
231
+ // -----------------------------------------------------------------------------
232
+
233
+ // Public ObjectCache API.
234
+
235
+ VALUE weak_obj_cache = Qnil;
236
+ ID item_get;
237
+ ID item_try_add;
238
+
239
+ static void ObjectCache_Init(VALUE protobuf) {
240
+ item_get = rb_intern("get");
241
+ item_try_add = rb_intern("try_add");
242
+
243
+ rb_gc_register_address(&weak_obj_cache);
244
+ #if SIZEOF_LONG >= SIZEOF_VALUE
245
+ VALUE cache_class = rb_const_get(protobuf, rb_intern("ObjectCache"));
246
+ #else
247
+ VALUE cache_class = rb_const_get(protobuf, rb_intern("LegacyObjectCache"));
248
+ #endif
249
+
250
+ weak_obj_cache = rb_class_new_instance(0, NULL, cache_class);
251
+ rb_const_set(protobuf, rb_intern("OBJECT_CACHE"), weak_obj_cache);
252
+ rb_const_set(protobuf, rb_intern("SIZEOF_LONG"), INT2NUM(SIZEOF_LONG));
253
+ rb_const_set(protobuf, rb_intern("SIZEOF_VALUE"), INT2NUM(SIZEOF_VALUE));
254
+ }
255
+
256
+ static VALUE ObjectCache_GetKey(const void *key) {
257
+ VALUE key_val = (VALUE)key;
258
+ PBRUBY_ASSERT((key_val & 3) == 0);
259
+ // Ensure the key can be stored as a Fixnum since 1 bit is needed for
260
+ // FIXNUM_FLAG and 1 bit is needed for the sign bit.
261
+ VALUE new_key = LL2NUM(key_val >> 2);
262
+ PBRUBY_ASSERT(FIXNUM_P(new_key));
263
+ return new_key;
264
+ }
265
+
266
+ VALUE ObjectCache_TryAdd(const void *key, VALUE val) {
267
+ VALUE key_val = ObjectCache_GetKey(key);
268
+ return rb_funcall(weak_obj_cache, item_try_add, 2, key_val, val);
269
+ }
270
+
271
+ // Returns the cached object for this key, if any. Otherwise returns Qnil.
272
+ VALUE ObjectCache_Get(const void *key) {
273
+ VALUE key_val = ObjectCache_GetKey(key);
274
+ return rb_funcall(weak_obj_cache, item_get, 1, key_val);
275
+ }
276
+
277
+ /*
278
+ * call-seq:
279
+ * Google::Protobuf.discard_unknown(msg)
280
+ *
281
+ * Discard unknown fields in the given message object and recursively discard
282
+ * unknown fields in submessages.
283
+ */
284
+ static VALUE Google_Protobuf_discard_unknown(VALUE self, VALUE msg_rb) {
285
+ const upb_MessageDef *m;
286
+ upb_Message *msg = Message_GetMutable(msg_rb, &m);
287
+ if (!upb_Message_DiscardUnknown(msg, m, 128)) {
288
+ rb_raise(rb_eRuntimeError, "Messages nested too deeply.");
289
+ }
290
+
291
+ return Qnil;
292
+ }
293
+
294
+ /*
295
+ * call-seq:
296
+ * Google::Protobuf.deep_copy(obj) => copy_of_obj
297
+ *
298
+ * Performs a deep copy of a RepeatedField instance, a Map instance, or a
299
+ * message object, recursively copying its members.
300
+ */
301
+ VALUE Google_Protobuf_deep_copy(VALUE self, VALUE obj) {
302
+ VALUE klass = CLASS_OF(obj);
303
+ if (klass == cRepeatedField) {
304
+ return RepeatedField_deep_copy(obj);
305
+ } else if (klass == cMap) {
306
+ return Map_deep_copy(obj);
307
+ } else {
308
+ VALUE new_arena_rb = Arena_new();
309
+ upb_Arena *new_arena = Arena_get(new_arena_rb);
310
+ const upb_MessageDef *m;
311
+ const upb_Message *msg = Message_Get(obj, &m);
312
+ upb_Message *new_msg = Message_deep_copy(msg, m, new_arena);
313
+ return Message_GetRubyWrapper(new_msg, m, new_arena_rb);
314
+ }
315
+ }
316
+
317
+ // -----------------------------------------------------------------------------
318
+ // Initialization/entry point.
319
+ // -----------------------------------------------------------------------------
320
+
321
+ // This must be named "Init_protobuf_c" because the Ruby module is named
322
+ // "protobuf_c" -- the VM looks for this symbol in our .so.
323
+ __attribute__((visibility("default"))) void Init_protobuf_c() {
324
+ VALUE google = rb_define_module("Google");
325
+ VALUE protobuf = rb_define_module_under(google, "Protobuf");
326
+
327
+ ObjectCache_Init(protobuf);
328
+ Arena_register(protobuf);
329
+ Defs_register(protobuf);
330
+ RepeatedField_register(protobuf);
331
+ Map_register(protobuf);
332
+ Message_register(protobuf);
333
+
334
+ cParseError = rb_const_get(protobuf, rb_intern("ParseError"));
335
+ rb_gc_register_mark_object(cParseError);
336
+ cTypeError = rb_const_get(protobuf, rb_intern("TypeError"));
337
+ rb_gc_register_mark_object(cTypeError);
338
+
339
+ rb_define_singleton_method(protobuf, "discard_unknown",
340
+ Google_Protobuf_discard_unknown, 1);
341
+ rb_define_singleton_method(protobuf, "deep_copy", Google_Protobuf_deep_copy,
342
+ 1);
343
+ }
@@ -0,0 +1,112 @@
1
+ // Protocol Buffers - Google's data interchange format
2
+ // Copyright 2014 Google Inc. All rights reserved.
3
+ //
4
+ // Use of this source code is governed by a BSD-style
5
+ // license that can be found in the LICENSE file or at
6
+ // https://developers.google.com/open-source/licenses/bsd
7
+
8
+ #ifndef __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
9
+ #define __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__
10
+
11
+ // Ruby 3+ defines NDEBUG itself, see: https://bugs.ruby-lang.org/issues/18777
12
+ #ifdef NDEBUG
13
+ #include <ruby.h>
14
+ #else
15
+ #include <ruby.h>
16
+ #undef NDEBUG
17
+ #endif
18
+
19
+ #include <ruby/version.h>
20
+
21
+ #if RUBY_API_VERSION_CODE < 20700
22
+ #error Protobuf requires Ruby >= 2.7
23
+ #endif
24
+
25
+ #include <assert.h> // Must be included after the NDEBUG logic above.
26
+ #include <ruby/encoding.h>
27
+ #include <ruby/vm.h>
28
+
29
+ #include "defs.h"
30
+ #include "ruby-upb.h"
31
+
32
+ // These operate on a map field (i.e., a repeated field of submessages whose
33
+ // submessage type is a map-entry msgdef).
34
+ const upb_FieldDef* map_field_key(const upb_FieldDef* field);
35
+ const upb_FieldDef* map_field_value(const upb_FieldDef* field);
36
+
37
+ // -----------------------------------------------------------------------------
38
+ // Arena
39
+ // -----------------------------------------------------------------------------
40
+
41
+ // A Ruby object that wraps an underlying upb_Arena. Any objects that are
42
+ // allocated from this arena should reference the Arena in rb_gc_mark(), to
43
+ // ensure that the object's underlying memory outlives any Ruby object that can
44
+ // reach it.
45
+
46
+ VALUE Arena_new();
47
+ upb_Arena* Arena_get(VALUE arena);
48
+
49
+ // Fuses this arena to another, throwing a Ruby exception if this is not
50
+ // possible.
51
+ void Arena_fuse(VALUE arena, upb_Arena* other);
52
+
53
+ // Pins this Ruby object to the lifetime of this arena, so that as long as the
54
+ // arena is alive this object will not be collected.
55
+ //
56
+ // We use this to guarantee that the "frozen" bit on the object will be
57
+ // remembered, even if the user drops their reference to this precise object.
58
+ void Arena_Pin(VALUE arena, VALUE obj);
59
+
60
+ // -----------------------------------------------------------------------------
61
+ // ObjectCache
62
+ // -----------------------------------------------------------------------------
63
+
64
+ // Global object cache from upb array/map/message/symtab to wrapper object.
65
+ //
66
+ // This is a conceptually "weak" cache, in that it does not prevent "val" from
67
+ // being collected (though in Ruby <2.7 is it effectively strong, due to
68
+ // implementation limitations).
69
+
70
+ // Tries to add a new entry to the cache, returning the newly installed value or
71
+ // the pre-existing entry.
72
+ VALUE ObjectCache_TryAdd(const void* key, VALUE val);
73
+
74
+ // Returns the cached object for this key, if any. Otherwise returns Qnil.
75
+ VALUE ObjectCache_Get(const void* key);
76
+
77
+ // -----------------------------------------------------------------------------
78
+ // StringBuilder, for inspect
79
+ // -----------------------------------------------------------------------------
80
+
81
+ struct StringBuilder;
82
+ typedef struct StringBuilder StringBuilder;
83
+
84
+ StringBuilder* StringBuilder_New();
85
+ void StringBuilder_Free(StringBuilder* b);
86
+ void StringBuilder_Printf(StringBuilder* b, const char* fmt, ...);
87
+ VALUE StringBuilder_ToRubyString(StringBuilder* b);
88
+
89
+ void StringBuilder_PrintMsgval(StringBuilder* b, upb_MessageValue val,
90
+ TypeInfo info);
91
+
92
+ // -----------------------------------------------------------------------------
93
+ // Utilities.
94
+ // -----------------------------------------------------------------------------
95
+
96
+ extern VALUE cTypeError;
97
+
98
+ #ifdef NDEBUG
99
+ #define PBRUBY_ASSERT(expr) \
100
+ do { \
101
+ } while (false && (expr))
102
+ #else
103
+ #define PBRUBY_ASSERT(expr) \
104
+ if (!(expr)) \
105
+ rb_bug("Assertion failed at %s:%d, expr: %s", __FILE__, __LINE__, #expr)
106
+ #endif
107
+
108
+ #define PBRUBY_MAX(x, y) (((x) > (y)) ? (x) : (y))
109
+
110
+ #define UPB_UNUSED(var) (void)var
111
+
112
+ #endif // __GOOGLE_PROTOBUF_RUBY_PROTOBUF_H__