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/Assertions.h" | 30 #include "wtf/Assertions.h" |
31 #include "wtf/Noncopyable.h" | 31 #include "wtf/Noncopyable.h" |
32 #include "wtf/RefPtr.h" | |
33 #include "wtf/ThreadSafeRefCounted.h" | |
32 #include "wtf/WTF.h" | 34 #include "wtf/WTF.h" |
33 #include "wtf/WTFExport.h" | 35 #include "wtf/WTFExport.h" |
34 | 36 |
35 namespace WTF { | 37 namespace WTF { |
36 | 38 |
37 class WTF_EXPORT ArrayBufferContents { | 39 class WTF_EXPORT ArrayBufferContents { |
38 WTF_MAKE_NONCOPYABLE(ArrayBufferContents); | 40 WTF_MAKE_NONCOPYABLE(ArrayBufferContents); |
39 public: | 41 public: |
40 enum InitializationPolicy { | 42 enum InitializationPolicy { |
41 ZeroInitialize, | 43 ZeroInitialize, |
42 DontInitialize | 44 DontInitialize |
43 }; | 45 }; |
44 | 46 |
47 enum SharingType { | |
48 NotShared, | |
49 Shared, | |
50 }; | |
51 | |
45 ArrayBufferContents(); | 52 ArrayBufferContents(); |
46 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBuf ferContents::InitializationPolicy); | 53 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, SharingT ype isShared, ArrayBufferContents::InitializationPolicy); |
47 | 54 |
48 // Use with care. data must be allocated with allocateMemory. | 55 // Use with care. data must be allocated with allocateMemory. |
49 // ArrayBufferContents will take ownership of the data and free it (using fr eeMemory) | 56 // ArrayBufferContents will take ownership of the data and free it (using fr eeMemory) |
50 // upon destruction. | 57 // upon destruction. |
51 // This constructor will not call observer->StartObserving(), so it is a res ponsibility | 58 // This constructor will not call observer->StartObserving(), so it is a res ponsibility |
52 // of the caller to make sure JS knows about external memory. | 59 // of the caller to make sure JS knows about external memory. |
53 ArrayBufferContents(void* data, unsigned sizeInBytes); | 60 ArrayBufferContents(void* data, unsigned sizeInBytes, SharingType isShared); |
54 | 61 |
55 ~ArrayBufferContents(); | 62 ~ArrayBufferContents(); |
56 | 63 |
57 void clear(); | 64 void clear(); |
58 | 65 |
59 void* data() const { return m_data; } | 66 void* data() const { return m_holder ? m_holder->data() : nullptr; } |
60 unsigned sizeInBytes() const { return m_sizeInBytes; } | 67 unsigned sizeInBytes() const { return m_holder ? m_holder->sizeInBytes() : 0 ; } |
68 bool isShared() const { return m_holder ? m_holder->isShared() : false; } | |
haraken
2015/06/19 12:05:12
If we introduce DataHolder::clear(), we won't need
| |
61 | 69 |
62 void transfer(ArrayBufferContents& other); | 70 void transfer(ArrayBufferContents& other); |
63 void copyTo(ArrayBufferContents& other); | 71 void copyTo(ArrayBufferContents& other); |
64 | 72 |
65 static void allocateMemory(size_t, InitializationPolicy, void*&); | 73 static void allocateMemory(size_t, InitializationPolicy, void*&); |
66 static void freeMemory(void*, size_t); | 74 static void freeMemory(void*, size_t); |
67 static void setAdjustAmoutOfExternalAllocatedMemoryFunction(AdjustAmountOfEx ternalAllocatedMemoryFunction function) | 75 static void setAdjustAmoutOfExternalAllocatedMemoryFunction(AdjustAmountOfEx ternalAllocatedMemoryFunction function) |
68 { | 76 { |
69 ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction); | 77 ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction); |
70 s_adjustAmountOfExternalAllocatedMemoryFunction = function; | 78 s_adjustAmountOfExternalAllocatedMemoryFunction = function; |
71 } | 79 } |
72 | 80 |
73 private: | 81 private: |
74 void* m_data; | 82 class DataHolder : public ThreadSafeRefCounted<DataHolder> { |
75 unsigned m_sizeInBytes; | 83 WTF_MAKE_NONCOPYABLE(DataHolder); |
84 public: | |
85 DataHolder(); | |
86 ~DataHolder(); | |
87 | |
88 void allocateNew(unsigned sizeInBytes, SharingType isShared, Initializat ionPolicy); | |
89 void adopt(void* data, unsigned sizeInBytes, SharingType isShared); | |
90 void copyMemoryTo(DataHolder& other); | |
91 | |
92 void* data() const { return m_data; } | |
93 unsigned sizeInBytes() const { return m_sizeInBytes; } | |
94 bool isShared() const { return m_isShared == Shared; } | |
95 | |
96 private: | |
97 void* m_data; | |
98 unsigned m_sizeInBytes; | |
99 SharingType m_isShared; | |
100 }; | |
101 | |
102 RefPtr<DataHolder> m_holder; | |
76 static AdjustAmountOfExternalAllocatedMemoryFunction s_adjustAmountOfExterna lAllocatedMemoryFunction; | 103 static AdjustAmountOfExternalAllocatedMemoryFunction s_adjustAmountOfExterna lAllocatedMemoryFunction; |
77 }; | 104 }; |
78 | 105 |
79 } // namespace WTF | 106 } // namespace WTF |
80 | 107 |
81 #endif // ArrayBufferContents_h | 108 #endif // ArrayBufferContents_h |
OLD | NEW |