Index: base/pickle.h |
diff --git a/base/pickle.h b/base/pickle.h |
index cb9bab98965f141b5ea9bffe264ed5cc76ba77e2..072c949b986a611211522120ec782d99f81e3125 100644 |
--- a/base/pickle.h |
+++ b/base/pickle.h |
@@ -216,7 +216,8 @@ class BASE_EXPORT Pickle { |
return WriteInt(value ? 1 : 0); |
} |
bool WriteInt(int value) { |
- return WriteBytes(&value, sizeof(value)); |
+ WriteBytesStatic<sizeof(value)>(&value); |
+ return true; |
} |
// WARNING: DO NOT USE THIS METHOD IF PICKLES ARE PERSISTED IN ANY WAY. |
// It will write whatever a "long" is on this architecture. On 32-bit |
@@ -224,22 +225,28 @@ class BASE_EXPORT Pickle { |
// pickles are still around after upgrading to 64-bit, or if they are copied |
// between dissimilar systems, YOUR PICKLES WILL HAVE GONE BAD. |
bool WriteLongUsingDangerousNonPortableLessPersistableForm(long value) { |
- return WriteBytes(&value, sizeof(value)); |
+ WriteBytesStatic<sizeof(value)>(&value); |
+ return true; |
} |
bool WriteUInt16(uint16 value) { |
- return WriteBytes(&value, sizeof(value)); |
+ WriteBytesStatic<sizeof(value)>(&value); |
+ return true; |
} |
bool WriteUInt32(uint32 value) { |
- return WriteBytes(&value, sizeof(value)); |
+ WriteBytesStatic<sizeof(value)>(&value); |
+ return true; |
} |
bool WriteInt64(int64 value) { |
- return WriteBytes(&value, sizeof(value)); |
+ WriteBytesStatic<sizeof(value)>(&value); |
+ return true; |
} |
bool WriteUInt64(uint64 value) { |
- return WriteBytes(&value, sizeof(value)); |
+ WriteBytesStatic<sizeof(value)>(&value); |
+ return true; |
} |
bool WriteFloat(float value) { |
- return WriteBytes(&value, sizeof(value)); |
+ WriteBytesStatic<sizeof(value)>(&value); |
+ return true; |
} |
bool WriteString(const std::string& value); |
bool WriteWString(const std::wstring& value); |
@@ -273,6 +280,8 @@ class BASE_EXPORT Pickle { |
// not been changed. |
void TrimWriteData(int length); |
+ void Reserve(size_t size); |
+ |
// Payload follows after allocation of Header (header size is customizable). |
struct Header { |
uint32 payload_size; // Specifies the size of the payload. |
@@ -354,10 +363,33 @@ class BASE_EXPORT Pickle { |
// Allocation size of payload (or -1 if allocation is const). |
size_t capacity_; |
size_t variable_buffer_offset_; // IF non-zero, then offset to a buffer. |
+ size_t offset_; |
+ |
+ template<size_t length> void WriteBytesStatic(const void* data); |
FRIEND_TEST_ALL_PREFIXES(PickleTest, Resize); |
FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNext); |
FRIEND_TEST_ALL_PREFIXES(PickleTest, FindNextWithIncompleteHeader); |
}; |
+#if 0 |
+template <size_t length> void Pickle::WriteBytesStatic(const void* data) { |
+ //DCHECK_NE(kCapacityReadOnly, capacity_) << "oops: pickle is readonly"; |
+#ifdef ARCH_CPU_64_BITS |
+ DCHECK_LE(length, kuint32max); |
danakj
2013/10/22 17:22:05
how come? and why ifdefed only on 64bit?
piman
2013/10/24 06:17:14
This comes from the original BeginWriteData.
I ass
|
+#endif |
+ size_t data_len = AlignInt(length, sizeof(uint32)); |
+ size_t new_size = offset_ + data_len; |
+ if (new_size > capacity_) { |
+ Resize(std::max(capacity_ * 2, new_size)); |
+ } |
+ |
+ char* write = mutable_payload() + offset_; |
+ memcpy(write, data, length); |
+ memset(write + length, 0, data_len - length); |
+ offset_ = new_size; |
+ header_->payload_size = new_size; |
+} |
+#endif |
+ |
#endif // BASE_PICKLE_H__ |