repeated_field.c 21 KB

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