storage.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 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. #include "protobuf.h"
  31. #include <math.h>
  32. #include <ruby/encoding.h>
  33. // -----------------------------------------------------------------------------
  34. // Ruby <-> native slot management.
  35. // -----------------------------------------------------------------------------
  36. #define DEREF(memory, type) *(type*)(memory)
  37. size_t native_slot_size(upb_fieldtype_t type) {
  38. switch (type) {
  39. case UPB_TYPE_FLOAT: return 4;
  40. case UPB_TYPE_DOUBLE: return 8;
  41. case UPB_TYPE_BOOL: return 1;
  42. case UPB_TYPE_STRING: return sizeof(VALUE);
  43. case UPB_TYPE_BYTES: return sizeof(VALUE);
  44. case UPB_TYPE_MESSAGE: return sizeof(VALUE);
  45. case UPB_TYPE_ENUM: return 4;
  46. case UPB_TYPE_INT32: return 4;
  47. case UPB_TYPE_INT64: return 8;
  48. case UPB_TYPE_UINT32: return 4;
  49. case UPB_TYPE_UINT64: return 8;
  50. default: return 0;
  51. }
  52. }
  53. static bool is_ruby_num(VALUE value) {
  54. return (TYPE(value) == T_FLOAT ||
  55. TYPE(value) == T_FIXNUM ||
  56. TYPE(value) == T_BIGNUM);
  57. }
  58. void native_slot_check_int_range_precision(upb_fieldtype_t type, VALUE val) {
  59. if (!is_ruby_num(val)) {
  60. rb_raise(rb_eTypeError, "Expected number type for integral field.");
  61. }
  62. // NUM2{INT,UINT,LL,ULL} macros do the appropriate range checks on upper
  63. // bound; we just need to do precision checks (i.e., disallow rounding) and
  64. // check for < 0 on unsigned types.
  65. if (TYPE(val) == T_FLOAT) {
  66. double dbl_val = NUM2DBL(val);
  67. if (floor(dbl_val) != dbl_val) {
  68. rb_raise(rb_eRangeError,
  69. "Non-integral floating point value assigned to integer field.");
  70. }
  71. }
  72. if (type == UPB_TYPE_UINT32 || type == UPB_TYPE_UINT64) {
  73. if (NUM2DBL(val) < 0) {
  74. rb_raise(rb_eRangeError,
  75. "Assigning negative value to unsigned integer field.");
  76. }
  77. }
  78. }
  79. void native_slot_validate_string_encoding(upb_fieldtype_t type, VALUE value) {
  80. bool bad_encoding = false;
  81. rb_encoding* string_encoding = rb_enc_from_index(ENCODING_GET(value));
  82. if (type == UPB_TYPE_STRING) {
  83. bad_encoding =
  84. string_encoding != kRubyStringUtf8Encoding &&
  85. string_encoding != kRubyStringASCIIEncoding;
  86. } else {
  87. bad_encoding =
  88. string_encoding != kRubyString8bitEncoding;
  89. }
  90. // Check that encoding is UTF-8 or ASCII (for string fields) or ASCII-8BIT
  91. // (for bytes fields).
  92. if (bad_encoding) {
  93. rb_raise(rb_eTypeError, "Encoding for '%s' fields must be %s (was %s)",
  94. (type == UPB_TYPE_STRING) ? "string" : "bytes",
  95. (type == UPB_TYPE_STRING) ? "UTF-8 or ASCII" : "ASCII-8BIT",
  96. rb_enc_name(string_encoding));
  97. }
  98. }
  99. void native_slot_set(upb_fieldtype_t type, VALUE type_class,
  100. void* memory, VALUE value) {
  101. switch (type) {
  102. case UPB_TYPE_FLOAT:
  103. if (!is_ruby_num(value)) {
  104. rb_raise(rb_eTypeError, "Expected number type for float field.");
  105. }
  106. DEREF(memory, float) = NUM2DBL(value);
  107. break;
  108. case UPB_TYPE_DOUBLE:
  109. if (!is_ruby_num(value)) {
  110. rb_raise(rb_eTypeError, "Expected number type for double field.");
  111. }
  112. DEREF(memory, double) = NUM2DBL(value);
  113. break;
  114. case UPB_TYPE_BOOL: {
  115. int8_t val = -1;
  116. if (value == Qtrue) {
  117. val = 1;
  118. } else if (value == Qfalse) {
  119. val = 0;
  120. } else {
  121. rb_raise(rb_eTypeError, "Invalid argument for boolean field.");
  122. }
  123. DEREF(memory, int8_t) = val;
  124. break;
  125. }
  126. case UPB_TYPE_STRING:
  127. case UPB_TYPE_BYTES: {
  128. if (CLASS_OF(value) != rb_cString) {
  129. rb_raise(rb_eTypeError, "Invalid argument for string field.");
  130. }
  131. native_slot_validate_string_encoding(type, value);
  132. DEREF(memory, VALUE) = value;
  133. break;
  134. }
  135. case UPB_TYPE_MESSAGE: {
  136. if (CLASS_OF(value) != type_class) {
  137. rb_raise(rb_eTypeError,
  138. "Invalid type %s to assign to submessage field.",
  139. rb_class2name(CLASS_OF(value)));
  140. }
  141. DEREF(memory, VALUE) = value;
  142. break;
  143. }
  144. case UPB_TYPE_ENUM: {
  145. if (!is_ruby_num(value) && TYPE(value) != T_SYMBOL) {
  146. rb_raise(rb_eTypeError,
  147. "Expected number or symbol type for enum field.");
  148. }
  149. int32_t int_val = 0;
  150. if (TYPE(value) == T_SYMBOL) {
  151. // Ensure that the given symbol exists in the enum module.
  152. VALUE lookup = rb_funcall(type_class, rb_intern("resolve"), 1, value);
  153. if (lookup == Qnil) {
  154. rb_raise(rb_eRangeError, "Unknown symbol value for enum field.");
  155. } else {
  156. int_val = NUM2INT(lookup);
  157. }
  158. } else {
  159. native_slot_check_int_range_precision(UPB_TYPE_INT32, value);
  160. int_val = NUM2INT(value);
  161. }
  162. DEREF(memory, int32_t) = int_val;
  163. break;
  164. }
  165. case UPB_TYPE_INT32:
  166. case UPB_TYPE_INT64:
  167. case UPB_TYPE_UINT32:
  168. case UPB_TYPE_UINT64:
  169. native_slot_check_int_range_precision(type, value);
  170. switch (type) {
  171. case UPB_TYPE_INT32:
  172. DEREF(memory, int32_t) = NUM2INT(value);
  173. break;
  174. case UPB_TYPE_INT64:
  175. DEREF(memory, int64_t) = NUM2LL(value);
  176. break;
  177. case UPB_TYPE_UINT32:
  178. DEREF(memory, uint32_t) = NUM2UINT(value);
  179. break;
  180. case UPB_TYPE_UINT64:
  181. DEREF(memory, uint64_t) = NUM2ULL(value);
  182. break;
  183. default:
  184. break;
  185. }
  186. break;
  187. default:
  188. break;
  189. }
  190. }
  191. VALUE native_slot_get(upb_fieldtype_t type,
  192. VALUE type_class,
  193. const void* memory) {
  194. switch (type) {
  195. case UPB_TYPE_FLOAT:
  196. return DBL2NUM(DEREF(memory, float));
  197. case UPB_TYPE_DOUBLE:
  198. return DBL2NUM(DEREF(memory, double));
  199. case UPB_TYPE_BOOL:
  200. return DEREF(memory, int8_t) ? Qtrue : Qfalse;
  201. case UPB_TYPE_STRING:
  202. case UPB_TYPE_BYTES:
  203. case UPB_TYPE_MESSAGE:
  204. return DEREF(memory, VALUE);
  205. case UPB_TYPE_ENUM: {
  206. int32_t val = DEREF(memory, int32_t);
  207. VALUE symbol = enum_lookup(type_class, INT2NUM(val));
  208. if (symbol == Qnil) {
  209. return INT2NUM(val);
  210. } else {
  211. return symbol;
  212. }
  213. }
  214. case UPB_TYPE_INT32:
  215. return INT2NUM(DEREF(memory, int32_t));
  216. case UPB_TYPE_INT64:
  217. return LL2NUM(DEREF(memory, int64_t));
  218. case UPB_TYPE_UINT32:
  219. return UINT2NUM(DEREF(memory, uint32_t));
  220. case UPB_TYPE_UINT64:
  221. return ULL2NUM(DEREF(memory, uint64_t));
  222. default:
  223. return Qnil;
  224. }
  225. }
  226. void native_slot_init(upb_fieldtype_t type, void* memory) {
  227. switch (type) {
  228. case UPB_TYPE_FLOAT:
  229. DEREF(memory, float) = 0.0;
  230. break;
  231. case UPB_TYPE_DOUBLE:
  232. DEREF(memory, double) = 0.0;
  233. break;
  234. case UPB_TYPE_BOOL:
  235. DEREF(memory, int8_t) = 0;
  236. break;
  237. case UPB_TYPE_STRING:
  238. case UPB_TYPE_BYTES:
  239. DEREF(memory, VALUE) = rb_str_new2("");
  240. rb_enc_associate(DEREF(memory, VALUE), (type == UPB_TYPE_BYTES) ?
  241. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  242. break;
  243. case UPB_TYPE_MESSAGE:
  244. DEREF(memory, VALUE) = Qnil;
  245. break;
  246. case UPB_TYPE_ENUM:
  247. case UPB_TYPE_INT32:
  248. DEREF(memory, int32_t) = 0;
  249. break;
  250. case UPB_TYPE_INT64:
  251. DEREF(memory, int64_t) = 0;
  252. break;
  253. case UPB_TYPE_UINT32:
  254. DEREF(memory, uint32_t) = 0;
  255. break;
  256. case UPB_TYPE_UINT64:
  257. DEREF(memory, uint64_t) = 0;
  258. break;
  259. default:
  260. break;
  261. }
  262. }
  263. void native_slot_mark(upb_fieldtype_t type, void* memory) {
  264. switch (type) {
  265. case UPB_TYPE_STRING:
  266. case UPB_TYPE_BYTES:
  267. case UPB_TYPE_MESSAGE:
  268. rb_gc_mark(DEREF(memory, VALUE));
  269. break;
  270. default:
  271. break;
  272. }
  273. }
  274. void native_slot_dup(upb_fieldtype_t type, void* to, void* from) {
  275. memcpy(to, from, native_slot_size(type));
  276. }
  277. void native_slot_deep_copy(upb_fieldtype_t type, void* to, void* from) {
  278. switch (type) {
  279. case UPB_TYPE_STRING:
  280. case UPB_TYPE_BYTES: {
  281. VALUE from_val = DEREF(from, VALUE);
  282. DEREF(to, VALUE) = (from_val != Qnil) ?
  283. rb_funcall(from_val, rb_intern("dup"), 0) : Qnil;
  284. break;
  285. }
  286. case UPB_TYPE_MESSAGE: {
  287. VALUE from_val = DEREF(from, VALUE);
  288. DEREF(to, VALUE) = (from_val != Qnil) ?
  289. Message_deep_copy(from_val) : Qnil;
  290. break;
  291. }
  292. default:
  293. memcpy(to, from, native_slot_size(type));
  294. }
  295. }
  296. bool native_slot_eq(upb_fieldtype_t type, void* mem1, void* mem2) {
  297. switch (type) {
  298. case UPB_TYPE_STRING:
  299. case UPB_TYPE_BYTES:
  300. case UPB_TYPE_MESSAGE: {
  301. VALUE val1 = DEREF(mem1, VALUE);
  302. VALUE val2 = DEREF(mem2, VALUE);
  303. VALUE ret = rb_funcall(val1, rb_intern("=="), 1, val2);
  304. return ret == Qtrue;
  305. }
  306. default:
  307. return !memcmp(mem1, mem2, native_slot_size(type));
  308. }
  309. }
  310. // -----------------------------------------------------------------------------
  311. // Map field utilities.
  312. // -----------------------------------------------------------------------------
  313. bool is_map_field(const upb_fielddef* field) {
  314. if (upb_fielddef_label(field) != UPB_LABEL_REPEATED ||
  315. upb_fielddef_type(field) != UPB_TYPE_MESSAGE) {
  316. return false;
  317. }
  318. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  319. return upb_msgdef_mapentry(subdef);
  320. }
  321. const upb_fielddef* map_field_key(const upb_fielddef* field) {
  322. assert(is_map_field(field));
  323. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  324. return map_entry_key(subdef);
  325. }
  326. const upb_fielddef* map_field_value(const upb_fielddef* field) {
  327. assert(is_map_field(field));
  328. const upb_msgdef* subdef = upb_fielddef_msgsubdef(field);
  329. return map_entry_value(subdef);
  330. }
  331. const upb_fielddef* map_entry_key(const upb_msgdef* msgdef) {
  332. const upb_fielddef* key_field = upb_msgdef_itof(msgdef, MAP_KEY_FIELD);
  333. assert(key_field != NULL);
  334. return key_field;
  335. }
  336. const upb_fielddef* map_entry_value(const upb_msgdef* msgdef) {
  337. const upb_fielddef* value_field = upb_msgdef_itof(msgdef, MAP_VALUE_FIELD);
  338. assert(value_field != NULL);
  339. return value_field;
  340. }
  341. // -----------------------------------------------------------------------------
  342. // Memory layout management.
  343. // -----------------------------------------------------------------------------
  344. MessageLayout* create_layout(const upb_msgdef* msgdef) {
  345. MessageLayout* layout = ALLOC(MessageLayout);
  346. int nfields = upb_msgdef_numfields(msgdef);
  347. layout->offsets = ALLOC_N(size_t, nfields);
  348. upb_msg_iter it;
  349. size_t off = 0;
  350. for (upb_msg_begin(&it, msgdef); !upb_msg_done(&it); upb_msg_next(&it)) {
  351. const upb_fielddef* field = upb_msg_iter_field(&it);
  352. size_t field_size = 0;
  353. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  354. field_size = sizeof(VALUE);
  355. } else {
  356. field_size = native_slot_size(upb_fielddef_type(field));
  357. }
  358. // align current offset
  359. off = (off + field_size - 1) & ~(field_size - 1);
  360. layout->offsets[upb_fielddef_index(field)] = off;
  361. off += field_size;
  362. }
  363. layout->size = off;
  364. layout->msgdef = msgdef;
  365. upb_msgdef_ref(layout->msgdef, &layout->msgdef);
  366. return layout;
  367. }
  368. void free_layout(MessageLayout* layout) {
  369. xfree(layout->offsets);
  370. upb_msgdef_unref(layout->msgdef, &layout->msgdef);
  371. xfree(layout);
  372. }
  373. VALUE field_type_class(const upb_fielddef* field) {
  374. VALUE type_class = Qnil;
  375. if (upb_fielddef_type(field) == UPB_TYPE_MESSAGE) {
  376. VALUE submsgdesc =
  377. get_def_obj(upb_fielddef_subdef(field));
  378. type_class = Descriptor_msgclass(submsgdesc);
  379. } else if (upb_fielddef_type(field) == UPB_TYPE_ENUM) {
  380. VALUE subenumdesc =
  381. get_def_obj(upb_fielddef_subdef(field));
  382. type_class = EnumDescriptor_enummodule(subenumdesc);
  383. }
  384. return type_class;
  385. }
  386. VALUE layout_get(MessageLayout* layout,
  387. const void* storage,
  388. const upb_fielddef* field) {
  389. void* memory = ((uint8_t *)storage) +
  390. layout->offsets[upb_fielddef_index(field)];
  391. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  392. return *((VALUE *)memory);
  393. } else {
  394. return native_slot_get(upb_fielddef_type(field),
  395. field_type_class(field),
  396. memory);
  397. }
  398. }
  399. static void check_repeated_field_type(VALUE val, const upb_fielddef* field) {
  400. assert(upb_fielddef_label(field) == UPB_LABEL_REPEATED);
  401. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  402. RTYPEDDATA_TYPE(val) != &RepeatedField_type) {
  403. rb_raise(rb_eTypeError, "Expected repeated field array");
  404. }
  405. RepeatedField* self = ruby_to_RepeatedField(val);
  406. if (self->field_type != upb_fielddef_type(field)) {
  407. rb_raise(rb_eTypeError, "Repeated field array has wrong element type");
  408. }
  409. if (self->field_type == UPB_TYPE_MESSAGE ||
  410. self->field_type == UPB_TYPE_ENUM) {
  411. if (self->field_type_class !=
  412. get_def_obj(upb_fielddef_subdef(field))) {
  413. rb_raise(rb_eTypeError,
  414. "Repeated field array has wrong message/enum class");
  415. }
  416. }
  417. }
  418. static void check_map_field_type(VALUE val, const upb_fielddef* field) {
  419. assert(is_map_field(field));
  420. const upb_fielddef* key_field = map_field_key(field);
  421. const upb_fielddef* value_field = map_field_value(field);
  422. if (!RB_TYPE_P(val, T_DATA) || !RTYPEDDATA_P(val) ||
  423. RTYPEDDATA_TYPE(val) != &Map_type) {
  424. rb_raise(rb_eTypeError, "Expected Map instance");
  425. }
  426. Map* self = ruby_to_Map(val);
  427. if (self->key_type != upb_fielddef_type(key_field)) {
  428. rb_raise(rb_eTypeError, "Map key type does not match field's key type");
  429. }
  430. if (self->value_type != upb_fielddef_type(value_field)) {
  431. rb_raise(rb_eTypeError, "Map value type does not match field's value type");
  432. }
  433. if (upb_fielddef_type(value_field) == UPB_TYPE_MESSAGE ||
  434. upb_fielddef_type(value_field) == UPB_TYPE_ENUM) {
  435. if (self->value_type_class !=
  436. get_def_obj(upb_fielddef_subdef(value_field))) {
  437. rb_raise(rb_eTypeError,
  438. "Map value type has wrong message/enum class");
  439. }
  440. }
  441. }
  442. void layout_set(MessageLayout* layout,
  443. void* storage,
  444. const upb_fielddef* field,
  445. VALUE val) {
  446. void* memory = ((uint8_t *)storage) +
  447. layout->offsets[upb_fielddef_index(field)];
  448. if (is_map_field(field)) {
  449. check_map_field_type(val, field);
  450. DEREF(memory, VALUE) = val;
  451. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  452. check_repeated_field_type(val, field);
  453. DEREF(memory, VALUE) = val;
  454. } else {
  455. native_slot_set(upb_fielddef_type(field), field_type_class(field),
  456. memory, val);
  457. }
  458. }
  459. void layout_init(MessageLayout* layout,
  460. void* storage) {
  461. upb_msg_iter it;
  462. for (upb_msg_begin(&it, layout->msgdef);
  463. !upb_msg_done(&it);
  464. upb_msg_next(&it)) {
  465. const upb_fielddef* field = upb_msg_iter_field(&it);
  466. void* memory = ((uint8_t *)storage) +
  467. layout->offsets[upb_fielddef_index(field)];
  468. if (is_map_field(field)) {
  469. VALUE map = Qnil;
  470. const upb_fielddef* key_field = map_field_key(field);
  471. const upb_fielddef* value_field = map_field_value(field);
  472. VALUE type_class = field_type_class(value_field);
  473. if (type_class != Qnil) {
  474. VALUE args[3] = {
  475. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  476. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  477. type_class,
  478. };
  479. map = rb_class_new_instance(3, args, cMap);
  480. } else {
  481. VALUE args[2] = {
  482. fieldtype_to_ruby(upb_fielddef_type(key_field)),
  483. fieldtype_to_ruby(upb_fielddef_type(value_field)),
  484. };
  485. map = rb_class_new_instance(2, args, cMap);
  486. }
  487. DEREF(memory, VALUE) = map;
  488. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  489. VALUE ary = Qnil;
  490. VALUE type_class = field_type_class(field);
  491. if (type_class != Qnil) {
  492. VALUE args[2] = {
  493. fieldtype_to_ruby(upb_fielddef_type(field)),
  494. type_class,
  495. };
  496. ary = rb_class_new_instance(2, args, cRepeatedField);
  497. } else {
  498. VALUE args[1] = { fieldtype_to_ruby(upb_fielddef_type(field)) };
  499. ary = rb_class_new_instance(1, args, cRepeatedField);
  500. }
  501. DEREF(memory, VALUE) = ary;
  502. } else {
  503. native_slot_init(upb_fielddef_type(field), memory);
  504. }
  505. }
  506. }
  507. void layout_mark(MessageLayout* layout, void* storage) {
  508. upb_msg_iter it;
  509. for (upb_msg_begin(&it, layout->msgdef);
  510. !upb_msg_done(&it);
  511. upb_msg_next(&it)) {
  512. const upb_fielddef* field = upb_msg_iter_field(&it);
  513. void* memory = ((uint8_t *)storage) +
  514. layout->offsets[upb_fielddef_index(field)];
  515. if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  516. rb_gc_mark(DEREF(memory, VALUE));
  517. } else {
  518. native_slot_mark(upb_fielddef_type(field), memory);
  519. }
  520. }
  521. }
  522. void layout_dup(MessageLayout* layout, void* to, void* from) {
  523. upb_msg_iter it;
  524. for (upb_msg_begin(&it, layout->msgdef);
  525. !upb_msg_done(&it);
  526. upb_msg_next(&it)) {
  527. const upb_fielddef* field = upb_msg_iter_field(&it);
  528. void* to_memory = ((uint8_t *)to) +
  529. layout->offsets[upb_fielddef_index(field)];
  530. void* from_memory = ((uint8_t *)from) +
  531. layout->offsets[upb_fielddef_index(field)];
  532. if (is_map_field(field)) {
  533. DEREF(to_memory, VALUE) = Map_dup(DEREF(from_memory, VALUE));
  534. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  535. DEREF(to_memory, VALUE) = RepeatedField_dup(DEREF(from_memory, VALUE));
  536. } else {
  537. native_slot_dup(upb_fielddef_type(field), to_memory, from_memory);
  538. }
  539. }
  540. }
  541. void layout_deep_copy(MessageLayout* layout, void* to, void* from) {
  542. upb_msg_iter it;
  543. for (upb_msg_begin(&it, layout->msgdef);
  544. !upb_msg_done(&it);
  545. upb_msg_next(&it)) {
  546. const upb_fielddef* field = upb_msg_iter_field(&it);
  547. void* to_memory = ((uint8_t *)to) +
  548. layout->offsets[upb_fielddef_index(field)];
  549. void* from_memory = ((uint8_t *)from) +
  550. layout->offsets[upb_fielddef_index(field)];
  551. if (is_map_field(field)) {
  552. DEREF(to_memory, VALUE) =
  553. Map_deep_copy(DEREF(from_memory, VALUE));
  554. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  555. DEREF(to_memory, VALUE) =
  556. RepeatedField_deep_copy(DEREF(from_memory, VALUE));
  557. } else {
  558. native_slot_deep_copy(upb_fielddef_type(field), to_memory, from_memory);
  559. }
  560. }
  561. }
  562. VALUE layout_eq(MessageLayout* layout, void* msg1, void* msg2) {
  563. upb_msg_iter it;
  564. for (upb_msg_begin(&it, layout->msgdef);
  565. !upb_msg_done(&it);
  566. upb_msg_next(&it)) {
  567. const upb_fielddef* field = upb_msg_iter_field(&it);
  568. void* msg1_memory = ((uint8_t *)msg1) +
  569. layout->offsets[upb_fielddef_index(field)];
  570. void* msg2_memory = ((uint8_t *)msg2) +
  571. layout->offsets[upb_fielddef_index(field)];
  572. if (is_map_field(field)) {
  573. return Map_eq(DEREF(msg1_memory, VALUE),
  574. DEREF(msg2_memory, VALUE));
  575. } else if (upb_fielddef_label(field) == UPB_LABEL_REPEATED) {
  576. return RepeatedField_eq(DEREF(msg1_memory, VALUE),
  577. DEREF(msg2_memory, VALUE));
  578. } else {
  579. if (!native_slot_eq(upb_fielddef_type(field),
  580. msg1_memory, msg2_memory)) {
  581. return Qfalse;
  582. }
  583. }
  584. }
  585. return Qtrue;
  586. }
  587. VALUE layout_hash(MessageLayout* layout, void* storage) {
  588. upb_msg_iter it;
  589. st_index_t h = rb_hash_start(0);
  590. VALUE hash_sym = rb_intern("hash");
  591. for (upb_msg_begin(&it, layout->msgdef);
  592. !upb_msg_done(&it);
  593. upb_msg_next(&it)) {
  594. const upb_fielddef* field = upb_msg_iter_field(&it);
  595. VALUE field_val = layout_get(layout, storage, field);
  596. h = rb_hash_uint(h, NUM2LONG(rb_funcall(field_val, hash_sym, 0)));
  597. }
  598. h = rb_hash_end(h);
  599. return INT2FIX(h);
  600. }
  601. VALUE layout_inspect(MessageLayout* layout, void* storage) {
  602. VALUE str = rb_str_new2("");
  603. upb_msg_iter it;
  604. bool first = true;
  605. for (upb_msg_begin(&it, layout->msgdef);
  606. !upb_msg_done(&it);
  607. upb_msg_next(&it)) {
  608. const upb_fielddef* field = upb_msg_iter_field(&it);
  609. VALUE field_val = layout_get(layout, storage, field);
  610. if (!first) {
  611. str = rb_str_cat2(str, ", ");
  612. } else {
  613. first = false;
  614. }
  615. str = rb_str_cat2(str, upb_fielddef_name(field));
  616. str = rb_str_cat2(str, ": ");
  617. str = rb_str_append(str, rb_funcall(field_val, rb_intern("inspect"), 0));
  618. }
  619. return str;
  620. }