诸暨麻将添加redis
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

583 строки
21 KiB

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