Chromium Code Reviews| 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 11 matching lines...) Expand all Loading... | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 */ | 25 */ |
| 26 | 26 |
| 27 #ifndef ArrayBufferContents_h | 27 #ifndef ArrayBufferContents_h |
| 28 #define ArrayBufferContents_h | 28 #define ArrayBufferContents_h |
| 29 | 29 |
| 30 #include "wtf/ArrayBufferDeallocationObserver.h" | 30 #include "wtf/ArrayBufferDeallocationObserver.h" |
| 31 #include "wtf/Noncopyable.h" | 31 #include "wtf/Noncopyable.h" |
| 32 #include "wtf/RefPtr.h" | |
| 33 #include "wtf/ThreadSafeRefCounted.h" | |
| 32 #include "wtf/WTFExport.h" | 34 #include "wtf/WTFExport.h" |
| 33 | 35 |
| 34 namespace WTF { | 36 namespace WTF { |
| 35 | 37 |
| 36 class WTF_EXPORT ArrayBufferContents { | 38 class WTF_EXPORT ArrayBufferContents { |
| 37 WTF_MAKE_NONCOPYABLE(ArrayBufferContents); | 39 WTF_MAKE_NONCOPYABLE(ArrayBufferContents); |
| 38 public: | 40 public: |
| 39 enum InitializationPolicy { | 41 enum InitializationPolicy { |
| 40 ZeroInitialize, | 42 ZeroInitialize, |
| 41 DontInitialize | 43 DontInitialize |
| 42 }; | 44 }; |
| 43 | 45 |
| 46 enum SharingType { | |
| 47 NotShared, | |
| 48 Shared, | |
| 49 }; | |
| 50 | |
| 44 ArrayBufferContents(); | 51 ArrayBufferContents(); |
| 45 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBuf ferContents::InitializationPolicy); | 52 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, SharingT ype isShared, ArrayBufferContents::InitializationPolicy); |
| 46 | 53 |
| 47 // Use with care. data must be allocated with allocateMemory. | 54 // Use with care. data must be allocated with allocateMemory. |
| 48 // ArrayBufferContents will take ownership of the data and free it (using fr eeMemory) | 55 // ArrayBufferContents will take ownership of the data and free it (using fr eeMemory) |
| 49 // upon destruction. | 56 // upon destruction. |
| 50 // This constructor will not call observer->StartObserving(), so it is a res ponsibility | 57 // This constructor will not call observer->StartObserving(), so it is a res ponsibility |
| 51 // of the caller to make sure JS knows about external memory. | 58 // of the caller to make sure JS knows about external memory. |
| 52 ArrayBufferContents(void* data, unsigned sizeInBytes, ArrayBufferDeallocatio nObserver*); | 59 ArrayBufferContents(void* data, unsigned sizeInBytes, SharingType isShared, ArrayBufferDeallocationObserver*); |
| 53 | 60 |
| 54 ~ArrayBufferContents(); | 61 ~ArrayBufferContents(); |
| 55 | 62 |
| 56 void clear(); | 63 void clear(); |
| 57 | 64 |
| 58 void* data() const { return m_data; } | 65 void* data() const { return m_holder ? m_holder->data() : nullptr; } |
|
haraken
2015/06/11 05:59:29
Is it a valid thing to access these fields after t
| |
| 59 unsigned sizeInBytes() const { return m_sizeInBytes; } | 66 unsigned sizeInBytes() const { return m_holder ? m_holder->sizeInBytes() : 0 ; } |
| 67 bool isShared() const { return m_holder ? m_holder->isShared() : false; } | |
| 60 | 68 |
| 61 void setDeallocationObserver(ArrayBufferDeallocationObserver& observer) | 69 void setDeallocationObserver(ArrayBufferDeallocationObserver& observer) |
| 62 { | 70 { |
| 63 if (!m_deallocationObserver) { | 71 ASSERT(m_holder); |
| 64 m_deallocationObserver = &observer; | 72 m_holder->setDeallocationObserver(observer); |
| 65 m_deallocationObserver->blinkAllocatedMemory(m_sizeInBytes); | |
| 66 } | |
| 67 } | 73 } |
| 68 void setDeallocationObserverWithoutAllocationNotification(ArrayBufferDealloc ationObserver& observer) | 74 void setDeallocationObserverWithoutAllocationNotification(ArrayBufferDealloc ationObserver& observer) |
| 69 { | 75 { |
| 70 if (!m_deallocationObserver) { | 76 ASSERT(m_holder); |
| 71 m_deallocationObserver = &observer; | 77 m_holder->setDeallocationObserverWithoutAllocationNotification(observer) ; |
| 72 } | |
| 73 } | 78 } |
| 74 | 79 |
| 75 void transfer(ArrayBufferContents& other); | 80 void transfer(ArrayBufferContents& other); |
| 76 void copyTo(ArrayBufferContents& other); | 81 void copyTo(ArrayBufferContents& other); |
| 77 | 82 |
| 78 static void allocateMemory(size_t, InitializationPolicy, void*&); | 83 static void allocateMemory(size_t, InitializationPolicy, void*&); |
| 79 static void freeMemory(void*, size_t); | 84 static void freeMemory(void*, size_t); |
| 80 | 85 |
| 81 private: | 86 private: |
| 82 void* m_data; | 87 class DataHolder : public ThreadSafeRefCounted<DataHolder> { |
| 83 unsigned m_sizeInBytes; | 88 WTF_MAKE_NONCOPYABLE(DataHolder); |
| 84 ArrayBufferDeallocationObserver* m_deallocationObserver; | 89 public: |
| 90 DataHolder(); | |
| 91 ~DataHolder(); | |
| 92 | |
| 93 void allocateNew(unsigned sizeInBytes, SharingType isShared, Initializat ionPolicy, ArrayBufferDeallocationObserver*); | |
| 94 void adopt(void* data, unsigned sizeInBytes, SharingType isShared, Array BufferDeallocationObserver*); | |
| 95 void copyMemoryTo(DataHolder& other); | |
| 96 | |
| 97 void* data() const { return m_data; } | |
| 98 unsigned sizeInBytes() const { return m_sizeInBytes; } | |
| 99 bool isShared() const { return m_isShared == Shared; } | |
| 100 void setDeallocationObserver(ArrayBufferDeallocationObserver&); | |
| 101 void setDeallocationObserverWithoutAllocationNotification(ArrayBufferDea llocationObserver&); | |
| 102 | |
| 103 private: | |
| 104 void* m_data; | |
| 105 unsigned m_sizeInBytes; | |
| 106 SharingType m_isShared; | |
| 107 ArrayBufferDeallocationObserver* m_deallocationObserver; | |
| 108 }; | |
| 109 | |
| 110 RefPtr<DataHolder> m_holder; | |
| 85 }; | 111 }; |
| 86 | 112 |
| 87 } // namespace WTF | 113 } // namespace WTF |
| 88 | 114 |
| 89 #endif // ArrayBufferContents_h | 115 #endif // ArrayBufferContents_h |
| OLD | NEW |