OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
3 * Copyright (C) 2013 Google Inc. All rights reserved. | 3 * Copyright (C) 2013 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
95 static void freeMemory(void*); | 95 static void freeMemory(void*); |
96 static DataHandle createDataHandle(size_t, InitializationPolicy); | 96 static DataHandle createDataHandle(size_t, InitializationPolicy); |
97 static void initialize( | 97 static void initialize( |
98 AdjustAmountOfExternalAllocatedMemoryFunction function) { | 98 AdjustAmountOfExternalAllocatedMemoryFunction function) { |
99 DCHECK(isMainThread()); | 99 DCHECK(isMainThread()); |
100 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, | 100 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, |
101 defaultAdjustAmountOfExternalAllocatedMemoryFunction); | 101 defaultAdjustAmountOfExternalAllocatedMemoryFunction); |
102 s_adjustAmountOfExternalAllocatedMemoryFunction = function; | 102 s_adjustAmountOfExternalAllocatedMemoryFunction = function; |
103 } | 103 } |
104 | 104 |
105 enum LeaveOrEnter { | 105 void registerExternalAllocationWithCurrentContext() { |
106 Leave, | 106 if (m_holder) |
107 Enter, | 107 m_holder->registerExternalAllocationWithCurrentContext(); |
108 }; | 108 } |
109 | 109 |
110 // Externally allocated memory is kept track of per context (isolate), | 110 void unregisterExternalAllocationWithCurrentContext() { |
111 // hence when moving ArrayBufferContents to another context, its | 111 if (m_holder) |
112 // externally allocated memory needs to be registered with its | 112 m_holder->unregisterExternalAllocationWithCurrentContext(); |
113 // destination context. | |
114 // | |
115 // Expose |adjustExternalAllocatedMemoryUponContextTransfer| in order to do | |
116 // so, which postMessage() implementations make use of when transferring | |
117 // array buffers. | |
118 void adjustExternalAllocatedMemoryUponContextTransfer( | |
119 LeaveOrEnter direction) { | |
120 int64_t diff = static_cast<int64_t>(sizeInBytes()); | |
121 if (!diff) | |
122 return; | |
123 if (direction == Leave) | |
124 diff = -diff; | |
125 m_holder->adjustAmountOfExternalAllocatedMemory(diff); | |
126 } | 113 } |
127 | 114 |
128 private: | 115 private: |
129 static void* allocateMemoryWithFlags(size_t, InitializationPolicy, int); | 116 static void* allocateMemoryWithFlags(size_t, InitializationPolicy, int); |
130 | 117 |
131 static void defaultAdjustAmountOfExternalAllocatedMemoryFunction( | 118 static void defaultAdjustAmountOfExternalAllocatedMemoryFunction( |
132 int64_t diff); | 119 int64_t diff); |
133 | 120 |
134 class DataHolder : public ThreadSafeRefCounted<DataHolder> { | 121 class DataHolder : public ThreadSafeRefCounted<DataHolder> { |
135 WTF_MAKE_NONCOPYABLE(DataHolder); | 122 WTF_MAKE_NONCOPYABLE(DataHolder); |
136 | 123 |
137 public: | 124 public: |
138 DataHolder(); | 125 DataHolder(); |
139 ~DataHolder(); | 126 ~DataHolder(); |
140 | 127 |
141 void allocateNew(unsigned sizeInBytes, | 128 void allocateNew(unsigned sizeInBytes, |
142 SharingType isShared, | 129 SharingType isShared, |
143 InitializationPolicy); | 130 InitializationPolicy); |
144 void adopt(DataHandle, unsigned sizeInBytes, SharingType isShared); | 131 void adopt(DataHandle, unsigned sizeInBytes, SharingType isShared); |
145 void copyMemoryFrom(const DataHolder& source); | 132 void copyMemoryFrom(const DataHolder& source); |
146 | 133 |
147 const void* data() const { return m_data.get(); } | 134 const void* data() const { return m_data.get(); } |
148 void* data() { return m_data.get(); } | 135 void* data() { return m_data.get(); } |
149 unsigned sizeInBytes() const { return m_sizeInBytes; } | 136 unsigned sizeInBytes() const { return m_sizeInBytes; } |
150 bool isShared() const { return m_isShared == Shared; } | 137 bool isShared() const { return m_isShared == Shared; } |
151 | 138 |
139 void registerExternalAllocationWithCurrentContext(); | |
140 void unregisterExternalAllocationWithCurrentContext(); | |
141 | |
142 private: | |
152 void adjustAmountOfExternalAllocatedMemory(int64_t diff) { | 143 void adjustAmountOfExternalAllocatedMemory(int64_t diff) { |
144 m_hasRegisteredExternalAllocation = !m_hasRegisteredExternalAllocation; | |
jbroman
2017/03/14 15:43:33
nit: sanity DCHECK that this is becoming true if d
sof
2017/03/14 16:51:39
Done.
| |
153 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent(); | 145 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent(); |
154 s_adjustAmountOfExternalAllocatedMemoryFunction(diff); | 146 s_adjustAmountOfExternalAllocatedMemoryFunction(diff); |
155 } | 147 } |
156 | 148 |
157 private: | |
158 void adjustAmountOfExternalAllocatedMemory(unsigned diff) { | 149 void adjustAmountOfExternalAllocatedMemory(unsigned diff) { |
159 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff)); | 150 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff)); |
160 } | 151 } |
161 | 152 |
162 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() { | 153 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() { |
163 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction); | 154 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction); |
164 | 155 |
165 #if DCHECK_IS_ON() | 156 #if DCHECK_IS_ON() |
166 // Make sure that the function actually used is always the same. | 157 // Make sure that the function actually used is always the same. |
167 // Shouldn't be updated during its use. | 158 // Shouldn't be updated during its use. |
168 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) { | 159 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) { |
169 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction = | 160 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction = |
170 s_adjustAmountOfExternalAllocatedMemoryFunction; | 161 s_adjustAmountOfExternalAllocatedMemoryFunction; |
171 } | 162 } |
172 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, | 163 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, |
173 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction); | 164 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction); |
174 #endif | 165 #endif |
175 } | 166 } |
176 | 167 |
177 DataHandle m_data; | 168 DataHandle m_data; |
178 unsigned m_sizeInBytes; | 169 unsigned m_sizeInBytes; |
179 SharingType m_isShared; | 170 SharingType m_isShared; |
171 bool m_hasRegisteredExternalAllocation; | |
180 }; | 172 }; |
181 | 173 |
182 RefPtr<DataHolder> m_holder; | 174 RefPtr<DataHolder> m_holder; |
183 static AdjustAmountOfExternalAllocatedMemoryFunction | 175 static AdjustAmountOfExternalAllocatedMemoryFunction |
184 s_adjustAmountOfExternalAllocatedMemoryFunction; | 176 s_adjustAmountOfExternalAllocatedMemoryFunction; |
185 #if DCHECK_IS_ON() | 177 #if DCHECK_IS_ON() |
186 static AdjustAmountOfExternalAllocatedMemoryFunction | 178 static AdjustAmountOfExternalAllocatedMemoryFunction |
187 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; | 179 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; |
188 #endif | 180 #endif |
189 }; | 181 }; |
190 | 182 |
191 } // namespace WTF | 183 } // namespace WTF |
192 | 184 |
193 #endif // ArrayBufferContents_h | 185 #endif // ArrayBufferContents_h |
OLD | NEW |