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

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: initialize SerializedScriptValue::m_dataBufferSize 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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 for (size_t i = 0; i < stringLength; i++) 107 for (size_t i = 0; i < stringLength; i++)
108 dst[i] = ntohs(src[i]); 108 dst[i] = ntohs(src[i]);
109 109
110 return adoptRef(new SerializedScriptValue(String::adopt(buffer))); 110 return adoptRef(new SerializedScriptValue(String::adopt(buffer)));
111 } 111 }
112 112
113 SerializedScriptValue::SerializedScriptValue() 113 SerializedScriptValue::SerializedScriptValue()
114 : m_externallyAllocatedMemory(0) {} 114 : m_externallyAllocatedMemory(0) {}
115 115
116 SerializedScriptValue::SerializedScriptValue(const String& wireData) 116 SerializedScriptValue::SerializedScriptValue(const String& wireData)
117 : m_data(wireData.isolatedCopy()), m_externallyAllocatedMemory(0) {} 117 : m_dataString(wireData.isolatedCopy()), m_externallyAllocatedMemory(0) {}
118 118
119 SerializedScriptValue::~SerializedScriptValue() { 119 SerializedScriptValue::~SerializedScriptValue() {
120 // If the allocated memory was not registered before, then this class is 120 // If the allocated memory was not registered before, then this class is
121 // likely used in a context other than Worker's onmessage environment and the 121 // likely used in a context other than Worker's onmessage environment and the
122 // presence of current v8 context is not guaranteed. Avoid calling v8 then. 122 // presence of current v8 context is not guaranteed. Avoid calling v8 then.
123 if (m_externallyAllocatedMemory) { 123 if (m_externallyAllocatedMemory) {
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_dataString.isNull())
136 return m_dataString;
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;
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());
137 size_t length = m_data.length(); 153
154 if (m_dataString.isNull()) {
155 size_t wireSizeBytes = (m_dataBufferSize + 1) & ~1;
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
171 size_t length = m_dataString.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_dataString.is8Bit()) {
142 const LChar* src = m_data.characters8(); 176 const LChar* src = m_dataString.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_dataString.characters16();
147 for (size_t i = 0; i < length; i++) 181 for (size_t i = 0; i < length; i++)
148 dst[i] = htons(src[i]); 182 dst[i] = htons(src[i]);
149 } 183 }
150 } 184 }
151 185
152 static void accumulateArrayBuffersForAllWorlds( 186 static void accumulateArrayBuffersForAllWorlds(
153 v8::Isolate* isolate, 187 v8::Isolate* isolate,
154 DOMArrayBuffer* object, 188 DOMArrayBuffer* object,
155 Vector<v8::Local<v8::ArrayBuffer>, 4>& buffers) { 189 Vector<v8::Local<v8::ArrayBuffer>, 4>& buffers) {
156 if (isMainThread()) { 190 if (isMainThread()) {
(...skipping 253 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