| 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; |
| 145 DCHECK(!diff || (m_hasRegisteredExternalAllocation == (diff > 0))); |
| 153 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent(); | 146 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent(); |
| 154 s_adjustAmountOfExternalAllocatedMemoryFunction(diff); | 147 s_adjustAmountOfExternalAllocatedMemoryFunction(diff); |
| 155 } | 148 } |
| 156 | 149 |
| 157 private: | |
| 158 void adjustAmountOfExternalAllocatedMemory(unsigned diff) { | 150 void adjustAmountOfExternalAllocatedMemory(unsigned diff) { |
| 159 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff)); | 151 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff)); |
| 160 } | 152 } |
| 161 | 153 |
| 162 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() { | 154 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() { |
| 163 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction); | 155 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction); |
| 164 | 156 |
| 165 #if DCHECK_IS_ON() | 157 #if DCHECK_IS_ON() |
| 166 // Make sure that the function actually used is always the same. | 158 // Make sure that the function actually used is always the same. |
| 167 // Shouldn't be updated during its use. | 159 // Shouldn't be updated during its use. |
| 168 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) { | 160 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) { |
| 169 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction = | 161 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction = |
| 170 s_adjustAmountOfExternalAllocatedMemoryFunction; | 162 s_adjustAmountOfExternalAllocatedMemoryFunction; |
| 171 } | 163 } |
| 172 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, | 164 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, |
| 173 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction); | 165 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction); |
| 174 #endif | 166 #endif |
| 175 } | 167 } |
| 176 | 168 |
| 177 DataHandle m_data; | 169 DataHandle m_data; |
| 178 unsigned m_sizeInBytes; | 170 unsigned m_sizeInBytes; |
| 179 SharingType m_isShared; | 171 SharingType m_isShared; |
| 172 bool m_hasRegisteredExternalAllocation; |
| 180 }; | 173 }; |
| 181 | 174 |
| 182 RefPtr<DataHolder> m_holder; | 175 RefPtr<DataHolder> m_holder; |
| 183 static AdjustAmountOfExternalAllocatedMemoryFunction | 176 static AdjustAmountOfExternalAllocatedMemoryFunction |
| 184 s_adjustAmountOfExternalAllocatedMemoryFunction; | 177 s_adjustAmountOfExternalAllocatedMemoryFunction; |
| 185 #if DCHECK_IS_ON() | 178 #if DCHECK_IS_ON() |
| 186 static AdjustAmountOfExternalAllocatedMemoryFunction | 179 static AdjustAmountOfExternalAllocatedMemoryFunction |
| 187 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; | 180 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; |
| 188 #endif | 181 #endif |
| 189 }; | 182 }; |
| 190 | 183 |
| 191 } // namespace WTF | 184 } // namespace WTF |
| 192 | 185 |
| 193 #endif // ArrayBufferContents_h | 186 #endif // ArrayBufferContents_h |
| OLD | NEW |