dynamic_message.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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. // 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/scoped_ptr.h>
  66. #include <google/protobuf/stubs/common.h>
  67. #include <google/protobuf/dynamic_message.h>
  68. #include <google/protobuf/descriptor.h>
  69. #include <google/protobuf/descriptor.pb.h>
  70. #include <google/protobuf/generated_message_util.h>
  71. #include <google/protobuf/generated_message_reflection.h>
  72. #include <google/protobuf/arenastring.h>
  73. #include <google/protobuf/map_field_inl.h>
  74. #include <google/protobuf/reflection_ops.h>
  75. #include <google/protobuf/repeated_field.h>
  76. #include <google/protobuf/map_type_handler.h>
  77. #include <google/protobuf/extension_set.h>
  78. #include <google/protobuf/wire_format.h>
  79. #include <google/protobuf/map_field.h>
  80. namespace google {
  81. namespace protobuf {
  82. using internal::WireFormat;
  83. using internal::ExtensionSet;
  84. using internal::GeneratedMessageReflection;
  85. using internal::MapField;
  86. using internal::DynamicMapField;
  87. using internal::ArenaStringPtr;
  88. // ===================================================================
  89. // Some helper tables and functions...
  90. namespace {
  91. bool IsMapFieldInApi(const FieldDescriptor* field) {
  92. return field->is_map();
  93. }
  94. // Compute the byte size of the in-memory representation of the field.
  95. int FieldSpaceUsed(const FieldDescriptor* field) {
  96. typedef FieldDescriptor FD; // avoid line wrapping
  97. if (field->label() == FD::LABEL_REPEATED) {
  98. switch (field->cpp_type()) {
  99. case FD::CPPTYPE_INT32 : return sizeof(RepeatedField<int32 >);
  100. case FD::CPPTYPE_INT64 : return sizeof(RepeatedField<int64 >);
  101. case FD::CPPTYPE_UINT32 : return sizeof(RepeatedField<uint32 >);
  102. case FD::CPPTYPE_UINT64 : return sizeof(RepeatedField<uint64 >);
  103. case FD::CPPTYPE_DOUBLE : return sizeof(RepeatedField<double >);
  104. case FD::CPPTYPE_FLOAT : return sizeof(RepeatedField<float >);
  105. case FD::CPPTYPE_BOOL : return sizeof(RepeatedField<bool >);
  106. case FD::CPPTYPE_ENUM : return sizeof(RepeatedField<int >);
  107. case FD::CPPTYPE_MESSAGE:
  108. if (IsMapFieldInApi(field)) {
  109. return sizeof(DynamicMapField);
  110. } else {
  111. return sizeof(RepeatedPtrField<Message>);
  112. }
  113. case FD::CPPTYPE_STRING:
  114. switch (field->options().ctype()) {
  115. default: // TODO(kenton): Support other string reps.
  116. case FieldOptions::STRING:
  117. return sizeof(RepeatedPtrField<string>);
  118. }
  119. break;
  120. }
  121. } else {
  122. switch (field->cpp_type()) {
  123. case FD::CPPTYPE_INT32 : return sizeof(int32 );
  124. case FD::CPPTYPE_INT64 : return sizeof(int64 );
  125. case FD::CPPTYPE_UINT32 : return sizeof(uint32 );
  126. case FD::CPPTYPE_UINT64 : return sizeof(uint64 );
  127. case FD::CPPTYPE_DOUBLE : return sizeof(double );
  128. case FD::CPPTYPE_FLOAT : return sizeof(float );
  129. case FD::CPPTYPE_BOOL : return sizeof(bool );
  130. case FD::CPPTYPE_ENUM : return sizeof(int );
  131. case FD::CPPTYPE_MESSAGE:
  132. return sizeof(Message*);
  133. case FD::CPPTYPE_STRING:
  134. switch (field->options().ctype()) {
  135. default: // TODO(kenton): Support other string reps.
  136. case FieldOptions::STRING:
  137. return sizeof(ArenaStringPtr);
  138. }
  139. break;
  140. }
  141. }
  142. GOOGLE_LOG(DFATAL) << "Can't get here.";
  143. return 0;
  144. }
  145. // Compute the byte size of in-memory representation of the oneof fields
  146. // in default oneof instance.
  147. int OneofFieldSpaceUsed(const FieldDescriptor* field) {
  148. typedef FieldDescriptor FD; // avoid line wrapping
  149. switch (field->cpp_type()) {
  150. case FD::CPPTYPE_INT32 : return sizeof(int32 );
  151. case FD::CPPTYPE_INT64 : return sizeof(int64 );
  152. case FD::CPPTYPE_UINT32 : return sizeof(uint32 );
  153. case FD::CPPTYPE_UINT64 : return sizeof(uint64 );
  154. case FD::CPPTYPE_DOUBLE : return sizeof(double );
  155. case FD::CPPTYPE_FLOAT : return sizeof(float );
  156. case FD::CPPTYPE_BOOL : return sizeof(bool );
  157. case FD::CPPTYPE_ENUM : return sizeof(int );
  158. case FD::CPPTYPE_MESSAGE:
  159. return sizeof(Message*);
  160. case FD::CPPTYPE_STRING:
  161. switch (field->options().ctype()) {
  162. default:
  163. case FieldOptions::STRING:
  164. return sizeof(ArenaStringPtr);
  165. }
  166. break;
  167. }
  168. GOOGLE_LOG(DFATAL) << "Can't get here.";
  169. return 0;
  170. }
  171. inline int DivideRoundingUp(int i, int j) {
  172. return (i + (j - 1)) / j;
  173. }
  174. static const int kSafeAlignment = sizeof(uint64);
  175. static const int kMaxOneofUnionSize = sizeof(uint64);
  176. inline int AlignTo(int offset, int alignment) {
  177. return DivideRoundingUp(offset, alignment) * alignment;
  178. }
  179. // Rounds the given byte offset up to the next offset aligned such that any
  180. // type may be stored at it.
  181. inline int AlignOffset(int offset) {
  182. return AlignTo(offset, kSafeAlignment);
  183. }
  184. #define bitsizeof(T) (sizeof(T) * 8)
  185. } // namespace
  186. // ===================================================================
  187. class DynamicMessage : public Message {
  188. public:
  189. struct TypeInfo {
  190. int size;
  191. int has_bits_offset;
  192. int oneof_case_offset;
  193. int unknown_fields_offset;
  194. int extensions_offset;
  195. int is_default_instance_offset;
  196. // Not owned by the TypeInfo.
  197. DynamicMessageFactory* factory; // The factory that created this object.
  198. const DescriptorPool* pool; // The factory's DescriptorPool.
  199. const Descriptor* type; // Type of this DynamicMessage.
  200. // Warning: The order in which the following pointers are defined is
  201. // important (the prototype must be deleted *before* the offsets).
  202. scoped_array<int> offsets;
  203. scoped_ptr<const GeneratedMessageReflection> reflection;
  204. // Don't use a scoped_ptr to hold the prototype: the destructor for
  205. // DynamicMessage needs to know whether it is the prototype, and does so by
  206. // looking back at this field. This would assume details about the
  207. // implementation of scoped_ptr.
  208. const DynamicMessage* prototype;
  209. void* default_oneof_instance;
  210. TypeInfo() : prototype(NULL), default_oneof_instance(NULL) {}
  211. ~TypeInfo() {
  212. delete prototype;
  213. operator delete(default_oneof_instance);
  214. }
  215. };
  216. DynamicMessage(const TypeInfo* type_info);
  217. ~DynamicMessage();
  218. // Called on the prototype after construction to initialize message fields.
  219. void CrossLinkPrototypes();
  220. // implements Message ----------------------------------------------
  221. Message* New() const;
  222. Message* New(::google::protobuf::Arena* arena) const;
  223. ::google::protobuf::Arena* GetArena() const { return NULL; };
  224. int GetCachedSize() const;
  225. void SetCachedSize(int size) const;
  226. Metadata GetMetadata() const;
  227. private:
  228. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessage);
  229. DynamicMessage(const TypeInfo* type_info, ::google::protobuf::Arena* arena);
  230. void SharedCtor();
  231. inline bool is_prototype() const {
  232. return type_info_->prototype == this ||
  233. // If type_info_->prototype is NULL, then we must be constructing
  234. // the prototype now, which means we must be the prototype.
  235. type_info_->prototype == NULL;
  236. }
  237. inline void* OffsetToPointer(int offset) {
  238. return reinterpret_cast<uint8*>(this) + offset;
  239. }
  240. inline const void* OffsetToPointer(int offset) const {
  241. return reinterpret_cast<const uint8*>(this) + offset;
  242. }
  243. const TypeInfo* type_info_;
  244. // TODO(kenton): Make this an atomic<int> when C++ supports it.
  245. mutable int cached_byte_size_;
  246. };
  247. DynamicMessage::DynamicMessage(const TypeInfo* type_info)
  248. : type_info_(type_info),
  249. cached_byte_size_(0) {
  250. SharedCtor();
  251. }
  252. DynamicMessage::DynamicMessage(const TypeInfo* type_info,
  253. ::google::protobuf::Arena* arena)
  254. : type_info_(type_info),
  255. cached_byte_size_(0) {
  256. SharedCtor();
  257. }
  258. void DynamicMessage::SharedCtor() {
  259. // We need to call constructors for various fields manually and set
  260. // default values where appropriate. We use placement new to call
  261. // constructors. If you haven't heard of placement new, I suggest Googling
  262. // it now. We use placement new even for primitive types that don't have
  263. // constructors for consistency. (In theory, placement new should be used
  264. // any time you are trying to convert untyped memory to typed memory, though
  265. // in practice that's not strictly necessary for types that don't have a
  266. // constructor.)
  267. const Descriptor* descriptor = type_info_->type;
  268. // Initialize oneof cases.
  269. for (int i = 0 ; i < descriptor->oneof_decl_count(); ++i) {
  270. new(OffsetToPointer(type_info_->oneof_case_offset + sizeof(uint32) * i))
  271. uint32(0);
  272. }
  273. if (type_info_->is_default_instance_offset != -1) {
  274. *reinterpret_cast<bool*>(
  275. OffsetToPointer(type_info_->is_default_instance_offset)) = false;
  276. }
  277. new(OffsetToPointer(type_info_->unknown_fields_offset)) UnknownFieldSet;
  278. if (type_info_->extensions_offset != -1) {
  279. new(OffsetToPointer(type_info_->extensions_offset)) ExtensionSet;
  280. }
  281. for (int i = 0; i < descriptor->field_count(); i++) {
  282. const FieldDescriptor* field = descriptor->field(i);
  283. void* field_ptr = OffsetToPointer(type_info_->offsets[i]);
  284. if (field->containing_oneof()) {
  285. continue;
  286. }
  287. switch (field->cpp_type()) {
  288. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  289. case FieldDescriptor::CPPTYPE_##CPPTYPE: \
  290. if (!field->is_repeated()) { \
  291. new(field_ptr) TYPE(field->default_value_##TYPE()); \
  292. } else { \
  293. new(field_ptr) RepeatedField<TYPE>(); \
  294. } \
  295. break;
  296. HANDLE_TYPE(INT32 , int32 );
  297. HANDLE_TYPE(INT64 , int64 );
  298. HANDLE_TYPE(UINT32, uint32);
  299. HANDLE_TYPE(UINT64, uint64);
  300. HANDLE_TYPE(DOUBLE, double);
  301. HANDLE_TYPE(FLOAT , float );
  302. HANDLE_TYPE(BOOL , bool );
  303. #undef HANDLE_TYPE
  304. case FieldDescriptor::CPPTYPE_ENUM:
  305. if (!field->is_repeated()) {
  306. new(field_ptr) int(field->default_value_enum()->number());
  307. } else {
  308. new(field_ptr) RepeatedField<int>();
  309. }
  310. break;
  311. case FieldDescriptor::CPPTYPE_STRING:
  312. switch (field->options().ctype()) {
  313. default: // TODO(kenton): Support other string reps.
  314. case FieldOptions::STRING:
  315. if (!field->is_repeated()) {
  316. const string* default_value;
  317. if (is_prototype()) {
  318. default_value = &field->default_value_string();
  319. } else {
  320. default_value =
  321. &(reinterpret_cast<const ArenaStringPtr*>(
  322. type_info_->prototype->OffsetToPointer(
  323. type_info_->offsets[i]))->Get(NULL));
  324. }
  325. ArenaStringPtr* asp = new(field_ptr) ArenaStringPtr();
  326. asp->UnsafeSetDefault(default_value);
  327. } else {
  328. new(field_ptr) RepeatedPtrField<string>();
  329. }
  330. break;
  331. }
  332. break;
  333. case FieldDescriptor::CPPTYPE_MESSAGE: {
  334. if (!field->is_repeated()) {
  335. new(field_ptr) Message*(NULL);
  336. } else {
  337. if (IsMapFieldInApi(field)) {
  338. new (field_ptr) DynamicMapField(
  339. type_info_->factory->GetPrototypeNoLock(field->message_type()));
  340. } else {
  341. new (field_ptr) RepeatedPtrField<Message>();
  342. }
  343. }
  344. break;
  345. }
  346. }
  347. }
  348. }
  349. DynamicMessage::~DynamicMessage() {
  350. const Descriptor* descriptor = type_info_->type;
  351. reinterpret_cast<UnknownFieldSet*>(
  352. OffsetToPointer(type_info_->unknown_fields_offset))->~UnknownFieldSet();
  353. if (type_info_->extensions_offset != -1) {
  354. reinterpret_cast<ExtensionSet*>(
  355. OffsetToPointer(type_info_->extensions_offset))->~ExtensionSet();
  356. }
  357. // We need to manually run the destructors for repeated fields and strings,
  358. // just as we ran their constructors in the the DynamicMessage constructor.
  359. // We also need to manually delete oneof fields if it is set and is string
  360. // or message.
  361. // Additionally, if any singular embedded messages have been allocated, we
  362. // need to delete them, UNLESS we are the prototype message of this type,
  363. // in which case any embedded messages are other prototypes and shouldn't
  364. // be touched.
  365. for (int i = 0; i < descriptor->field_count(); i++) {
  366. const FieldDescriptor* field = descriptor->field(i);
  367. if (field->containing_oneof()) {
  368. void* field_ptr = OffsetToPointer(
  369. type_info_->oneof_case_offset
  370. + sizeof(uint32) * field->containing_oneof()->index());
  371. if (*(reinterpret_cast<const uint32*>(field_ptr)) ==
  372. field->number()) {
  373. field_ptr = OffsetToPointer(type_info_->offsets[
  374. descriptor->field_count() + field->containing_oneof()->index()]);
  375. if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
  376. switch (field->options().ctype()) {
  377. default:
  378. case FieldOptions::STRING: {
  379. const ::std::string* default_value =
  380. &(reinterpret_cast<const ArenaStringPtr*>(
  381. type_info_->prototype->OffsetToPointer(
  382. type_info_->offsets[i]))->Get(NULL));
  383. reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy(
  384. default_value, NULL);
  385. break;
  386. }
  387. }
  388. } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  389. delete *reinterpret_cast<Message**>(field_ptr);
  390. }
  391. }
  392. continue;
  393. }
  394. void* field_ptr = OffsetToPointer(type_info_->offsets[i]);
  395. if (field->is_repeated()) {
  396. switch (field->cpp_type()) {
  397. #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \
  398. case FieldDescriptor::CPPTYPE_##UPPERCASE : \
  399. reinterpret_cast<RepeatedField<LOWERCASE>*>(field_ptr) \
  400. ->~RepeatedField<LOWERCASE>(); \
  401. break
  402. HANDLE_TYPE( INT32, int32);
  403. HANDLE_TYPE( INT64, int64);
  404. HANDLE_TYPE(UINT32, uint32);
  405. HANDLE_TYPE(UINT64, uint64);
  406. HANDLE_TYPE(DOUBLE, double);
  407. HANDLE_TYPE( FLOAT, float);
  408. HANDLE_TYPE( BOOL, bool);
  409. HANDLE_TYPE( ENUM, int);
  410. #undef HANDLE_TYPE
  411. case FieldDescriptor::CPPTYPE_STRING:
  412. switch (field->options().ctype()) {
  413. default: // TODO(kenton): Support other string reps.
  414. case FieldOptions::STRING:
  415. reinterpret_cast<RepeatedPtrField<string>*>(field_ptr)
  416. ->~RepeatedPtrField<string>();
  417. break;
  418. }
  419. break;
  420. case FieldDescriptor::CPPTYPE_MESSAGE:
  421. if (IsMapFieldInApi(field)) {
  422. reinterpret_cast<DynamicMapField*>(field_ptr)->~DynamicMapField();
  423. } else {
  424. reinterpret_cast<RepeatedPtrField<Message>*>(field_ptr)
  425. ->~RepeatedPtrField<Message>();
  426. }
  427. break;
  428. }
  429. } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
  430. switch (field->options().ctype()) {
  431. default: // TODO(kenton): Support other string reps.
  432. case FieldOptions::STRING: {
  433. const ::std::string* default_value =
  434. &(reinterpret_cast<const ArenaStringPtr*>(
  435. type_info_->prototype->OffsetToPointer(
  436. type_info_->offsets[i]))->Get(NULL));
  437. reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy(
  438. default_value, NULL);
  439. break;
  440. }
  441. }
  442. } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  443. if (!is_prototype()) {
  444. Message* message = *reinterpret_cast<Message**>(field_ptr);
  445. if (message != NULL) {
  446. delete message;
  447. }
  448. }
  449. }
  450. }
  451. }
  452. void DynamicMessage::CrossLinkPrototypes() {
  453. // This should only be called on the prototype message.
  454. GOOGLE_CHECK(is_prototype());
  455. DynamicMessageFactory* factory = type_info_->factory;
  456. const Descriptor* descriptor = type_info_->type;
  457. // Cross-link default messages.
  458. for (int i = 0; i < descriptor->field_count(); i++) {
  459. const FieldDescriptor* field = descriptor->field(i);
  460. void* field_ptr = OffsetToPointer(type_info_->offsets[i]);
  461. if (field->containing_oneof()) {
  462. field_ptr = reinterpret_cast<uint8*>(
  463. type_info_->default_oneof_instance) + type_info_->offsets[i];
  464. }
  465. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE &&
  466. !field->is_repeated()) {
  467. // For fields with message types, we need to cross-link with the
  468. // prototype for the field's type.
  469. // For singular fields, the field is just a pointer which should
  470. // point to the prototype.
  471. *reinterpret_cast<const Message**>(field_ptr) =
  472. factory->GetPrototypeNoLock(field->message_type());
  473. }
  474. }
  475. // Set as the default instance -- this affects field-presence semantics for
  476. // proto3.
  477. if (type_info_->is_default_instance_offset != -1) {
  478. void* is_default_instance_ptr =
  479. OffsetToPointer(type_info_->is_default_instance_offset);
  480. *reinterpret_cast<bool*>(is_default_instance_ptr) = true;
  481. }
  482. }
  483. Message* DynamicMessage::New() const {
  484. void* new_base = operator new(type_info_->size);
  485. memset(new_base, 0, type_info_->size);
  486. return new(new_base) DynamicMessage(type_info_);
  487. }
  488. Message* DynamicMessage::New(::google::protobuf::Arena* arena) const {
  489. if (arena != NULL) {
  490. Message* message = New();
  491. arena->Own(message);
  492. return message;
  493. } else {
  494. return New();
  495. }
  496. }
  497. int DynamicMessage::GetCachedSize() const {
  498. return cached_byte_size_;
  499. }
  500. void DynamicMessage::SetCachedSize(int size) const {
  501. // This is theoretically not thread-compatible, but in practice it works
  502. // because if multiple threads write this simultaneously, they will be
  503. // writing the exact same value.
  504. GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();
  505. cached_byte_size_ = size;
  506. GOOGLE_SAFE_CONCURRENT_WRITES_END();
  507. }
  508. Metadata DynamicMessage::GetMetadata() const {
  509. Metadata metadata;
  510. metadata.descriptor = type_info_->type;
  511. metadata.reflection = type_info_->reflection.get();
  512. return metadata;
  513. }
  514. // ===================================================================
  515. struct DynamicMessageFactory::PrototypeMap {
  516. typedef hash_map<const Descriptor*, const DynamicMessage::TypeInfo*> Map;
  517. Map map_;
  518. };
  519. DynamicMessageFactory::DynamicMessageFactory()
  520. : pool_(NULL), delegate_to_generated_factory_(false),
  521. prototypes_(new PrototypeMap) {
  522. }
  523. DynamicMessageFactory::DynamicMessageFactory(const DescriptorPool* pool)
  524. : pool_(pool), delegate_to_generated_factory_(false),
  525. prototypes_(new PrototypeMap) {
  526. }
  527. DynamicMessageFactory::~DynamicMessageFactory() {
  528. for (PrototypeMap::Map::iterator iter = prototypes_->map_.begin();
  529. iter != prototypes_->map_.end(); ++iter) {
  530. DeleteDefaultOneofInstance(iter->second->type,
  531. iter->second->offsets.get(),
  532. iter->second->default_oneof_instance);
  533. delete iter->second;
  534. }
  535. }
  536. const Message* DynamicMessageFactory::GetPrototype(const Descriptor* type) {
  537. MutexLock lock(&prototypes_mutex_);
  538. return GetPrototypeNoLock(type);
  539. }
  540. const Message* DynamicMessageFactory::GetPrototypeNoLock(
  541. const Descriptor* type) {
  542. if (delegate_to_generated_factory_ &&
  543. type->file()->pool() == DescriptorPool::generated_pool()) {
  544. return MessageFactory::generated_factory()->GetPrototype(type);
  545. }
  546. const DynamicMessage::TypeInfo** target = &prototypes_->map_[type];
  547. if (*target != NULL) {
  548. // Already exists.
  549. return (*target)->prototype;
  550. }
  551. DynamicMessage::TypeInfo* type_info = new DynamicMessage::TypeInfo;
  552. *target = type_info;
  553. type_info->type = type;
  554. type_info->pool = (pool_ == NULL) ? type->file()->pool() : pool_;
  555. type_info->factory = this;
  556. // We need to construct all the structures passed to
  557. // GeneratedMessageReflection's constructor. This includes:
  558. // - A block of memory that contains space for all the message's fields.
  559. // - An array of integers indicating the byte offset of each field within
  560. // this block.
  561. // - A big bitfield containing a bit for each field indicating whether
  562. // or not that field is set.
  563. // Compute size and offsets.
  564. int* offsets = new int[type->field_count() + type->oneof_decl_count()];
  565. type_info->offsets.reset(offsets);
  566. // Decide all field offsets by packing in order.
  567. // We place the DynamicMessage object itself at the beginning of the allocated
  568. // space.
  569. int size = sizeof(DynamicMessage);
  570. size = AlignOffset(size);
  571. // Next the has_bits, which is an array of uint32s.
  572. if (type->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
  573. type_info->has_bits_offset = -1;
  574. } else {
  575. type_info->has_bits_offset = size;
  576. int has_bits_array_size =
  577. DivideRoundingUp(type->field_count(), bitsizeof(uint32));
  578. size += has_bits_array_size * sizeof(uint32);
  579. size = AlignOffset(size);
  580. }
  581. // The is_default_instance member, if any.
  582. if (type->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
  583. type_info->is_default_instance_offset = size;
  584. size += sizeof(bool);
  585. size = AlignOffset(size);
  586. } else {
  587. type_info->is_default_instance_offset = -1;
  588. }
  589. // The oneof_case, if any. It is an array of uint32s.
  590. if (type->oneof_decl_count() > 0) {
  591. type_info->oneof_case_offset = size;
  592. size += type->oneof_decl_count() * sizeof(uint32);
  593. size = AlignOffset(size);
  594. }
  595. // The ExtensionSet, if any.
  596. if (type->extension_range_count() > 0) {
  597. type_info->extensions_offset = size;
  598. size += sizeof(ExtensionSet);
  599. size = AlignOffset(size);
  600. } else {
  601. // No extensions.
  602. type_info->extensions_offset = -1;
  603. }
  604. // All the fields.
  605. for (int i = 0; i < type->field_count(); i++) {
  606. // Make sure field is aligned to avoid bus errors.
  607. // Oneof fields do not use any space.
  608. if (!type->field(i)->containing_oneof()) {
  609. int field_size = FieldSpaceUsed(type->field(i));
  610. size = AlignTo(size, min(kSafeAlignment, field_size));
  611. offsets[i] = size;
  612. size += field_size;
  613. }
  614. }
  615. // The oneofs.
  616. for (int i = 0; i < type->oneof_decl_count(); i++) {
  617. size = AlignTo(size, kSafeAlignment);
  618. offsets[type->field_count() + i] = size;
  619. size += kMaxOneofUnionSize;
  620. }
  621. // Add the UnknownFieldSet to the end.
  622. size = AlignOffset(size);
  623. type_info->unknown_fields_offset = size;
  624. size += sizeof(UnknownFieldSet);
  625. // Align the final size to make sure no clever allocators think that
  626. // alignment is not necessary.
  627. size = AlignOffset(size);
  628. type_info->size = size;
  629. // Allocate the prototype.
  630. void* base = operator new(size);
  631. memset(base, 0, size);
  632. // The prototype in type_info has to be set before creating the prototype
  633. // instance on memory. e.g., message Foo { map<int32, Foo> a = 1; }. When
  634. // creating prototype for Foo, prototype of the map entry will also be
  635. // created, which needs the address of the prototype of Foo (the value in
  636. // map). To break the cyclic dependency, we have to assgin the address of
  637. // prototype into type_info first.
  638. type_info->prototype = static_cast<DynamicMessage*>(base);
  639. DynamicMessage* prototype = new(base) DynamicMessage(type_info);
  640. // Construct the reflection object.
  641. if (type->oneof_decl_count() > 0) {
  642. // Compute the size of default oneof instance and offsets of default
  643. // oneof fields.
  644. int oneof_size = 0;
  645. for (int i = 0; i < type->oneof_decl_count(); i++) {
  646. for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) {
  647. const FieldDescriptor* field = type->oneof_decl(i)->field(j);
  648. int field_size = OneofFieldSpaceUsed(field);
  649. oneof_size = AlignTo(oneof_size, min(kSafeAlignment, field_size));
  650. offsets[field->index()] = oneof_size;
  651. oneof_size += field_size;
  652. }
  653. }
  654. // Construct default oneof instance.
  655. type_info->default_oneof_instance = ::operator new(oneof_size);
  656. ConstructDefaultOneofInstance(type_info->type,
  657. type_info->offsets.get(),
  658. type_info->default_oneof_instance);
  659. type_info->reflection.reset(
  660. new GeneratedMessageReflection(
  661. type_info->type,
  662. type_info->prototype,
  663. type_info->offsets.get(),
  664. type_info->has_bits_offset,
  665. type_info->unknown_fields_offset,
  666. type_info->extensions_offset,
  667. type_info->default_oneof_instance,
  668. type_info->oneof_case_offset,
  669. type_info->pool,
  670. this,
  671. type_info->size,
  672. -1 /* arena_offset */,
  673. type_info->is_default_instance_offset));
  674. } else {
  675. type_info->reflection.reset(
  676. new GeneratedMessageReflection(
  677. type_info->type,
  678. type_info->prototype,
  679. type_info->offsets.get(),
  680. type_info->has_bits_offset,
  681. type_info->unknown_fields_offset,
  682. type_info->extensions_offset,
  683. type_info->pool,
  684. this,
  685. type_info->size,
  686. -1 /* arena_offset */,
  687. type_info->is_default_instance_offset));
  688. }
  689. // Cross link prototypes.
  690. prototype->CrossLinkPrototypes();
  691. return prototype;
  692. }
  693. void DynamicMessageFactory::ConstructDefaultOneofInstance(
  694. const Descriptor* type,
  695. const int offsets[],
  696. void* default_oneof_instance) {
  697. for (int i = 0; i < type->oneof_decl_count(); i++) {
  698. for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) {
  699. const FieldDescriptor* field = type->oneof_decl(i)->field(j);
  700. void* field_ptr = reinterpret_cast<uint8*>(
  701. default_oneof_instance) + offsets[field->index()];
  702. switch (field->cpp_type()) {
  703. #define HANDLE_TYPE(CPPTYPE, TYPE) \
  704. case FieldDescriptor::CPPTYPE_##CPPTYPE: \
  705. new(field_ptr) TYPE(field->default_value_##TYPE()); \
  706. break;
  707. HANDLE_TYPE(INT32 , int32 );
  708. HANDLE_TYPE(INT64 , int64 );
  709. HANDLE_TYPE(UINT32, uint32);
  710. HANDLE_TYPE(UINT64, uint64);
  711. HANDLE_TYPE(DOUBLE, double);
  712. HANDLE_TYPE(FLOAT , float );
  713. HANDLE_TYPE(BOOL , bool );
  714. #undef HANDLE_TYPE
  715. case FieldDescriptor::CPPTYPE_ENUM:
  716. new(field_ptr) int(field->default_value_enum()->number());
  717. break;
  718. case FieldDescriptor::CPPTYPE_STRING:
  719. switch (field->options().ctype()) {
  720. default:
  721. case FieldOptions::STRING:
  722. ArenaStringPtr* asp = new (field_ptr) ArenaStringPtr();
  723. asp->UnsafeSetDefault(&field->default_value_string());
  724. break;
  725. }
  726. break;
  727. case FieldDescriptor::CPPTYPE_MESSAGE: {
  728. new(field_ptr) Message*(NULL);
  729. break;
  730. }
  731. }
  732. }
  733. }
  734. }
  735. void DynamicMessageFactory::DeleteDefaultOneofInstance(
  736. const Descriptor* type,
  737. const int offsets[],
  738. void* default_oneof_instance) {
  739. for (int i = 0; i < type->oneof_decl_count(); i++) {
  740. for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) {
  741. const FieldDescriptor* field = type->oneof_decl(i)->field(j);
  742. if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) {
  743. switch (field->options().ctype()) {
  744. default:
  745. case FieldOptions::STRING:
  746. break;
  747. }
  748. }
  749. }
  750. }
  751. }
  752. } // namespace protobuf
  753. } // namespace google