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/CompressableString.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 CompressableString& string) |
| 67 : m_compressableString(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_compressableString.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_compressableString); |
| 88 } |
73 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced
ExternalMemory); | 89 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced
ExternalMemory); |
| 90 |
| 91 // FIX this! |
74 } | 92 } |
75 | 93 |
76 const String& webcoreString() { return m_plainString; } | 94 String webcoreString() |
| 95 { |
| 96 if (!m_compressableString.isNull()) { |
| 97 ASSERT(m_plainString.isNull()); |
| 98 ASSERT(m_atomicString.isNull()); |
| 99 return m_compressableString.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_compressableString.isNull()) { |
| 110 ASSERT(m_plainString.isNull()); |
| 111 ASSERT(m_atomicString.isNull()); |
| 112 return AtomicString(m_compressableString.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 CompressableString& compressableString() { return m_compressableString
; } |
| 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 CompressableString m_compressableString; |
| 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 // TODO(hajimehoshi): This returns the size of |string| not compressed so |
| 144 // far. Is this really OK? |
| 145 static int memoryConsumption(const CompressableString& string) |
| 146 { |
| 147 return string.sizeInBytes(); |
| 148 } |
| 149 |
107 #if ENABLE(ASSERT) | 150 #if ENABLE(ASSERT) |
108 WTF::ThreadIdentifier m_threadId; | 151 WTF::ThreadIdentifier m_threadId; |
109 #endif | 152 #endif |
110 }; | 153 }; |
111 | 154 |
112 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { | 155 class WebCoreStringResource16 final : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { |
113 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); | 156 WTF_MAKE_NONCOPYABLE(WebCoreStringResource16); |
114 public: | 157 public: |
115 explicit WebCoreStringResource16(const String& string) | 158 explicit WebCoreStringResource16(const String& string) |
116 : WebCoreStringResourceBase(string) | 159 : WebCoreStringResourceBase(string) |
(...skipping 29 matching lines...) Expand all Loading... |
146 ASSERT(string.is8Bit()); | 189 ASSERT(string.is8Bit()); |
147 } | 190 } |
148 | 191 |
149 size_t length() const override { return m_plainString.impl()->length(); } | 192 size_t length() const override { return m_plainString.impl()->length(); } |
150 const char* data() const override | 193 const char* data() const override |
151 { | 194 { |
152 return reinterpret_cast<const char*>(m_plainString.impl()->characters8()
); | 195 return reinterpret_cast<const char*>(m_plainString.impl()->characters8()
); |
153 } | 196 } |
154 }; | 197 }; |
155 | 198 |
| 199 class WebCoreCompressableStringResource16 final : public WebCoreStringResourceBa
se, public v8::String::ExternalStringResource { |
| 200 WTF_MAKE_NONCOPYABLE(WebCoreCompressableStringResource16); |
| 201 public: |
| 202 explicit WebCoreCompressableStringResource16(const CompressableString& strin
g) |
| 203 : WebCoreStringResourceBase(string) |
| 204 { |
| 205 // ASSERT(m_string); |
| 206 ASSERT(!m_compressableString.is8Bit()); |
| 207 } |
| 208 |
| 209 bool isCompressable() override { return true; } |
| 210 |
| 211 size_t length() const override |
| 212 { |
| 213 return m_compressableString.length(); |
| 214 } |
| 215 |
| 216 const uint16_t* data() const override |
| 217 { |
| 218 return reinterpret_cast<const uint16_t*>(m_compressableString.characters
16()); |
| 219 } |
| 220 }; |
| 221 |
| 222 class WebCoreCompressableStringResource8 final : public WebCoreStringResourceBas
e, public v8::String::ExternalOneByteStringResource { |
| 223 WTF_MAKE_NONCOPYABLE(WebCoreCompressableStringResource8); |
| 224 public: |
| 225 explicit WebCoreCompressableStringResource8(const CompressableString& string
) |
| 226 : WebCoreStringResourceBase(string) |
| 227 { |
| 228 ASSERT(m_compressableString.is8Bit()); |
| 229 } |
| 230 |
| 231 bool isCompressable() override { return true; } |
| 232 |
| 233 size_t length() const override |
| 234 { |
| 235 return m_compressableString.length(); |
| 236 } |
| 237 |
| 238 const char* data() const override |
| 239 { |
| 240 return reinterpret_cast<const char*>(m_compressableString.characters8())
; |
| 241 } |
| 242 }; |
| 243 |
156 enum ExternalMode { | 244 enum ExternalMode { |
157 Externalize, | 245 Externalize, |
158 DoNotExternalize | 246 DoNotExternalize |
159 }; | 247 }; |
160 | 248 |
161 template <typename StringType> | 249 template <typename StringType> |
162 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo
de); | 250 CORE_EXPORT StringType v8StringToWebCoreString(v8::Local<v8::String>, ExternalMo
de); |
163 CORE_EXPORT String int32ToWebCoreString(int value); | 251 CORE_EXPORT String int32ToWebCoreString(int value); |
164 | 252 |
| 253 template <typename StringType> |
| 254 CORE_EXPORT StringType v8StringToWebCoreString2(v8::Local<v8::String>, ExternalM
ode); |
| 255 |
165 // V8StringResource is an adapter class that converts V8 values to Strings | 256 // V8StringResource is an adapter class that converts V8 values to Strings |
166 // or AtomicStrings as appropriate, using multiple typecast operators. | 257 // or AtomicStrings as appropriate, using multiple typecast operators. |
167 enum V8StringResourceMode { | 258 enum V8StringResourceMode { |
168 DefaultMode, | 259 DefaultMode, |
169 TreatNullAsEmptyString, | 260 TreatNullAsEmptyString, |
170 TreatNullAsNullString, | 261 TreatNullAsNullString, |
171 TreatNullAndUndefinedAsNullString | 262 TreatNullAndUndefinedAsNullString |
172 }; | 263 }; |
173 | 264 |
174 template <V8StringResourceMode Mode = DefaultMode> | 265 template <V8StringResourceMode Mode = DefaultMode> |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 } | 411 } |
321 | 412 |
322 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const | 413 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const |
323 { | 414 { |
324 return String(); | 415 return String(); |
325 } | 416 } |
326 | 417 |
327 } // namespace blink | 418 } // namespace blink |
328 | 419 |
329 #endif // V8StringResource_h | 420 #endif // V8StringResource_h |
OLD | NEW |