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