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

Side by Side Diff: third_party/WebKit/Source/wtf/typed_arrays/ArrayBufferContents.h

Issue 2719883004: Adds support for ArrayBufferContents with external buffer. (Closed)
Patch Set: passes unique_ptr Created 3 years, 9 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 25 matching lines...) Expand all
36 #include "wtf/WTFExport.h" 36 #include "wtf/WTFExport.h"
37 37
38 namespace WTF { 38 namespace WTF {
39 39
40 class WTF_EXPORT ArrayBufferContents { 40 class WTF_EXPORT ArrayBufferContents {
41 WTF_MAKE_NONCOPYABLE(ArrayBufferContents); 41 WTF_MAKE_NONCOPYABLE(ArrayBufferContents);
42 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); 42 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
43 43
44 public: 44 public:
45 using AdjustAmountOfExternalAllocatedMemoryFunction = void (*)(int64_t diff); 45 using AdjustAmountOfExternalAllocatedMemoryFunction = void (*)(int64_t diff);
46 using DataDeleter = void (*)(void* data);
47 using ScopedData = std::unique_ptr<void, DataDeleter>;
jbroman 2017/02/28 20:01:19 A brief comment about the expected usage of this (
alokp 2017/02/28 20:56:08 Done.
46 48
47 enum InitializationPolicy { ZeroInitialize, DontInitialize }; 49 enum InitializationPolicy { ZeroInitialize, DontInitialize };
48 50
49 enum SharingType { 51 enum SharingType {
50 NotShared, 52 NotShared,
51 Shared, 53 Shared,
52 }; 54 };
53 55
54 ArrayBufferContents(); 56 ArrayBufferContents();
55 ArrayBufferContents(unsigned numElements, 57 ArrayBufferContents(unsigned numElements,
56 unsigned elementByteSize, 58 unsigned elementByteSize,
57 SharingType isShared, 59 SharingType isShared,
58 InitializationPolicy); 60 InitializationPolicy);
59 61 ArrayBufferContents(ScopedData, unsigned sizeInBytes, SharingType isShared);
60 // Use with care. data must be allocated with allocateMemory.
61 // ArrayBufferContents will take ownership of the data and free it (using
62 // freeMemory) upon destruction.
63 // This constructor will not call observer->StartObserving(), so it is a
64 // responsibility of the caller to make sure JS knows about external memory.
65 ArrayBufferContents(void* data, unsigned sizeInBytes, SharingType isShared);
66 62
67 ~ArrayBufferContents(); 63 ~ArrayBufferContents();
68 64
69 void neuter(); 65 void neuter();
70 66
71 void* data() const { 67 void* data() const {
72 DCHECK(!isShared()); 68 DCHECK(!isShared());
73 return dataMaybeShared(); 69 return dataMaybeShared();
74 } 70 }
75 void* dataShared() const { 71 void* dataShared() const {
76 DCHECK(isShared()); 72 DCHECK(isShared());
77 return dataMaybeShared(); 73 return dataMaybeShared();
78 } 74 }
79 void* dataMaybeShared() const { 75 void* dataMaybeShared() const {
80 return m_holder ? m_holder->data() : nullptr; 76 return m_holder ? m_holder->data() : nullptr;
81 } 77 }
82 unsigned sizeInBytes() const { 78 unsigned sizeInBytes() const {
83 return m_holder ? m_holder->sizeInBytes() : 0; 79 return m_holder ? m_holder->sizeInBytes() : 0;
84 } 80 }
85 bool isShared() const { return m_holder ? m_holder->isShared() : false; } 81 bool isShared() const { return m_holder ? m_holder->isShared() : false; }
86 82
87 void transfer(ArrayBufferContents& other); 83 void transfer(ArrayBufferContents& other);
88 void shareWith(ArrayBufferContents& other); 84 void shareWith(ArrayBufferContents& other);
89 void copyTo(ArrayBufferContents& other); 85 void copyTo(ArrayBufferContents& other);
90 86
91 static void allocateMemory(size_t, InitializationPolicy, void*&); 87 static void* allocateMemory(size_t, InitializationPolicy);
jbroman 2017/02/28 20:01:19 Possible bikeshed: but for most callers it might a
alokp 2017/02/28 20:56:08 Good idea. Although it would be a bit weird to not
92 static void allocateMemoryOrNull(size_t, InitializationPolicy, void*&); 88 static void* allocateMemoryOrNull(size_t, InitializationPolicy);
93 static void freeMemory(void*, size_t); 89 static void freeMemory(void*);
94 static void initialize( 90 static void initialize(
95 AdjustAmountOfExternalAllocatedMemoryFunction function) { 91 AdjustAmountOfExternalAllocatedMemoryFunction function) {
96 DCHECK(isMainThread()); 92 DCHECK(isMainThread());
97 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, 93 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction,
98 defaultAdjustAmountOfExternalAllocatedMemoryFunction); 94 defaultAdjustAmountOfExternalAllocatedMemoryFunction);
99 s_adjustAmountOfExternalAllocatedMemoryFunction = function; 95 s_adjustAmountOfExternalAllocatedMemoryFunction = function;
100 } 96 }
101 97
102 private: 98 private:
103 static void allocateMemoryWithFlags(size_t, 99 static void* allocateMemoryWithFlags(size_t, InitializationPolicy, int);
104 InitializationPolicy,
105 int,
106 void*&);
107 100
108 static void defaultAdjustAmountOfExternalAllocatedMemoryFunction( 101 static void defaultAdjustAmountOfExternalAllocatedMemoryFunction(
109 int64_t diff); 102 int64_t diff);
110 103
111 class DataHolder : public ThreadSafeRefCounted<DataHolder> { 104 class DataHolder : public ThreadSafeRefCounted<DataHolder> {
112 WTF_MAKE_NONCOPYABLE(DataHolder); 105 WTF_MAKE_NONCOPYABLE(DataHolder);
113 106
114 public: 107 public:
115 DataHolder(); 108 DataHolder();
116 ~DataHolder(); 109 ~DataHolder();
117 110
118 void allocateNew(unsigned sizeInBytes, 111 void allocateNew(unsigned sizeInBytes,
119 SharingType isShared, 112 SharingType isShared,
120 InitializationPolicy); 113 InitializationPolicy);
121 void adopt(void* data, unsigned sizeInBytes, SharingType isShared); 114 void adopt(ScopedData, unsigned sizeInBytes, SharingType isShared);
122 void copyMemoryFrom(const DataHolder& source); 115 void copyMemoryFrom(const DataHolder& source);
123 116
124 const void* data() const { return m_data; } 117 const void* data() const { return m_data.get(); }
125 void* data() { return m_data; } 118 void* data() { return m_data.get(); }
126 unsigned sizeInBytes() const { return m_sizeInBytes; } 119 unsigned sizeInBytes() const { return m_sizeInBytes; }
127 bool isShared() const { return m_isShared == Shared; } 120 bool isShared() const { return m_isShared == Shared; }
128 121
129 private: 122 private:
130 void adjustAmountOfExternalAllocatedMemory(int64_t diff) { 123 void adjustAmountOfExternalAllocatedMemory(int64_t diff) {
131 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent(); 124 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent();
132 s_adjustAmountOfExternalAllocatedMemoryFunction(diff); 125 s_adjustAmountOfExternalAllocatedMemoryFunction(diff);
133 } 126 }
134 void adjustAmountOfExternalAllocatedMemory(unsigned diff) { 127 void adjustAmountOfExternalAllocatedMemory(unsigned diff) {
135 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff)); 128 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff));
136 } 129 }
137 130
138 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() { 131 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() {
139 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction); 132 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction);
140 133
141 #if DCHECK_IS_ON() 134 #if DCHECK_IS_ON()
142 // Make sure that the function actually used is always the same. 135 // Make sure that the function actually used is always the same.
143 // Shouldn't be updated during its use. 136 // Shouldn't be updated during its use.
144 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) { 137 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) {
145 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction = 138 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction =
146 s_adjustAmountOfExternalAllocatedMemoryFunction; 139 s_adjustAmountOfExternalAllocatedMemoryFunction;
147 } 140 }
148 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, 141 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction,
149 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction); 142 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction);
150 #endif 143 #endif
151 } 144 }
152 145
153 void* m_data; 146 ScopedData m_data;
154 unsigned m_sizeInBytes; 147 unsigned m_sizeInBytes;
155 SharingType m_isShared; 148 SharingType m_isShared;
156 }; 149 };
157 150
158 RefPtr<DataHolder> m_holder; 151 RefPtr<DataHolder> m_holder;
159 static AdjustAmountOfExternalAllocatedMemoryFunction 152 static AdjustAmountOfExternalAllocatedMemoryFunction
160 s_adjustAmountOfExternalAllocatedMemoryFunction; 153 s_adjustAmountOfExternalAllocatedMemoryFunction;
161 #if DCHECK_IS_ON() 154 #if DCHECK_IS_ON()
162 static AdjustAmountOfExternalAllocatedMemoryFunction 155 static AdjustAmountOfExternalAllocatedMemoryFunction
163 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; 156 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction;
164 #endif 157 #endif
165 }; 158 };
166 159
167 } // namespace WTF 160 } // namespace WTF
168 161
169 #endif // ArrayBufferContents_h 162 #endif // ArrayBufferContents_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698