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" | |
32 #include "wtf/Allocator.h" | 31 #include "wtf/Allocator.h" |
33 #include "wtf/Threading.h" | 32 #include "wtf/Threading.h" |
34 #include "wtf/text/AtomicString.h" | 33 #include "wtf/text/AtomicString.h" |
35 #include <v8.h> | 34 #include <v8.h> |
36 | 35 |
37 namespace blink { | 36 namespace blink { |
38 | 37 |
39 // WebCoreStringResource is a helper class for v8ExternalString. It is used | 38 // WebCoreStringResource is a helper class for v8ExternalString. It is used |
40 // to manage the life-cycle of the underlying buffer of the external string. | 39 // to manage the life-cycle of the underlying buffer of the external string. |
41 class WebCoreStringResourceBase { | 40 class WebCoreStringResourceBase { |
(...skipping 14 matching lines...) Expand all Loading... |
56 : m_plainString(string.getString()) | 55 : m_plainString(string.getString()) |
57 , m_atomicString(string) | 56 , m_atomicString(string) |
58 { | 57 { |
59 #if ENABLE(ASSERT) | 58 #if ENABLE(ASSERT) |
60 m_threadId = WTF::currentThread(); | 59 m_threadId = WTF::currentThread(); |
61 #endif | 60 #endif |
62 ASSERT(!string.isNull()); | 61 ASSERT(!string.isNull()); |
63 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC
onsumption(string)); | 62 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC
onsumption(string)); |
64 } | 63 } |
65 | 64 |
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 | |
76 virtual ~WebCoreStringResourceBase() | 65 virtual ~WebCoreStringResourceBase() |
77 { | 66 { |
78 #if ENABLE(ASSERT) | 67 #if ENABLE(ASSERT) |
79 ASSERT(m_threadId == WTF::currentThread()); | 68 ASSERT(m_threadId == WTF::currentThread()); |
80 #endif | 69 #endif |
81 int reducedExternalMemory = 0; | 70 int reducedExternalMemory = -memoryConsumption(m_plainString); |
82 if (LIKELY(m_compressibleString.isNull())) { | 71 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isN
ull()) |
83 reducedExternalMemory = -memoryConsumption(m_plainString); | 72 reducedExternalMemory -= memoryConsumption(m_atomicString); |
84 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString
.isNull()) | |
85 reducedExternalMemory -= memoryConsumption(m_atomicString.getStr
ing()); | |
86 } else { | |
87 reducedExternalMemory = -memoryConsumption(m_compressibleString); | |
88 } | |
89 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced
ExternalMemory); | 73 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced
ExternalMemory); |
90 } | 74 } |
91 | 75 |
92 const String& webcoreString() | 76 const String& webcoreString() { return m_plainString; } |
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 } | |
101 | 77 |
102 AtomicString getAtomicString() | 78 const AtomicString& getAtomicString() |
103 { | 79 { |
104 #if ENABLE(ASSERT) | 80 #if ENABLE(ASSERT) |
105 ASSERT(m_threadId == WTF::currentThread()); | 81 ASSERT(m_threadId == WTF::currentThread()); |
106 #endif | 82 #endif |
107 if (UNLIKELY(!m_compressibleString.isNull())) { | |
108 ASSERT(m_plainString.isNull()); | |
109 ASSERT(m_atomicString.isNull()); | |
110 return AtomicString(m_compressibleString.toString()); | |
111 } | |
112 if (m_atomicString.isNull()) { | 83 if (m_atomicString.isNull()) { |
113 m_atomicString = AtomicString(m_plainString); | 84 m_atomicString = AtomicString(m_plainString); |
114 ASSERT(!m_atomicString.isNull()); | 85 ASSERT(!m_atomicString.isNull()); |
115 if (m_plainString.impl() != m_atomicString.impl()) | 86 if (m_plainString.impl() != m_atomicString.impl()) |
116 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory
(memoryConsumption(m_atomicString.getString())); | 87 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory
(memoryConsumption(m_atomicString.getString())); |
117 } | 88 } |
118 return m_atomicString; | 89 return m_atomicString; |
119 } | 90 } |
120 | 91 |
121 const CompressibleString& getCompressibleString() { return m_compressibleStr
ing; } | |
122 | |
123 protected: | 92 protected: |
124 // A shallow copy of the string. Keeps the string buffer alive until the V8
engine garbage collects it. | 93 // A shallow copy of the string. Keeps the string buffer alive until the V8
engine garbage collects it. |
125 String m_plainString; | 94 String m_plainString; |
126 // If this string is atomic or has been made atomic earlier the | 95 // If this string is atomic or has been made atomic earlier the |
127 // atomic string is held here. In the case where the string starts | 96 // atomic string is held here. In the case where the string starts |
128 // off non-atomic and becomes atomic later it is necessary to keep | 97 // off non-atomic and becomes atomic later it is necessary to keep |
129 // the original string alive because v8 may keep derived pointers | 98 // the original string alive because v8 may keep derived pointers |
130 // into that string. | 99 // into that string. |
131 AtomicString m_atomicString; | 100 AtomicString m_atomicString; |
132 | 101 |
133 CompressibleString m_compressibleString; | |
134 | |
135 private: | 102 private: |
136 static int memoryConsumption(const String& string) | 103 static int memoryConsumption(const String& string) |
137 { | 104 { |
138 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar
)); | 105 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar
)); |
139 } | 106 } |
140 | |
141 static int memoryConsumption(const CompressibleString& string) | |
142 { | |
143 return string.currentSizeInBytes(); | |
144 } | |
145 | |
146 #if ENABLE(ASSERT) | 107 #if ENABLE(ASSERT) |
147 WTF::ThreadIdentifier m_threadId; | 108 WTF::ThreadIdentifier m_threadId; |
148 #endif | 109 #endif |
149 }; | 110 }; |
150 | 111 |
151 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { | 112 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { |
152 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); | 113 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); |
153 public: | 114 public: |
154 explicit WebCoreStringResource16(const String& string) | 115 explicit WebCoreStringResource16(const String& string) |
155 : WebCoreStringResourceBase(string) | 116 : WebCoreStringResourceBase(string) |
(...skipping 29 matching lines...) Expand all Loading... |
185 ASSERT(string.is8Bit()); | 146 ASSERT(string.is8Bit()); |
186 } | 147 } |
187 | 148 |
188 size_t length() const override { return m_plainString.impl()->length(); } | 149 size_t length() const override { return m_plainString.impl()->length(); } |
189 const char* data() const override | 150 const char* data() const override |
190 { | 151 { |
191 return reinterpret_cast<const char*>(m_plainString.impl()->characters8()
); | 152 return reinterpret_cast<const char*>(m_plainString.impl()->characters8()
); |
192 } | 153 } |
193 }; | 154 }; |
194 | 155 |
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 | |
239 enum ExternalMode { | 156 enum ExternalMode { |
240 Externalize, | 157 Externalize, |
241 DoNotExternalize | 158 DoNotExternalize |
242 }; | 159 }; |
243 | 160 |
244 template <typename StringType> | 161 template <typename StringType> |
245 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo
de); | 162 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo
de); |
246 CORE_EXPORT String int32ToWebCoreString(int value); | 163 CORE_EXPORT String int32ToWebCoreString(int value); |
247 | 164 |
248 // V8StringResource is an adapter class that converts V8 values to Strings | 165 // V8StringResource is an adapter class that converts V8 values to Strings |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 } | 320 } |
404 | 321 |
405 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const | 322 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const |
406 { | 323 { |
407 return String(); | 324 return String(); |
408 } | 325 } |
409 | 326 |
410 } // namespace blink | 327 } // namespace blink |
411 | 328 |
412 #endif // V8StringResource_h | 329 #endif // V8StringResource_h |
OLD | NEW |