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 24 matching lines...) Expand all Loading... |
35 | 35 |
36 class ExternalStringVisitor; | 36 class ExternalStringVisitor; |
37 | 37 |
38 // WebCoreStringResource is a helper class for v8ExternalString. It is used | 38 // WebCoreStringResource is a helper class for v8ExternalString. It is used |
39 // 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. |
40 class WebCoreStringResourceBase { | 40 class WebCoreStringResourceBase { |
41 public: | 41 public: |
42 explicit WebCoreStringResourceBase(const String& string) | 42 explicit WebCoreStringResourceBase(const String& string) |
43 : m_plainString(string) | 43 : m_plainString(string) |
44 { | 44 { |
45 #ifndef NDEBUG | 45 #if ENABLE(ASSERT) |
46 m_threadId = WTF::currentThread(); | 46 m_threadId = WTF::currentThread(); |
47 #endif | 47 #endif |
48 ASSERT(!string.isNull()); | 48 ASSERT(!string.isNull()); |
49 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC
onsumption(string)); | 49 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC
onsumption(string)); |
50 } | 50 } |
51 | 51 |
52 explicit WebCoreStringResourceBase(const AtomicString& string) | 52 explicit WebCoreStringResourceBase(const AtomicString& string) |
53 : m_plainString(string.string()) | 53 : m_plainString(string.string()) |
54 , m_atomicString(string) | 54 , m_atomicString(string) |
55 { | 55 { |
56 #ifndef NDEBUG | 56 #if ENABLE(ASSERT) |
57 m_threadId = WTF::currentThread(); | 57 m_threadId = WTF::currentThread(); |
58 #endif | 58 #endif |
59 ASSERT(!string.isNull()); | 59 ASSERT(!string.isNull()); |
60 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC
onsumption(string)); | 60 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(memoryC
onsumption(string)); |
61 } | 61 } |
62 | 62 |
63 virtual ~WebCoreStringResourceBase() | 63 virtual ~WebCoreStringResourceBase() |
64 { | 64 { |
65 #ifndef NDEBUG | 65 #if ENABLE(ASSERT) |
66 ASSERT(m_threadId == WTF::currentThread()); | 66 ASSERT(m_threadId == WTF::currentThread()); |
67 #endif | 67 #endif |
68 int reducedExternalMemory = -memoryConsumption(m_plainString); | 68 int reducedExternalMemory = -memoryConsumption(m_plainString); |
69 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isN
ull()) | 69 if (m_plainString.impl() != m_atomicString.impl() && !m_atomicString.isN
ull()) |
70 reducedExternalMemory -= memoryConsumption(m_atomicString.string()); | 70 reducedExternalMemory -= memoryConsumption(m_atomicString.string()); |
71 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced
ExternalMemory); | 71 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(reduced
ExternalMemory); |
72 } | 72 } |
73 | 73 |
74 const String& webcoreString() { return m_plainString; } | 74 const String& webcoreString() { return m_plainString; } |
75 | 75 |
76 const AtomicString& atomicString() | 76 const AtomicString& atomicString() |
77 { | 77 { |
78 #ifndef NDEBUG | 78 #if ENABLE(ASSERT) |
79 ASSERT(m_threadId == WTF::currentThread()); | 79 ASSERT(m_threadId == WTF::currentThread()); |
80 #endif | 80 #endif |
81 if (m_atomicString.isNull()) { | 81 if (m_atomicString.isNull()) { |
82 m_atomicString = AtomicString(m_plainString); | 82 m_atomicString = AtomicString(m_plainString); |
83 ASSERT(!m_atomicString.isNull()); | 83 ASSERT(!m_atomicString.isNull()); |
84 if (m_plainString.impl() != m_atomicString.impl()) | 84 if (m_plainString.impl() != m_atomicString.impl()) |
85 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory
(memoryConsumption(m_atomicString.string())); | 85 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory
(memoryConsumption(m_atomicString.string())); |
86 } | 86 } |
87 return m_atomicString; | 87 return m_atomicString; |
88 } | 88 } |
89 | 89 |
90 protected: | 90 protected: |
91 // A shallow copy of the string. Keeps the string buffer alive until the V8
engine garbage collects it. | 91 // A shallow copy of the string. Keeps the string buffer alive until the V8
engine garbage collects it. |
92 String m_plainString; | 92 String m_plainString; |
93 // If this string is atomic or has been made atomic earlier the | 93 // If this string is atomic or has been made atomic earlier the |
94 // atomic string is held here. In the case where the string starts | 94 // atomic string is held here. In the case where the string starts |
95 // off non-atomic and becomes atomic later it is necessary to keep | 95 // off non-atomic and becomes atomic later it is necessary to keep |
96 // the original string alive because v8 may keep derived pointers | 96 // the original string alive because v8 may keep derived pointers |
97 // into that string. | 97 // into that string. |
98 AtomicString m_atomicString; | 98 AtomicString m_atomicString; |
99 | 99 |
100 private: | 100 private: |
101 static int memoryConsumption(const String& string) | 101 static int memoryConsumption(const String& string) |
102 { | 102 { |
103 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar
)); | 103 return string.length() * (string.is8Bit() ? sizeof(LChar) : sizeof(UChar
)); |
104 } | 104 } |
105 #ifndef NDEBUG | 105 #if ENABLE(ASSERT) |
106 WTF::ThreadIdentifier m_threadId; | 106 WTF::ThreadIdentifier m_threadId; |
107 #endif | 107 #endif |
108 }; | 108 }; |
109 | 109 |
110 class WebCoreStringResource16 FINAL : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { | 110 class WebCoreStringResource16 FINAL : public WebCoreStringResourceBase, public v
8::String::ExternalStringResource { |
111 public: | 111 public: |
112 explicit WebCoreStringResource16(const String& string) | 112 explicit WebCoreStringResource16(const String& string) |
113 : WebCoreStringResourceBase(string) | 113 : WebCoreStringResourceBase(string) |
114 { | 114 { |
115 ASSERT(!string.is8Bit()); | 115 ASSERT(!string.is8Bit()); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 288 } |
289 | 289 |
290 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const | 290 template<> inline String V8StringResource<TreatNullAndUndefinedAsNullString>::fa
llbackString() const |
291 { | 291 { |
292 return String(); | 292 return String(); |
293 } | 293 } |
294 | 294 |
295 } // namespace WebCore | 295 } // namespace WebCore |
296 | 296 |
297 #endif // V8StringResource_h | 297 #endif // V8StringResource_h |
OLD | NEW |