diff options
author | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2008-11-21 00:06:27 +0000 |
---|---|---|
committer | kenton@google.com <kenton@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2008-11-21 00:06:27 +0000 |
commit | 26bd9eee6ee6d116e1cc0dedeb660cd69d7aac45 (patch) | |
tree | d35cca89e0da44f136090a554ff9abc93a794fa8 /src/google/protobuf/repeated_field.h | |
parent | a2a32c20434807e9966e3f48375f9419134d1b55 (diff) | |
download | protobuf-26bd9eee6ee6d116e1cc0dedeb660cd69d7aac45.tar.gz protobuf-26bd9eee6ee6d116e1cc0dedeb660cd69d7aac45.tar.bz2 protobuf-26bd9eee6ee6d116e1cc0dedeb660cd69d7aac45.zip |
Integrate changes from internal code.
protoc
* Enum values may now have custom options, using syntax similar to field
options.
* Fixed bug where .proto files which use custom options but don't actually
define them (i.e. they import another .proto file defining the options)
had to explicitly import descriptor.proto.
* Adjacent string literals in .proto files will now be concatenated, like in
C.
C++
* Generated message classes now have a Swap() method which efficiently swaps
the contents of two objects.
* All message classes now have a SpaceUsed() method which returns an estimate
of the number of bytes of allocated memory currently owned by the object.
This is particularly useful when you are reusing a single message object
to improve performance but want to make sure it doesn't bloat up too large.
* New method Message::SerializeAsString() returns a string containing the
serialized data. May be more convenient than calling
SerializeToString(string*).
* In debug mode, log error messages when string-type fields are found to
contain bytes that are not valid UTF-8.
* Fixed bug where a message with multiple extension ranges couldn't parse
extensions.
* Fixed bug where MergeFrom(const Message&) didn't do anything if invoked on
a message that contained no fields (but possibly contained extensions).
* Fixed ShortDebugString() to not be O(n^2). Durr.
* Fixed crash in TextFormat parsing if the first token in the input caused a
tokenization error.
Java
* New overload of mergeFrom() which parses a slice of a byte array instead
of the whole thing.
* New method ByteString.asReadOnlyByteBuffer() does what it sounds like.
* Improved performance of isInitialized() when optimizing for code size.
Python
* Corrected ListFields() signature in Message base class to match what
subclasses actually implement.
* Some minor refactoring.
Diffstat (limited to 'src/google/protobuf/repeated_field.h')
-rw-r--r-- | src/google/protobuf/repeated_field.h | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/google/protobuf/repeated_field.h b/src/google/protobuf/repeated_field.h index 203aa9bf..44cd875b 100644 --- a/src/google/protobuf/repeated_field.h +++ b/src/google/protobuf/repeated_field.h @@ -87,10 +87,14 @@ class LIBPROTOBUF_EXPORT GenericRepeatedField { virtual void* GenericAdd() = 0; virtual void GenericClear() = 0; virtual int GenericSize() const = 0; + virtual int GenericSpaceUsedExcludingSelf() const = 0; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GenericRepeatedField); }; +// We need this (from generated_message_reflection.cc). +int StringSpaceUsedExcludingSelf(const string& str); + } // namespace internal // RepeatedField is used to represent repeated fields of a primitive type (in @@ -140,6 +144,10 @@ class RepeatedField : public internal::GenericRepeatedField { iterator end(); const_iterator end() const; + // Returns the number of bytes used by the repeated field, excluding + // sizeof(*this) + int SpaceUsedExcludingSelf() const; + private: // See GenericRepeatedField for why this is private. // implements GenericRepeatedField --------------------------------- const void* GenericGet(int index) const; @@ -147,6 +155,7 @@ class RepeatedField : public internal::GenericRepeatedField { void* GenericAdd(); void GenericClear(); int GenericSize() const; + int GenericSpaceUsedExcludingSelf() const; private: GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedField); @@ -214,6 +223,10 @@ class RepeatedPtrField : public internal::GenericRepeatedField { iterator end(); const_iterator end() const; + // Returns (an estimate of) the number of bytes used by the repeated field, + // excluding sizeof(*this). + int SpaceUsedExcludingSelf() const; + // Advanced memory management -------------------------------------- // When hardcore memory management becomes necessary -- as it often // does here at Google -- the following methods may be useful. @@ -254,8 +267,13 @@ class RepeatedPtrField : public internal::GenericRepeatedField { void* GenericAdd(); void GenericClear(); int GenericSize() const; + int GenericSpaceUsedExcludingSelf() const; private: + // Returns (an estimate of) the number of bytes used by an individual + // element. + int ElementSpaceUsed(Element* element) const; + GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedPtrField); static const int kInitialSize = 4; @@ -398,6 +416,10 @@ RepeatedField<Element>::end() const { return elements_ + current_size_; } +template <typename Element> +inline int RepeatedField<Element>::SpaceUsedExcludingSelf() const { + return (elements_ != initial_space_) ? total_size_ * sizeof(elements_[0]) : 0; +} template <typename Element> const void* RepeatedField<Element>::GenericGet(int index) const { @@ -427,6 +449,11 @@ int RepeatedField<Element>::GenericSize() const { } template <typename Element> +int RepeatedField<Element>::GenericSpaceUsedExcludingSelf() const { + return SpaceUsedExcludingSelf(); +} + +template <typename Element> inline void RepeatedField<Element>::Reserve(int new_size) { if (total_size_ >= new_size) return; @@ -595,6 +622,26 @@ void RepeatedPtrField<Element>::Swap(RepeatedPtrField* other) { } } +template <typename Element> +inline int RepeatedPtrField<Element>::SpaceUsedExcludingSelf() const { + int allocated_bytes = + (elements_ != initial_space_) ? total_size_ * sizeof(elements_[0]) : 0; + for (int i = 0; i < allocated_size_; ++i) { + allocated_bytes += ElementSpaceUsed(elements_[i]); + } + return allocated_bytes; +} + +template <typename Element> +inline int RepeatedPtrField<Element>::ElementSpaceUsed(Element* e) const { + return e->SpaceUsed(); +} + +template <> +inline int RepeatedPtrField<string>::ElementSpaceUsed(string* s) const { + return sizeof(*s) + internal::StringSpaceUsedExcludingSelf(*s); +} + template <typename Element> inline void RepeatedPtrField<Element>::AddAllocated(Element* value) { @@ -665,6 +712,11 @@ int RepeatedPtrField<Element>::GenericSize() const { return size(); } +template <typename Element> +int RepeatedPtrField<Element>::GenericSpaceUsedExcludingSelf() const { + return SpaceUsedExcludingSelf(); +} + template <typename Element> inline void RepeatedPtrField<Element>::Reserve(int new_size) { |