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 #include <google/protobuf/map_field_inl.h>
33
34 #include <vector>
35
36 #include <google/protobuf/port_def.inc>
37
38 namespace google {
39 namespace protobuf {
40 namespace internal {
41
~MapFieldBase()42 MapFieldBase::~MapFieldBase() {
43 if (repeated_field_ != NULL && arena_ == NULL) delete repeated_field_;
44 }
45
GetRepeatedField() const46 const RepeatedPtrFieldBase& MapFieldBase::GetRepeatedField() const {
47 SyncRepeatedFieldWithMap();
48 return *reinterpret_cast<RepeatedPtrFieldBase*>(repeated_field_);
49 }
50
MutableRepeatedField()51 RepeatedPtrFieldBase* MapFieldBase::MutableRepeatedField() {
52 SyncRepeatedFieldWithMap();
53 SetRepeatedDirty();
54 return reinterpret_cast<RepeatedPtrFieldBase*>(repeated_field_);
55 }
56
SpaceUsedExcludingSelfLong() const57 size_t MapFieldBase::SpaceUsedExcludingSelfLong() const {
58 mutex_.Lock();
59 size_t size = SpaceUsedExcludingSelfNoLock();
60 mutex_.Unlock();
61 return size;
62 }
63
SpaceUsedExcludingSelfNoLock() const64 size_t MapFieldBase::SpaceUsedExcludingSelfNoLock() const {
65 if (repeated_field_ != NULL) {
66 return repeated_field_->SpaceUsedExcludingSelfLong();
67 } else {
68 return 0;
69 }
70 }
71
IsMapValid() const72 bool MapFieldBase::IsMapValid() const {
73 // "Acquire" insures the operation after SyncRepeatedFieldWithMap won't get
74 // executed before state_ is checked.
75 int state = state_.load(std::memory_order_acquire);
76 return state != STATE_MODIFIED_REPEATED;
77 }
78
IsRepeatedFieldValid() const79 bool MapFieldBase::IsRepeatedFieldValid() const {
80 int state = state_.load(std::memory_order_acquire);
81 return state != STATE_MODIFIED_MAP;
82 }
83
SetMapDirty()84 void MapFieldBase::SetMapDirty() {
85 // These are called by (non-const) mutator functions. So by our API it's the
86 // callers responsibility to have these calls properly ordered.
87 state_.store(STATE_MODIFIED_MAP, std::memory_order_relaxed);
88 }
89
SetRepeatedDirty()90 void MapFieldBase::SetRepeatedDirty() {
91 // These are called by (non-const) mutator functions. So by our API it's the
92 // callers responsibility to have these calls properly ordered.
93 state_.store(STATE_MODIFIED_REPEATED, std::memory_order_relaxed);
94 }
95
SyncRepeatedFieldWithMap() const96 void MapFieldBase::SyncRepeatedFieldWithMap() const {
97 // acquire here matches with release below to ensure that we can only see a
98 // value of CLEAN after all previous changes have been synced.
99 switch (state_.load(std::memory_order_acquire)) {
100 case STATE_MODIFIED_MAP:
101 mutex_.Lock();
102 // Double check state, because another thread may have seen the same
103 // state and done the synchronization before the current thread.
104 if (state_.load(std::memory_order_relaxed) == STATE_MODIFIED_MAP) {
105 SyncRepeatedFieldWithMapNoLock();
106 state_.store(CLEAN, std::memory_order_release);
107 }
108 mutex_.Unlock();
109 break;
110 case CLEAN:
111 mutex_.Lock();
112 // Double check state
113 if (state_.load(std::memory_order_relaxed) == CLEAN) {
114 if (repeated_field_ == nullptr) {
115 if (arena_ == nullptr) {
116 repeated_field_ = new RepeatedPtrField<Message>();
117 } else {
118 repeated_field_ =
119 Arena::CreateMessage<RepeatedPtrField<Message> >(arena_);
120 }
121 }
122 state_.store(CLEAN, std::memory_order_release);
123 }
124 mutex_.Unlock();
125 break;
126 default:
127 break;
128 }
129 }
130
SyncRepeatedFieldWithMapNoLock() const131 void MapFieldBase::SyncRepeatedFieldWithMapNoLock() const {
132 if (repeated_field_ == NULL) {
133 repeated_field_ = Arena::CreateMessage<RepeatedPtrField<Message> >(arena_);
134 }
135 }
136
SyncMapWithRepeatedField() const137 void MapFieldBase::SyncMapWithRepeatedField() const {
138 // acquire here matches with release below to ensure that we can only see a
139 // value of CLEAN after all previous changes have been synced.
140 if (state_.load(std::memory_order_acquire) == STATE_MODIFIED_REPEATED) {
141 mutex_.Lock();
142 // Double check state, because another thread may have seen the same state
143 // and done the synchronization before the current thread.
144 if (state_.load(std::memory_order_relaxed) == STATE_MODIFIED_REPEATED) {
145 SyncMapWithRepeatedFieldNoLock();
146 state_.store(CLEAN, std::memory_order_release);
147 }
148 mutex_.Unlock();
149 }
150 }
151
152 // ------------------DynamicMapField------------------
DynamicMapField(const Message * default_entry)153 DynamicMapField::DynamicMapField(const Message* default_entry)
154 : default_entry_(default_entry) {}
155
DynamicMapField(const Message * default_entry,Arena * arena)156 DynamicMapField::DynamicMapField(const Message* default_entry, Arena* arena)
157 : TypeDefinedMapFieldBase<MapKey, MapValueRef>(arena),
158 map_(arena),
159 default_entry_(default_entry) {}
160
~DynamicMapField()161 DynamicMapField::~DynamicMapField() {
162 // DynamicMapField owns map values. Need to delete them before clearing
163 // the map.
164 for (Map<MapKey, MapValueRef>::iterator iter = map_.begin();
165 iter != map_.end(); ++iter) {
166 iter->second.DeleteData();
167 }
168 map_.clear();
169 }
170
size() const171 int DynamicMapField::size() const { return GetMap().size(); }
172
Clear()173 void DynamicMapField::Clear() {
174 Map<MapKey, MapValueRef>* map = &const_cast<DynamicMapField*>(this)->map_;
175 if (MapFieldBase::arena_ == nullptr) {
176 for (Map<MapKey, MapValueRef>::iterator iter = map->begin();
177 iter != map->end(); ++iter) {
178 iter->second.DeleteData();
179 }
180 }
181
182 map->clear();
183
184 if (MapFieldBase::repeated_field_ != nullptr) {
185 MapFieldBase::repeated_field_->Clear();
186 }
187 // Data in map and repeated field are both empty, but we can't set status
188 // CLEAN which will invalidate previous reference to map.
189 MapFieldBase::SetMapDirty();
190 }
191
ContainsMapKey(const MapKey & map_key) const192 bool DynamicMapField::ContainsMapKey(const MapKey& map_key) const {
193 const Map<MapKey, MapValueRef>& map = GetMap();
194 Map<MapKey, MapValueRef>::const_iterator iter = map.find(map_key);
195 return iter != map.end();
196 }
197
AllocateMapValue(MapValueRef * map_val)198 void DynamicMapField::AllocateMapValue(MapValueRef* map_val) {
199 const FieldDescriptor* val_des =
200 default_entry_->GetDescriptor()->FindFieldByName("value");
201 map_val->SetType(val_des->cpp_type());
202 // Allocate memory for the MapValueRef, and initialize to
203 // default value.
204 switch (val_des->cpp_type()) {
205 #define HANDLE_TYPE(CPPTYPE, TYPE) \
206 case FieldDescriptor::CPPTYPE_##CPPTYPE: { \
207 TYPE* value = Arena::Create<TYPE>(MapFieldBase::arena_); \
208 map_val->SetValue(value); \
209 break; \
210 }
211 HANDLE_TYPE(INT32, int32);
212 HANDLE_TYPE(INT64, int64);
213 HANDLE_TYPE(UINT32, uint32);
214 HANDLE_TYPE(UINT64, uint64);
215 HANDLE_TYPE(DOUBLE, double);
216 HANDLE_TYPE(FLOAT, float);
217 HANDLE_TYPE(BOOL, bool);
218 HANDLE_TYPE(STRING, std::string);
219 HANDLE_TYPE(ENUM, int32);
220 #undef HANDLE_TYPE
221 case FieldDescriptor::CPPTYPE_MESSAGE: {
222 const Message& message =
223 default_entry_->GetReflection()->GetMessage(*default_entry_, val_des);
224 Message* value = message.New(MapFieldBase::arena_);
225 map_val->SetValue(value);
226 break;
227 }
228 }
229 }
230
InsertOrLookupMapValue(const MapKey & map_key,MapValueRef * val)231 bool DynamicMapField::InsertOrLookupMapValue(const MapKey& map_key,
232 MapValueRef* val) {
233 // Always use mutable map because users may change the map value by
234 // MapValueRef.
235 Map<MapKey, MapValueRef>* map = MutableMap();
236 Map<MapKey, MapValueRef>::iterator iter = map->find(map_key);
237 if (iter == map->end()) {
238 MapValueRef& map_val = map_[map_key];
239 AllocateMapValue(&map_val);
240 val->CopyFrom(map_val);
241 return true;
242 }
243 // map_key is already in the map. Make sure (*map)[map_key] is not called.
244 // [] may reorder the map and iterators.
245 val->CopyFrom(iter->second);
246 return false;
247 }
248
DeleteMapValue(const MapKey & map_key)249 bool DynamicMapField::DeleteMapValue(const MapKey& map_key) {
250 MapFieldBase::SyncMapWithRepeatedField();
251 Map<MapKey, MapValueRef>::iterator iter = map_.find(map_key);
252 if (iter == map_.end()) {
253 return false;
254 }
255 // Set map dirty only if the delete is successful.
256 MapFieldBase::SetMapDirty();
257 if (MapFieldBase::arena_ == nullptr) {
258 iter->second.DeleteData();
259 }
260 map_.erase(iter);
261 return true;
262 }
263
GetMap() const264 const Map<MapKey, MapValueRef>& DynamicMapField::GetMap() const {
265 MapFieldBase::SyncMapWithRepeatedField();
266 return map_;
267 }
268
MutableMap()269 Map<MapKey, MapValueRef>* DynamicMapField::MutableMap() {
270 MapFieldBase::SyncMapWithRepeatedField();
271 MapFieldBase::SetMapDirty();
272 return &map_;
273 }
274
SetMapIteratorValue(MapIterator * map_iter) const275 void DynamicMapField::SetMapIteratorValue(MapIterator* map_iter) const {
276 Map<MapKey, MapValueRef>::const_iterator iter =
277 TypeDefinedMapFieldBase<MapKey, MapValueRef>::InternalGetIterator(
278 map_iter);
279 if (iter == map_.end()) return;
280 map_iter->key_.CopyFrom(iter->first);
281 map_iter->value_.CopyFrom(iter->second);
282 }
283
MergeFrom(const MapFieldBase & other)284 void DynamicMapField::MergeFrom(const MapFieldBase& other) {
285 GOOGLE_DCHECK(IsMapValid() && other.IsMapValid());
286 Map<MapKey, MapValueRef>* map = MutableMap();
287 const DynamicMapField& other_field =
288 reinterpret_cast<const DynamicMapField&>(other);
289 for (Map<MapKey, MapValueRef>::const_iterator other_it =
290 other_field.map_.begin();
291 other_it != other_field.map_.end(); ++other_it) {
292 Map<MapKey, MapValueRef>::iterator iter = map->find(other_it->first);
293 MapValueRef* map_val;
294 if (iter == map->end()) {
295 map_val = &map_[other_it->first];
296 AllocateMapValue(map_val);
297 } else {
298 map_val = &iter->second;
299 }
300
301 // Copy map value
302 const FieldDescriptor* field_descriptor =
303 default_entry_->GetDescriptor()->FindFieldByName("value");
304 switch (field_descriptor->cpp_type()) {
305 case FieldDescriptor::CPPTYPE_INT32: {
306 map_val->SetInt32Value(other_it->second.GetInt32Value());
307 break;
308 }
309 case FieldDescriptor::CPPTYPE_INT64: {
310 map_val->SetInt64Value(other_it->second.GetInt64Value());
311 break;
312 }
313 case FieldDescriptor::CPPTYPE_UINT32: {
314 map_val->SetUInt32Value(other_it->second.GetUInt32Value());
315 break;
316 }
317 case FieldDescriptor::CPPTYPE_UINT64: {
318 map_val->SetUInt64Value(other_it->second.GetUInt64Value());
319 break;
320 }
321 case FieldDescriptor::CPPTYPE_FLOAT: {
322 map_val->SetFloatValue(other_it->second.GetFloatValue());
323 break;
324 }
325 case FieldDescriptor::CPPTYPE_DOUBLE: {
326 map_val->SetDoubleValue(other_it->second.GetDoubleValue());
327 break;
328 }
329 case FieldDescriptor::CPPTYPE_BOOL: {
330 map_val->SetBoolValue(other_it->second.GetBoolValue());
331 break;
332 }
333 case FieldDescriptor::CPPTYPE_STRING: {
334 map_val->SetStringValue(other_it->second.GetStringValue());
335 break;
336 }
337 case FieldDescriptor::CPPTYPE_ENUM: {
338 map_val->SetEnumValue(other_it->second.GetEnumValue());
339 break;
340 }
341 case FieldDescriptor::CPPTYPE_MESSAGE: {
342 map_val->MutableMessageValue()->CopyFrom(
343 other_it->second.GetMessageValue());
344 break;
345 }
346 }
347 }
348 }
349
Swap(MapFieldBase * other)350 void DynamicMapField::Swap(MapFieldBase* other) {
351 DynamicMapField* other_field = down_cast<DynamicMapField*>(other);
352 std::swap(this->MapFieldBase::repeated_field_, other_field->repeated_field_);
353 map_.swap(other_field->map_);
354 // a relaxed swap of the atomic
355 auto other_state = other_field->state_.load(std::memory_order_relaxed);
356 auto this_state = this->MapFieldBase::state_.load(std::memory_order_relaxed);
357 other_field->state_.store(this_state, std::memory_order_relaxed);
358 this->MapFieldBase::state_.store(other_state, std::memory_order_relaxed);
359 }
360
SyncRepeatedFieldWithMapNoLock() const361 void DynamicMapField::SyncRepeatedFieldWithMapNoLock() const {
362 const Reflection* reflection = default_entry_->GetReflection();
363 const FieldDescriptor* key_des =
364 default_entry_->GetDescriptor()->FindFieldByName("key");
365 const FieldDescriptor* val_des =
366 default_entry_->GetDescriptor()->FindFieldByName("value");
367 if (MapFieldBase::repeated_field_ == NULL) {
368 if (MapFieldBase::arena_ == NULL) {
369 MapFieldBase::repeated_field_ = new RepeatedPtrField<Message>();
370 } else {
371 MapFieldBase::repeated_field_ =
372 Arena::CreateMessage<RepeatedPtrField<Message> >(
373 MapFieldBase::arena_);
374 }
375 }
376
377 MapFieldBase::repeated_field_->Clear();
378
379 for (Map<MapKey, MapValueRef>::const_iterator it = map_.begin();
380 it != map_.end(); ++it) {
381 Message* new_entry = default_entry_->New(MapFieldBase::arena_);
382 MapFieldBase::repeated_field_->AddAllocated(new_entry);
383 const MapKey& map_key = it->first;
384 switch (key_des->cpp_type()) {
385 case FieldDescriptor::CPPTYPE_STRING:
386 reflection->SetString(new_entry, key_des, map_key.GetStringValue());
387 break;
388 case FieldDescriptor::CPPTYPE_INT64:
389 reflection->SetInt64(new_entry, key_des, map_key.GetInt64Value());
390 break;
391 case FieldDescriptor::CPPTYPE_INT32:
392 reflection->SetInt32(new_entry, key_des, map_key.GetInt32Value());
393 break;
394 case FieldDescriptor::CPPTYPE_UINT64:
395 reflection->SetUInt64(new_entry, key_des, map_key.GetUInt64Value());
396 break;
397 case FieldDescriptor::CPPTYPE_UINT32:
398 reflection->SetUInt32(new_entry, key_des, map_key.GetUInt32Value());
399 break;
400 case FieldDescriptor::CPPTYPE_BOOL:
401 reflection->SetBool(new_entry, key_des, map_key.GetBoolValue());
402 break;
403 case FieldDescriptor::CPPTYPE_DOUBLE:
404 case FieldDescriptor::CPPTYPE_FLOAT:
405 case FieldDescriptor::CPPTYPE_ENUM:
406 case FieldDescriptor::CPPTYPE_MESSAGE:
407 GOOGLE_LOG(FATAL) << "Can't get here.";
408 break;
409 }
410 const MapValueRef& map_val = it->second;
411 switch (val_des->cpp_type()) {
412 case FieldDescriptor::CPPTYPE_STRING:
413 reflection->SetString(new_entry, val_des, map_val.GetStringValue());
414 break;
415 case FieldDescriptor::CPPTYPE_INT64:
416 reflection->SetInt64(new_entry, val_des, map_val.GetInt64Value());
417 break;
418 case FieldDescriptor::CPPTYPE_INT32:
419 reflection->SetInt32(new_entry, val_des, map_val.GetInt32Value());
420 break;
421 case FieldDescriptor::CPPTYPE_UINT64:
422 reflection->SetUInt64(new_entry, val_des, map_val.GetUInt64Value());
423 break;
424 case FieldDescriptor::CPPTYPE_UINT32:
425 reflection->SetUInt32(new_entry, val_des, map_val.GetUInt32Value());
426 break;
427 case FieldDescriptor::CPPTYPE_BOOL:
428 reflection->SetBool(new_entry, val_des, map_val.GetBoolValue());
429 break;
430 case FieldDescriptor::CPPTYPE_DOUBLE:
431 reflection->SetDouble(new_entry, val_des, map_val.GetDoubleValue());
432 break;
433 case FieldDescriptor::CPPTYPE_FLOAT:
434 reflection->SetFloat(new_entry, val_des, map_val.GetFloatValue());
435 break;
436 case FieldDescriptor::CPPTYPE_ENUM:
437 reflection->SetEnumValue(new_entry, val_des, map_val.GetEnumValue());
438 break;
439 case FieldDescriptor::CPPTYPE_MESSAGE: {
440 const Message& message = map_val.GetMessageValue();
441 reflection->MutableMessage(new_entry, val_des)->CopyFrom(message);
442 break;
443 }
444 }
445 }
446 }
447
SyncMapWithRepeatedFieldNoLock() const448 void DynamicMapField::SyncMapWithRepeatedFieldNoLock() const {
449 Map<MapKey, MapValueRef>* map = &const_cast<DynamicMapField*>(this)->map_;
450 const Reflection* reflection = default_entry_->GetReflection();
451 const FieldDescriptor* key_des =
452 default_entry_->GetDescriptor()->FindFieldByName("key");
453 const FieldDescriptor* val_des =
454 default_entry_->GetDescriptor()->FindFieldByName("value");
455 // DynamicMapField owns map values. Need to delete them before clearing
456 // the map.
457 if (MapFieldBase::arena_ == nullptr) {
458 for (Map<MapKey, MapValueRef>::iterator iter = map->begin();
459 iter != map->end(); ++iter) {
460 iter->second.DeleteData();
461 }
462 }
463 map->clear();
464 for (RepeatedPtrField<Message>::iterator it =
465 MapFieldBase::repeated_field_->begin();
466 it != MapFieldBase::repeated_field_->end(); ++it) {
467 // MapKey type will be set later.
468 MapKey map_key;
469 switch (key_des->cpp_type()) {
470 case FieldDescriptor::CPPTYPE_STRING:
471 map_key.SetStringValue(reflection->GetString(*it, key_des));
472 break;
473 case FieldDescriptor::CPPTYPE_INT64:
474 map_key.SetInt64Value(reflection->GetInt64(*it, key_des));
475 break;
476 case FieldDescriptor::CPPTYPE_INT32:
477 map_key.SetInt32Value(reflection->GetInt32(*it, key_des));
478 break;
479 case FieldDescriptor::CPPTYPE_UINT64:
480 map_key.SetUInt64Value(reflection->GetUInt64(*it, key_des));
481 break;
482 case FieldDescriptor::CPPTYPE_UINT32:
483 map_key.SetUInt32Value(reflection->GetUInt32(*it, key_des));
484 break;
485 case FieldDescriptor::CPPTYPE_BOOL:
486 map_key.SetBoolValue(reflection->GetBool(*it, key_des));
487 break;
488 case FieldDescriptor::CPPTYPE_DOUBLE:
489 case FieldDescriptor::CPPTYPE_FLOAT:
490 case FieldDescriptor::CPPTYPE_ENUM:
491 case FieldDescriptor::CPPTYPE_MESSAGE:
492 GOOGLE_LOG(FATAL) << "Can't get here.";
493 break;
494 }
495
496 if (MapFieldBase::arena_ == nullptr) {
497 // Remove existing map value with same key.
498 Map<MapKey, MapValueRef>::iterator iter = map->find(map_key);
499 if (iter != map->end()) {
500 iter->second.DeleteData();
501 }
502 }
503
504 MapValueRef& map_val = (*map)[map_key];
505 map_val.SetType(val_des->cpp_type());
506 switch (val_des->cpp_type()) {
507 #define HANDLE_TYPE(CPPTYPE, TYPE, METHOD) \
508 case FieldDescriptor::CPPTYPE_##CPPTYPE: { \
509 TYPE* value = Arena::Create<TYPE>(MapFieldBase::arena_); \
510 *value = reflection->Get##METHOD(*it, val_des); \
511 map_val.SetValue(value); \
512 break; \
513 }
514 HANDLE_TYPE(INT32, int32, Int32);
515 HANDLE_TYPE(INT64, int64, Int64);
516 HANDLE_TYPE(UINT32, uint32, UInt32);
517 HANDLE_TYPE(UINT64, uint64, UInt64);
518 HANDLE_TYPE(DOUBLE, double, Double);
519 HANDLE_TYPE(FLOAT, float, Float);
520 HANDLE_TYPE(BOOL, bool, Bool);
521 HANDLE_TYPE(STRING, std::string, String);
522 HANDLE_TYPE(ENUM, int32, EnumValue);
523 #undef HANDLE_TYPE
524 case FieldDescriptor::CPPTYPE_MESSAGE: {
525 const Message& message = reflection->GetMessage(*it, val_des);
526 Message* value = message.New(MapFieldBase::arena_);
527 value->CopyFrom(message);
528 map_val.SetValue(value);
529 break;
530 }
531 }
532 }
533 }
534
SpaceUsedExcludingSelfNoLock() const535 size_t DynamicMapField::SpaceUsedExcludingSelfNoLock() const {
536 size_t size = 0;
537 if (MapFieldBase::repeated_field_ != NULL) {
538 size += MapFieldBase::repeated_field_->SpaceUsedExcludingSelfLong();
539 }
540 size += sizeof(map_);
541 size_t map_size = map_.size();
542 if (map_size) {
543 Map<MapKey, MapValueRef>::const_iterator it = map_.begin();
544 size += sizeof(it->first) * map_size;
545 size += sizeof(it->second) * map_size;
546 // If key is string, add the allocated space.
547 if (it->first.type() == FieldDescriptor::CPPTYPE_STRING) {
548 size += sizeof(std::string) * map_size;
549 }
550 // Add the allocated space in MapValueRef.
551 switch (it->second.type()) {
552 #define HANDLE_TYPE(CPPTYPE, TYPE) \
553 case FieldDescriptor::CPPTYPE_##CPPTYPE: { \
554 size += sizeof(TYPE) * map_size; \
555 break; \
556 }
557 HANDLE_TYPE(INT32, int32);
558 HANDLE_TYPE(INT64, int64);
559 HANDLE_TYPE(UINT32, uint32);
560 HANDLE_TYPE(UINT64, uint64);
561 HANDLE_TYPE(DOUBLE, double);
562 HANDLE_TYPE(FLOAT, float);
563 HANDLE_TYPE(BOOL, bool);
564 HANDLE_TYPE(STRING, std::string);
565 HANDLE_TYPE(ENUM, int32);
566 #undef HANDLE_TYPE
567 case FieldDescriptor::CPPTYPE_MESSAGE: {
568 while (it != map_.end()) {
569 const Message& message = it->second.GetMessageValue();
570 size += message.GetReflection()->SpaceUsedLong(message);
571 ++it;
572 }
573 break;
574 }
575 }
576 }
577 return size;
578 }
579
580 } // namespace internal
581 } // namespace protobuf
582 } // namespace google
583