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 |
| 38 class DataHolder : public ThreadSafeRefCounted<DataHolder> { |
| 39 WTF_MAKE_NONCOPYABLE(DataHolder); |
| 40 public: |
| 41 DataHolder(); |
| 42 ~DataHolder(); |
| 43 |
| 44 void adopt(void* data, unsigned sizeInBytes, bool shared); |
| 45 |
| 46 void* m_data; |
| 47 unsigned m_sizeInBytes; |
| 48 bool m_shared; |
| 49 }; |
| 50 |
36 class WTF_EXPORT ArrayBufferContents { | 51 class WTF_EXPORT ArrayBufferContents { |
37 WTF_MAKE_NONCOPYABLE(ArrayBufferContents); | 52 WTF_MAKE_NONCOPYABLE(ArrayBufferContents); |
38 public: | 53 public: |
39 enum InitializationPolicy { | 54 enum InitializationPolicy { |
40 ZeroInitialize, | 55 ZeroInitialize, |
41 DontInitialize | 56 DontInitialize |
42 }; | 57 }; |
43 | 58 |
44 ArrayBufferContents(); | 59 ArrayBufferContents(); |
45 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBuf
ferContents::InitializationPolicy); | 60 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBuf
ferContents::InitializationPolicy); |
46 | 61 |
47 // Use with care. data must be allocated with allocateMemory. | 62 // Use with care. data must be allocated with allocateMemory. |
48 // ArrayBufferContents will take ownership of the data and free it (using fr
eeMemorY) | 63 // ArrayBufferContents will take ownership of the data and free it (using fr
eeMemorY) |
49 // upon destruction. | 64 // upon destruction. |
50 // This constructor will not call observer->StartObserving(), so it is a res
ponsibility | 65 // 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. | 66 // of the caller to make sure JS knows about external memory. |
52 ArrayBufferContents(void* data, unsigned sizeInBytes, ArrayBufferDeallocatio
nObserver*); | 67 ArrayBufferContents(void* data, unsigned sizeInBytes, bool shared, ArrayBuff
erDeallocationObserver*); |
53 | 68 |
54 ~ArrayBufferContents(); | 69 ~ArrayBufferContents(); |
55 | 70 |
56 void clear(); | 71 void clear(); |
57 | 72 |
58 void* data() const { return m_data; } | 73 void* data() const { return m_holder->m_data; } |
59 unsigned sizeInBytes() const { return m_sizeInBytes; } | 74 unsigned sizeInBytes() const { return m_holder->m_sizeInBytes; } |
| 75 bool shared() const { return m_holder->m_shared; } |
60 | 76 |
61 void setDeallocationObserver(ArrayBufferDeallocationObserver* observer) | 77 void setDeallocationObserver(ArrayBufferDeallocationObserver* observer) |
62 { | 78 { |
63 if (!m_deallocationObserver) { | 79 if (!m_deallocationObserver) { |
64 m_deallocationObserver = observer; | 80 m_deallocationObserver = observer; |
65 m_deallocationObserver->blinkAllocatedMemory(m_sizeInBytes); | 81 m_deallocationObserver->blinkAllocatedMemory(m_holder->m_sizeInBytes
); |
66 } | 82 } |
67 } | 83 } |
68 | 84 |
69 void transfer(ArrayBufferContents& other); | 85 void transfer(ArrayBufferContents& other); |
70 void copyTo(ArrayBufferContents& other); | 86 void copyTo(ArrayBufferContents& other); |
71 | 87 |
72 static void allocateMemory(size_t, InitializationPolicy, void*&); | 88 static void allocateMemory(size_t, InitializationPolicy, void*&); |
73 static void freeMemory(void*, size_t); | 89 static void freeMemory(void*, size_t); |
74 | 90 |
75 private: | 91 private: |
76 void* m_data; | 92 RefPtr<DataHolder> m_holder; |
77 unsigned m_sizeInBytes; | |
78 ArrayBufferDeallocationObserver* m_deallocationObserver; | 93 ArrayBufferDeallocationObserver* m_deallocationObserver; |
79 }; | 94 }; |
80 | 95 |
81 } // namespace WTF | 96 } // namespace WTF |
82 | 97 |
83 #endif // ArrayBufferContents_h | 98 #endif // ArrayBufferContents_h |
OLD | NEW |