Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 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 | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ | 24 */ |
| 25 | 25 |
| 26 #ifndef V8StringResource_h | 26 #ifndef V8StringResource_h |
| 27 #define V8StringResource_h | 27 #define V8StringResource_h |
| 28 | 28 |
| 29 #include "bindings/core/v8/ExceptionState.h" | 29 #include "bindings/core/v8/ExceptionState.h" |
| 30 #include "core/CoreExport.h" | 30 #include "core/CoreExport.h" |
| 31 #include "platform/text/CompressibleString.h" | |
| 31 #include "wtf/Allocator.h" | 32 #include "wtf/Allocator.h" |
| 32 #include "wtf/Threading.h" | 33 #include "wtf/Threading.h" |
| 33 #include "wtf/text/AtomicString.h" | 34 #include "wtf/text/AtomicString.h" |
| 34 #include <v8.h> | 35 #include <v8.h> |
| 35 | 36 |
| 36 namespace blink { | 37 namespace blink { |
| 37 | 38 |
| 38 // WebCoreStringResource is a helper class for v8ExternalString. It is used | 39 // WebCoreStringResource is a helper class for v8ExternalString. It is used |
| 39 // to manage the life-cycle of the underlying buffer of the external string. | 40 // to manage the life-cycle of the underlying buffer of the external string. |
| 40 class WebCoreStringResourceBase { | 41 class WebCoreStringResourceBase { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 55 : m_plainString(string.string()) | 56 : m_plainString(string.string()) |
| 56 , m_atomicString(string) | 57 , m_atomicString(string) |
| 57 { | 58 { |
| 58 #if ENABLE(ASSERT) | 59 #if ENABLE(ASSERT) |
| 59 m_threadId = WTF::currentThread(); | 60 m_threadId = WTF::currentThread(); |
| 60 #endif | 61 #endif |
| 61 ASSERT(!string.isNull()); | 62 ASSERT(!string.isNull()); |
| 62 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC onsumption(string)); | 63 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC onsumption(string)); |
| 63 } | 64 } |
| 64 | 65 |
| 66 explicit WebCoreStringResourceBase(const CompressibleString& string) | |
| 67 : m_compressibleString(string) | |
| 68 { | |
| 69 #if ENABLE(ASSERT) | |
| 70 m_threadId = WTF::currentThread(); | |
| 71 #endif | |
| 72 ASSERT(!string.isNull()); | |
| 73 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC onsumption(string)); | |
| 74 } | |
| 75 | |
| 65 virtual ~WebCoreStringResourceBase() | 76 virtual ~WebCoreStringResourceBase() |
| 66 { | 77 { |
| 67 #if ENABLE(ASSERT) | 78 #if ENABLE(ASSERT) |
| 68 ASSERT(m_threadId == WTF::currentThread()); | 79 ASSERT(m_threadId == WTF::currentThread()); |
| 69 #endif | 80 #endif |
| 70 int reducedExternalMemory = -memoryConsumption(m_plainString); | 81 int reducedExternalMemory = 0; |
| 71 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isN ull()) | 82 if (LIKELY(m_compressibleString.isNull())) { |
| 72 reducedExternalMemory -= memoryConsumption(m_atomicString.string()); | 83 reducedExternalMemory = -memoryConsumption(m_plainString); |
| 84 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString .isNull()) | |
| 85 reducedExternalMemory -= memoryConsumption(m_atomicString.string ()); | |
| 86 } else { | |
| 87 reducedExternalMemory = -memoryConsumption(m_compressibleString); | |
| 88 } | |
| 73 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced ExternalMemory); | 89 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced ExternalMemory); |
| 90 | |
| 91 // FIX this! | |
|
haraken
2016/01/15 12:46:21
What do you mean?
hajimehoshi
2016/01/18 09:42:26
Ah, sorry... I mean we need to decide if memoryCon
| |
| 74 } | 92 } |
| 75 | 93 |
| 76 const String& webcoreString() { return m_plainString; } | 94 String webcoreString() |
| 95 { | |
| 96 if (!m_compressibleString.isNull()) { | |
|
haraken
2016/01/15 12:46:20
Add UNLIKELY.
hajimehoshi
2016/01/18 09:42:26
Done.
| |
| 97 ASSERT(m_plainString.isNull()); | |
| 98 ASSERT(m_atomicString.isNull()); | |
| 99 return m_compressibleString.toString(); | |
| 100 } | |
| 101 return m_plainString; | |
| 102 } | |
| 77 | 103 |
| 78 const AtomicString& atomicString() | 104 AtomicString atomicString() |
| 79 { | 105 { |
| 80 #if ENABLE(ASSERT) | 106 #if ENABLE(ASSERT) |
| 81 ASSERT(m_threadId == WTF::currentThread()); | 107 ASSERT(m_threadId == WTF::currentThread()); |
| 82 #endif | 108 #endif |
| 109 if (!m_compressibleString.isNull()) { | |
|
haraken
2016/01/15 12:46:20
Add UNLIKELY.
hajimehoshi
2016/01/18 09:42:26
Done.
| |
| 110 ASSERT(m_plainString.isNull()); | |
| 111 ASSERT(m_atomicString.isNull()); | |
| 112 return AtomicString(m_compressibleString.toString()); | |
| 113 } | |
| 83 if (m_atomicString.isNull()) { | 114 if (m_atomicString.isNull()) { |
| 84 m_atomicString = AtomicString(m_plainString); | 115 m_atomicString = AtomicString(m_plainString); |
| 85 ASSERT(!m_atomicString.isNull()); | 116 ASSERT(!m_atomicString.isNull()); |
| 86 if (m_plainString.impl() != m_atomicString.impl()) | 117 if (m_plainString.impl() != m_atomicString.impl()) |
| 87 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory (memoryConsumption(m_atomicString.string())); | 118 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory (memoryConsumption(m_atomicString.string())); |
| 88 } | 119 } |
| 89 return m_atomicString; | 120 return m_atomicString; |
| 90 } | 121 } |
| 91 | 122 |
| 123 const CompressibleString& compressibleString() { return m_compressibleString ; } | |
| 124 | |
| 92 protected: | 125 protected: |
| 93 // A shallow copy of the string. Keeps the string buffer alive until the V8 engine garbage collects it. | 126 // A shallow copy of the string. Keeps the string buffer alive until the V8 engine garbage collects it. |
| 94 String m_plainString; | 127 String m_plainString; |
| 95 // If this string is atomic or has been made atomic earlier the | 128 // If this string is atomic or has been made atomic earlier the |
| 96 // atomic string is held here. In the case where the string starts | 129 // atomic string is held here. In the case where the string starts |
| 97 // off non-atomic and becomes atomic later it is necessary to keep | 130 // off non-atomic and becomes atomic later it is necessary to keep |
| 98 // the original string alive because v8 may keep derived pointers | 131 // the original string alive because v8 may keep derived pointers |
| 99 // into that string. | 132 // into that string. |
| 100 AtomicString m_atomicString; | 133 AtomicString m_atomicString; |
| 101 | 134 |
| 135 CompressibleString m_compressibleString; | |
| 136 | |
| 102 private: | 137 private: |
| 103 static int memoryConsumption(const String& string) | 138 static int memoryConsumption(const String& string) |
| 104 { | 139 { |
| 105 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar )); | 140 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar )); |
| 106 } | 141 } |
| 142 | |
| 143 static int memoryConsumption(const CompressibleString& string) | |
| 144 { | |
| 145 return string.currentSizeInBytes(); | |
| 146 } | |
| 147 | |
| 107 #if ENABLE(ASSERT) | 148 #if ENABLE(ASSERT) |
| 108 WTF::ThreadIdentifier m_threadId; | 149 WTF::ThreadIdentifier m_threadId; |
| 109 #endif | 150 #endif |
| 110 }; | 151 }; |
| 111 | 152 |
| 112 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v 8::String::ExternalStringResource { | 153 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v 8::String::ExternalStringResource { |
| 113 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); | 154 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); |
| 114 public: | 155 public: |
| 115 explicit WebCoreStringResource16(const String& string) | 156 explicit WebCoreStringResource16(const String& string) |
| 116 : WebCoreStringResourceBase(string) | 157 : WebCoreStringResourceBase(string) |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 146 ASSERT(string.is8Bit()); | 187 ASSERT(string.is8Bit()); |
| 147 } | 188 } |
| 148 | 189 |
| 149 size_t length() const override { return m_plainString.impl()->length(); } | 190 size_t length() const override { return m_plainString.impl()->length(); } |
| 150 const char* data() const override | 191 const char* data() const override |
| 151 { | 192 { |
| 152 return reinterpret_cast<const char*>(m_plainString.impl()->characters8() ); | 193 return reinterpret_cast<const char*>(m_plainString.impl()->characters8() ); |
| 153 } | 194 } |
| 154 }; | 195 }; |
| 155 | 196 |
| 197 class WebCoreCompressibleStringResource16 final : public WebCoreStringResourceBa se, public v8::String::ExternalStringResource { | |
| 198 WTF_MAKE_NONCOPYABLE(WebCoreCompressibleStringResource16); | |
| 199 public: | |
| 200 explicit WebCoreCompressibleStringResource16(const CompressibleString& strin g) | |
| 201 : WebCoreStringResourceBase(string) | |
| 202 { | |
| 203 // ASSERT(m_string); | |
|
haraken
2016/01/15 12:46:21
Remove this.
hajimehoshi
2016/01/18 09:42:26
Done.
| |
| 204 ASSERT(!m_compressibleString.is8Bit()); | |
| 205 } | |
| 206 | |
| 207 bool IsCompressible() const override { return true; } | |
| 208 | |
| 209 size_t length() const override | |
| 210 { | |
| 211 return m_compressibleString.length(); | |
| 212 } | |
| 213 | |
| 214 const uint16_t* data() const override | |
| 215 { | |
| 216 return reinterpret_cast<const uint16_t*>(m_compressibleString.characters 16()); | |
| 217 } | |
| 218 }; | |
| 219 | |
| 220 class WebCoreCompressibleStringResource8 final : public WebCoreStringResourceBas e, public v8::String::ExternalOneByteStringResource { | |
| 221 WTF_MAKE_NONCOPYABLE(WebCoreCompressibleStringResource8); | |
| 222 public: | |
| 223 explicit WebCoreCompressibleStringResource8(const CompressibleString& string ) | |
| 224 : WebCoreStringResourceBase(string) | |
| 225 { | |
| 226 ASSERT(m_compressibleString.is8Bit()); | |
| 227 } | |
| 228 | |
| 229 bool IsCompressible() const override { return true; } | |
| 230 | |
| 231 size_t length() const override | |
| 232 { | |
| 233 return m_compressibleString.length(); | |
| 234 } | |
| 235 | |
| 236 const char* data() const override | |
| 237 { | |
| 238 return reinterpret_cast<const char*>(m_compressibleString.characters8()) ; | |
| 239 } | |
| 240 }; | |
| 241 | |
| 156 enum ExternalMode { | 242 enum ExternalMode { |
| 157 Externalize, | 243 Externalize, |
| 158 DoNotExternalize | 244 DoNotExternalize |
| 159 }; | 245 }; |
| 160 | 246 |
| 161 template <typename StringType> | 247 template <typename StringType> |
| 162 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo de); | 248 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo de); |
| 163 CORE_EXPORT String int32ToWebCoreString(int value); | 249 CORE_EXPORT String int32ToWebCoreString(int value); |
| 164 | 250 |
| 165 // V8StringResource is an adapter class that converts V8 values to Strings | 251 // V8StringResource is an adapter class that converts V8 values to Strings |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 } | 406 } |
| 321 | 407 |
| 322 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa llbackString() const | 408 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa llbackString() const |
| 323 { | 409 { |
| 324 return String(); | 410 return String(); |
| 325 } | 411 } |
| 326 | 412 |
| 327 } // namespace blink | 413 } // namespace blink |
| 328 | 414 |
| 329 #endif // V8StringResource_h | 415 #endif // V8StringResource_h |
| OLD | NEW |