Brian Silverman | 9c614bc | 2016-02-15 20:20:02 -0500 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // https://developers.google.com/protocol-buffers/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | // |
| 35 | // DynamicMessage is implemented by constructing a data structure which |
| 36 | // has roughly the same memory layout as a generated message would have. |
| 37 | // Then, we use GeneratedMessageReflection to implement our reflection |
| 38 | // interface. All the other operations we need to implement (e.g. |
| 39 | // parsing, copying, etc.) are already implemented in terms of |
| 40 | // Reflection, so the rest is easy. |
| 41 | // |
| 42 | // The up side of this strategy is that it's very efficient. We don't |
| 43 | // need to use hash_maps or generic representations of fields. The |
| 44 | // down side is that this is a low-level memory management hack which |
| 45 | // can be tricky to get right. |
| 46 | // |
| 47 | // As mentioned in the header, we only expose a DynamicMessageFactory |
| 48 | // publicly, not the DynamicMessage class itself. This is because |
| 49 | // GenericMessageReflection wants to have a pointer to a "default" |
| 50 | // copy of the class, with all fields initialized to their default |
| 51 | // values. We only want to construct one of these per message type, |
| 52 | // so DynamicMessageFactory stores a cache of default messages for |
| 53 | // each type it sees (each unique Descriptor pointer). The code |
| 54 | // refers to the "default" copy of the class as the "prototype". |
| 55 | // |
| 56 | // Note on memory allocation: This module often calls "operator new()" |
| 57 | // to allocate untyped memory, rather than calling something like |
| 58 | // "new uint8[]". This is because "operator new()" means "Give me some |
| 59 | // space which I can use as I please." while "new uint8[]" means "Give |
| 60 | // me an array of 8-bit integers.". In practice, the later may return |
| 61 | // a pointer that is not aligned correctly for general use. I believe |
| 62 | // Item 8 of "More Effective C++" discusses this in more detail, though |
| 63 | // I don't have the book on me right now so I'm not sure. |
| 64 | |
| 65 | #include <algorithm> |
| 66 | #include <google/protobuf/stubs/hash.h> |
| 67 | #include <memory> |
| 68 | #ifndef _SHARED_PTR_H |
| 69 | #include <google/protobuf/stubs/shared_ptr.h> |
| 70 | #endif |
| 71 | |
| 72 | #include <google/protobuf/stubs/common.h> |
| 73 | #include <google/protobuf/stubs/scoped_ptr.h> |
| 74 | |
| 75 | #include <google/protobuf/dynamic_message.h> |
| 76 | #include <google/protobuf/descriptor.h> |
| 77 | #include <google/protobuf/descriptor.pb.h> |
| 78 | #include <google/protobuf/generated_message_util.h> |
| 79 | #include <google/protobuf/generated_message_reflection.h> |
| 80 | #include <google/protobuf/arenastring.h> |
| 81 | #include <google/protobuf/map_field_inl.h> |
| 82 | #include <google/protobuf/reflection_ops.h> |
| 83 | #include <google/protobuf/repeated_field.h> |
| 84 | #include <google/protobuf/map_type_handler.h> |
| 85 | #include <google/protobuf/extension_set.h> |
| 86 | #include <google/protobuf/wire_format.h> |
| 87 | #include <google/protobuf/map_field.h> |
| 88 | |
| 89 | namespace google { |
| 90 | namespace protobuf { |
| 91 | |
| 92 | using internal::WireFormat; |
| 93 | using internal::ExtensionSet; |
| 94 | using internal::GeneratedMessageReflection; |
| 95 | using internal::MapField; |
| 96 | using internal::DynamicMapField; |
| 97 | |
| 98 | |
| 99 | using internal::ArenaStringPtr; |
| 100 | |
| 101 | // =================================================================== |
| 102 | // Some helper tables and functions... |
| 103 | |
| 104 | namespace { |
| 105 | |
| 106 | bool IsMapFieldInApi(const FieldDescriptor* field) { |
| 107 | return field->is_map(); |
| 108 | } |
| 109 | |
| 110 | // Compute the byte size of the in-memory representation of the field. |
| 111 | int FieldSpaceUsed(const FieldDescriptor* field) { |
| 112 | typedef FieldDescriptor FD; // avoid line wrapping |
| 113 | if (field->label() == FD::LABEL_REPEATED) { |
| 114 | switch (field->cpp_type()) { |
| 115 | case FD::CPPTYPE_INT32 : return sizeof(RepeatedField<int32 >); |
| 116 | case FD::CPPTYPE_INT64 : return sizeof(RepeatedField<int64 >); |
| 117 | case FD::CPPTYPE_UINT32 : return sizeof(RepeatedField<uint32 >); |
| 118 | case FD::CPPTYPE_UINT64 : return sizeof(RepeatedField<uint64 >); |
| 119 | case FD::CPPTYPE_DOUBLE : return sizeof(RepeatedField<double >); |
| 120 | case FD::CPPTYPE_FLOAT : return sizeof(RepeatedField<float >); |
| 121 | case FD::CPPTYPE_BOOL : return sizeof(RepeatedField<bool >); |
| 122 | case FD::CPPTYPE_ENUM : return sizeof(RepeatedField<int >); |
| 123 | case FD::CPPTYPE_MESSAGE: |
| 124 | if (IsMapFieldInApi(field)) { |
| 125 | return sizeof(DynamicMapField); |
| 126 | } else { |
| 127 | return sizeof(RepeatedPtrField<Message>); |
| 128 | } |
| 129 | |
| 130 | case FD::CPPTYPE_STRING: |
| 131 | switch (field->options().ctype()) { |
| 132 | default: // TODO(kenton): Support other string reps. |
| 133 | case FieldOptions::STRING: |
| 134 | return sizeof(RepeatedPtrField<string>); |
| 135 | } |
| 136 | break; |
| 137 | } |
| 138 | } else { |
| 139 | switch (field->cpp_type()) { |
| 140 | case FD::CPPTYPE_INT32 : return sizeof(int32 ); |
| 141 | case FD::CPPTYPE_INT64 : return sizeof(int64 ); |
| 142 | case FD::CPPTYPE_UINT32 : return sizeof(uint32 ); |
| 143 | case FD::CPPTYPE_UINT64 : return sizeof(uint64 ); |
| 144 | case FD::CPPTYPE_DOUBLE : return sizeof(double ); |
| 145 | case FD::CPPTYPE_FLOAT : return sizeof(float ); |
| 146 | case FD::CPPTYPE_BOOL : return sizeof(bool ); |
| 147 | case FD::CPPTYPE_ENUM : return sizeof(int ); |
| 148 | |
| 149 | case FD::CPPTYPE_MESSAGE: |
| 150 | return sizeof(Message*); |
| 151 | |
| 152 | case FD::CPPTYPE_STRING: |
| 153 | switch (field->options().ctype()) { |
| 154 | default: // TODO(kenton): Support other string reps. |
| 155 | case FieldOptions::STRING: |
| 156 | return sizeof(ArenaStringPtr); |
| 157 | } |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | GOOGLE_LOG(DFATAL) << "Can't get here."; |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | // Compute the byte size of in-memory representation of the oneof fields |
| 167 | // in default oneof instance. |
| 168 | int OneofFieldSpaceUsed(const FieldDescriptor* field) { |
| 169 | typedef FieldDescriptor FD; // avoid line wrapping |
| 170 | switch (field->cpp_type()) { |
| 171 | case FD::CPPTYPE_INT32 : return sizeof(int32 ); |
| 172 | case FD::CPPTYPE_INT64 : return sizeof(int64 ); |
| 173 | case FD::CPPTYPE_UINT32 : return sizeof(uint32 ); |
| 174 | case FD::CPPTYPE_UINT64 : return sizeof(uint64 ); |
| 175 | case FD::CPPTYPE_DOUBLE : return sizeof(double ); |
| 176 | case FD::CPPTYPE_FLOAT : return sizeof(float ); |
| 177 | case FD::CPPTYPE_BOOL : return sizeof(bool ); |
| 178 | case FD::CPPTYPE_ENUM : return sizeof(int ); |
| 179 | |
| 180 | case FD::CPPTYPE_MESSAGE: |
| 181 | return sizeof(Message*); |
| 182 | |
| 183 | case FD::CPPTYPE_STRING: |
| 184 | switch (field->options().ctype()) { |
| 185 | default: |
| 186 | case FieldOptions::STRING: |
| 187 | return sizeof(ArenaStringPtr); |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | GOOGLE_LOG(DFATAL) << "Can't get here."; |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | inline int DivideRoundingUp(int i, int j) { |
| 197 | return (i + (j - 1)) / j; |
| 198 | } |
| 199 | |
| 200 | static const int kSafeAlignment = sizeof(uint64); |
| 201 | static const int kMaxOneofUnionSize = sizeof(uint64); |
| 202 | |
| 203 | inline int AlignTo(int offset, int alignment) { |
| 204 | return DivideRoundingUp(offset, alignment) * alignment; |
| 205 | } |
| 206 | |
| 207 | // Rounds the given byte offset up to the next offset aligned such that any |
| 208 | // type may be stored at it. |
| 209 | inline int AlignOffset(int offset) { |
| 210 | return AlignTo(offset, kSafeAlignment); |
| 211 | } |
| 212 | |
| 213 | #define bitsizeof(T) (sizeof(T) * 8) |
| 214 | |
| 215 | } // namespace |
| 216 | |
| 217 | // =================================================================== |
| 218 | |
| 219 | class DynamicMessage : public Message { |
| 220 | public: |
| 221 | struct TypeInfo { |
| 222 | int size; |
| 223 | int has_bits_offset; |
| 224 | int oneof_case_offset; |
| 225 | int unknown_fields_offset; |
| 226 | int extensions_offset; |
| 227 | int is_default_instance_offset; |
| 228 | |
| 229 | // Not owned by the TypeInfo. |
| 230 | DynamicMessageFactory* factory; // The factory that created this object. |
| 231 | const DescriptorPool* pool; // The factory's DescriptorPool. |
| 232 | const Descriptor* type; // Type of this DynamicMessage. |
| 233 | |
| 234 | // Warning: The order in which the following pointers are defined is |
| 235 | // important (the prototype must be deleted *before* the offsets). |
| 236 | google::protobuf::scoped_array<int> offsets; |
| 237 | google::protobuf::scoped_ptr<const GeneratedMessageReflection> reflection; |
| 238 | // Don't use a scoped_ptr to hold the prototype: the destructor for |
| 239 | // DynamicMessage needs to know whether it is the prototype, and does so by |
| 240 | // looking back at this field. This would assume details about the |
| 241 | // implementation of scoped_ptr. |
| 242 | const DynamicMessage* prototype; |
| 243 | void* default_oneof_instance; |
| 244 | |
| 245 | TypeInfo() : prototype(NULL), default_oneof_instance(NULL) {} |
| 246 | |
| 247 | ~TypeInfo() { |
| 248 | delete prototype; |
| 249 | operator delete(default_oneof_instance); |
| 250 | } |
| 251 | }; |
| 252 | |
| 253 | DynamicMessage(const TypeInfo* type_info); |
| 254 | ~DynamicMessage(); |
| 255 | |
| 256 | // Called on the prototype after construction to initialize message fields. |
| 257 | void CrossLinkPrototypes(); |
| 258 | |
| 259 | // implements Message ---------------------------------------------- |
| 260 | |
| 261 | Message* New() const; |
| 262 | Message* New(::google::protobuf::Arena* arena) const; |
| 263 | ::google::protobuf::Arena* GetArena() const { return NULL; }; |
| 264 | |
| 265 | int GetCachedSize() const; |
| 266 | void SetCachedSize(int size) const; |
| 267 | |
| 268 | Metadata GetMetadata() const; |
| 269 | |
| 270 | |
| 271 | private: |
| 272 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DynamicMessage); |
| 273 | DynamicMessage(const TypeInfo* type_info, ::google::protobuf::Arena* arena); |
| 274 | void SharedCtor(); |
| 275 | |
| 276 | inline bool is_prototype() const { |
| 277 | return type_info_->prototype == this || |
| 278 | // If type_info_->prototype is NULL, then we must be constructing |
| 279 | // the prototype now, which means we must be the prototype. |
| 280 | type_info_->prototype == NULL; |
| 281 | } |
| 282 | |
| 283 | inline void* OffsetToPointer(int offset) { |
| 284 | return reinterpret_cast<uint8*>(this) + offset; |
| 285 | } |
| 286 | inline const void* OffsetToPointer(int offset) const { |
| 287 | return reinterpret_cast<const uint8*>(this) + offset; |
| 288 | } |
| 289 | |
| 290 | const TypeInfo* type_info_; |
| 291 | // TODO(kenton): Make this an atomic<int> when C++ supports it. |
| 292 | mutable int cached_byte_size_; |
| 293 | }; |
| 294 | |
| 295 | DynamicMessage::DynamicMessage(const TypeInfo* type_info) |
| 296 | : type_info_(type_info), |
| 297 | cached_byte_size_(0) { |
| 298 | SharedCtor(); |
| 299 | } |
| 300 | |
| 301 | DynamicMessage::DynamicMessage(const TypeInfo* type_info, |
| 302 | ::google::protobuf::Arena* arena) |
| 303 | : type_info_(type_info), |
| 304 | cached_byte_size_(0) { |
| 305 | SharedCtor(); |
| 306 | } |
| 307 | |
| 308 | void DynamicMessage::SharedCtor() { |
| 309 | // We need to call constructors for various fields manually and set |
| 310 | // default values where appropriate. We use placement new to call |
| 311 | // constructors. If you haven't heard of placement new, I suggest Googling |
| 312 | // it now. We use placement new even for primitive types that don't have |
| 313 | // constructors for consistency. (In theory, placement new should be used |
| 314 | // any time you are trying to convert untyped memory to typed memory, though |
| 315 | // in practice that's not strictly necessary for types that don't have a |
| 316 | // constructor.) |
| 317 | |
| 318 | const Descriptor* descriptor = type_info_->type; |
| 319 | |
| 320 | // Initialize oneof cases. |
| 321 | for (int i = 0 ; i < descriptor->oneof_decl_count(); ++i) { |
| 322 | new(OffsetToPointer(type_info_->oneof_case_offset + sizeof(uint32) * i)) |
| 323 | uint32(0); |
| 324 | } |
| 325 | |
| 326 | if (type_info_->is_default_instance_offset != -1) { |
| 327 | *reinterpret_cast<bool*>( |
| 328 | OffsetToPointer(type_info_->is_default_instance_offset)) = false; |
| 329 | } |
| 330 | |
| 331 | new(OffsetToPointer(type_info_->unknown_fields_offset)) UnknownFieldSet; |
| 332 | |
| 333 | if (type_info_->extensions_offset != -1) { |
| 334 | new(OffsetToPointer(type_info_->extensions_offset)) ExtensionSet; |
| 335 | } |
| 336 | |
| 337 | for (int i = 0; i < descriptor->field_count(); i++) { |
| 338 | const FieldDescriptor* field = descriptor->field(i); |
| 339 | void* field_ptr = OffsetToPointer(type_info_->offsets[i]); |
| 340 | if (field->containing_oneof()) { |
| 341 | continue; |
| 342 | } |
| 343 | switch (field->cpp_type()) { |
| 344 | #define HANDLE_TYPE(CPPTYPE, TYPE) \ |
| 345 | case FieldDescriptor::CPPTYPE_##CPPTYPE: \ |
| 346 | if (!field->is_repeated()) { \ |
| 347 | new(field_ptr) TYPE(field->default_value_##TYPE()); \ |
| 348 | } else { \ |
| 349 | new(field_ptr) RepeatedField<TYPE>(); \ |
| 350 | } \ |
| 351 | break; |
| 352 | |
| 353 | HANDLE_TYPE(INT32 , int32 ); |
| 354 | HANDLE_TYPE(INT64 , int64 ); |
| 355 | HANDLE_TYPE(UINT32, uint32); |
| 356 | HANDLE_TYPE(UINT64, uint64); |
| 357 | HANDLE_TYPE(DOUBLE, double); |
| 358 | HANDLE_TYPE(FLOAT , float ); |
| 359 | HANDLE_TYPE(BOOL , bool ); |
| 360 | #undef HANDLE_TYPE |
| 361 | |
| 362 | case FieldDescriptor::CPPTYPE_ENUM: |
| 363 | if (!field->is_repeated()) { |
| 364 | new(field_ptr) int(field->default_value_enum()->number()); |
| 365 | } else { |
| 366 | new(field_ptr) RepeatedField<int>(); |
| 367 | } |
| 368 | break; |
| 369 | |
| 370 | case FieldDescriptor::CPPTYPE_STRING: |
| 371 | switch (field->options().ctype()) { |
| 372 | default: // TODO(kenton): Support other string reps. |
| 373 | case FieldOptions::STRING: |
| 374 | if (!field->is_repeated()) { |
| 375 | const string* default_value; |
| 376 | if (is_prototype()) { |
| 377 | default_value = &field->default_value_string(); |
| 378 | } else { |
| 379 | default_value = |
| 380 | &(reinterpret_cast<const ArenaStringPtr*>( |
| 381 | type_info_->prototype->OffsetToPointer( |
| 382 | type_info_->offsets[i]))->Get(NULL)); |
| 383 | } |
| 384 | ArenaStringPtr* asp = new(field_ptr) ArenaStringPtr(); |
| 385 | asp->UnsafeSetDefault(default_value); |
| 386 | } else { |
| 387 | new(field_ptr) RepeatedPtrField<string>(); |
| 388 | } |
| 389 | break; |
| 390 | } |
| 391 | break; |
| 392 | |
| 393 | case FieldDescriptor::CPPTYPE_MESSAGE: { |
| 394 | if (!field->is_repeated()) { |
| 395 | new(field_ptr) Message*(NULL); |
| 396 | } else { |
| 397 | if (IsMapFieldInApi(field)) { |
| 398 | new (field_ptr) DynamicMapField( |
| 399 | type_info_->factory->GetPrototypeNoLock(field->message_type())); |
| 400 | } else { |
| 401 | new (field_ptr) RepeatedPtrField<Message>(); |
| 402 | } |
| 403 | } |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | DynamicMessage::~DynamicMessage() { |
| 411 | const Descriptor* descriptor = type_info_->type; |
| 412 | |
| 413 | reinterpret_cast<UnknownFieldSet*>( |
| 414 | OffsetToPointer(type_info_->unknown_fields_offset))->~UnknownFieldSet(); |
| 415 | |
| 416 | if (type_info_->extensions_offset != -1) { |
| 417 | reinterpret_cast<ExtensionSet*>( |
| 418 | OffsetToPointer(type_info_->extensions_offset))->~ExtensionSet(); |
| 419 | } |
| 420 | |
| 421 | // We need to manually run the destructors for repeated fields and strings, |
| 422 | // just as we ran their constructors in the DynamicMessage constructor. |
| 423 | // We also need to manually delete oneof fields if it is set and is string |
| 424 | // or message. |
| 425 | // Additionally, if any singular embedded messages have been allocated, we |
| 426 | // need to delete them, UNLESS we are the prototype message of this type, |
| 427 | // in which case any embedded messages are other prototypes and shouldn't |
| 428 | // be touched. |
| 429 | for (int i = 0; i < descriptor->field_count(); i++) { |
| 430 | const FieldDescriptor* field = descriptor->field(i); |
| 431 | if (field->containing_oneof()) { |
| 432 | void* field_ptr = OffsetToPointer( |
| 433 | type_info_->oneof_case_offset |
| 434 | + sizeof(uint32) * field->containing_oneof()->index()); |
| 435 | if (*(reinterpret_cast<const uint32*>(field_ptr)) == |
| 436 | field->number()) { |
| 437 | field_ptr = OffsetToPointer(type_info_->offsets[ |
| 438 | descriptor->field_count() + field->containing_oneof()->index()]); |
| 439 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { |
| 440 | switch (field->options().ctype()) { |
| 441 | default: |
| 442 | case FieldOptions::STRING: { |
| 443 | const ::std::string* default_value = |
| 444 | &(reinterpret_cast<const ArenaStringPtr*>( |
| 445 | type_info_->prototype->OffsetToPointer( |
| 446 | type_info_->offsets[i]))->Get(NULL)); |
| 447 | reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy( |
| 448 | default_value, NULL); |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 453 | delete *reinterpret_cast<Message**>(field_ptr); |
| 454 | } |
| 455 | } |
| 456 | continue; |
| 457 | } |
| 458 | void* field_ptr = OffsetToPointer(type_info_->offsets[i]); |
| 459 | |
| 460 | if (field->is_repeated()) { |
| 461 | switch (field->cpp_type()) { |
| 462 | #define HANDLE_TYPE(UPPERCASE, LOWERCASE) \ |
| 463 | case FieldDescriptor::CPPTYPE_##UPPERCASE : \ |
| 464 | reinterpret_cast<RepeatedField<LOWERCASE>*>(field_ptr) \ |
| 465 | ->~RepeatedField<LOWERCASE>(); \ |
| 466 | break |
| 467 | |
| 468 | HANDLE_TYPE( INT32, int32); |
| 469 | HANDLE_TYPE( INT64, int64); |
| 470 | HANDLE_TYPE(UINT32, uint32); |
| 471 | HANDLE_TYPE(UINT64, uint64); |
| 472 | HANDLE_TYPE(DOUBLE, double); |
| 473 | HANDLE_TYPE( FLOAT, float); |
| 474 | HANDLE_TYPE( BOOL, bool); |
| 475 | HANDLE_TYPE( ENUM, int); |
| 476 | #undef HANDLE_TYPE |
| 477 | |
| 478 | case FieldDescriptor::CPPTYPE_STRING: |
| 479 | switch (field->options().ctype()) { |
| 480 | default: // TODO(kenton): Support other string reps. |
| 481 | case FieldOptions::STRING: |
| 482 | reinterpret_cast<RepeatedPtrField<string>*>(field_ptr) |
| 483 | ->~RepeatedPtrField<string>(); |
| 484 | break; |
| 485 | } |
| 486 | break; |
| 487 | |
| 488 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 489 | if (IsMapFieldInApi(field)) { |
| 490 | reinterpret_cast<DynamicMapField*>(field_ptr)->~DynamicMapField(); |
| 491 | } else { |
| 492 | reinterpret_cast<RepeatedPtrField<Message>*>(field_ptr) |
| 493 | ->~RepeatedPtrField<Message>(); |
| 494 | } |
| 495 | break; |
| 496 | } |
| 497 | |
| 498 | } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { |
| 499 | switch (field->options().ctype()) { |
| 500 | default: // TODO(kenton): Support other string reps. |
| 501 | case FieldOptions::STRING: { |
| 502 | const ::std::string* default_value = |
| 503 | &(reinterpret_cast<const ArenaStringPtr*>( |
| 504 | type_info_->prototype->OffsetToPointer( |
| 505 | type_info_->offsets[i]))->Get(NULL)); |
| 506 | reinterpret_cast<ArenaStringPtr*>(field_ptr)->Destroy( |
| 507 | default_value, NULL); |
| 508 | break; |
| 509 | } |
| 510 | } |
| 511 | } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 512 | if (!is_prototype()) { |
| 513 | Message* message = *reinterpret_cast<Message**>(field_ptr); |
| 514 | if (message != NULL) { |
| 515 | delete message; |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | void DynamicMessage::CrossLinkPrototypes() { |
| 523 | // This should only be called on the prototype message. |
| 524 | GOOGLE_CHECK(is_prototype()); |
| 525 | |
| 526 | DynamicMessageFactory* factory = type_info_->factory; |
| 527 | const Descriptor* descriptor = type_info_->type; |
| 528 | |
| 529 | // Cross-link default messages. |
| 530 | for (int i = 0; i < descriptor->field_count(); i++) { |
| 531 | const FieldDescriptor* field = descriptor->field(i); |
| 532 | void* field_ptr = OffsetToPointer(type_info_->offsets[i]); |
| 533 | if (field->containing_oneof()) { |
| 534 | field_ptr = reinterpret_cast<uint8*>( |
| 535 | type_info_->default_oneof_instance) + type_info_->offsets[i]; |
| 536 | } |
| 537 | |
| 538 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE && |
| 539 | !field->is_repeated()) { |
| 540 | // For fields with message types, we need to cross-link with the |
| 541 | // prototype for the field's type. |
| 542 | // For singular fields, the field is just a pointer which should |
| 543 | // point to the prototype. |
| 544 | *reinterpret_cast<const Message**>(field_ptr) = |
| 545 | factory->GetPrototypeNoLock(field->message_type()); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // Set as the default instance -- this affects field-presence semantics for |
| 550 | // proto3. |
| 551 | if (type_info_->is_default_instance_offset != -1) { |
| 552 | void* is_default_instance_ptr = |
| 553 | OffsetToPointer(type_info_->is_default_instance_offset); |
| 554 | *reinterpret_cast<bool*>(is_default_instance_ptr) = true; |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | Message* DynamicMessage::New() const { |
| 559 | void* new_base = operator new(type_info_->size); |
| 560 | memset(new_base, 0, type_info_->size); |
| 561 | return new(new_base) DynamicMessage(type_info_); |
| 562 | } |
| 563 | |
| 564 | Message* DynamicMessage::New(::google::protobuf::Arena* arena) const { |
| 565 | if (arena != NULL) { |
| 566 | Message* message = New(); |
| 567 | arena->Own(message); |
| 568 | return message; |
| 569 | } else { |
| 570 | return New(); |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | int DynamicMessage::GetCachedSize() const { |
| 575 | return cached_byte_size_; |
| 576 | } |
| 577 | |
| 578 | void DynamicMessage::SetCachedSize(int size) const { |
| 579 | // This is theoretically not thread-compatible, but in practice it works |
| 580 | // because if multiple threads write this simultaneously, they will be |
| 581 | // writing the exact same value. |
| 582 | GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN(); |
| 583 | cached_byte_size_ = size; |
| 584 | GOOGLE_SAFE_CONCURRENT_WRITES_END(); |
| 585 | } |
| 586 | |
| 587 | Metadata DynamicMessage::GetMetadata() const { |
| 588 | Metadata metadata; |
| 589 | metadata.descriptor = type_info_->type; |
| 590 | metadata.reflection = type_info_->reflection.get(); |
| 591 | return metadata; |
| 592 | } |
| 593 | |
| 594 | // =================================================================== |
| 595 | |
| 596 | struct DynamicMessageFactory::PrototypeMap { |
| 597 | typedef hash_map<const Descriptor*, const DynamicMessage::TypeInfo*> Map; |
| 598 | Map map_; |
| 599 | }; |
| 600 | |
| 601 | DynamicMessageFactory::DynamicMessageFactory() |
| 602 | : pool_(NULL), delegate_to_generated_factory_(false), |
| 603 | prototypes_(new PrototypeMap) { |
| 604 | } |
| 605 | |
| 606 | DynamicMessageFactory::DynamicMessageFactory(const DescriptorPool* pool) |
| 607 | : pool_(pool), delegate_to_generated_factory_(false), |
| 608 | prototypes_(new PrototypeMap) { |
| 609 | } |
| 610 | |
| 611 | DynamicMessageFactory::~DynamicMessageFactory() { |
| 612 | for (PrototypeMap::Map::iterator iter = prototypes_->map_.begin(); |
| 613 | iter != prototypes_->map_.end(); ++iter) { |
| 614 | DeleteDefaultOneofInstance(iter->second->type, |
| 615 | iter->second->offsets.get(), |
| 616 | iter->second->default_oneof_instance); |
| 617 | delete iter->second; |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | const Message* DynamicMessageFactory::GetPrototype(const Descriptor* type) { |
| 622 | MutexLock lock(&prototypes_mutex_); |
| 623 | return GetPrototypeNoLock(type); |
| 624 | } |
| 625 | |
| 626 | const Message* DynamicMessageFactory::GetPrototypeNoLock( |
| 627 | const Descriptor* type) { |
| 628 | if (delegate_to_generated_factory_ && |
| 629 | type->file()->pool() == DescriptorPool::generated_pool()) { |
| 630 | return MessageFactory::generated_factory()->GetPrototype(type); |
| 631 | } |
| 632 | |
| 633 | const DynamicMessage::TypeInfo** target = &prototypes_->map_[type]; |
| 634 | if (*target != NULL) { |
| 635 | // Already exists. |
| 636 | return (*target)->prototype; |
| 637 | } |
| 638 | |
| 639 | DynamicMessage::TypeInfo* type_info = new DynamicMessage::TypeInfo; |
| 640 | *target = type_info; |
| 641 | |
| 642 | type_info->type = type; |
| 643 | type_info->pool = (pool_ == NULL) ? type->file()->pool() : pool_; |
| 644 | type_info->factory = this; |
| 645 | |
| 646 | // We need to construct all the structures passed to |
| 647 | // GeneratedMessageReflection's constructor. This includes: |
| 648 | // - A block of memory that contains space for all the message's fields. |
| 649 | // - An array of integers indicating the byte offset of each field within |
| 650 | // this block. |
| 651 | // - A big bitfield containing a bit for each field indicating whether |
| 652 | // or not that field is set. |
| 653 | |
| 654 | // Compute size and offsets. |
| 655 | int* offsets = new int[type->field_count() + type->oneof_decl_count()]; |
| 656 | type_info->offsets.reset(offsets); |
| 657 | |
| 658 | // Decide all field offsets by packing in order. |
| 659 | // We place the DynamicMessage object itself at the beginning of the allocated |
| 660 | // space. |
| 661 | int size = sizeof(DynamicMessage); |
| 662 | size = AlignOffset(size); |
| 663 | |
| 664 | // Next the has_bits, which is an array of uint32s. |
| 665 | if (type->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { |
| 666 | type_info->has_bits_offset = -1; |
| 667 | } else { |
| 668 | type_info->has_bits_offset = size; |
| 669 | int has_bits_array_size = |
| 670 | DivideRoundingUp(type->field_count(), bitsizeof(uint32)); |
| 671 | size += has_bits_array_size * sizeof(uint32); |
| 672 | size = AlignOffset(size); |
| 673 | } |
| 674 | |
| 675 | // The is_default_instance member, if any. |
| 676 | if (type->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) { |
| 677 | type_info->is_default_instance_offset = size; |
| 678 | size += sizeof(bool); |
| 679 | size = AlignOffset(size); |
| 680 | } else { |
| 681 | type_info->is_default_instance_offset = -1; |
| 682 | } |
| 683 | |
| 684 | // The oneof_case, if any. It is an array of uint32s. |
| 685 | if (type->oneof_decl_count() > 0) { |
| 686 | type_info->oneof_case_offset = size; |
| 687 | size += type->oneof_decl_count() * sizeof(uint32); |
| 688 | size = AlignOffset(size); |
| 689 | } |
| 690 | |
| 691 | // The ExtensionSet, if any. |
| 692 | if (type->extension_range_count() > 0) { |
| 693 | type_info->extensions_offset = size; |
| 694 | size += sizeof(ExtensionSet); |
| 695 | size = AlignOffset(size); |
| 696 | } else { |
| 697 | // No extensions. |
| 698 | type_info->extensions_offset = -1; |
| 699 | } |
| 700 | |
| 701 | // All the fields. |
| 702 | for (int i = 0; i < type->field_count(); i++) { |
| 703 | // Make sure field is aligned to avoid bus errors. |
| 704 | // Oneof fields do not use any space. |
| 705 | if (!type->field(i)->containing_oneof()) { |
| 706 | int field_size = FieldSpaceUsed(type->field(i)); |
| 707 | size = AlignTo(size, min(kSafeAlignment, field_size)); |
| 708 | offsets[i] = size; |
| 709 | size += field_size; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | // The oneofs. |
| 714 | for (int i = 0; i < type->oneof_decl_count(); i++) { |
| 715 | size = AlignTo(size, kSafeAlignment); |
| 716 | offsets[type->field_count() + i] = size; |
| 717 | size += kMaxOneofUnionSize; |
| 718 | } |
| 719 | |
| 720 | // Add the UnknownFieldSet to the end. |
| 721 | size = AlignOffset(size); |
| 722 | type_info->unknown_fields_offset = size; |
| 723 | size += sizeof(UnknownFieldSet); |
| 724 | |
| 725 | // Align the final size to make sure no clever allocators think that |
| 726 | // alignment is not necessary. |
| 727 | size = AlignOffset(size); |
| 728 | type_info->size = size; |
| 729 | |
| 730 | // Allocate the prototype. |
| 731 | void* base = operator new(size); |
| 732 | memset(base, 0, size); |
| 733 | // The prototype in type_info has to be set before creating the prototype |
| 734 | // instance on memory. e.g., message Foo { map<int32, Foo> a = 1; }. When |
| 735 | // creating prototype for Foo, prototype of the map entry will also be |
| 736 | // created, which needs the address of the prototype of Foo (the value in |
| 737 | // map). To break the cyclic dependency, we have to assgin the address of |
| 738 | // prototype into type_info first. |
| 739 | type_info->prototype = static_cast<DynamicMessage*>(base); |
| 740 | DynamicMessage* prototype = new(base) DynamicMessage(type_info); |
| 741 | |
| 742 | // Construct the reflection object. |
| 743 | if (type->oneof_decl_count() > 0) { |
| 744 | // Compute the size of default oneof instance and offsets of default |
| 745 | // oneof fields. |
| 746 | int oneof_size = 0; |
| 747 | for (int i = 0; i < type->oneof_decl_count(); i++) { |
| 748 | for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) { |
| 749 | const FieldDescriptor* field = type->oneof_decl(i)->field(j); |
| 750 | int field_size = OneofFieldSpaceUsed(field); |
| 751 | oneof_size = AlignTo(oneof_size, min(kSafeAlignment, field_size)); |
| 752 | offsets[field->index()] = oneof_size; |
| 753 | oneof_size += field_size; |
| 754 | } |
| 755 | } |
| 756 | // Construct default oneof instance. |
| 757 | type_info->default_oneof_instance = ::operator new(oneof_size); |
| 758 | ConstructDefaultOneofInstance(type_info->type, |
| 759 | type_info->offsets.get(), |
| 760 | type_info->default_oneof_instance); |
| 761 | type_info->reflection.reset( |
| 762 | new GeneratedMessageReflection( |
| 763 | type_info->type, |
| 764 | type_info->prototype, |
| 765 | type_info->offsets.get(), |
| 766 | type_info->has_bits_offset, |
| 767 | type_info->unknown_fields_offset, |
| 768 | type_info->extensions_offset, |
| 769 | type_info->default_oneof_instance, |
| 770 | type_info->oneof_case_offset, |
| 771 | type_info->pool, |
| 772 | this, |
| 773 | type_info->size, |
| 774 | -1 /* arena_offset */, |
| 775 | type_info->is_default_instance_offset)); |
| 776 | } else { |
| 777 | type_info->reflection.reset( |
| 778 | new GeneratedMessageReflection( |
| 779 | type_info->type, |
| 780 | type_info->prototype, |
| 781 | type_info->offsets.get(), |
| 782 | type_info->has_bits_offset, |
| 783 | type_info->unknown_fields_offset, |
| 784 | type_info->extensions_offset, |
| 785 | type_info->pool, |
| 786 | this, |
| 787 | type_info->size, |
| 788 | -1 /* arena_offset */, |
| 789 | type_info->is_default_instance_offset)); |
| 790 | } |
| 791 | // Cross link prototypes. |
| 792 | prototype->CrossLinkPrototypes(); |
| 793 | |
| 794 | return prototype; |
| 795 | } |
| 796 | |
| 797 | void DynamicMessageFactory::ConstructDefaultOneofInstance( |
| 798 | const Descriptor* type, |
| 799 | const int offsets[], |
| 800 | void* default_oneof_instance) { |
| 801 | for (int i = 0; i < type->oneof_decl_count(); i++) { |
| 802 | for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) { |
| 803 | const FieldDescriptor* field = type->oneof_decl(i)->field(j); |
| 804 | void* field_ptr = reinterpret_cast<uint8*>( |
| 805 | default_oneof_instance) + offsets[field->index()]; |
| 806 | switch (field->cpp_type()) { |
| 807 | #define HANDLE_TYPE(CPPTYPE, TYPE) \ |
| 808 | case FieldDescriptor::CPPTYPE_##CPPTYPE: \ |
| 809 | new(field_ptr) TYPE(field->default_value_##TYPE()); \ |
| 810 | break; |
| 811 | |
| 812 | HANDLE_TYPE(INT32 , int32 ); |
| 813 | HANDLE_TYPE(INT64 , int64 ); |
| 814 | HANDLE_TYPE(UINT32, uint32); |
| 815 | HANDLE_TYPE(UINT64, uint64); |
| 816 | HANDLE_TYPE(DOUBLE, double); |
| 817 | HANDLE_TYPE(FLOAT , float ); |
| 818 | HANDLE_TYPE(BOOL , bool ); |
| 819 | #undef HANDLE_TYPE |
| 820 | |
| 821 | case FieldDescriptor::CPPTYPE_ENUM: |
| 822 | new(field_ptr) int(field->default_value_enum()->number()); |
| 823 | break; |
| 824 | case FieldDescriptor::CPPTYPE_STRING: |
| 825 | switch (field->options().ctype()) { |
| 826 | default: |
| 827 | case FieldOptions::STRING: |
| 828 | ArenaStringPtr* asp = new (field_ptr) ArenaStringPtr(); |
| 829 | asp->UnsafeSetDefault(&field->default_value_string()); |
| 830 | break; |
| 831 | } |
| 832 | break; |
| 833 | |
| 834 | case FieldDescriptor::CPPTYPE_MESSAGE: { |
| 835 | new(field_ptr) Message*(NULL); |
| 836 | break; |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | void DynamicMessageFactory::DeleteDefaultOneofInstance( |
| 844 | const Descriptor* type, |
| 845 | const int offsets[], |
| 846 | void* default_oneof_instance) { |
| 847 | for (int i = 0; i < type->oneof_decl_count(); i++) { |
| 848 | for (int j = 0; j < type->oneof_decl(i)->field_count(); j++) { |
| 849 | const FieldDescriptor* field = type->oneof_decl(i)->field(j); |
| 850 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_STRING) { |
| 851 | switch (field->options().ctype()) { |
| 852 | default: |
| 853 | case FieldOptions::STRING: |
| 854 | break; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | } |
| 859 | } |
| 860 | |
| 861 | } // namespace protobuf |
| 862 | } // namespace google |