repeated_field.h 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // RepeatedField and RepeatedPtrField are used by generated protocol message
  35. // classes to manipulate repeated fields. These classes are very similar to
  36. // STL's vector, but include a number of optimizations found to be useful
  37. // specifically in the case of Protocol Buffers. RepeatedPtrField is
  38. // particularly different from STL vector as it manages ownership of the
  39. // pointers that it contains.
  40. //
  41. // Typically, clients should not need to access RepeatedField objects directly,
  42. // but should instead use the accessor functions generated automatically by the
  43. // protocol compiler.
  44. //
  45. // This header covers RepeatedField.
  46. #ifndef GOOGLE_PROTOBUF_REPEATED_FIELD_H__
  47. #define GOOGLE_PROTOBUF_REPEATED_FIELD_H__
  48. #include <utility>
  49. #ifdef _MSC_VER
  50. // This is required for min/max on VS2013 only.
  51. #include <algorithm>
  52. #endif
  53. #include <iterator>
  54. #include <limits>
  55. #include <string>
  56. #include <type_traits>
  57. #include <google/protobuf/stubs/logging.h>
  58. #include <google/protobuf/stubs/common.h>
  59. #include <google/protobuf/repeated_ptr_field.h>
  60. #include <google/protobuf/arena.h>
  61. #include <google/protobuf/message_lite.h>
  62. #include <google/protobuf/port.h>
  63. // Must be included last.
  64. #include <google/protobuf/port_def.inc>
  65. #ifdef SWIG
  66. #error "You cannot SWIG proto headers"
  67. #endif
  68. namespace google {
  69. namespace protobuf {
  70. class Message;
  71. namespace internal {
  72. // kRepeatedFieldLowerClampLimit is the smallest size that will be allocated
  73. // when growing a repeated field.
  74. constexpr int kRepeatedFieldLowerClampLimit = 4;
  75. // kRepeatedFieldUpperClampLimit is the lowest signed integer value that
  76. // overflows when multiplied by 2 (which is undefined behavior). Sizes above
  77. // this will clamp to the maximum int value instead of following exponential
  78. // growth when growing a repeated field.
  79. constexpr int kRepeatedFieldUpperClampLimit =
  80. (std::numeric_limits<int>::max() / 2) + 1;
  81. template <typename Iter>
  82. inline int CalculateReserve(Iter begin, Iter end, std::forward_iterator_tag) {
  83. return static_cast<int>(std::distance(begin, end));
  84. }
  85. template <typename Iter>
  86. inline int CalculateReserve(Iter /*begin*/, Iter /*end*/,
  87. std::input_iterator_tag /*unused*/) {
  88. return -1;
  89. }
  90. template <typename Iter>
  91. inline int CalculateReserve(Iter begin, Iter end) {
  92. typedef typename std::iterator_traits<Iter>::iterator_category Category;
  93. return CalculateReserve(begin, end, Category());
  94. }
  95. // Swaps two blocks of memory of size sizeof(T).
  96. template <typename T>
  97. inline void SwapBlock(char* p, char* q) {
  98. T tmp;
  99. memcpy(&tmp, p, sizeof(T));
  100. memcpy(p, q, sizeof(T));
  101. memcpy(q, &tmp, sizeof(T));
  102. }
  103. // Swaps two blocks of memory of size kSize:
  104. // template <int kSize> void memswap(char* p, char* q);
  105. template <int kSize>
  106. inline typename std::enable_if<(kSize == 0), void>::type memswap(char*, char*) {
  107. }
  108. #define PROTO_MEMSWAP_DEF_SIZE(reg_type, max_size) \
  109. template <int kSize> \
  110. typename std::enable_if<(kSize >= sizeof(reg_type) && kSize < (max_size)), \
  111. void>::type \
  112. memswap(char* p, char* q) { \
  113. SwapBlock<reg_type>(p, q); \
  114. memswap<kSize - sizeof(reg_type)>(p + sizeof(reg_type), \
  115. q + sizeof(reg_type)); \
  116. }
  117. PROTO_MEMSWAP_DEF_SIZE(uint8_t, 2)
  118. PROTO_MEMSWAP_DEF_SIZE(uint16_t, 4)
  119. PROTO_MEMSWAP_DEF_SIZE(uint32_t, 8)
  120. #ifdef __SIZEOF_INT128__
  121. PROTO_MEMSWAP_DEF_SIZE(uint64_t, 16)
  122. PROTO_MEMSWAP_DEF_SIZE(__uint128_t, (1u << 31))
  123. #else
  124. PROTO_MEMSWAP_DEF_SIZE(uint64_t, (1u << 31))
  125. #endif
  126. #undef PROTO_MEMSWAP_DEF_SIZE
  127. } // namespace internal
  128. // RepeatedField is used to represent repeated fields of a primitive type (in
  129. // other words, everything except strings and nested Messages). Most users will
  130. // not ever use a RepeatedField directly; they will use the get-by-index,
  131. // set-by-index, and add accessors that are generated for all repeated fields.
  132. template <typename Element>
  133. class RepeatedField final {
  134. static_assert(
  135. alignof(Arena) >= alignof(Element),
  136. "We only support types that have an alignment smaller than Arena");
  137. public:
  138. constexpr RepeatedField();
  139. explicit RepeatedField(Arena* arena);
  140. RepeatedField(const RepeatedField& other);
  141. template <typename Iter,
  142. typename = typename std::enable_if<std::is_constructible<
  143. Element, decltype(*std::declval<Iter>())>::value>::type>
  144. RepeatedField(Iter begin, Iter end);
  145. ~RepeatedField();
  146. RepeatedField& operator=(const RepeatedField& other);
  147. RepeatedField(RepeatedField&& other) noexcept;
  148. RepeatedField& operator=(RepeatedField&& other) noexcept;
  149. bool empty() const;
  150. int size() const;
  151. const Element& Get(int index) const;
  152. Element* Mutable(int index);
  153. const Element& operator[](int index) const { return Get(index); }
  154. Element& operator[](int index) { return *Mutable(index); }
  155. const Element& at(int index) const;
  156. Element& at(int index);
  157. void Set(int index, const Element& value);
  158. void Add(const Element& value);
  159. // Appends a new element and return a pointer to it.
  160. // The new element is uninitialized if |Element| is a POD type.
  161. Element* Add();
  162. // Append elements in the range [begin, end) after reserving
  163. // the appropriate number of elements.
  164. template <typename Iter>
  165. void Add(Iter begin, Iter end);
  166. // Remove the last element in the array.
  167. void RemoveLast();
  168. // Extract elements with indices in "[start .. start+num-1]".
  169. // Copy them into "elements[0 .. num-1]" if "elements" is not nullptr.
  170. // Caution: implementation also moves elements with indices [start+num ..].
  171. // Calling this routine inside a loop can cause quadratic behavior.
  172. void ExtractSubrange(int start, int num, Element* elements);
  173. PROTOBUF_ATTRIBUTE_REINITIALIZES void Clear();
  174. void MergeFrom(const RepeatedField& other);
  175. PROTOBUF_ATTRIBUTE_REINITIALIZES void CopyFrom(const RepeatedField& other);
  176. // Replaces the contents with RepeatedField(begin, end).
  177. template <typename Iter>
  178. PROTOBUF_ATTRIBUTE_REINITIALIZES void Assign(Iter begin, Iter end);
  179. // Reserve space to expand the field to at least the given size. If the
  180. // array is grown, it will always be at least doubled in size.
  181. void Reserve(int new_size);
  182. // Resize the RepeatedField to a new, smaller size. This is O(1).
  183. void Truncate(int new_size);
  184. void AddAlreadyReserved(const Element& value);
  185. // Appends a new element and return a pointer to it.
  186. // The new element is uninitialized if |Element| is a POD type.
  187. // Should be called only if Capacity() > Size().
  188. Element* AddAlreadyReserved();
  189. Element* AddNAlreadyReserved(int elements);
  190. int Capacity() const;
  191. // Like STL resize. Uses value to fill appended elements.
  192. // Like Truncate() if new_size <= size(), otherwise this is
  193. // O(new_size - size()).
  194. void Resize(int new_size, const Element& value);
  195. // Gets the underlying array. This pointer is possibly invalidated by
  196. // any add or remove operation.
  197. Element* mutable_data();
  198. const Element* data() const;
  199. // Swap entire contents with "other". If they are separate arenas then, copies
  200. // data between each other.
  201. void Swap(RepeatedField* other);
  202. // Swap entire contents with "other". Should be called only if the caller can
  203. // guarantee that both repeated fields are on the same arena or are on the
  204. // heap. Swapping between different arenas is disallowed and caught by a
  205. // GOOGLE_DCHECK (see API docs for details).
  206. void UnsafeArenaSwap(RepeatedField* other);
  207. // Swap two elements.
  208. void SwapElements(int index1, int index2);
  209. // STL-like iterator support
  210. typedef Element* iterator;
  211. typedef const Element* const_iterator;
  212. typedef Element value_type;
  213. typedef value_type& reference;
  214. typedef const value_type& const_reference;
  215. typedef value_type* pointer;
  216. typedef const value_type* const_pointer;
  217. typedef int size_type;
  218. typedef ptrdiff_t difference_type;
  219. iterator begin();
  220. const_iterator begin() const;
  221. const_iterator cbegin() const;
  222. iterator end();
  223. const_iterator end() const;
  224. const_iterator cend() const;
  225. // Reverse iterator support
  226. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  227. typedef std::reverse_iterator<iterator> reverse_iterator;
  228. reverse_iterator rbegin() { return reverse_iterator(end()); }
  229. const_reverse_iterator rbegin() const {
  230. return const_reverse_iterator(end());
  231. }
  232. reverse_iterator rend() { return reverse_iterator(begin()); }
  233. const_reverse_iterator rend() const {
  234. return const_reverse_iterator(begin());
  235. }
  236. // Returns the number of bytes used by the repeated field, excluding
  237. // sizeof(*this)
  238. size_t SpaceUsedExcludingSelfLong() const;
  239. int SpaceUsedExcludingSelf() const {
  240. return internal::ToIntSize(SpaceUsedExcludingSelfLong());
  241. }
  242. // Removes the element referenced by position.
  243. //
  244. // Returns an iterator to the element immediately following the removed
  245. // element.
  246. //
  247. // Invalidates all iterators at or after the removed element, including end().
  248. iterator erase(const_iterator position);
  249. // Removes the elements in the range [first, last).
  250. //
  251. // Returns an iterator to the element immediately following the removed range.
  252. //
  253. // Invalidates all iterators at or after the removed range, including end().
  254. iterator erase(const_iterator first, const_iterator last);
  255. // Get the Arena on which this RepeatedField stores its elements.
  256. inline Arena* GetArena() const {
  257. return (total_size_ == 0) ? static_cast<Arena*>(arena_or_elements_)
  258. : rep()->arena;
  259. }
  260. // For internal use only.
  261. //
  262. // This is public due to it being called by generated code.
  263. inline void InternalSwap(RepeatedField* other);
  264. private:
  265. static constexpr int kInitialSize = 0;
  266. // A note on the representation here (see also comment below for
  267. // RepeatedPtrFieldBase's struct Rep):
  268. //
  269. // We maintain the same sizeof(RepeatedField) as before we added arena support
  270. // so that we do not degrade performance by bloating memory usage. Directly
  271. // adding an arena_ element to RepeatedField is quite costly. By using
  272. // indirection in this way, we keep the same size when the RepeatedField is
  273. // empty (common case), and add only an 8-byte header to the elements array
  274. // when non-empty. We make sure to place the size fields directly in the
  275. // RepeatedField class to avoid costly cache misses due to the indirection.
  276. int current_size_;
  277. int total_size_;
  278. struct Rep {
  279. Arena* arena;
  280. // Here we declare a huge array as a way of approximating C's "flexible
  281. // array member" feature without relying on undefined behavior.
  282. Element elements[(std::numeric_limits<int>::max() - 2 * sizeof(Arena*)) /
  283. sizeof(Element)];
  284. };
  285. static constexpr size_t kRepHeaderSize = offsetof(Rep, elements);
  286. // If total_size_ == 0 this points to an Arena otherwise it points to the
  287. // elements member of a Rep struct. Using this invariant allows the storage of
  288. // the arena pointer without an extra allocation in the constructor.
  289. void* arena_or_elements_;
  290. // Return pointer to elements array.
  291. // pre-condition: the array must have been allocated.
  292. Element* elements() const {
  293. GOOGLE_DCHECK_GT(total_size_, 0);
  294. // Because of above pre-condition this cast is safe.
  295. return unsafe_elements();
  296. }
  297. // Return pointer to elements array if it exists otherwise either null or
  298. // a invalid pointer is returned. This only happens for empty repeated fields,
  299. // where you can't dereference this pointer anyway (it's empty).
  300. Element* unsafe_elements() const {
  301. return static_cast<Element*>(arena_or_elements_);
  302. }
  303. // Return pointer to the Rep struct.
  304. // pre-condition: the Rep must have been allocated, ie elements() is safe.
  305. Rep* rep() const {
  306. char* addr = reinterpret_cast<char*>(elements()) - offsetof(Rep, elements);
  307. return reinterpret_cast<Rep*>(addr);
  308. }
  309. friend class Arena;
  310. typedef void InternalArenaConstructable_;
  311. // Move the contents of |from| into |to|, possibly clobbering |from| in the
  312. // process. For primitive types this is just a memcpy(), but it could be
  313. // specialized for non-primitive types to, say, swap each element instead.
  314. void MoveArray(Element* to, Element* from, int size);
  315. // Copy the elements of |from| into |to|.
  316. void CopyArray(Element* to, const Element* from, int size);
  317. // Internal helper to delete all elements and deallocate the storage.
  318. void InternalDeallocate(Rep* rep, int size) {
  319. if (rep != nullptr) {
  320. Element* e = &rep->elements[0];
  321. if (!std::is_trivial<Element>::value) {
  322. Element* limit = &rep->elements[size];
  323. for (; e < limit; e++) {
  324. e->~Element();
  325. }
  326. }
  327. if (rep->arena == nullptr) {
  328. #if defined(__GXX_DELETE_WITH_SIZE__) || defined(__cpp_sized_deallocation)
  329. const size_t bytes = size * sizeof(*e) + kRepHeaderSize;
  330. ::operator delete(static_cast<void*>(rep), bytes);
  331. #else
  332. ::operator delete(static_cast<void*>(rep));
  333. #endif
  334. }
  335. }
  336. }
  337. // This class is a performance wrapper around RepeatedField::Add(const T&)
  338. // function. In general unless a RepeatedField is a local stack variable LLVM
  339. // has a hard time optimizing Add. The machine code tends to be
  340. // loop:
  341. // mov %size, dword ptr [%repeated_field] // load
  342. // cmp %size, dword ptr [%repeated_field + 4]
  343. // jae fallback
  344. // mov %buffer, qword ptr [%repeated_field + 8]
  345. // mov dword [%buffer + %size * 4], %value
  346. // inc %size // increment
  347. // mov dword ptr [%repeated_field], %size // store
  348. // jmp loop
  349. //
  350. // This puts a load/store in each iteration of the important loop variable
  351. // size. It's a pretty bad compile that happens even in simple cases, but
  352. // largely the presence of the fallback path disturbs the compilers mem-to-reg
  353. // analysis.
  354. //
  355. // This class takes ownership of a repeated field for the duration of it's
  356. // lifetime. The repeated field should not be accessed during this time, ie.
  357. // only access through this class is allowed. This class should always be a
  358. // function local stack variable. Intended use
  359. //
  360. // void AddSequence(const int* begin, const int* end, RepeatedField<int>* out)
  361. // {
  362. // RepeatedFieldAdder<int> adder(out); // Take ownership of out
  363. // for (auto it = begin; it != end; ++it) {
  364. // adder.Add(*it);
  365. // }
  366. // }
  367. //
  368. // Typically due to the fact adder is a local stack variable. The compiler
  369. // will be successful in mem-to-reg transformation and the machine code will
  370. // be loop: cmp %size, %capacity jae fallback mov dword ptr [%buffer + %size *
  371. // 4], %val inc %size jmp loop
  372. //
  373. // The first version executes at 7 cycles per iteration while the second
  374. // version near 1 or 2 cycles.
  375. template <int = 0, bool = std::is_trivial<Element>::value>
  376. class FastAdderImpl {
  377. public:
  378. explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {
  379. index_ = repeated_field_->current_size_;
  380. capacity_ = repeated_field_->total_size_;
  381. buffer_ = repeated_field_->unsafe_elements();
  382. }
  383. ~FastAdderImpl() { repeated_field_->current_size_ = index_; }
  384. void Add(Element val) {
  385. if (index_ == capacity_) {
  386. repeated_field_->current_size_ = index_;
  387. repeated_field_->Reserve(index_ + 1);
  388. capacity_ = repeated_field_->total_size_;
  389. buffer_ = repeated_field_->unsafe_elements();
  390. }
  391. buffer_[index_++] = val;
  392. }
  393. private:
  394. RepeatedField* repeated_field_;
  395. int index_;
  396. int capacity_;
  397. Element* buffer_;
  398. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  399. };
  400. // FastAdder is a wrapper for adding fields. The specialization above handles
  401. // POD types more efficiently than RepeatedField.
  402. template <int I>
  403. class FastAdderImpl<I, false> {
  404. public:
  405. explicit FastAdderImpl(RepeatedField* rf) : repeated_field_(rf) {}
  406. void Add(const Element& val) { repeated_field_->Add(val); }
  407. private:
  408. RepeatedField* repeated_field_;
  409. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FastAdderImpl);
  410. };
  411. using FastAdder = FastAdderImpl<>;
  412. friend class TestRepeatedFieldHelper;
  413. friend class ::google::protobuf::internal::ParseContext;
  414. };
  415. namespace internal {
  416. // This is a helper template to copy an array of elements efficiently when they
  417. // have a trivial copy constructor, and correctly otherwise. This really
  418. // shouldn't be necessary, but our compiler doesn't optimize std::copy very
  419. // effectively.
  420. template <typename Element,
  421. bool HasTrivialCopy = std::is_trivial<Element>::value>
  422. struct ElementCopier {
  423. void operator()(Element* to, const Element* from, int array_size);
  424. };
  425. } // namespace internal
  426. // implementation ====================================================
  427. template <typename Element>
  428. constexpr RepeatedField<Element>::RepeatedField()
  429. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {}
  430. template <typename Element>
  431. inline RepeatedField<Element>::RepeatedField(Arena* arena)
  432. : current_size_(0), total_size_(0), arena_or_elements_(arena) {}
  433. template <typename Element>
  434. inline RepeatedField<Element>::RepeatedField(const RepeatedField& other)
  435. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {
  436. if (other.current_size_ != 0) {
  437. Reserve(other.size());
  438. AddNAlreadyReserved(other.size());
  439. CopyArray(Mutable(0), &other.Get(0), other.size());
  440. }
  441. }
  442. template <typename Element>
  443. template <typename Iter, typename>
  444. RepeatedField<Element>::RepeatedField(Iter begin, Iter end)
  445. : current_size_(0), total_size_(0), arena_or_elements_(nullptr) {
  446. Add(begin, end);
  447. }
  448. template <typename Element>
  449. RepeatedField<Element>::~RepeatedField() {
  450. #ifndef NDEBUG
  451. // Try to trigger segfault / asan failure in non-opt builds. If arena_
  452. // lifetime has ended before the destructor.
  453. auto arena = GetArena();
  454. if (arena) (void)arena->SpaceAllocated();
  455. #endif
  456. if (total_size_ > 0) {
  457. InternalDeallocate(rep(), total_size_);
  458. }
  459. }
  460. template <typename Element>
  461. inline RepeatedField<Element>& RepeatedField<Element>::operator=(
  462. const RepeatedField& other) {
  463. if (this != &other) CopyFrom(other);
  464. return *this;
  465. }
  466. template <typename Element>
  467. inline RepeatedField<Element>::RepeatedField(RepeatedField&& other) noexcept
  468. : RepeatedField() {
  469. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  470. CopyFrom(other);
  471. #else // PROTOBUF_FORCE_COPY_IN_MOVE
  472. // We don't just call Swap(&other) here because it would perform 3 copies if
  473. // other is on an arena. This field can't be on an arena because arena
  474. // construction always uses the Arena* accepting constructor.
  475. if (other.GetArena()) {
  476. CopyFrom(other);
  477. } else {
  478. InternalSwap(&other);
  479. }
  480. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  481. }
  482. template <typename Element>
  483. inline RepeatedField<Element>& RepeatedField<Element>::operator=(
  484. RepeatedField&& other) noexcept {
  485. // We don't just call Swap(&other) here because it would perform 3 copies if
  486. // the two fields are on different arenas.
  487. if (this != &other) {
  488. if (GetArena() != other.GetArena()
  489. #ifdef PROTOBUF_FORCE_COPY_IN_MOVE
  490. || GetArena() == nullptr
  491. #endif // !PROTOBUF_FORCE_COPY_IN_MOVE
  492. ) {
  493. CopyFrom(other);
  494. } else {
  495. InternalSwap(&other);
  496. }
  497. }
  498. return *this;
  499. }
  500. template <typename Element>
  501. inline bool RepeatedField<Element>::empty() const {
  502. return current_size_ == 0;
  503. }
  504. template <typename Element>
  505. inline int RepeatedField<Element>::size() const {
  506. return current_size_;
  507. }
  508. template <typename Element>
  509. inline int RepeatedField<Element>::Capacity() const {
  510. return total_size_;
  511. }
  512. template <typename Element>
  513. inline void RepeatedField<Element>::AddAlreadyReserved(const Element& value) {
  514. GOOGLE_DCHECK_LT(current_size_, total_size_);
  515. elements()[current_size_++] = value;
  516. }
  517. template <typename Element>
  518. inline Element* RepeatedField<Element>::AddAlreadyReserved() {
  519. GOOGLE_DCHECK_LT(current_size_, total_size_);
  520. return &elements()[current_size_++];
  521. }
  522. template <typename Element>
  523. inline Element* RepeatedField<Element>::AddNAlreadyReserved(int n) {
  524. GOOGLE_DCHECK_GE(total_size_ - current_size_, n)
  525. << total_size_ << ", " << current_size_;
  526. // Warning: sometimes people call this when n == 0 and total_size_ == 0. In
  527. // this case the return pointer points to a zero size array (n == 0). Hence
  528. // we can just use unsafe_elements(), because the user cannot dereference the
  529. // pointer anyway.
  530. Element* ret = unsafe_elements() + current_size_;
  531. current_size_ += n;
  532. return ret;
  533. }
  534. template <typename Element>
  535. inline void RepeatedField<Element>::Resize(int new_size, const Element& value) {
  536. GOOGLE_DCHECK_GE(new_size, 0);
  537. if (new_size > current_size_) {
  538. Reserve(new_size);
  539. std::fill(&elements()[current_size_], &elements()[new_size], value);
  540. }
  541. current_size_ = new_size;
  542. }
  543. template <typename Element>
  544. inline const Element& RepeatedField<Element>::Get(int index) const {
  545. GOOGLE_DCHECK_GE(index, 0);
  546. GOOGLE_DCHECK_LT(index, current_size_);
  547. return elements()[index];
  548. }
  549. template <typename Element>
  550. inline const Element& RepeatedField<Element>::at(int index) const {
  551. GOOGLE_CHECK_GE(index, 0);
  552. GOOGLE_CHECK_LT(index, current_size_);
  553. return elements()[index];
  554. }
  555. template <typename Element>
  556. inline Element& RepeatedField<Element>::at(int index) {
  557. GOOGLE_CHECK_GE(index, 0);
  558. GOOGLE_CHECK_LT(index, current_size_);
  559. return elements()[index];
  560. }
  561. template <typename Element>
  562. inline Element* RepeatedField<Element>::Mutable(int index) {
  563. GOOGLE_DCHECK_GE(index, 0);
  564. GOOGLE_DCHECK_LT(index, current_size_);
  565. return &elements()[index];
  566. }
  567. template <typename Element>
  568. inline void RepeatedField<Element>::Set(int index, const Element& value) {
  569. GOOGLE_DCHECK_GE(index, 0);
  570. GOOGLE_DCHECK_LT(index, current_size_);
  571. elements()[index] = value;
  572. }
  573. template <typename Element>
  574. inline void RepeatedField<Element>::Add(const Element& value) {
  575. uint32_t size = current_size_;
  576. if (static_cast<int>(size) == total_size_) {
  577. // value could reference an element of the array. Reserving new space will
  578. // invalidate the reference. So we must make a copy first.
  579. auto tmp = value;
  580. Reserve(total_size_ + 1);
  581. elements()[size] = std::move(tmp);
  582. } else {
  583. elements()[size] = value;
  584. }
  585. current_size_ = size + 1;
  586. }
  587. template <typename Element>
  588. inline Element* RepeatedField<Element>::Add() {
  589. uint32_t size = current_size_;
  590. if (static_cast<int>(size) == total_size_) Reserve(total_size_ + 1);
  591. auto ptr = &elements()[size];
  592. current_size_ = size + 1;
  593. return ptr;
  594. }
  595. template <typename Element>
  596. template <typename Iter>
  597. inline void RepeatedField<Element>::Add(Iter begin, Iter end) {
  598. int reserve = internal::CalculateReserve(begin, end);
  599. if (reserve != -1) {
  600. if (reserve == 0) {
  601. return;
  602. }
  603. Reserve(reserve + size());
  604. // TODO(ckennelly): The compiler loses track of the buffer freshly
  605. // allocated by Reserve() by the time we call elements, so it cannot
  606. // guarantee that elements does not alias [begin(), end()).
  607. //
  608. // If restrict is available, annotating the pointer obtained from elements()
  609. // causes this to lower to memcpy instead of memmove.
  610. std::copy(begin, end, elements() + size());
  611. current_size_ = reserve + size();
  612. } else {
  613. FastAdder fast_adder(this);
  614. for (; begin != end; ++begin) fast_adder.Add(*begin);
  615. }
  616. }
  617. template <typename Element>
  618. inline void RepeatedField<Element>::RemoveLast() {
  619. GOOGLE_DCHECK_GT(current_size_, 0);
  620. current_size_--;
  621. }
  622. template <typename Element>
  623. void RepeatedField<Element>::ExtractSubrange(int start, int num,
  624. Element* elements) {
  625. GOOGLE_DCHECK_GE(start, 0);
  626. GOOGLE_DCHECK_GE(num, 0);
  627. GOOGLE_DCHECK_LE(start + num, this->current_size_);
  628. // Save the values of the removed elements if requested.
  629. if (elements != nullptr) {
  630. for (int i = 0; i < num; ++i) elements[i] = this->Get(i + start);
  631. }
  632. // Slide remaining elements down to fill the gap.
  633. if (num > 0) {
  634. for (int i = start + num; i < this->current_size_; ++i)
  635. this->Set(i - num, this->Get(i));
  636. this->Truncate(this->current_size_ - num);
  637. }
  638. }
  639. template <typename Element>
  640. inline void RepeatedField<Element>::Clear() {
  641. current_size_ = 0;
  642. }
  643. template <typename Element>
  644. inline void RepeatedField<Element>::MergeFrom(const RepeatedField& other) {
  645. GOOGLE_DCHECK_NE(&other, this);
  646. if (other.current_size_ != 0) {
  647. int existing_size = size();
  648. Reserve(existing_size + other.size());
  649. AddNAlreadyReserved(other.size());
  650. CopyArray(Mutable(existing_size), &other.Get(0), other.size());
  651. }
  652. }
  653. template <typename Element>
  654. inline void RepeatedField<Element>::CopyFrom(const RepeatedField& other) {
  655. if (&other == this) return;
  656. Clear();
  657. MergeFrom(other);
  658. }
  659. template <typename Element>
  660. template <typename Iter>
  661. inline void RepeatedField<Element>::Assign(Iter begin, Iter end) {
  662. Clear();
  663. Add(begin, end);
  664. }
  665. template <typename Element>
  666. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
  667. const_iterator position) {
  668. return erase(position, position + 1);
  669. }
  670. template <typename Element>
  671. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::erase(
  672. const_iterator first, const_iterator last) {
  673. size_type first_offset = first - cbegin();
  674. if (first != last) {
  675. Truncate(std::copy(last, cend(), begin() + first_offset) - cbegin());
  676. }
  677. return begin() + first_offset;
  678. }
  679. template <typename Element>
  680. inline Element* RepeatedField<Element>::mutable_data() {
  681. return unsafe_elements();
  682. }
  683. template <typename Element>
  684. inline const Element* RepeatedField<Element>::data() const {
  685. return unsafe_elements();
  686. }
  687. template <typename Element>
  688. inline void RepeatedField<Element>::InternalSwap(RepeatedField* other) {
  689. GOOGLE_DCHECK(this != other);
  690. // Swap all fields at once.
  691. static_assert(std::is_standard_layout<RepeatedField<Element>>::value,
  692. "offsetof() requires standard layout before c++17");
  693. internal::memswap<offsetof(RepeatedField, arena_or_elements_) +
  694. sizeof(this->arena_or_elements_) -
  695. offsetof(RepeatedField, current_size_)>(
  696. reinterpret_cast<char*>(this) + offsetof(RepeatedField, current_size_),
  697. reinterpret_cast<char*>(other) + offsetof(RepeatedField, current_size_));
  698. }
  699. template <typename Element>
  700. void RepeatedField<Element>::Swap(RepeatedField* other) {
  701. if (this == other) return;
  702. #ifdef PROTOBUF_FORCE_COPY_IN_SWAP
  703. if (GetArena() != nullptr && GetArena() == other->GetArena()) {
  704. #else // PROTOBUF_FORCE_COPY_IN_SWAP
  705. if (GetArena() == other->GetArena()) {
  706. #endif // !PROTOBUF_FORCE_COPY_IN_SWAP
  707. InternalSwap(other);
  708. } else {
  709. RepeatedField<Element> temp(other->GetArena());
  710. temp.MergeFrom(*this);
  711. CopyFrom(*other);
  712. other->UnsafeArenaSwap(&temp);
  713. }
  714. }
  715. template <typename Element>
  716. void RepeatedField<Element>::UnsafeArenaSwap(RepeatedField* other) {
  717. if (this == other) return;
  718. InternalSwap(other);
  719. }
  720. template <typename Element>
  721. void RepeatedField<Element>::SwapElements(int index1, int index2) {
  722. using std::swap; // enable ADL with fallback
  723. swap(elements()[index1], elements()[index2]);
  724. }
  725. template <typename Element>
  726. inline typename RepeatedField<Element>::iterator
  727. RepeatedField<Element>::begin() {
  728. return unsafe_elements();
  729. }
  730. template <typename Element>
  731. inline typename RepeatedField<Element>::const_iterator
  732. RepeatedField<Element>::begin() const {
  733. return unsafe_elements();
  734. }
  735. template <typename Element>
  736. inline typename RepeatedField<Element>::const_iterator
  737. RepeatedField<Element>::cbegin() const {
  738. return unsafe_elements();
  739. }
  740. template <typename Element>
  741. inline typename RepeatedField<Element>::iterator RepeatedField<Element>::end() {
  742. return unsafe_elements() + current_size_;
  743. }
  744. template <typename Element>
  745. inline typename RepeatedField<Element>::const_iterator
  746. RepeatedField<Element>::end() const {
  747. return unsafe_elements() + current_size_;
  748. }
  749. template <typename Element>
  750. inline typename RepeatedField<Element>::const_iterator
  751. RepeatedField<Element>::cend() const {
  752. return unsafe_elements() + current_size_;
  753. }
  754. template <typename Element>
  755. inline size_t RepeatedField<Element>::SpaceUsedExcludingSelfLong() const {
  756. return total_size_ > 0 ? (total_size_ * sizeof(Element) + kRepHeaderSize) : 0;
  757. }
  758. namespace internal {
  759. // Returns the new size for a reserved field based on its 'total_size' and the
  760. // requested 'new_size'. The result is clamped to the closed interval:
  761. // [internal::kMinRepeatedFieldAllocationSize,
  762. // std::numeric_limits<int>::max()]
  763. // Requires:
  764. // new_size > total_size &&
  765. // (total_size == 0 ||
  766. // total_size >= kRepeatedFieldLowerClampLimit)
  767. inline int CalculateReserveSize(int total_size, int new_size) {
  768. if (new_size < kRepeatedFieldLowerClampLimit) {
  769. // Clamp to smallest allowed size.
  770. return kRepeatedFieldLowerClampLimit;
  771. }
  772. if (total_size < kRepeatedFieldUpperClampLimit) {
  773. return std::max(total_size * 2, new_size);
  774. } else {
  775. // Clamp to largest allowed size.
  776. GOOGLE_DCHECK_GT(new_size, kRepeatedFieldUpperClampLimit);
  777. return std::numeric_limits<int>::max();
  778. }
  779. }
  780. } // namespace internal
  781. // Avoid inlining of Reserve(): new, copy, and delete[] lead to a significant
  782. // amount of code bloat.
  783. template <typename Element>
  784. void RepeatedField<Element>::Reserve(int new_size) {
  785. if (total_size_ >= new_size) return;
  786. Rep* old_rep = total_size_ > 0 ? rep() : nullptr;
  787. Rep* new_rep;
  788. Arena* arena = GetArena();
  789. new_size = internal::CalculateReserveSize(total_size_, new_size);
  790. GOOGLE_DCHECK_LE(
  791. static_cast<size_t>(new_size),
  792. (std::numeric_limits<size_t>::max() - kRepHeaderSize) / sizeof(Element))
  793. << "Requested size is too large to fit into size_t.";
  794. size_t bytes =
  795. kRepHeaderSize + sizeof(Element) * static_cast<size_t>(new_size);
  796. if (arena == nullptr) {
  797. new_rep = static_cast<Rep*>(::operator new(bytes));
  798. } else {
  799. new_rep = reinterpret_cast<Rep*>(Arena::CreateArray<char>(arena, bytes));
  800. }
  801. new_rep->arena = arena;
  802. int old_total_size = total_size_;
  803. // Already known: new_size >= internal::kMinRepeatedFieldAllocationSize
  804. // Maintain invariant:
  805. // total_size_ == 0 ||
  806. // total_size_ >= internal::kMinRepeatedFieldAllocationSize
  807. total_size_ = new_size;
  808. arena_or_elements_ = new_rep->elements;
  809. // Invoke placement-new on newly allocated elements. We shouldn't have to do
  810. // this, since Element is supposed to be POD, but a previous version of this
  811. // code allocated storage with "new Element[size]" and some code uses
  812. // RepeatedField with non-POD types, relying on constructor invocation. If
  813. // Element has a trivial constructor (e.g., int32_t), gcc (tested with -O2)
  814. // completely removes this loop because the loop body is empty, so this has no
  815. // effect unless its side-effects are required for correctness.
  816. // Note that we do this before MoveArray() below because Element's copy
  817. // assignment implementation will want an initialized instance first.
  818. Element* e = &elements()[0];
  819. Element* limit = e + total_size_;
  820. for (; e < limit; e++) {
  821. new (e) Element;
  822. }
  823. if (current_size_ > 0) {
  824. MoveArray(&elements()[0], old_rep->elements, current_size_);
  825. }
  826. // Likewise, we need to invoke destructors on the old array.
  827. InternalDeallocate(old_rep, old_total_size);
  828. }
  829. template <typename Element>
  830. inline void RepeatedField<Element>::Truncate(int new_size) {
  831. GOOGLE_DCHECK_LE(new_size, current_size_);
  832. if (current_size_ > 0) {
  833. current_size_ = new_size;
  834. }
  835. }
  836. template <typename Element>
  837. inline void RepeatedField<Element>::MoveArray(Element* to, Element* from,
  838. int array_size) {
  839. CopyArray(to, from, array_size);
  840. }
  841. template <typename Element>
  842. inline void RepeatedField<Element>::CopyArray(Element* to, const Element* from,
  843. int array_size) {
  844. internal::ElementCopier<Element>()(to, from, array_size);
  845. }
  846. namespace internal {
  847. template <typename Element, bool HasTrivialCopy>
  848. void ElementCopier<Element, HasTrivialCopy>::operator()(Element* to,
  849. const Element* from,
  850. int array_size) {
  851. std::copy(from, from + array_size, to);
  852. }
  853. template <typename Element>
  854. struct ElementCopier<Element, true> {
  855. void operator()(Element* to, const Element* from, int array_size) {
  856. memcpy(to, from, static_cast<size_t>(array_size) * sizeof(Element));
  857. }
  858. };
  859. } // namespace internal
  860. // -------------------------------------------------------------------
  861. // Iterators and helper functions that follow the spirit of the STL
  862. // std::back_insert_iterator and std::back_inserter but are tailor-made
  863. // for RepeatedField and RepeatedPtrField. Typical usage would be:
  864. //
  865. // std::copy(some_sequence.begin(), some_sequence.end(),
  866. // RepeatedFieldBackInserter(proto.mutable_sequence()));
  867. //
  868. // Ported by johannes from util/gtl/proto-array-iterators.h
  869. namespace internal {
  870. // A back inserter for RepeatedField objects.
  871. template <typename T>
  872. class RepeatedFieldBackInsertIterator {
  873. public:
  874. using iterator_category = std::output_iterator_tag;
  875. using value_type = T;
  876. using pointer = void;
  877. using reference = void;
  878. using difference_type = std::ptrdiff_t;
  879. explicit RepeatedFieldBackInsertIterator(
  880. RepeatedField<T>* const mutable_field)
  881. : field_(mutable_field) {}
  882. RepeatedFieldBackInsertIterator<T>& operator=(const T& value) {
  883. field_->Add(value);
  884. return *this;
  885. }
  886. RepeatedFieldBackInsertIterator<T>& operator*() { return *this; }
  887. RepeatedFieldBackInsertIterator<T>& operator++() { return *this; }
  888. RepeatedFieldBackInsertIterator<T>& operator++(int /* unused */) {
  889. return *this;
  890. }
  891. private:
  892. RepeatedField<T>* field_;
  893. };
  894. } // namespace internal
  895. // Provides a back insert iterator for RepeatedField instances,
  896. // similar to std::back_inserter().
  897. template <typename T>
  898. internal::RepeatedFieldBackInsertIterator<T> RepeatedFieldBackInserter(
  899. RepeatedField<T>* const mutable_field) {
  900. return internal::RepeatedFieldBackInsertIterator<T>(mutable_field);
  901. }
  902. // Extern declarations of common instantiations to reduce library bloat.
  903. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<bool>;
  904. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<int32_t>;
  905. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<uint32_t>;
  906. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<int64_t>;
  907. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<uint64_t>;
  908. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<float>;
  909. extern template class PROTOBUF_EXPORT_TEMPLATE_DECLARE RepeatedField<double>;
  910. } // namespace protobuf
  911. } // namespace google
  912. #include <google/protobuf/port_undef.inc>
  913. #endif // GOOGLE_PROTOBUF_REPEATED_FIELD_H__