Chromium Code Reviews

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

Issue 1097773004: Sharing of SharedArrayBuffer via PostMessage transfer (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: update for non-split typedarray hierarchy Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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...)
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
44 ArrayBufferContents(); 46 ArrayBufferContents();
45 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, ArrayBuf ferContents::InitializationPolicy); 47 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, bool isS hared, ArrayBufferContents::InitializationPolicy);
Yuki 2015/06/09 06:18:08 Our policy (style guide) prefers enum value to boo
binji 2015/06/10 06:04:39 Done.
46 48
47 // Use with care. data must be allocated with allocateMemory. 49 // Use with care. data must be allocated with allocateMemory.
48 // ArrayBufferContents will take ownership of the data and free it (using fr eeMemory) 50 // ArrayBufferContents will take ownership of the data and free it (using fr eeMemory)
49 // upon destruction. 51 // upon destruction.
50 // This constructor will not call observer->StartObserving(), so it is a res ponsibility 52 // 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. 53 // of the caller to make sure JS knows about external memory.
52 ArrayBufferContents(void* data, unsigned sizeInBytes, ArrayBufferDeallocatio nObserver*); 54 ArrayBufferContents(void* data, unsigned sizeInBytes, bool isShared, ArrayBu fferDeallocationObserver*);
53 55
54 ~ArrayBufferContents(); 56 ~ArrayBufferContents();
55 57
56 void clear(); 58 void clear();
57 59
58 void* data() const { return m_data; } 60 void* data() const { return m_holder ? m_holder->m_data : nullptr; }
59 unsigned sizeInBytes() const { return m_sizeInBytes; } 61 unsigned sizeInBytes() const { return m_holder ? m_holder->m_sizeInBytes : 0 ; }
62 bool isShared() const { return m_holder ? m_holder->m_isShared : false; }
60 63
61 void setDeallocationObserver(ArrayBufferDeallocationObserver& observer) 64 void setDeallocationObserver(ArrayBufferDeallocationObserver& observer)
62 { 65 {
63 if (!m_deallocationObserver) { 66 ASSERT(m_holder);
64 m_deallocationObserver = &observer; 67 if (!m_holder->m_deallocationObserver) {
65 m_deallocationObserver->blinkAllocatedMemory(m_sizeInBytes); 68 m_holder->m_deallocationObserver = &observer;
69 m_holder->m_deallocationObserver->blinkAllocatedMemory(m_holder->m_s izeInBytes);
66 } 70 }
67 } 71 }
68 void setDeallocationObserverWithoutAllocationNotification(ArrayBufferDealloc ationObserver& observer) 72 void setDeallocationObserverWithoutAllocationNotification(ArrayBufferDealloc ationObserver& observer)
69 { 73 {
70 if (!m_deallocationObserver) { 74 ASSERT(m_holder);
71 m_deallocationObserver = &observer; 75 if (!m_holder->m_deallocationObserver) {
76 m_holder->m_deallocationObserver = &observer;
72 } 77 }
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 adopt(void* data, unsigned sizeInBytes, bool shared, ArrayBufferDea llocationObserver*);
94 void copyToWithoutBuffer(DataHolder& other);
95
96 void* m_data;
Yuki 2015/06/09 06:18:08 Maybe you think it's overkilling, but I'd recommen
binji 2015/06/10 06:04:39 Done.
97 unsigned m_sizeInBytes;
98 bool m_isShared;
99 ArrayBufferDeallocationObserver* m_deallocationObserver;
100 };
101
102 RefPtr<DataHolder> m_holder;
85 }; 103 };
86 104
87 } // namespace WTF 105 } // namespace WTF
88 106
89 #endif // ArrayBufferContents_h 107 #endif // ArrayBufferContents_h
OLDNEW

Powered by Google App Engine