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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/SerializedScriptValue.cpp

Issue 2494823002: V8ScriptValueSerializer: Use PartitionAlloc for the buffer, and avoid copying it into a String. (Closed)
Patch Set: mild streamlining Created 4 years, 1 month 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 unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 ASSERT(v8::Isolate::GetCurrent()); 124 ASSERT(v8::Isolate::GetCurrent());
125 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( 125 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
126 -m_externallyAllocatedMemory); 126 -m_externallyAllocatedMemory);
127 } 127 }
128 } 128 }
129 129
130 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() { 130 PassRefPtr<SerializedScriptValue> SerializedScriptValue::nullValue() {
131 return create(ScriptValueSerializer::serializeNullValue()); 131 return create(ScriptValueSerializer::serializeNullValue());
132 } 132 }
133 133
134 String SerializedScriptValue::toWireString() const {
135 if (!m_data.isNull())
136 return m_data;
137
138 // Add the padding '\0', but don't put it in |m_dataBuffer|.
139 // This requires direct use of uninitialized strings, though.
140 UChar* destination;
141 size_t stringSizeBytes = (m_dataBufferSize + 1) & ~1;
haraken 2016/11/11 03:50:01 Shall we use CheckedNumeric<>?
jbroman 2016/11/11 16:14:58 I can if you'd like, but the addition could only o
142 String wireString =
143 String::createUninitialized(stringSizeBytes / 2, destination);
144 memcpy(destination, m_dataBuffer.get(), m_dataBufferSize);
145 if (stringSizeBytes > m_dataBufferSize)
146 reinterpret_cast<char*>(destination)[stringSizeBytes - 1] = '\0';
147 return wireString;
148 }
149
134 // Convert serialized string to big endian wire data. 150 // Convert serialized string to big endian wire data.
135 void SerializedScriptValue::toWireBytes(Vector<char>& result) const { 151 void SerializedScriptValue::toWireBytes(Vector<char>& result) const {
136 ASSERT(result.isEmpty()); 152 DCHECK(result.isEmpty());
153
154 if (m_data.isNull()) {
155 size_t wireSizeBytes = (m_dataBufferSize + 1) & ~1;
haraken 2016/11/11 03:50:01 Ditto.
156 result.resize(wireSizeBytes);
157
158 const UChar* src = reinterpret_cast<UChar*>(m_dataBuffer.get());
159 UChar* dst = reinterpret_cast<UChar*>(result.data());
160 for (size_t i = 0; i < m_dataBufferSize / 2; i++)
161 dst[i] = htons(src[i]);
162
163 // This is equivalent to swapping the byte order of the two bytes (x, 0),
164 // depending on endianness.
165 if (m_dataBufferSize % 1)
166 dst[wireSizeBytes / 2 - 1] = m_dataBuffer[m_dataBufferSize - 1] << 8;
167
168 return;
169 }
170
137 size_t length = m_data.length(); 171 size_t length = m_data.length();
138 result.resize(length * sizeof(UChar)); 172 result.resize(length * sizeof(UChar));
139 UChar* dst = reinterpret_cast<UChar*>(result.data()); 173 UChar* dst = reinterpret_cast<UChar*>(result.data());
140 174
141 if (m_data.is8Bit()) { 175 if (m_data.is8Bit()) {
142 const LChar* src = m_data.characters8(); 176 const LChar* src = m_data.characters8();
143 for (size_t i = 0; i < length; i++) 177 for (size_t i = 0; i < length; i++)
144 dst[i] = htons(static_cast<UChar>(src[i])); 178 dst[i] = htons(static_cast<UChar>(src[i]));
145 } else { 179 } else {
146 const UChar* src = m_data.characters16(); 180 const UChar* src = m_data.characters16();
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
410 bufferHandle->Neuter(); 444 bufferHandle->Neuter();
411 } 445 }
412 } 446 }
413 } 447 }
414 return contents; 448 return contents;
415 } 449 }
416 450
417 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() { 451 void SerializedScriptValue::registerMemoryAllocatedWithCurrentScriptContext() {
418 if (m_externallyAllocatedMemory) 452 if (m_externallyAllocatedMemory)
419 return; 453 return;
420 m_externallyAllocatedMemory = static_cast<intptr_t>(m_data.length()); 454
455 m_externallyAllocatedMemory = static_cast<intptr_t>(dataLengthInBytes());
421 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory( 456 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(
422 m_externallyAllocatedMemory); 457 m_externallyAllocatedMemory);
423 } 458 }
424 459
425 } // namespace blink 460 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698