repeated_field.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. // -----------------------------------------------------------------------------
  32. // Repeated field container type.
  33. // -----------------------------------------------------------------------------
  34. const rb_data_type_t RepeatedField_type = {
  35. "Google::Protobuf::RepeatedField",
  36. { RepeatedField_mark, RepeatedField_free, NULL },
  37. };
  38. VALUE cRepeatedField;
  39. RepeatedField* ruby_to_RepeatedField(VALUE _self) {
  40. RepeatedField* self;
  41. TypedData_Get_Struct(_self, RepeatedField, &RepeatedField_type, self);
  42. return self;
  43. }
  44. static int index_position(VALUE _index, RepeatedField* repeated_field) {
  45. int index = NUM2INT(_index);
  46. if (index < 0 && repeated_field->size > 0) {
  47. index = repeated_field->size + index;
  48. }
  49. return index;
  50. }
  51. VALUE RepeatedField_subarray(VALUE _self, long beg, long len) {
  52. RepeatedField* self = ruby_to_RepeatedField(_self);
  53. int element_size = native_slot_size(self->field_type);
  54. upb_fieldtype_t field_type = self->field_type;
  55. VALUE field_type_class = self->field_type_class;
  56. size_t off = beg * element_size;
  57. VALUE ary = rb_ary_new2(len);
  58. for (int i = beg; i < beg + len; i++, off += element_size) {
  59. void* mem = ((uint8_t *)self->elements) + off;
  60. VALUE elem = native_slot_get(field_type, field_type_class, mem);
  61. rb_ary_push(ary, elem);
  62. }
  63. return ary;
  64. }
  65. /*
  66. * call-seq:
  67. * RepeatedField.each(&block)
  68. *
  69. * Invokes the block once for each element of the repeated field. RepeatedField
  70. * also includes Enumerable; combined with this method, the repeated field thus
  71. * acts like an ordinary Ruby sequence.
  72. */
  73. VALUE RepeatedField_each(VALUE _self) {
  74. RepeatedField* self = ruby_to_RepeatedField(_self);
  75. upb_fieldtype_t field_type = self->field_type;
  76. VALUE field_type_class = self->field_type_class;
  77. int element_size = native_slot_size(field_type);
  78. size_t off = 0;
  79. for (int i = 0; i < self->size; i++, off += element_size) {
  80. void* memory = (void *) (((uint8_t *)self->elements) + off);
  81. VALUE val = native_slot_get(field_type, field_type_class, memory);
  82. rb_yield(val);
  83. }
  84. return _self;
  85. }
  86. /*
  87. * call-seq:
  88. * RepeatedField.[](index) => value
  89. *
  90. * Accesses the element at the given index. Returns nil on out-of-bounds
  91. */
  92. VALUE RepeatedField_index(int argc, VALUE* argv, VALUE _self) {
  93. RepeatedField* self = ruby_to_RepeatedField(_self);
  94. int element_size = native_slot_size(self->field_type);
  95. upb_fieldtype_t field_type = self->field_type;
  96. VALUE field_type_class = self->field_type_class;
  97. VALUE arg = argv[0];
  98. long beg, len;
  99. if (argc == 1){
  100. if (FIXNUM_P(arg)) {
  101. /* standard case */
  102. int index = index_position(argv[0], self);
  103. if (index < 0 || index >= self->size) {
  104. return Qnil;
  105. }
  106. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  107. return native_slot_get(field_type, field_type_class, memory);
  108. }else{
  109. /* check if idx is Range */
  110. size_t off;
  111. switch (rb_range_beg_len(arg, &beg, &len, self->size, 0)) {
  112. case Qfalse:
  113. break;
  114. case Qnil:
  115. return Qnil;
  116. default:
  117. return RepeatedField_subarray(_self, beg, len);
  118. }
  119. }
  120. }
  121. /* assume 2 arguments */
  122. beg = NUM2LONG(argv[0]);
  123. len = NUM2LONG(argv[1]);
  124. if (beg < 0) {
  125. beg += self->size;
  126. }
  127. if (beg >= self->size) {
  128. return Qnil;
  129. }
  130. return RepeatedField_subarray(_self, beg, len);
  131. }
  132. /*
  133. * call-seq:
  134. * RepeatedField.[]=(index, value)
  135. *
  136. * Sets the element at the given index. On out-of-bounds assignments, extends
  137. * the array and fills the hole (if any) with default values.
  138. */
  139. VALUE RepeatedField_index_set(VALUE _self, VALUE _index, VALUE val) {
  140. RepeatedField* self = ruby_to_RepeatedField(_self);
  141. upb_fieldtype_t field_type = self->field_type;
  142. VALUE field_type_class = self->field_type_class;
  143. int element_size = native_slot_size(field_type);
  144. int index = index_position(_index, self);
  145. if (index < 0 || index >= (INT_MAX - 1)) {
  146. return Qnil;
  147. }
  148. if (index >= self->size) {
  149. RepeatedField_reserve(self, index + 1);
  150. upb_fieldtype_t field_type = self->field_type;
  151. int element_size = native_slot_size(field_type);
  152. for (int i = self->size; i <= index; i++) {
  153. void* elem = (void *)(((uint8_t *)self->elements) + i * element_size);
  154. native_slot_init(field_type, elem);
  155. }
  156. self->size = index + 1;
  157. }
  158. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  159. native_slot_set(field_type, field_type_class, memory, val);
  160. return Qnil;
  161. }
  162. static int kInitialSize = 8;
  163. void RepeatedField_reserve(RepeatedField* self, int new_size) {
  164. if (new_size <= self->capacity) {
  165. return;
  166. }
  167. if (self->capacity == 0) {
  168. self->capacity = kInitialSize;
  169. }
  170. while (self->capacity < new_size) {
  171. self->capacity *= 2;
  172. }
  173. void* old_elems = self->elements;
  174. int elem_size = native_slot_size(self->field_type);
  175. self->elements = ALLOC_N(uint8_t, elem_size * self->capacity);
  176. if (old_elems != NULL) {
  177. memcpy(self->elements, old_elems, self->size * elem_size);
  178. xfree(old_elems);
  179. }
  180. }
  181. /*
  182. * call-seq:
  183. * RepeatedField.push(value)
  184. *
  185. * Adds a new element to the repeated field.
  186. */
  187. VALUE RepeatedField_push(VALUE _self, VALUE val) {
  188. RepeatedField* self = ruby_to_RepeatedField(_self);
  189. upb_fieldtype_t field_type = self->field_type;
  190. int element_size = native_slot_size(field_type);
  191. RepeatedField_reserve(self, self->size + 1);
  192. int index = self->size;
  193. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  194. native_slot_set(field_type, self->field_type_class, memory, val);
  195. // native_slot_set may raise an error; bump index only after set.
  196. self->size++;
  197. return _self;
  198. }
  199. // Used by parsing handlers.
  200. void RepeatedField_push_native(VALUE _self, void* data) {
  201. RepeatedField* self = ruby_to_RepeatedField(_self);
  202. upb_fieldtype_t field_type = self->field_type;
  203. int element_size = native_slot_size(field_type);
  204. RepeatedField_reserve(self, self->size + 1);
  205. int index = self->size;
  206. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  207. memcpy(memory, data, element_size);
  208. self->size++;
  209. }
  210. void* RepeatedField_index_native(VALUE _self, int index) {
  211. RepeatedField* self = ruby_to_RepeatedField(_self);
  212. upb_fieldtype_t field_type = self->field_type;
  213. int element_size = native_slot_size(field_type);
  214. return ((uint8_t *)self->elements) + index * element_size;
  215. }
  216. /*
  217. * Private ruby method, used by RepeatedField.pop
  218. */
  219. VALUE RepeatedField_pop_one(VALUE _self) {
  220. RepeatedField* self = ruby_to_RepeatedField(_self);
  221. upb_fieldtype_t field_type = self->field_type;
  222. VALUE field_type_class = self->field_type_class;
  223. int element_size = native_slot_size(field_type);
  224. if (self->size == 0) {
  225. return Qnil;
  226. }
  227. int index = self->size - 1;
  228. void* memory = (void *) (((uint8_t *)self->elements) + index * element_size);
  229. VALUE ret = native_slot_get(field_type, field_type_class, memory);
  230. self->size--;
  231. return ret;
  232. }
  233. /*
  234. * call-seq:
  235. * RepeatedField.replace(list)
  236. *
  237. * Replaces the contents of the repeated field with the given list of elements.
  238. */
  239. VALUE RepeatedField_replace(VALUE _self, VALUE list) {
  240. RepeatedField* self = ruby_to_RepeatedField(_self);
  241. Check_Type(list, T_ARRAY);
  242. self->size = 0;
  243. for (int i = 0; i < RARRAY_LEN(list); i++) {
  244. RepeatedField_push(_self, rb_ary_entry(list, i));
  245. }
  246. return list;
  247. }
  248. /*
  249. * call-seq:
  250. * RepeatedField.clear
  251. *
  252. * Clears (removes all elements from) this repeated field.
  253. */
  254. VALUE RepeatedField_clear(VALUE _self) {
  255. RepeatedField* self = ruby_to_RepeatedField(_self);
  256. self->size = 0;
  257. return _self;
  258. }
  259. /*
  260. * call-seq:
  261. * RepeatedField.length
  262. *
  263. * Returns the length of this repeated field.
  264. */
  265. VALUE RepeatedField_length(VALUE _self) {
  266. RepeatedField* self = ruby_to_RepeatedField(_self);
  267. return INT2NUM(self->size);
  268. }
  269. static VALUE RepeatedField_new_this_type(VALUE _self) {
  270. RepeatedField* self = ruby_to_RepeatedField(_self);
  271. VALUE new_rptfield = Qnil;
  272. VALUE element_type = fieldtype_to_ruby(self->field_type);
  273. if (self->field_type_class != Qnil) {
  274. new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
  275. element_type, self->field_type_class);
  276. } else {
  277. new_rptfield = rb_funcall(CLASS_OF(_self), rb_intern("new"), 1,
  278. element_type);
  279. }
  280. return new_rptfield;
  281. }
  282. /*
  283. * call-seq:
  284. * RepeatedField.dup => repeated_field
  285. *
  286. * Duplicates this repeated field with a shallow copy. References to all
  287. * non-primitive element objects (e.g., submessages) are shared.
  288. */
  289. VALUE RepeatedField_dup(VALUE _self) {
  290. RepeatedField* self = ruby_to_RepeatedField(_self);
  291. VALUE new_rptfield = RepeatedField_new_this_type(_self);
  292. RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
  293. RepeatedField_reserve(new_rptfield_self, self->size);
  294. upb_fieldtype_t field_type = self->field_type;
  295. size_t elem_size = native_slot_size(field_type);
  296. size_t off = 0;
  297. for (int i = 0; i < self->size; i++, off += elem_size) {
  298. void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
  299. void* from_mem = (uint8_t *)self->elements + off;
  300. native_slot_dup(field_type, to_mem, from_mem);
  301. new_rptfield_self->size++;
  302. }
  303. return new_rptfield;
  304. }
  305. // Internal only: used by Google::Protobuf.deep_copy.
  306. VALUE RepeatedField_deep_copy(VALUE _self) {
  307. RepeatedField* self = ruby_to_RepeatedField(_self);
  308. VALUE new_rptfield = RepeatedField_new_this_type(_self);
  309. RepeatedField* new_rptfield_self = ruby_to_RepeatedField(new_rptfield);
  310. RepeatedField_reserve(new_rptfield_self, self->size);
  311. upb_fieldtype_t field_type = self->field_type;
  312. size_t elem_size = native_slot_size(field_type);
  313. size_t off = 0;
  314. for (int i = 0; i < self->size; i++, off += elem_size) {
  315. void* to_mem = (uint8_t *)new_rptfield_self->elements + off;
  316. void* from_mem = (uint8_t *)self->elements + off;
  317. native_slot_deep_copy(field_type, to_mem, from_mem);
  318. new_rptfield_self->size++;
  319. }
  320. return new_rptfield;
  321. }
  322. /*
  323. * call-seq:
  324. * RepeatedField.to_ary => array
  325. *
  326. * Used when converted implicitly into array, e.g. compared to an Array.
  327. * Also called as a fallback of Object#to_a
  328. */
  329. VALUE RepeatedField_to_ary(VALUE _self) {
  330. RepeatedField* self = ruby_to_RepeatedField(_self);
  331. upb_fieldtype_t field_type = self->field_type;
  332. size_t elem_size = native_slot_size(field_type);
  333. size_t off = 0;
  334. VALUE ary = rb_ary_new2(self->size);
  335. for (int i = 0; i < self->size; i++, off += elem_size) {
  336. void* mem = ((uint8_t *)self->elements) + off;
  337. VALUE elem = native_slot_get(field_type, self->field_type_class, mem);
  338. rb_ary_push(ary, elem);
  339. }
  340. return ary;
  341. }
  342. /*
  343. * call-seq:
  344. * RepeatedField.==(other) => boolean
  345. *
  346. * Compares this repeated field to another. Repeated fields are equal if their
  347. * element types are equal, their lengths are equal, and each element is equal.
  348. * Elements are compared as per normal Ruby semantics, by calling their :==
  349. * methods (or performing a more efficient comparison for primitive types).
  350. *
  351. * Repeated fields with dissimilar element types are never equal, even if value
  352. * comparison (for example, between integers and floats) would have otherwise
  353. * indicated that every element has equal value.
  354. */
  355. VALUE RepeatedField_eq(VALUE _self, VALUE _other) {
  356. if (_self == _other) {
  357. return Qtrue;
  358. }
  359. RepeatedField* self = ruby_to_RepeatedField(_self);
  360. if (TYPE(_other) == T_ARRAY) {
  361. VALUE self_ary = RepeatedField_to_ary(_self);
  362. return rb_equal(self_ary, _other);
  363. }
  364. RepeatedField* other = ruby_to_RepeatedField(_other);
  365. if (self->field_type != other->field_type ||
  366. self->field_type_class != other->field_type_class ||
  367. self->size != other->size) {
  368. return Qfalse;
  369. }
  370. upb_fieldtype_t field_type = self->field_type;
  371. size_t elem_size = native_slot_size(field_type);
  372. size_t off = 0;
  373. for (int i = 0; i < self->size; i++, off += elem_size) {
  374. void* self_mem = ((uint8_t *)self->elements) + off;
  375. void* other_mem = ((uint8_t *)other->elements) + off;
  376. if (!native_slot_eq(field_type, self_mem, other_mem)) {
  377. return Qfalse;
  378. }
  379. }
  380. return Qtrue;
  381. }
  382. /*
  383. * call-seq:
  384. * RepeatedField.hash => hash_value
  385. *
  386. * Returns a hash value computed from this repeated field's elements.
  387. */
  388. VALUE RepeatedField_hash(VALUE _self) {
  389. RepeatedField* self = ruby_to_RepeatedField(_self);
  390. VALUE hash = LL2NUM(0);
  391. upb_fieldtype_t field_type = self->field_type;
  392. VALUE field_type_class = self->field_type_class;
  393. size_t elem_size = native_slot_size(field_type);
  394. size_t off = 0;
  395. for (int i = 0; i < self->size; i++, off += elem_size) {
  396. void* mem = ((uint8_t *)self->elements) + off;
  397. VALUE elem = native_slot_get(field_type, field_type_class, mem);
  398. hash = rb_funcall(hash, rb_intern("<<"), 1, INT2NUM(2));
  399. hash = rb_funcall(hash, rb_intern("^"), 1,
  400. rb_funcall(elem, rb_intern("hash"), 0));
  401. }
  402. return hash;
  403. }
  404. /*
  405. * call-seq:
  406. * RepeatedField.+(other) => repeated field
  407. *
  408. * Returns a new repeated field that contains the concatenated list of this
  409. * repeated field's elements and other's elements. The other (second) list may
  410. * be either another repeated field or a Ruby array.
  411. */
  412. VALUE RepeatedField_plus(VALUE _self, VALUE list) {
  413. VALUE dupped = RepeatedField_dup(_self);
  414. if (TYPE(list) == T_ARRAY) {
  415. for (int i = 0; i < RARRAY_LEN(list); i++) {
  416. VALUE elem = rb_ary_entry(list, i);
  417. RepeatedField_push(dupped, elem);
  418. }
  419. } else if (RB_TYPE_P(list, T_DATA) && RTYPEDDATA_P(list) &&
  420. RTYPEDDATA_TYPE(list) == &RepeatedField_type) {
  421. RepeatedField* self = ruby_to_RepeatedField(_self);
  422. RepeatedField* list_rptfield = ruby_to_RepeatedField(list);
  423. if (self->field_type != list_rptfield->field_type ||
  424. self->field_type_class != list_rptfield->field_type_class) {
  425. rb_raise(rb_eArgError,
  426. "Attempt to append RepeatedField with different element type.");
  427. }
  428. for (int i = 0; i < list_rptfield->size; i++) {
  429. void* mem = RepeatedField_index_native(list, i);
  430. RepeatedField_push_native(dupped, mem);
  431. }
  432. } else {
  433. rb_raise(rb_eArgError, "Unknown type appending to RepeatedField");
  434. }
  435. return dupped;
  436. }
  437. /*
  438. * call-seq:
  439. * RepeatedField.concat(other) => self
  440. *
  441. * concats the passed in array to self. Returns a Ruby array.
  442. */
  443. VALUE RepeatedField_concat(VALUE _self, VALUE list) {
  444. RepeatedField* self = ruby_to_RepeatedField(_self);
  445. Check_Type(list, T_ARRAY);
  446. for (int i = 0; i < RARRAY_LEN(list); i++) {
  447. RepeatedField_push(_self, rb_ary_entry(list, i));
  448. }
  449. return _self;
  450. }
  451. void validate_type_class(upb_fieldtype_t type, VALUE klass) {
  452. if (rb_iv_get(klass, kDescriptorInstanceVar) == Qnil) {
  453. rb_raise(rb_eArgError,
  454. "Type class has no descriptor. Please pass a "
  455. "class or enum as returned by the DescriptorPool.");
  456. }
  457. if (type == UPB_TYPE_MESSAGE) {
  458. VALUE desc = rb_iv_get(klass, kDescriptorInstanceVar);
  459. if (!RB_TYPE_P(desc, T_DATA) || !RTYPEDDATA_P(desc) ||
  460. RTYPEDDATA_TYPE(desc) != &_Descriptor_type) {
  461. rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
  462. }
  463. if (rb_get_alloc_func(klass) != &Message_alloc) {
  464. rb_raise(rb_eArgError,
  465. "Message class was not returned by the DescriptorPool.");
  466. }
  467. } else if (type == UPB_TYPE_ENUM) {
  468. VALUE enumdesc = rb_iv_get(klass, kDescriptorInstanceVar);
  469. if (!RB_TYPE_P(enumdesc, T_DATA) || !RTYPEDDATA_P(enumdesc) ||
  470. RTYPEDDATA_TYPE(enumdesc) != &_EnumDescriptor_type) {
  471. rb_raise(rb_eArgError, "Descriptor has an incorrect type.");
  472. }
  473. }
  474. }
  475. void RepeatedField_init_args(int argc, VALUE* argv,
  476. VALUE _self) {
  477. RepeatedField* self = ruby_to_RepeatedField(_self);
  478. VALUE ary = Qnil;
  479. if (argc < 1) {
  480. rb_raise(rb_eArgError, "Expected at least 1 argument.");
  481. }
  482. self->field_type = ruby_to_fieldtype(argv[0]);
  483. if (self->field_type == UPB_TYPE_MESSAGE ||
  484. self->field_type == UPB_TYPE_ENUM) {
  485. if (argc < 2) {
  486. rb_raise(rb_eArgError, "Expected at least 2 arguments for message/enum.");
  487. }
  488. self->field_type_class = argv[1];
  489. if (argc > 2) {
  490. ary = argv[2];
  491. }
  492. validate_type_class(self->field_type, self->field_type_class);
  493. } else {
  494. if (argc > 2) {
  495. rb_raise(rb_eArgError, "Too many arguments: expected 1 or 2.");
  496. }
  497. if (argc > 1) {
  498. ary = argv[1];
  499. }
  500. }
  501. if (ary != Qnil) {
  502. if (!RB_TYPE_P(ary, T_ARRAY)) {
  503. rb_raise(rb_eArgError, "Expected array as initialize argument");
  504. }
  505. for (int i = 0; i < RARRAY_LEN(ary); i++) {
  506. RepeatedField_push(_self, rb_ary_entry(ary, i));
  507. }
  508. }
  509. }
  510. // Mark, free, alloc, init and class setup functions.
  511. void RepeatedField_mark(void* _self) {
  512. RepeatedField* self = (RepeatedField*)_self;
  513. rb_gc_mark(self->field_type_class);
  514. upb_fieldtype_t field_type = self->field_type;
  515. int element_size = native_slot_size(field_type);
  516. for (int i = 0; i < self->size; i++) {
  517. void* memory = (((uint8_t *)self->elements) + i * element_size);
  518. native_slot_mark(self->field_type, memory);
  519. }
  520. }
  521. void RepeatedField_free(void* _self) {
  522. RepeatedField* self = (RepeatedField*)_self;
  523. xfree(self->elements);
  524. xfree(self);
  525. }
  526. /*
  527. * call-seq:
  528. * RepeatedField.new(type, type_class = nil, initial_elems = [])
  529. *
  530. * Creates a new repeated field. The provided type must be a Ruby symbol, and
  531. * can take on the same values as those accepted by FieldDescriptor#type=. If
  532. * the type is :message or :enum, type_class must be non-nil, and must be the
  533. * Ruby class or module returned by Descriptor#msgclass or
  534. * EnumDescriptor#enummodule, respectively. An initial list of elements may also
  535. * be provided.
  536. */
  537. VALUE RepeatedField_alloc(VALUE klass) {
  538. RepeatedField* self = ALLOC(RepeatedField);
  539. self->elements = NULL;
  540. self->size = 0;
  541. self->capacity = 0;
  542. self->field_type = -1;
  543. self->field_type_class = Qnil;
  544. VALUE ret = TypedData_Wrap_Struct(klass, &RepeatedField_type, self);
  545. return ret;
  546. }
  547. VALUE RepeatedField_init(int argc, VALUE* argv, VALUE self) {
  548. RepeatedField_init_args(argc, argv, self);
  549. return Qnil;
  550. }
  551. void RepeatedField_register(VALUE module) {
  552. VALUE klass = rb_define_class_under(
  553. module, "RepeatedField", rb_cObject);
  554. rb_define_alloc_func(klass, RepeatedField_alloc);
  555. cRepeatedField = klass;
  556. rb_gc_register_address(&cRepeatedField);
  557. rb_define_method(klass, "initialize",
  558. RepeatedField_init, -1);
  559. rb_define_method(klass, "each", RepeatedField_each, 0);
  560. rb_define_method(klass, "[]", RepeatedField_index, -1);
  561. rb_define_method(klass, "at", RepeatedField_index, -1);
  562. rb_define_method(klass, "[]=", RepeatedField_index_set, 2);
  563. rb_define_method(klass, "push", RepeatedField_push, 1);
  564. rb_define_method(klass, "<<", RepeatedField_push, 1);
  565. rb_define_private_method(klass, "pop_one", RepeatedField_pop_one, 0);
  566. rb_define_method(klass, "replace", RepeatedField_replace, 1);
  567. rb_define_method(klass, "clear", RepeatedField_clear, 0);
  568. rb_define_method(klass, "length", RepeatedField_length, 0);
  569. rb_define_method(klass, "size", RepeatedField_length, 0);
  570. rb_define_method(klass, "dup", RepeatedField_dup, 0);
  571. // Also define #clone so that we don't inherit Object#clone.
  572. rb_define_method(klass, "clone", RepeatedField_dup, 0);
  573. rb_define_method(klass, "==", RepeatedField_eq, 1);
  574. rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0);
  575. rb_define_method(klass, "hash", RepeatedField_hash, 0);
  576. rb_define_method(klass, "+", RepeatedField_plus, 1);
  577. rb_define_method(klass, "concat", RepeatedField_concat, 1);
  578. rb_include_module(klass, rb_mEnumerable);
  579. }