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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 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> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 38 | #include <google/protobuf/stubs/common.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 39 | |
| 40 | namespace google { |
| 41 | namespace protobuf { |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 42 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 43 | namespace internal { |
| 44 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 45 | void RepeatedPtrFieldBase::Reserve(int new_size) { |
| 46 | if (total_size_ >= new_size) return; |
| 47 | |
| 48 | void** old_elements = elements_; |
| 49 | total_size_ = max(total_size_ * 2, new_size); |
| 50 | elements_ = new void*[total_size_]; |
| 51 | memcpy(elements_, old_elements, allocated_size_ * sizeof(elements_[0])); |
| 52 | if (old_elements != initial_space_) { |
| 53 | delete [] old_elements; |
| 54 | } |
| 55 | } |
| 56 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 57 | void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) { |
| 58 | void** swap_elements = elements_; |
| 59 | int swap_current_size = current_size_; |
| 60 | int swap_allocated_size = allocated_size_; |
| 61 | int swap_total_size = total_size_; |
| 62 | // We may not be using initial_space_ but it's not worth checking. Just |
| 63 | // copy it anyway. |
| 64 | void* swap_initial_space[kInitialSize]; |
| 65 | memcpy(swap_initial_space, initial_space_, sizeof(initial_space_)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 66 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 67 | elements_ = other->elements_; |
| 68 | current_size_ = other->current_size_; |
| 69 | allocated_size_ = other->allocated_size_; |
| 70 | total_size_ = other->total_size_; |
| 71 | memcpy(initial_space_, other->initial_space_, sizeof(initial_space_)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 72 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 73 | other->elements_ = swap_elements; |
| 74 | other->current_size_ = swap_current_size; |
| 75 | other->allocated_size_ = swap_allocated_size; |
| 76 | other->total_size_ = swap_total_size; |
| 77 | memcpy(other->initial_space_, swap_initial_space, sizeof(swap_initial_space)); |
| 78 | |
| 79 | if (elements_ == other->initial_space_) { |
| 80 | elements_ = initial_space_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 81 | } |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 82 | if (other->elements_ == initial_space_) { |
| 83 | other->elements_ = other->initial_space_; |
| 84 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 85 | } |
| 86 | |
kenton@google.com | 9270a99 | 2009-08-01 02:16:55 +0000 | [diff] [blame] | 87 | string* StringTypeHandlerBase::New() { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 88 | return new string; |
| 89 | } |
kenton@google.com | 9270a99 | 2009-08-01 02:16:55 +0000 | [diff] [blame] | 90 | void StringTypeHandlerBase::Delete(string* value) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 91 | delete value; |
| 92 | } |
| 93 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 94 | } // namespace internal |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 95 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 96 | |
kenton@google.com | a2a32c2 | 2008-11-14 17:29:32 +0000 | [diff] [blame] | 97 | } // namespace protobuf |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 98 | } // namespace google |