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); |
74 } | 90 } |
75 | 91 |
76 const String& webcoreString() { return m_plainString; } | 92 const String& webcoreString() |
| 93 { |
| 94 if (UNLIKELY(!m_compressibleString.isNull())) { |
| 95 ASSERT(m_plainString.isNull()); |
| 96 ASSERT(m_atomicString.isNull()); |
| 97 return m_compressibleString.toString(); |
| 98 } |
| 99 return m_plainString; |
| 100 } |
77 | 101 |
78 const AtomicString& atomicString() | 102 AtomicString atomicString() |
79 { | 103 { |
80 #if ENABLE(ASSERT) | 104 #if ENABLE(ASSERT) |
81 ASSERT(m_threadId == WTF::currentThread()); | 105 ASSERT(m_threadId == WTF::currentThread()); |
82 #endif | 106 #endif |
| 107 if (UNLIKELY(!m_compressibleString.isNull())) { |
| 108 ASSERT(m_plainString.isNull()); |
| 109 ASSERT(m_atomicString.isNull()); |
| 110 return AtomicString(m_compressibleString.toString()); |
| 111 } |
83 if (m_atomicString.isNull()) { | 112 if (m_atomicString.isNull()) { |
84 m_atomicString = AtomicString(m_plainString); | 113 m_atomicString = AtomicString(m_plainString); |
85 ASSERT(!m_atomicString.isNull()); | 114 ASSERT(!m_atomicString.isNull()); |
86 if (m_plainString.impl() != m_atomicString.impl()) | 115 if (m_plainString.impl() != m_atomicString.impl()) |
87 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory
(memoryConsumption(m_atomicString.string())); | 116 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory
(memoryConsumption(m_atomicString.string())); |
88 } | 117 } |
89 return m_atomicString; | 118 return m_atomicString; |
90 } | 119 } |
91 | 120 |
| 121 const CompressibleString& compressibleString() { return m_compressibleString
; } |
| 122 |
92 protected: | 123 protected: |
93 // A shallow copy of the string. Keeps the string buffer alive until the V8
engine garbage collects it. | 124 // A shallow copy of the string. Keeps the string buffer alive until the V8
engine garbage collects it. |
94 String m_plainString; | 125 String m_plainString; |
95 // If this string is atomic or has been made atomic earlier the | 126 // 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 | 127 // 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 | 128 // off non-atomic and becomes atomic later it is necessary to keep |
98 // the original string alive because v8 may keep derived pointers | 129 // the original string alive because v8 may keep derived pointers |
99 // into that string. | 130 // into that string. |
100 AtomicString m_atomicString; | 131 AtomicString m_atomicString; |
101 | 132 |
| 133 CompressibleString m_compressibleString; |
| 134 |
102 private: | 135 private: |
103 static int memoryConsumption(const String& string) | 136 static int memoryConsumption(const String& string) |
104 { | 137 { |
105 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar
)); | 138 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar
)); |
106 } | 139 } |
| 140 |
| 141 static int memoryConsumption(const CompressibleString& string) |
| 142 { |
| 143 return string.currentSizeInBytes(); |
| 144 } |
| 145 |
107 #if ENABLE(ASSERT) | 146 #if ENABLE(ASSERT) |
108 WTF::ThreadIdentifier m_threadId; | 147 WTF::ThreadIdentifier m_threadId; |
109 #endif | 148 #endif |
110 }; | 149 }; |
111 | 150 |
112 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { | 151 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { |
113 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); | 152 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); |
114 public: | 153 public: |
115 explicit WebCoreStringResource16(const String& string) | 154 explicit WebCoreStringResource16(const String& string) |
116 : WebCoreStringResourceBase(string) | 155 : WebCoreStringResourceBase(string) |
(...skipping 29 matching lines...) Expand all Loading... |
146 ASSERT(string.is8Bit()); | 185 ASSERT(string.is8Bit()); |
147 } | 186 } |
148 | 187 |
149 size_t length() const override { return m_plainString.impl()->length(); } | 188 size_t length() const override { return m_plainString.impl()->length(); } |
150 const char* data() const override | 189 const char* data() const override |
151 { | 190 { |
152 return reinterpret_cast<const char*>(m_plainString.impl()->characters8()
); | 191 return reinterpret_cast<const char*>(m_plainString.impl()->characters8()
); |
153 } | 192 } |
154 }; | 193 }; |
155 | 194 |
| 195 class WebCoreCompressibleStringResource16 final : public WebCoreStringResourceBa
se, public v8::String::ExternalStringResource { |
| 196 WTF_MAKE_NONCOPYABLE(WebCoreCompressibleStringResource16); |
| 197 public: |
| 198 explicit WebCoreCompressibleStringResource16(const CompressibleString& strin
g) |
| 199 : WebCoreStringResourceBase(string) |
| 200 { |
| 201 ASSERT(!m_compressibleString.is8Bit()); |
| 202 } |
| 203 |
| 204 bool IsCompressible() const override { return true; } |
| 205 |
| 206 size_t length() const override |
| 207 { |
| 208 return m_compressibleString.length(); |
| 209 } |
| 210 |
| 211 const uint16_t* data() const override |
| 212 { |
| 213 return reinterpret_cast<const uint16_t*>(m_compressibleString.characters
16()); |
| 214 } |
| 215 }; |
| 216 |
| 217 class WebCoreCompressibleStringResource8 final : public WebCoreStringResourceBas
e, public v8::String::ExternalOneByteStringResource { |
| 218 WTF_MAKE_NONCOPYABLE(WebCoreCompressibleStringResource8); |
| 219 public: |
| 220 explicit WebCoreCompressibleStringResource8(const CompressibleString& string
) |
| 221 : WebCoreStringResourceBase(string) |
| 222 { |
| 223 ASSERT(m_compressibleString.is8Bit()); |
| 224 } |
| 225 |
| 226 bool IsCompressible() const override { return true; } |
| 227 |
| 228 size_t length() const override |
| 229 { |
| 230 return m_compressibleString.length(); |
| 231 } |
| 232 |
| 233 const char* data() const override |
| 234 { |
| 235 return reinterpret_cast<const char*>(m_compressibleString.characters8())
; |
| 236 } |
| 237 }; |
| 238 |
156 enum ExternalMode { | 239 enum ExternalMode { |
157 Externalize, | 240 Externalize, |
158 DoNotExternalize | 241 DoNotExternalize |
159 }; | 242 }; |
160 | 243 |
161 template <typename StringType> | 244 template <typename StringType> |
162 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo
de); | 245 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo
de); |
163 CORE_EXPORT String int32ToWebCoreString(int value); | 246 CORE_EXPORT String int32ToWebCoreString(int value); |
164 | 247 |
165 // V8StringResource is an adapter class that converts V8 values to Strings | 248 // 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 } | 403 } |
321 | 404 |
322 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const | 405 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const |
323 { | 406 { |
324 return String(); | 407 return String(); |
325 } | 408 } |
326 | 409 |
327 } // namespace blink | 410 } // namespace blink |
328 | 411 |
329 #endif // V8StringResource_h | 412 #endif // V8StringResource_h |
OLD | NEW |