Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Unified Diff: third_party/protobuf/src/google/protobuf/repeated_field.cc

Issue 1842653006: Update //third_party/protobuf to version 3. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: merge Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/protobuf/src/google/protobuf/repeated_field.cc
diff --git a/third_party/protobuf/src/google/protobuf/repeated_field.cc b/third_party/protobuf/src/google/protobuf/repeated_field.cc
index 2c1f74c58a40e226ab1763f8637bd7e341e4af37..949e0a235dffa35604378e79684d6f156e6061ce 100644
--- a/third_party/protobuf/src/google/protobuf/repeated_field.cc
+++ b/third_party/protobuf/src/google/protobuf/repeated_field.cc
@@ -1,6 +1,6 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
-// http://code.google.com/p/protobuf/
+// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
@@ -35,6 +35,7 @@
#include <algorithm>
#include <google/protobuf/repeated_field.h>
+#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/common.h>
namespace google {
@@ -42,42 +43,56 @@ namespace protobuf {
namespace internal {
-void RepeatedPtrFieldBase::Reserve(int new_size) {
- if (total_size_ >= new_size) return;
-
- void** old_elements = elements_;
- total_size_ = max(kMinRepeatedFieldAllocationSize,
- max(total_size_ * 2, new_size));
- elements_ = new void*[total_size_];
- if (old_elements != NULL) {
- memcpy(elements_, old_elements, allocated_size_ * sizeof(elements_[0]));
- delete [] old_elements;
+void** RepeatedPtrFieldBase::InternalExtend(int extend_amount) {
+ int new_size = current_size_ + extend_amount;
+ if (total_size_ >= new_size) {
+ // N.B.: rep_ is non-NULL because extend_amount is always > 0, hence
+ // total_size must be non-zero since it is lower-bounded by new_size.
+ return &rep_->elements[current_size_];
+ }
+ Rep* old_rep = rep_;
+ Arena* arena = GetArenaNoVirtual();
+ new_size = max(kMinRepeatedFieldAllocationSize,
+ max(total_size_ * 2, new_size));
+ GOOGLE_CHECK_LE(new_size,
+ (std::numeric_limits<size_t>::max() - kRepHeaderSize) /
+ sizeof(old_rep->elements[0]))
+ << "Requested size is too large to fit into size_t.";
+ if (arena == NULL) {
+ rep_ = reinterpret_cast<Rep*>(
+ new char[kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size]);
+ } else {
+ rep_ = reinterpret_cast<Rep*>(
+ ::google::protobuf::Arena::CreateArray<char>(arena,
+ kRepHeaderSize + sizeof(old_rep->elements[0]) * new_size));
+ }
+ total_size_ = new_size;
+ if (old_rep && old_rep->allocated_size > 0) {
+ memcpy(rep_->elements, old_rep->elements,
+ old_rep->allocated_size * sizeof(rep_->elements[0]));
+ rep_->allocated_size = old_rep->allocated_size;
+ } else {
+ rep_->allocated_size = 0;
+ }
+ if (arena == NULL) {
+ delete [] reinterpret_cast<char*>(old_rep);
}
+ return &rep_->elements[current_size_];
}
-void RepeatedPtrFieldBase::Swap(RepeatedPtrFieldBase* other) {
- if (this == other) return;
- void** swap_elements = elements_;
- int swap_current_size = current_size_;
- int swap_allocated_size = allocated_size_;
- int swap_total_size = total_size_;
-
- elements_ = other->elements_;
- current_size_ = other->current_size_;
- allocated_size_ = other->allocated_size_;
- total_size_ = other->total_size_;
-
- other->elements_ = swap_elements;
- other->current_size_ = swap_current_size;
- other->allocated_size_ = swap_allocated_size;
- other->total_size_ = swap_total_size;
+void RepeatedPtrFieldBase::Reserve(int new_size) {
+ if (new_size > current_size_) {
+ InternalExtend(new_size - current_size_);
+ }
}
-string* StringTypeHandlerBase::New() {
- return new string;
-}
-void StringTypeHandlerBase::Delete(string* value) {
- delete value;
+void RepeatedPtrFieldBase::CloseGap(int start, int num) {
+ if (rep_ == NULL) return;
+ // Close up a gap of "num" elements starting at offset "start".
+ for (int i = start + num; i < rep_->allocated_size; ++i)
+ rep_->elements[i - num] = rep_->elements[i];
+ current_size_ -= num;
+ rep_->allocated_size -= num;
}
} // namespace internal

Powered by Google App Engine
This is Rietveld 408576698