temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 35 | #include <algorithm> |
| 36 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 37 | #include <google/protobuf/repeated_field.h> |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 38 | #include <google/protobuf/stubs/logging.h> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 39 | #include <google/protobuf/stubs/common.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 40 | |
| 41 | namespace google { |
| 42 | namespace protobuf { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 43 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 44 | namespace internal { |
| 45 | |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 46 | void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) { |
| 47 | int new_size = current_size_ + extend_amount; |
| 48 | if (total_size_ >= new_size) { |
| 49 | // N.B.: rep_ is non-NULL because extend_amount is always > 0, hence |
| 50 | // total_size must be non-zero since it is lower-bounded by new_size. |
| 51 | return &rep_->elements[current_size_]; |
| 52 | } |
| 53 | Rep* old_rep = rep_; |
| 54 | Arena* arena = GetArenaNoVirtual(); |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 55 | new_size = std::max(kMinRepeatedFieldAllocationSize, |
| 56 | std::max(total_size_ * 2, new_size)); |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 57 | GOOGLE_CHECK_LE(new_size, |
| 58 | (std::numeric_limits<size_t>::max() - kRepHeaderSize) / |
| 59 | sizeof(old_rep->elements[0])) |
| 60 | << "Requested size is too large to fit into size_t."; |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 61 | if (arena == NULL) { |
| 62 | rep_ = reinterpret_cast<Rep*>( |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 63 | new char[kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size]); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 64 | } else { |
| 65 | rep_ = reinterpret_cast<Rep*>( |
| 66 | ::google::protobuf::Arena::CreateArray<char>(arena, |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 67 | kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size)); |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 68 | } |
| 69 | total_size_ = new_size; |
| 70 | if (old_rep && old_rep->allocated_size > 0) { |
| 71 | memcpy(rep_->elements, old_rep->elements, |
| 72 | old_rep->allocated_size * sizeof(rep_->elements[0])); |
| 73 | rep_->allocated_size = old_rep->allocated_size; |
| 74 | } else { |
| 75 | rep_->allocated_size = 0; |
| 76 | } |
| 77 | if (arena == NULL) { |
| 78 | delete [] reinterpret_cast<char*>(old_rep); |
| 79 | } |
| 80 | return &rep_->elements[current_size_]; |
| 81 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 82 | |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 83 | void RepeatedPtrFieldBase::Reserve(int new_size) { |
| 84 | if (new_size > current_size_) { |
| 85 | InternalExtend(new_size - current_size_); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Feng Xiao | 6ef984a | 2014-11-10 17:34:54 -0800 | [diff] [blame] | 89 | void RepeatedPtrFieldBase::CloseGap(int start, int num) { |
| 90 | if (rep_ == NULL) return; |
| 91 | // Close up a gap of "num" elements starting at offset "start". |
| 92 | for (int i = start + num; i < rep_->allocated_size; ++i) |
| 93 | rep_->elements[i - num] = rep_->elements[i]; |
| 94 | current_size_ -= num; |
| 95 | rep_->allocated_size -= num; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 96 | } |
| 97 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 98 | } // namespace internal |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 99 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 100 | |
kenton@google.com | a2a32c2 | 2008-11-14 17:29:32 +0000 | [diff] [blame] | 101 | } // namespace protobuf |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 102 | } // namespace google |