Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 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 | |
| 31 | #include <google/protobuf/map_field.h> |
| 32 | |
Feng Xiao | 137dd0f | 2014-12-03 16:31:47 -0800 | [diff] [blame] | 33 | #include <vector> |
| 34 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 35 | namespace google { |
| 36 | namespace protobuf { |
| 37 | namespace internal { |
| 38 | |
Feng Xiao | 137dd0f | 2014-12-03 16:31:47 -0800 | [diff] [blame] | 39 | ProtobufOnceType map_entry_default_instances_once_; |
| 40 | Mutex* map_entry_default_instances_mutex_; |
| 41 | vector<MessageLite*>* map_entry_default_instances_; |
| 42 | |
| 43 | void 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 | |
| 51 | void InitMapEntryDefaultInstances() { |
| 52 | map_entry_default_instances_mutex_ = new Mutex(); |
| 53 | map_entry_default_instances_ = new vector<MessageLite*>(); |
| 54 | OnShutdown(&DeleteMapEntryDefaultInstances); |
| 55 | } |
| 56 | |
| 57 | void RegisterMapEntryDefaultInstance(MessageLite* default_instance) { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 58 | ::google::protobuf::GoogleOnceInit(&map_entry_default_instances_once_, |
Feng Xiao | 137dd0f | 2014-12-03 16:31:47 -0800 | [diff] [blame] | 59 | &InitMapEntryDefaultInstances); |
| 60 | MutexLock lock(map_entry_default_instances_mutex_); |
| 61 | map_entry_default_instances_->push_back(default_instance); |
| 62 | } |
| 63 | |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 64 | MapFieldBase::~MapFieldBase() { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 65 | if (repeated_field_ != NULL && arena_ == NULL) delete repeated_field_; |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | const RepeatedPtrFieldBase& MapFieldBase::GetRepeatedField() const { |
| 69 | SyncRepeatedFieldWithMap(); |
| 70 | return *repeated_field_; |
| 71 | } |
| 72 | |
| 73 | RepeatedPtrFieldBase* MapFieldBase::MutableRepeatedField() { |
| 74 | SyncRepeatedFieldWithMap(); |
| 75 | SetRepeatedDirty(); |
| 76 | return repeated_field_; |
| 77 | } |
| 78 | |
| 79 | int MapFieldBase::SpaceUsedExcludingSelf() const { |
| 80 | mutex_.Lock(); |
| 81 | int size = SpaceUsedExcludingSelfNoLock(); |
| 82 | mutex_.Unlock(); |
| 83 | return size; |
| 84 | } |
| 85 | |
| 86 | int MapFieldBase::SpaceUsedExcludingSelfNoLock() const { |
| 87 | if (repeated_field_ != NULL) { |
| 88 | return repeated_field_->SpaceUsedExcludingSelf(); |
| 89 | } else { |
| 90 | return 0; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void MapFieldBase::InitMetadataOnce() const { |
| 95 | GOOGLE_CHECK(entry_descriptor_ != NULL); |
| 96 | GOOGLE_CHECK(assign_descriptor_callback_ != NULL); |
| 97 | (*assign_descriptor_callback_)(); |
| 98 | } |
| 99 | |
| 100 | void MapFieldBase::SetMapDirty() { state_ = STATE_MODIFIED_MAP; } |
| 101 | |
| 102 | void MapFieldBase::SetRepeatedDirty() { state_ = STATE_MODIFIED_REPEATED; } |
| 103 | |
| 104 | void* MapFieldBase::MutableRepeatedPtrField() const { return repeated_field_; } |
| 105 | |
| 106 | void 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 | |
| 120 | void MapFieldBase::SyncRepeatedFieldWithMapNoLock() const { |
Jisi Liu | 885b612 | 2015-02-28 14:51:22 -0800 | [diff] [blame^] | 121 | if (repeated_field_ == NULL) { |
| 122 | repeated_field_ = Arena::Create<RepeatedPtrField<Message> >(arena_, arena_); |
| 123 | } |
Feng Xiao | f157a56 | 2014-11-14 11:50:31 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | void 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 |