map_field.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. namespace google {
  34. namespace protobuf {
  35. namespace internal {
  36. ProtobufOnceType map_entry_default_instances_once_;
  37. Mutex* map_entry_default_instances_mutex_;
  38. vector<MessageLite*>* map_entry_default_instances_;
  39. void DeleteMapEntryDefaultInstances() {
  40. for (int i = 0; i < map_entry_default_instances_->size(); ++i) {
  41. delete map_entry_default_instances_->at(i);
  42. }
  43. delete map_entry_default_instances_mutex_;
  44. delete map_entry_default_instances_;
  45. }
  46. void InitMapEntryDefaultInstances() {
  47. map_entry_default_instances_mutex_ = new Mutex();
  48. map_entry_default_instances_ = new vector<MessageLite*>();
  49. OnShutdown(&DeleteMapEntryDefaultInstances);
  50. }
  51. void RegisterMapEntryDefaultInstance(MessageLite* default_instance) {
  52. ::google::protobuf::GoogleOnceInit(&map_entry_default_instances_once_,
  53. &InitMapEntryDefaultInstances);
  54. MutexLock lock(map_entry_default_instances_mutex_);
  55. map_entry_default_instances_->push_back(default_instance);
  56. }
  57. MapFieldBase::~MapFieldBase() {
  58. if (repeated_field_ != NULL && arena_ == NULL) delete repeated_field_;
  59. }
  60. const RepeatedPtrFieldBase& MapFieldBase::GetRepeatedField() const {
  61. SyncRepeatedFieldWithMap();
  62. return *repeated_field_;
  63. }
  64. RepeatedPtrFieldBase* MapFieldBase::MutableRepeatedField() {
  65. SyncRepeatedFieldWithMap();
  66. SetRepeatedDirty();
  67. return repeated_field_;
  68. }
  69. int MapFieldBase::SpaceUsedExcludingSelf() const {
  70. mutex_.Lock();
  71. int size = SpaceUsedExcludingSelfNoLock();
  72. mutex_.Unlock();
  73. return size;
  74. }
  75. int MapFieldBase::SpaceUsedExcludingSelfNoLock() const {
  76. if (repeated_field_ != NULL) {
  77. return repeated_field_->SpaceUsedExcludingSelf();
  78. } else {
  79. return 0;
  80. }
  81. }
  82. void MapFieldBase::InitMetadataOnce() const {
  83. GOOGLE_CHECK(entry_descriptor_ != NULL);
  84. GOOGLE_CHECK(assign_descriptor_callback_ != NULL);
  85. (*assign_descriptor_callback_)();
  86. }
  87. void MapFieldBase::SetMapDirty() { state_ = STATE_MODIFIED_MAP; }
  88. void MapFieldBase::SetRepeatedDirty() { state_ = STATE_MODIFIED_REPEATED; }
  89. void* MapFieldBase::MutableRepeatedPtrField() const { return repeated_field_; }
  90. void MapFieldBase::SyncRepeatedFieldWithMap() const {
  91. // "Acquire" insures the operation after SyncRepeatedFieldWithMap won't get
  92. // executed before state_ is checked.
  93. Atomic32 state = google::protobuf::internal::Acquire_Load(&state_);
  94. if (state == STATE_MODIFIED_MAP) {
  95. mutex_.Lock();
  96. // Double check state, because another thread may have seen the same state
  97. // and done the synchronization before the current thread.
  98. if (state_ == STATE_MODIFIED_MAP) {
  99. SyncRepeatedFieldWithMapNoLock();
  100. // "Release" insures state_ can only be changed "after"
  101. // SyncRepeatedFieldWithMapNoLock is finished.
  102. google::protobuf::internal::Release_Store(&state_, CLEAN);
  103. }
  104. mutex_.Unlock();
  105. }
  106. }
  107. void MapFieldBase::SyncRepeatedFieldWithMapNoLock() const {
  108. if (repeated_field_ == NULL) {
  109. repeated_field_ = Arena::CreateMessage<RepeatedPtrField<Message> >(arena_);
  110. }
  111. }
  112. void MapFieldBase::SyncMapWithRepeatedField() const {
  113. // "Acquire" insures the operation after SyncMapWithRepeatedField won't get
  114. // executed before state_ is checked.
  115. Atomic32 state = google::protobuf::internal::Acquire_Load(&state_);
  116. if (state == STATE_MODIFIED_REPEATED) {
  117. mutex_.Lock();
  118. // Double check state, because another thread may have seen the same state
  119. // and done the synchronization before the current thread.
  120. if (state_ == STATE_MODIFIED_REPEATED) {
  121. SyncMapWithRepeatedFieldNoLock();
  122. // "Release" insures state_ can only be changed "after"
  123. // SyncRepeatedFieldWithMapNoLock is finished.
  124. google::protobuf::internal::Release_Store(&state_, CLEAN);
  125. }
  126. mutex_.Unlock();
  127. }
  128. }
  129. // ------------------DynamicMapField------------------
  130. DynamicMapField::DynamicMapField(const Message* default_entry)
  131. : default_entry_(default_entry) {
  132. }
  133. DynamicMapField::DynamicMapField(const Message* default_entry,
  134. Arena* arena)
  135. : TypeDefinedMapFieldBase<MapKey, MapValueRef>(arena),
  136. default_entry_(default_entry) {
  137. }
  138. DynamicMapField::~DynamicMapField() {
  139. // DynamicMapField owns map values. Need to delete them before clearing
  140. // the map.
  141. for (Map<MapKey, MapValueRef>::iterator iter = map_.begin();
  142. iter != map_.end(); ++iter) {
  143. iter->second.DeleteData();
  144. }
  145. map_.clear();
  146. }
  147. int DynamicMapField::size() const {
  148. return GetMap().size();
  149. }
  150. bool DynamicMapField::ContainsMapKey(
  151. const MapKey& map_key) const {
  152. const Map<MapKey, MapValueRef>& map = GetMap();
  153. Map<MapKey, MapValueRef>::const_iterator iter = map.find(map_key);
  154. return iter != map.end();
  155. }
  156. bool DynamicMapField::InsertMapValue(
  157. const MapKey& map_key, MapValueRef* val) {
  158. bool result = false;
  159. MapValueRef& map_val = (*MutableMap())[map_key];
  160. // If map_val.data_ is not set, it is newly inserted by map_[map_key].
  161. if (map_val.data_ == NULL) {
  162. result = true;
  163. const FieldDescriptor* val_des =
  164. default_entry_->GetDescriptor()->FindFieldByName("value");
  165. map_val.SetType(val_des->cpp_type());
  166. // Allocate momery for the inserted MapValueRef, and initialize to
  167. // default value.
  168. switch (val_des->cpp_type()) {
  169. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  170. case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \
  171. TYPE * value = new TYPE(); \
  172. map_val.SetValue(value); \
  173. break; \
  174. }
  175. HANDLE_TYPE(INT32, int32);
  176. HANDLE_TYPE(INT64, int64);
  177. HANDLE_TYPE(UINT32, uint32);
  178. HANDLE_TYPE(UINT64, uint64);
  179. HANDLE_TYPE(DOUBLE, double);
  180. HANDLE_TYPE(FLOAT, float);
  181. HANDLE_TYPE(BOOL, bool);
  182. HANDLE_TYPE(STRING, string);
  183. HANDLE_TYPE(ENUM, int32);
  184. #undef HANDLE_TYPE
  185. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
  186. const Message& message = default_entry_->GetReflection()->GetMessage(
  187. *default_entry_, val_des);
  188. Message* value = message.New();
  189. map_val.SetValue(value);
  190. break;
  191. }
  192. }
  193. }
  194. val->CopyFrom(map_val);
  195. return result;
  196. }
  197. bool DynamicMapField::DeleteMapValue(const MapKey& map_key) {
  198. MapFieldBase::SyncMapWithRepeatedField();
  199. Map<MapKey, MapValueRef>::iterator iter = map_.find(map_key);
  200. if (iter == map_.end()) {
  201. return false;
  202. }
  203. // Set map dirty only if the delete is successful.
  204. MapFieldBase::SetMapDirty();
  205. iter->second.DeleteData();
  206. map_.erase(iter);
  207. return true;
  208. }
  209. const Map<MapKey, MapValueRef>& DynamicMapField::GetMap() const {
  210. MapFieldBase::SyncMapWithRepeatedField();
  211. return map_;
  212. }
  213. Map<MapKey, MapValueRef>* DynamicMapField::MutableMap() {
  214. MapFieldBase::SyncMapWithRepeatedField();
  215. MapFieldBase::SetMapDirty();
  216. return &map_;
  217. }
  218. void DynamicMapField::SetMapIteratorValue(MapIterator* map_iter) const {
  219. Map<MapKey, MapValueRef>::const_iterator iter =
  220. TypeDefinedMapFieldBase<MapKey, MapValueRef>::InternalGetIterator(
  221. map_iter);
  222. if (iter == map_.end()) return;
  223. map_iter->key_.CopyFrom(iter->first);
  224. map_iter->value_.CopyFrom(iter->second);
  225. }
  226. void DynamicMapField::SyncRepeatedFieldWithMapNoLock() const {
  227. const Reflection* reflection = default_entry_->GetReflection();
  228. const FieldDescriptor* key_des =
  229. default_entry_->GetDescriptor()->FindFieldByName("key");
  230. const FieldDescriptor* val_des =
  231. default_entry_->GetDescriptor()->FindFieldByName("value");
  232. if (MapFieldBase::repeated_field_ == NULL) {
  233. if (MapFieldBase::arena_ == NULL) {
  234. MapFieldBase::repeated_field_ = new RepeatedPtrField<Message>();
  235. } else {
  236. MapFieldBase::repeated_field_ =
  237. Arena::CreateMessage<RepeatedPtrField<Message> >(
  238. MapFieldBase::arena_);
  239. }
  240. }
  241. MapFieldBase::repeated_field_->Clear();
  242. for (Map<MapKey, MapValueRef>::const_iterator it = map_.begin();
  243. it != map_.end(); ++it) {
  244. Message* new_entry = default_entry_->New();
  245. MapFieldBase::repeated_field_->AddAllocated(new_entry);
  246. const MapKey& map_key = it->first;
  247. switch (key_des->cpp_type()) {
  248. case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
  249. reflection->SetString(new_entry, key_des, map_key.GetStringValue());
  250. break;
  251. case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
  252. reflection->SetInt64(new_entry, key_des, map_key.GetInt64Value());
  253. break;
  254. case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
  255. reflection->SetInt32(new_entry, key_des, map_key.GetInt32Value());
  256. break;
  257. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
  258. reflection->SetUInt64(new_entry, key_des, map_key.GetUInt64Value());
  259. break;
  260. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
  261. reflection->SetUInt32(new_entry, key_des, map_key.GetUInt32Value());
  262. break;
  263. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
  264. reflection->SetBool(new_entry, key_des, map_key.GetBoolValue());
  265. break;
  266. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
  267. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
  268. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
  269. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
  270. GOOGLE_LOG(FATAL) << "Can't get here.";
  271. break;
  272. }
  273. const MapValueRef& map_val = it->second;
  274. switch (val_des->cpp_type()) {
  275. case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
  276. reflection->SetString(new_entry, val_des, map_val.GetStringValue());
  277. break;
  278. case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
  279. reflection->SetInt64(new_entry, val_des, map_val.GetInt64Value());
  280. break;
  281. case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
  282. reflection->SetInt32(new_entry, val_des, map_val.GetInt32Value());
  283. break;
  284. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
  285. reflection->SetUInt64(new_entry, val_des, map_val.GetUInt64Value());
  286. break;
  287. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
  288. reflection->SetUInt32(new_entry, val_des, map_val.GetUInt32Value());
  289. break;
  290. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
  291. reflection->SetBool(new_entry, val_des, map_val.GetBoolValue());
  292. break;
  293. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
  294. reflection->SetDouble(new_entry, val_des, map_val.GetDoubleValue());
  295. break;
  296. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
  297. reflection->SetFloat(new_entry, val_des, map_val.GetFloatValue());
  298. break;
  299. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
  300. reflection->SetEnumValue(new_entry, val_des, map_val.GetEnumValue());
  301. break;
  302. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
  303. const Message& message = map_val.GetMessageValue();
  304. reflection->MutableMessage(new_entry, val_des)->CopyFrom(message);
  305. break;
  306. }
  307. }
  308. }
  309. }
  310. void DynamicMapField::SyncMapWithRepeatedFieldNoLock() const {
  311. Map<MapKey, MapValueRef>* map = &const_cast<DynamicMapField*>(this)->map_;
  312. const Reflection* reflection = default_entry_->GetReflection();
  313. const FieldDescriptor* key_des =
  314. default_entry_->GetDescriptor()->FindFieldByName("key");
  315. const FieldDescriptor* val_des =
  316. default_entry_->GetDescriptor()->FindFieldByName("value");
  317. // DynamicMapField owns map values. Need to delete them before clearing
  318. // the map.
  319. for (Map<MapKey, MapValueRef>::iterator iter = map->begin();
  320. iter != map->end(); ++iter) {
  321. iter->second.DeleteData();
  322. }
  323. map->clear();
  324. for (RepeatedPtrField<Message>::iterator it =
  325. MapFieldBase::repeated_field_->begin();
  326. it != MapFieldBase::repeated_field_->end(); ++it) {
  327. MapKey map_key;
  328. switch (key_des->cpp_type()) {
  329. case google::protobuf::FieldDescriptor::CPPTYPE_STRING:
  330. map_key.SetStringValue(reflection->GetString(*it, key_des));
  331. break;
  332. case google::protobuf::FieldDescriptor::CPPTYPE_INT64:
  333. map_key.SetInt64Value(reflection->GetInt64(*it, key_des));
  334. break;
  335. case google::protobuf::FieldDescriptor::CPPTYPE_INT32:
  336. map_key.SetInt32Value(reflection->GetInt32(*it, key_des));
  337. break;
  338. case google::protobuf::FieldDescriptor::CPPTYPE_UINT64:
  339. map_key.SetUInt64Value(reflection->GetUInt64(*it, key_des));
  340. break;
  341. case google::protobuf::FieldDescriptor::CPPTYPE_UINT32:
  342. map_key.SetUInt32Value(reflection->GetUInt32(*it, key_des));
  343. break;
  344. case google::protobuf::FieldDescriptor::CPPTYPE_BOOL:
  345. map_key.SetBoolValue(reflection->GetBool(*it, key_des));
  346. break;
  347. case google::protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
  348. case google::protobuf::FieldDescriptor::CPPTYPE_FLOAT:
  349. case google::protobuf::FieldDescriptor::CPPTYPE_ENUM:
  350. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
  351. GOOGLE_LOG(FATAL) << "Can't get here.";
  352. break;
  353. }
  354. MapValueRef& map_val = (*map)[map_key];
  355. map_val.SetType(val_des->cpp_type());
  356. switch (val_des->cpp_type()) {
  357. #define HANDLE_TYPE(CPPTYPE, TYPE, METHOD) \
  358. case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \
  359. TYPE * value = new TYPE; \
  360. *value = reflection->Get##METHOD(*it, val_des); \
  361. map_val.SetValue(value); \
  362. break; \
  363. }
  364. HANDLE_TYPE(INT32, int32, Int32);
  365. HANDLE_TYPE(INT64, int64, Int64);
  366. HANDLE_TYPE(UINT32, uint32, UInt32);
  367. HANDLE_TYPE(UINT64, uint64, UInt64);
  368. HANDLE_TYPE(DOUBLE, double, Double);
  369. HANDLE_TYPE(FLOAT, float, Float);
  370. HANDLE_TYPE(BOOL, bool, Bool);
  371. HANDLE_TYPE(STRING, string, String);
  372. HANDLE_TYPE(ENUM, int32, EnumValue);
  373. #undef HANDLE_TYPE
  374. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
  375. const Message& message = reflection->GetMessage(*it, val_des);
  376. Message* value = message.New();
  377. value->CopyFrom(message);
  378. map_val.SetValue(value);
  379. break;
  380. }
  381. }
  382. }
  383. }
  384. int DynamicMapField::SpaceUsedExcludingSelfNoLock() const {
  385. int size = 0;
  386. if (MapFieldBase::repeated_field_ != NULL) {
  387. size += MapFieldBase::repeated_field_->SpaceUsedExcludingSelf();
  388. }
  389. size += sizeof(map_);
  390. int map_size = map_.size();
  391. if (map_size) {
  392. Map<MapKey, MapValueRef>::const_iterator it = map_.begin();
  393. size += sizeof(it->first) * map_size;
  394. size += sizeof(it->second) * map_size;
  395. // If key is string, add the allocated space.
  396. if (it->first.type() == google::protobuf::FieldDescriptor::CPPTYPE_STRING) {
  397. size += sizeof(string) * map_size;
  398. }
  399. // Add the allocated space in MapValueRef.
  400. switch (it->second.type()) {
  401. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  402. case google::protobuf::FieldDescriptor::CPPTYPE_##CPPTYPE: { \
  403. size += sizeof(TYPE) * map_size; \
  404. break; \
  405. }
  406. HANDLE_TYPE(INT32, int32);
  407. HANDLE_TYPE(INT64, int64);
  408. HANDLE_TYPE(UINT32, uint32);
  409. HANDLE_TYPE(UINT64, uint64);
  410. HANDLE_TYPE(DOUBLE, double);
  411. HANDLE_TYPE(FLOAT, float);
  412. HANDLE_TYPE(BOOL, bool);
  413. HANDLE_TYPE(STRING, string);
  414. HANDLE_TYPE(ENUM, int32);
  415. #undef HANDLE_TYPE
  416. case google::protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
  417. while (it != map_.end()) {
  418. const Message& message = it->second.GetMessageValue();
  419. size += message.GetReflection()->SpaceUsed(message);
  420. ++it;
  421. }
  422. break;
  423. }
  424. }
  425. }
  426. return size;
  427. }
  428. } // namespace internal
  429. } // namespace protobuf
  430. } // namespace google