blob: fd40c0d8f64afa48eec944b59d1c66daff074628 [file] [log] [blame]
Feng Xiaof157a562014-11-14 11:50:31 -08001// 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
31#include <google/protobuf/map_field.h>
32
Feng Xiao137dd0f2014-12-03 16:31:47 -080033#include <vector>
34
Feng Xiaof157a562014-11-14 11:50:31 -080035namespace google {
36namespace protobuf {
37namespace internal {
38
Feng Xiao137dd0f2014-12-03 16:31:47 -080039ProtobufOnceType map_entry_default_instances_once_;
40Mutex* map_entry_default_instances_mutex_;
41vector<MessageLite*>* map_entry_default_instances_;
42
43void DeleteMapEntryDefaultInstances() {
44 for (int i = 0; i < map_entry_default_instances_->size(); ++i) {
45 delete map_entry_default_instances_->at(i);
46 }
47 delete map_entry_default_instances_mutex_;
48 delete map_entry_default_instances_;
49}
50
51void InitMapEntryDefaultInstances() {
52 map_entry_default_instances_mutex_ = new Mutex();
53 map_entry_default_instances_ = new vector<MessageLite*>();
54 OnShutdown(&DeleteMapEntryDefaultInstances);
55}
56
57void RegisterMapEntryDefaultInstance(MessageLite* default_instance) {
Jisi Liu885b6122015-02-28 14:51:22 -080058 ::google::protobuf::GoogleOnceInit(&map_entry_default_instances_once_,
Feng Xiao137dd0f2014-12-03 16:31:47 -080059 &InitMapEntryDefaultInstances);
60 MutexLock lock(map_entry_default_instances_mutex_);
61 map_entry_default_instances_->push_back(default_instance);
62}
63
Feng Xiaof157a562014-11-14 11:50:31 -080064MapFieldBase::~MapFieldBase() {
Jisi Liu885b6122015-02-28 14:51:22 -080065 if (repeated_field_ != NULL && arena_ == NULL) delete repeated_field_;
Feng Xiaof157a562014-11-14 11:50:31 -080066}
67
68const RepeatedPtrFieldBase& MapFieldBase::GetRepeatedField() const {
69 SyncRepeatedFieldWithMap();
70 return *repeated_field_;
71}
72
73RepeatedPtrFieldBase* MapFieldBase::MutableRepeatedField() {
74 SyncRepeatedFieldWithMap();
75 SetRepeatedDirty();
76 return repeated_field_;
77}
78
79int MapFieldBase::SpaceUsedExcludingSelf() const {
80 mutex_.Lock();
81 int size = SpaceUsedExcludingSelfNoLock();
82 mutex_.Unlock();
83 return size;
84}
85
86int MapFieldBase::SpaceUsedExcludingSelfNoLock() const {
87 if (repeated_field_ != NULL) {
88 return repeated_field_->SpaceUsedExcludingSelf();
89 } else {
90 return 0;
91 }
92}
93
94void MapFieldBase::InitMetadataOnce() const {
95 GOOGLE_CHECK(entry_descriptor_ != NULL);
96 GOOGLE_CHECK(assign_descriptor_callback_ != NULL);
97 (*assign_descriptor_callback_)();
98}
99
100void MapFieldBase::SetMapDirty() { state_ = STATE_MODIFIED_MAP; }
101
102void MapFieldBase::SetRepeatedDirty() { state_ = STATE_MODIFIED_REPEATED; }
103
104void* MapFieldBase::MutableRepeatedPtrField() const { return repeated_field_; }
105
106void MapFieldBase::SyncRepeatedFieldWithMap() const {
107 Atomic32 state = google::protobuf::internal::NoBarrier_Load(&state_);
108 if (state == STATE_MODIFIED_MAP) {
109 mutex_.Lock();
110 // Double check state, because another thread may have seen the same state
111 // and done the synchronization before the current thread.
112 if (state_ == STATE_MODIFIED_MAP) {
113 SyncRepeatedFieldWithMapNoLock();
114 google::protobuf::internal::NoBarrier_Store(&state_, CLEAN);
115 }
116 mutex_.Unlock();
117 }
118}
119
120void MapFieldBase::SyncRepeatedFieldWithMapNoLock() const {
Jisi Liu885b6122015-02-28 14:51:22 -0800121 if (repeated_field_ == NULL) {
122 repeated_field_ = Arena::Create<RepeatedPtrField<Message> >(arena_, arena_);
123 }
Feng Xiaof157a562014-11-14 11:50:31 -0800124}
125
126void MapFieldBase::SyncMapWithRepeatedField() const {
127 Atomic32 state = google::protobuf::internal::NoBarrier_Load(&state_);
128 if (state == STATE_MODIFIED_REPEATED) {
129 mutex_.Lock();
130 // Double check state, because another thread may have seen the same state
131 // and done the synchronization before the current thread.
132 if (state_ == STATE_MODIFIED_REPEATED) {
133 SyncMapWithRepeatedFieldNoLock();
134 google::protobuf::internal::NoBarrier_Store(&state_, CLEAN);
135 }
136 mutex_.Unlock();
137 }
138}
139
140} // namespace internal
141} // namespace protobuf
142} // namespace google