blob: 09377742af597e41dfb7cba36ced3a3ffba26062 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
temporal40ee5512008-07-10 02:12:20 +00003// http://code.google.com/p/protobuf/
4//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
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.com33165fe2010-11-02 13:14:58 +000035#include <algorithm>
36
temporal40ee5512008-07-10 02:12:20 +000037#include <google/protobuf/repeated_field.h>
kenton@google.com80b1d622009-07-29 01:13:20 +000038#include <google/protobuf/stubs/common.h>
temporal40ee5512008-07-10 02:12:20 +000039
40namespace google {
41namespace protobuf {
liujisi@google.com33165fe2010-11-02 13:14:58 +000042
temporal40ee5512008-07-10 02:12:20 +000043namespace internal {
44
kenton@google.comfccb1462009-12-18 02:11:36 +000045void 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.com80b1d622009-07-29 01:13:20 +000057void 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_));
temporal40ee5512008-07-10 02:12:20 +000066
kenton@google.com80b1d622009-07-29 01:13:20 +000067 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_));
temporal40ee5512008-07-10 02:12:20 +000072
kenton@google.com80b1d622009-07-29 01:13:20 +000073 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_;
temporal40ee5512008-07-10 02:12:20 +000081 }
kenton@google.com80b1d622009-07-29 01:13:20 +000082 if (other->elements_ == initial_space_) {
83 other->elements_ = other->initial_space_;
84 }
temporal40ee5512008-07-10 02:12:20 +000085}
86
kenton@google.com9270a992009-08-01 02:16:55 +000087string* StringTypeHandlerBase::New() {
kenton@google.com80b1d622009-07-29 01:13:20 +000088 return new string;
89}
kenton@google.com9270a992009-08-01 02:16:55 +000090void StringTypeHandlerBase::Delete(string* value) {
kenton@google.com80b1d622009-07-29 01:13:20 +000091 delete value;
92}
93
liujisi@google.com33165fe2010-11-02 13:14:58 +000094} // namespace internal
kenton@google.com80b1d622009-07-29 01:13:20 +000095
temporal40ee5512008-07-10 02:12:20 +000096
kenton@google.coma2a32c22008-11-14 17:29:32 +000097} // namespace protobuf
temporal40ee5512008-07-10 02:12:20 +000098} // namespace google