Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(475)

Side by Side Diff: Source/wtf/ArrayBufferContents.h

Issue 1186863005: Add a ref-counted data holder to ArrayBufferContents (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: type in ArrayBuffer::shareContentsWith Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 neuter();
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; }
61 69
62 void transfer(ArrayBufferContents& other); 70 void transfer(ArrayBufferContents& other);
71 void shareWith(ArrayBufferContents& other);
63 void copyTo(ArrayBufferContents& other); 72 void copyTo(ArrayBufferContents& other);
64 73
65 static void allocateMemory(size_t, InitializationPolicy, void*&); 74 static void allocateMemory(size_t, InitializationPolicy, void*&);
66 static void freeMemory(void*, size_t); 75 static void freeMemory(void*, size_t);
67 static void setAdjustAmoutOfExternalAllocatedMemoryFunction(AdjustAmountOfEx ternalAllocatedMemoryFunction function) 76 static void setAdjustAmoutOfExternalAllocatedMemoryFunction(AdjustAmountOfEx ternalAllocatedMemoryFunction function)
68 { 77 {
69 ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction); 78 ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction);
70 s_adjustAmountOfExternalAllocatedMemoryFunction = function; 79 s_adjustAmountOfExternalAllocatedMemoryFunction = function;
71 } 80 }
72 81
73 private: 82 private:
74 void* m_data; 83 class DataHolder : public ThreadSafeRefCounted<DataHolder> {
75 unsigned m_sizeInBytes; 84 WTF_MAKE_NONCOPYABLE(DataHolder);
85 public:
86 DataHolder();
87 ~DataHolder();
88
89 void allocateNew(unsigned sizeInBytes, SharingType isShared, Initializat ionPolicy);
90 void adopt(void* data, unsigned sizeInBytes, SharingType isShared);
91 void copyMemoryTo(DataHolder& other);
92
93 void* data() const { return m_data; }
94 unsigned sizeInBytes() const { return m_sizeInBytes; }
95 bool isShared() const { return m_isShared == Shared; }
96
97 private:
98 void* m_data;
99 unsigned m_sizeInBytes;
100 SharingType m_isShared;
101 };
102
103 RefPtr<DataHolder> m_holder;
76 static AdjustAmountOfExternalAllocatedMemoryFunction s_adjustAmountOfExterna lAllocatedMemoryFunction; 104 static AdjustAmountOfExternalAllocatedMemoryFunction s_adjustAmountOfExterna lAllocatedMemoryFunction;
77 }; 105 };
78 106
79 } // namespace WTF 107 } // namespace WTF
80 108
81 #endif // ArrayBufferContents_h 109 #endif // ArrayBufferContents_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698