PageRenderTime 62ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/src/breakpad/src/third_party/protobuf/protobuf/src/google/protobuf/dynamic_message.cc

https://gitlab.com/x33n/phantomjs
C++ | 558 lines | 352 code | 83 blank | 123 comment | 54 complexity | e6805a5a00b1c7fcceeb9979322782be MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // DynamicMessage is implemented by constructing a data structure which
  35. // has roughly the same memory layout as a generated message would have.
  36. // Then, we use GeneratedMessageReflection to implement our reflection
  37. // interface. All the other operations we need to implement (e.g.
  38. // parsing, copying, etc.) are already implemented in terms of
  39. // Reflection, so the rest is easy.
  40. //
  41. // The up side of this strategy is that it's very efficient. We don't
  42. // need to use hash_maps or generic representations of fields. The
  43. // down side is that this is a low-level memory management hack which
  44. // can be tricky to get right.
  45. //
  46. // As mentioned in the header, we only expose a DynamicMessageFactory
  47. // publicly, not the DynamicMessage class itself. This is because
  48. // GenericMessageReflection wants to have a pointer to a "default"
  49. // copy of the class, with all fields initialized to their default
  50. // values. We only want to construct one of these per message type,
  51. // so DynamicMessageFactory stores a cache of default messages for
  52. // each type it sees (each unique Descriptor pointer). The code
  53. // refers to the "default" copy of the class as the "prototype".
  54. //
  55. // Note on memory allocation: This module often calls "operator new()"
  56. // to allocate untyped memory, rather than calling something like
  57. // "new uint8[]". This is because "operator new()" means "Give me some
  58. // space which I can use as I please." while "new uint8[]" means "Give
  59. // me an array of 8-bit integers.". In practice, the later may return
  60. // a pointer that is not aligned correctly for general use. I believe
  61. // Item 8 of "More Effective C++" discusses this in more detail, though
  62. // I don't have the book on me right now so I'm not sure.
  63. #include <algorithm>
  64. #include <google/protobuf/stubs/hash.h>
  65. #include <google/protobuf/stubs/common.h>
  66. #include <google/protobuf/dynamic_message.h>
  67. #include <google/protobuf/descriptor.h>
  68. #include <google/protobuf/descriptor.pb.h>
  69. #include <google/protobuf/generated_message_util.h>
  70. #include <google/protobuf/generated_message_reflection.h>
  71. #include <google/protobuf/reflection_ops.h>
  72. #include <google/protobuf/repeated_field.h>
  73. #include <google/protobuf/extension_set.h>
  74. #include <google/protobuf/wire_format.h>
  75. namespace google {
  76. namespace protobuf {
  77. using internal::WireFormat;
  78. using internal::ExtensionSet;
  79. using internal::GeneratedMessageReflection;
  80. // ===================================================================
  81. // Some helper tables and functions...
  82. namespace {
  83. // Compute the byte size of the in-memory representation of the field.
  84. int FieldSpaceUsed(const FieldDescriptor* field) {
  85. typedef FieldDescriptor FD; // avoid line wrapping
  86. if (field->label() == FD::LABEL_REPEATED) {
  87. switch (field->cpp_type()) {
  88. case FD::CPPTYPE_INT32 : return sizeof(RepeatedField<int32 >);
  89. case FD::CPPTYPE_INT64 : return sizeof(RepeatedField<int64 >);
  90. case FD::CPPTYPE_UINT32 : return sizeof(RepeatedField<uint32 >);
  91. case FD::CPPTYPE_UINT64 : return sizeof(RepeatedField<uint64 >);
  92. case FD::CPPTYPE_DOUBLE : return sizeof(RepeatedField<double >);
  93. case FD::CPPTYPE_FLOAT : return sizeof(RepeatedField<float >);
  94. case FD::CPPTYPE_BOOL : return sizeof(RepeatedField<bool >);
  95. case FD::CPPTYPE_ENUM : return sizeof(RepeatedField<int >);
  96. case FD::CPPTYPE_MESSAGE: return sizeof(RepeatedPtrField<Message>);
  97. case FD::CPPTYPE_STRING:
  98. switch (field->options().ctype()) {
  99. default: // TODO(kenton): Support other string reps.
  100. case FieldOptions::STRING:
  101. return sizeof(RepeatedPtrField<string>);
  102. }
  103. break;
  104. }
  105. } else {
  106. switch (field->cpp_type()) {
  107. case FD::CPPTYPE_INT32 : return sizeof(int32 );
  108. case FD::CPPTYPE_INT64 : return sizeof(int64 );
  109. case FD::CPPTYPE_UINT32 : return sizeof(uint32 );
  110. case FD::CPPTYPE_UINT64 : return sizeof(uint64 );
  111. case FD::CPPTYPE_DOUBLE : return sizeof(double );
  112. case FD::CPPTYPE_FLOAT : return sizeof(float );
  113. case FD::CPPTYPE_BOOL : return sizeof(bool );
  114. case FD::CPPTYPE_ENUM : return sizeof(int );
  115. case FD::CPPTYPE_MESSAGE: return sizeof(Message*);
  116. case FD::CPPTYPE_STRING:
  117. switch (field->options().ctype()) {
  118. default: // TODO(kenton): Support other string reps.
  119. case FieldOptions::STRING:
  120. return sizeof(string*);
  121. }
  122. break;
  123. }
  124. }
  125. GOOGLE_LOG(DFATAL) << "Can't get here.";
  126. return 0;
  127. }
  128. inline int DivideRoundingUp(int i, int j) {
  129. return (i + (j - 1)) / j;
  130. }
  131. static const int kSafeAlignment = sizeof(uint64);
  132. inline int AlignTo(int offset, int alignment) {
  133. return DivideRoundingUp(offset, alignment) * alignment;
  134. }
  135. // Rounds the given byte offset up to the next offset aligned such that any
  136. // type may be stored at it.
  137. inline int AlignOffset(int offset) {
  138. return AlignTo(offset, kSafeAlignment);
  139. }
  140. #define bitsizeof(T) (sizeof(T) * 8)
  141. } // namespace
  142. // ===================================================================
  143. class DynamicMessage : public Message {
  144. public:
  145. struct TypeInfo {
  146. int size;
  147. int has_bits_offset;
  148. int unknown_fields_offset;
  149. int extensions_offset;
  150. // Not owned by the TypeInfo.
  151. DynamicMessageFactory* factory; // The factory that created this object.
  152. const DescriptorPool* pool; // The factory's DescriptorPool.
  153. const Descriptor* type; // Type of this DynamicMessage.
  154. // Warning: The order in which the following pointers are defined is
  155. // important (the prototype must be deleted *before* the offsets).
  156. scoped_array<int> offsets;
  157. scoped_ptr<const GeneratedMessageReflection> reflection;
  158. scoped_ptr<const DynamicMessage> prototype;
  159. };
  160. DynamicMessage(const TypeInfo* type_info);
  161. ~DynamicMessage();
  162. // Called on the prototype after construction to initialize message fields.
  163. void CrossLinkPrototypes();
  164. // implements Message ----------------------------------------------
  165. Message* New() const;
  166. int GetCachedSize() const;
  167. void SetCachedSize(int size) const;
  168. Metadata GetMetadata() const;
  169. private:
  170. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessage);
  171. inline bool is_prototype() const {
  172. return type_info_->prototype == this ||
  173. // If type_info_->prototype is NULL, then we must be constructing
  174. // the prototype now, which means we must be the prototype.
  175. type_info_->prototype == NULL;
  176. }
  177. inline void* OffsetToPointer(int offset) {
  178. return reinterpret_cast<uint8*>(this) + offset;
  179. }
  180. inline const void* OffsetToPointer(int offset) const {
  181. return reinterpret_cast<const uint8*>(this) + offset;
  182. }
  183. const TypeInfo* type_info_;
  184. // TODO(kenton): Make this an atomic<int> when C++ supports it.
  185. mutable int cached_byte_size_;
  186. };
  187. DynamicMessage::DynamicMessage(const TypeInfo* type_info)
  188. : type_info_(type_info),
  189. cached_byte_size_(0) {
  190. // We need to call constructors for various fields manually and set
  191. // default values where appropriate. We use placement new to call
  192. // constructors. If you haven't heard of placement new, I suggest Googling
  193. // it now. We use placement new even for primitive types that don't have
  194. // constructors for consistency. (In theory, placement new should be used
  195. // any time you are trying to convert untyped memory to typed memory, though
  196. // in practice that's not strictly necessary for types that don't have a
  197. // constructor.)
  198. const Descriptor* descriptor = type_info_->type;
  199. new(OffsetToPointer(type_info_->unknown_fields_offset)) UnknownFieldSet;
  200. if (type_info_->extensions_offset != -1) {
  201. new(OffsetToPointer(type_info_->extensions_offset)) ExtensionSet;
  202. }
  203. for (int i = 0; i < descriptor->field_count(); i++) {
  204. const FieldDescriptor* field = descriptor->field(i);
  205. void* field_ptr = OffsetToPointer(type_info_->offsets[i]);
  206. switch (field->cpp_type()) {
  207. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  208. case FieldDescriptor::CPPTYPE_##CPPTYPE: \
  209. if (!field->is_repeated()) { \
  210. new(field_ptr) TYPE(field->default_value_##TYPE()); \
  211. } else { \
  212. new(field_ptr) RepeatedField<TYPE>(); \
  213. } \
  214. break;
  215. HANDLE_TYPE(INT32 , int32 );
  216. HANDLE_TYPE(INT64 , int64 );
  217. HANDLE_TYPE(UINT32, uint32);
  218. HANDLE_TYPE(UINT64, uint64);
  219. HANDLE_TYPE(DOUBLE, double);
  220. HANDLE_TYPE(FLOAT , float );
  221. HANDLE_TYPE(BOOL , bool );
  222. #undef HANDLE_TYPE
  223. case FieldDescriptor::CPPTYPE_ENUM:
  224. if (!field->is_repeated()) {
  225. new(field_ptr) int(field->default_value_enum()->number());
  226. } else {
  227. new(field_ptr) RepeatedField<int>();
  228. }
  229. break;
  230. case FieldDescriptor::CPPTYPE_STRING:
  231. switch (field->options().ctype()) {
  232. default: // TODO(kenton): Support other string reps.
  233. case FieldOptions::STRING:
  234. if (!field->is_repeated()) {
  235. if (is_prototype()) {
  236. new(field_ptr) const string*(&field->default_value_string());
  237. } else {
  238. string* default_value =
  239. *reinterpret_cast<string* const*>(
  240. type_info_->prototype->OffsetToPointer(
  241. type_info_->offsets[i]));
  242. new(field_ptr) string*(default_value);
  243. }
  244. } else {
  245. new(field_ptr) RepeatedPtrField<string>();
  246. }
  247. break;
  248. }
  249. break;
  250. case FieldDescriptor::CPPTYPE_MESSAGE: {
  251. if (!field->is_repeated()) {
  252. new(field_ptr) Message*(NULL);
  253. } else {
  254. new(field_ptr) RepeatedPtrField<Message>();
  255. }
  256. break;
  257. }
  258. }
  259. }
  260. }
  261. DynamicMessage::~DynamicMessage() {
  262. const Descriptor* descriptor = type_info_->type;
  263. reinterpret_cast<UnknownFieldSet*>(
  264. OffsetToPointer(type_info_->unknown_fields_offset))->~UnknownFieldSet();
  265. if (type_info_->extensions_offset != -1) {
  266. reinterpret_cast<ExtensionSet*>(
  267. OffsetToPointer(type_info_->extensions_offset))->~ExtensionSet();
  268. }
  269. // We need to manually run the destructors for repeated fields and strings,
  270. // just as we ran their constructors in the the DynamicMessage constructor.
  271. // Additionally, if any singular embedded messages have been allocated, we
  272. // need to delete them, UNLESS we are the prototype message of this type,
  273. // in which case any embedded messages are other prototypes and shouldn't
  274. // be touched.
  275. for (int i = 0; i < descriptor->field_count(); i++) {
  276. const FieldDescriptor* field = descriptor->field(i);
  277. void* field_ptr = OffsetToPointer(type_info_->offsets[i]);
  278. if (field->is_repeated()) {
  279. switch (field->cpp_type()) {
  280. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  281. case FieldDescriptor::CPPTYPE_##UPPERCASE : \
  282. reinterpret_cast<RepeatedField<LOWERCASE>*>(field_ptr) \
  283. ->~RepeatedField<LOWERCASE>(); \
  284. break
  285. HANDLE_TYPE( INT32, int32);
  286. HANDLE_TYPE( INT64, int64);
  287. HANDLE_TYPE(UINT32, uint32);
  288. HANDLE_TYPE(UINT64, uint64);
  289. HANDLE_TYPE(DOUBLE, double);
  290. HANDLE_TYPE( FLOAT, float);
  291. HANDLE_TYPE( BOOL, bool);
  292. HANDLE_TYPE( ENUM, int);
  293. #undef HANDLE_TYPE
  294. case FieldDescriptor::CPPTYPE_STRING:
  295. switch (field->options().ctype()) {
  296. default: // TODO(kenton): Support other string reps.
  297. case FieldOptions::STRING:
  298. reinterpret_cast<RepeatedPtrField<string>*>(field_ptr)
  299. ->~RepeatedPtrField<string>();
  300. break;
  301. }
  302. break;
  303. case FieldDescriptor::CPPTYPE_MESSAGE:
  304. reinterpret_cast<RepeatedPtrField<Message>*>(field_ptr)
  305. ->~RepeatedPtrField<Message>();
  306. break;
  307. }
  308. } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
  309. switch (field->options().ctype()) {
  310. default: // TODO(kenton): Support other string reps.
  311. case FieldOptions::STRING: {
  312. string* ptr = *reinterpret_cast<string**>(field_ptr);
  313. if (ptr != &field->default_value_string()) {
  314. delete ptr;
  315. }
  316. break;
  317. }
  318. }
  319. } else if ((field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) &&
  320. !is_prototype()) {
  321. Message* message = *reinterpret_cast<Message**>(field_ptr);
  322. if (message != NULL) {
  323. delete message;
  324. }
  325. }
  326. }
  327. }
  328. void DynamicMessage::CrossLinkPrototypes() {
  329. // This should only be called on the prototype message.
  330. GOOGLE_CHECK(is_prototype());
  331. DynamicMessageFactory* factory = type_info_->factory;
  332. const Descriptor* descriptor = type_info_->type;
  333. // Cross-link default messages.
  334. for (int i = 0; i < descriptor->field_count(); i++) {
  335. const FieldDescriptor* field = descriptor->field(i);
  336. void* field_ptr = OffsetToPointer(type_info_->offsets[i]);
  337. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
  338. !field->is_repeated()) {
  339. // For fields with message types, we need to cross-link with the
  340. // prototype for the field's type.
  341. // For singular fields, the field is just a pointer which should
  342. // point to the prototype.
  343. *reinterpret_cast<const Message**>(field_ptr) =
  344. factory->GetPrototypeNoLock(field->message_type());
  345. }
  346. }
  347. }
  348. Message* DynamicMessage::New() const {
  349. void* new_base = reinterpret_cast<uint8*>(operator new(type_info_->size));
  350. memset(new_base, 0, type_info_->size);
  351. return new(new_base) DynamicMessage(type_info_);
  352. }
  353. int DynamicMessage::GetCachedSize() const {
  354. return cached_byte_size_;
  355. }
  356. void DynamicMessage::SetCachedSize(int size) const {
  357. // This is theoretically not thread-compatible, but in practice it works
  358. // because if multiple threads write this simultaneously, they will be
  359. // writing the exact same value.
  360. cached_byte_size_ = size;
  361. }
  362. Metadata DynamicMessage::GetMetadata() const {
  363. Metadata metadata;
  364. metadata.descriptor = type_info_->type;
  365. metadata.reflection = type_info_->reflection.get();
  366. return metadata;
  367. }
  368. // ===================================================================
  369. struct DynamicMessageFactory::PrototypeMap {
  370. typedef hash_map<const Descriptor*, const DynamicMessage::TypeInfo*> Map;
  371. Map map_;
  372. };
  373. DynamicMessageFactory::DynamicMessageFactory()
  374. : pool_(NULL), delegate_to_generated_factory_(false),
  375. prototypes_(new PrototypeMap) {
  376. }
  377. DynamicMessageFactory::DynamicMessageFactory(const DescriptorPool* pool)
  378. : pool_(pool), delegate_to_generated_factory_(false),
  379. prototypes_(new PrototypeMap) {
  380. }
  381. DynamicMessageFactory::~DynamicMessageFactory() {
  382. for (PrototypeMap::Map::iterator iter = prototypes_->map_.begin();
  383. iter != prototypes_->map_.end(); ++iter) {
  384. delete iter->second;
  385. }
  386. }
  387. const Message* DynamicMessageFactory::GetPrototype(const Descriptor* type) {
  388. MutexLock lock(&prototypes_mutex_);
  389. return GetPrototypeNoLock(type);
  390. }
  391. const Message* DynamicMessageFactory::GetPrototypeNoLock(
  392. const Descriptor* type) {
  393. if (delegate_to_generated_factory_ &&
  394. type->file()->pool() == DescriptorPool::generated_pool()) {
  395. return MessageFactory::generated_factory()->GetPrototype(type);
  396. }
  397. const DynamicMessage::TypeInfo** target = &prototypes_->map_[type];
  398. if (*target != NULL) {
  399. // Already exists.
  400. return (*target)->prototype.get();
  401. }
  402. DynamicMessage::TypeInfo* type_info = new DynamicMessage::TypeInfo;
  403. *target = type_info;
  404. type_info->type = type;
  405. type_info->pool = (pool_ == NULL) ? type->file()->pool() : pool_;
  406. type_info->factory = this;
  407. // We need to construct all the structures passed to
  408. // GeneratedMessageReflection's constructor. This includes:
  409. // - A block of memory that contains space for all the message's fields.
  410. // - An array of integers indicating the byte offset of each field within
  411. // this block.
  412. // - A big bitfield containing a bit for each field indicating whether
  413. // or not that field is set.
  414. // Compute size and offsets.
  415. int* offsets = new int[type->field_count()];
  416. type_info->offsets.reset(offsets);
  417. // Decide all field offsets by packing in order.
  418. // We place the DynamicMessage object itself at the beginning of the allocated
  419. // space.
  420. int size = sizeof(DynamicMessage);
  421. size = AlignOffset(size);
  422. // Next the has_bits, which is an array of uint32s.
  423. type_info->has_bits_offset = size;
  424. int has_bits_array_size =
  425. DivideRoundingUp(type->field_count(), bitsizeof(uint32));
  426. size += has_bits_array_size * sizeof(uint32);
  427. size = AlignOffset(size);
  428. // The ExtensionSet, if any.
  429. if (type->extension_range_count() > 0) {
  430. type_info->extensions_offset = size;
  431. size += sizeof(ExtensionSet);
  432. size = AlignOffset(size);
  433. } else {
  434. // No extensions.
  435. type_info->extensions_offset = -1;
  436. }
  437. // All the fields.
  438. for (int i = 0; i < type->field_count(); i++) {
  439. // Make sure field is aligned to avoid bus errors.
  440. int field_size = FieldSpaceUsed(type->field(i));
  441. size = AlignTo(size, min(kSafeAlignment, field_size));
  442. offsets[i] = size;
  443. size += field_size;
  444. }
  445. // Add the UnknownFieldSet to the end.
  446. size = AlignOffset(size);
  447. type_info->unknown_fields_offset = size;
  448. size += sizeof(UnknownFieldSet);
  449. // Align the final size to make sure no clever allocators think that
  450. // alignment is not necessary.
  451. size = AlignOffset(size);
  452. type_info->size = size;
  453. // Allocate the prototype.
  454. void* base = operator new(size);
  455. memset(base, 0, size);
  456. DynamicMessage* prototype = new(base) DynamicMessage(type_info);
  457. type_info->prototype.reset(prototype);
  458. // Construct the reflection object.
  459. type_info->reflection.reset(
  460. new GeneratedMessageReflection(
  461. type_info->type,
  462. type_info->prototype.get(),
  463. type_info->offsets.get(),
  464. type_info->has_bits_offset,
  465. type_info->unknown_fields_offset,
  466. type_info->extensions_offset,
  467. type_info->pool,
  468. this,
  469. type_info->size));
  470. // Cross link prototypes.
  471. prototype->CrossLinkPrototypes();
  472. return prototype;
  473. }
  474. } // namespace protobuf
  475. } // namespace google