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 25 matching lines...) Expand all Loading... |
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 // Types that need to be used when injecting external memory. |
| 47 // DataHandle allows specifying a deleter which will be invoked when |
| 48 // DataHandle instance goes out of scope. If the data memory is allocated |
| 49 // using ArrayBufferContents::allocateMemoryOrNull, it is necessary to |
| 50 // specify ArrayBufferContents::freeMemory as the DataDeleter. |
| 51 // Most clients would want to use ArrayBufferContents::createData, which |
| 52 // allocates memory and specifies the correct deleter. |
| 53 using DataDeleter = void (*)(void* data); |
| 54 using DataHandle = std::unique_ptr<void, DataDeleter>; |
46 | 55 |
47 enum InitializationPolicy { ZeroInitialize, DontInitialize }; | 56 enum InitializationPolicy { ZeroInitialize, DontInitialize }; |
48 | 57 |
49 enum SharingType { | 58 enum SharingType { |
50 NotShared, | 59 NotShared, |
51 Shared, | 60 Shared, |
52 }; | 61 }; |
53 | 62 |
54 ArrayBufferContents(); | 63 ArrayBufferContents(); |
55 ArrayBufferContents(unsigned numElements, | 64 ArrayBufferContents(unsigned numElements, |
56 unsigned elementByteSize, | 65 unsigned elementByteSize, |
57 SharingType isShared, | 66 SharingType isShared, |
58 InitializationPolicy); | 67 InitializationPolicy); |
59 | 68 ArrayBufferContents(DataHandle, 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 | 69 |
67 ~ArrayBufferContents(); | 70 ~ArrayBufferContents(); |
68 | 71 |
69 void neuter(); | 72 void neuter(); |
70 | 73 |
71 void* data() const { | 74 void* data() const { |
72 DCHECK(!isShared()); | 75 DCHECK(!isShared()); |
73 return dataMaybeShared(); | 76 return dataMaybeShared(); |
74 } | 77 } |
75 void* dataShared() const { | 78 void* dataShared() const { |
76 DCHECK(isShared()); | 79 DCHECK(isShared()); |
77 return dataMaybeShared(); | 80 return dataMaybeShared(); |
78 } | 81 } |
79 void* dataMaybeShared() const { | 82 void* dataMaybeShared() const { |
80 return m_holder ? m_holder->data() : nullptr; | 83 return m_holder ? m_holder->data() : nullptr; |
81 } | 84 } |
82 unsigned sizeInBytes() const { | 85 unsigned sizeInBytes() const { |
83 return m_holder ? m_holder->sizeInBytes() : 0; | 86 return m_holder ? m_holder->sizeInBytes() : 0; |
84 } | 87 } |
85 bool isShared() const { return m_holder ? m_holder->isShared() : false; } | 88 bool isShared() const { return m_holder ? m_holder->isShared() : false; } |
86 | 89 |
87 void transfer(ArrayBufferContents& other); | 90 void transfer(ArrayBufferContents& other); |
88 void shareWith(ArrayBufferContents& other); | 91 void shareWith(ArrayBufferContents& other); |
89 void copyTo(ArrayBufferContents& other); | 92 void copyTo(ArrayBufferContents& other); |
90 | 93 |
91 static void allocateMemory(size_t, InitializationPolicy, void*&); | 94 static void* allocateMemoryOrNull(size_t, InitializationPolicy); |
92 static void allocateMemoryOrNull(size_t, InitializationPolicy, void*&); | 95 static void freeMemory(void*); |
93 static void freeMemory(void*, size_t); | 96 static DataHandle createDataHandle(size_t, InitializationPolicy); |
94 static void initialize( | 97 static void initialize( |
95 AdjustAmountOfExternalAllocatedMemoryFunction function) { | 98 AdjustAmountOfExternalAllocatedMemoryFunction function) { |
96 DCHECK(isMainThread()); | 99 DCHECK(isMainThread()); |
97 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, | 100 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, |
98 defaultAdjustAmountOfExternalAllocatedMemoryFunction); | 101 defaultAdjustAmountOfExternalAllocatedMemoryFunction); |
99 s_adjustAmountOfExternalAllocatedMemoryFunction = function; | 102 s_adjustAmountOfExternalAllocatedMemoryFunction = function; |
100 } | 103 } |
101 | 104 |
102 private: | 105 private: |
103 static void allocateMemoryWithFlags(size_t, | 106 static void* allocateMemoryWithFlags(size_t, InitializationPolicy, int); |
104 InitializationPolicy, | |
105 int, | |
106 void*&); | |
107 | 107 |
108 static void defaultAdjustAmountOfExternalAllocatedMemoryFunction( | 108 static void defaultAdjustAmountOfExternalAllocatedMemoryFunction( |
109 int64_t diff); | 109 int64_t diff); |
110 | 110 |
111 class DataHolder : public ThreadSafeRefCounted<DataHolder> { | 111 class DataHolder : public ThreadSafeRefCounted<DataHolder> { |
112 WTF_MAKE_NONCOPYABLE(DataHolder); | 112 WTF_MAKE_NONCOPYABLE(DataHolder); |
113 | 113 |
114 public: | 114 public: |
115 DataHolder(); | 115 DataHolder(); |
116 ~DataHolder(); | 116 ~DataHolder(); |
117 | 117 |
118 void allocateNew(unsigned sizeInBytes, | 118 void allocateNew(unsigned sizeInBytes, |
119 SharingType isShared, | 119 SharingType isShared, |
120 InitializationPolicy); | 120 InitializationPolicy); |
121 void adopt(void* data, unsigned sizeInBytes, SharingType isShared); | 121 void adopt(DataHandle, unsigned sizeInBytes, SharingType isShared); |
122 void copyMemoryFrom(const DataHolder& source); | 122 void copyMemoryFrom(const DataHolder& source); |
123 | 123 |
124 const void* data() const { return m_data; } | 124 const void* data() const { return m_data.get(); } |
125 void* data() { return m_data; } | 125 void* data() { return m_data.get(); } |
126 unsigned sizeInBytes() const { return m_sizeInBytes; } | 126 unsigned sizeInBytes() const { return m_sizeInBytes; } |
127 bool isShared() const { return m_isShared == Shared; } | 127 bool isShared() const { return m_isShared == Shared; } |
128 | 128 |
129 private: | 129 private: |
130 void adjustAmountOfExternalAllocatedMemory(int64_t diff) { | 130 void adjustAmountOfExternalAllocatedMemory(int64_t diff) { |
131 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent(); | 131 checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent(); |
132 s_adjustAmountOfExternalAllocatedMemoryFunction(diff); | 132 s_adjustAmountOfExternalAllocatedMemoryFunction(diff); |
133 } | 133 } |
134 void adjustAmountOfExternalAllocatedMemory(unsigned diff) { | 134 void adjustAmountOfExternalAllocatedMemory(unsigned diff) { |
135 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff)); | 135 adjustAmountOfExternalAllocatedMemory(static_cast<int64_t>(diff)); |
136 } | 136 } |
137 | 137 |
138 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() { | 138 void checkIfAdjustAmountOfExternalAllocatedMemoryIsConsistent() { |
139 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction); | 139 DCHECK(s_adjustAmountOfExternalAllocatedMemoryFunction); |
140 | 140 |
141 #if DCHECK_IS_ON() | 141 #if DCHECK_IS_ON() |
142 // Make sure that the function actually used is always the same. | 142 // Make sure that the function actually used is always the same. |
143 // Shouldn't be updated during its use. | 143 // Shouldn't be updated during its use. |
144 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) { | 144 if (!s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction) { |
145 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction = | 145 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction = |
146 s_adjustAmountOfExternalAllocatedMemoryFunction; | 146 s_adjustAmountOfExternalAllocatedMemoryFunction; |
147 } | 147 } |
148 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, | 148 DCHECK_EQ(s_adjustAmountOfExternalAllocatedMemoryFunction, |
149 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction); | 149 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction); |
150 #endif | 150 #endif |
151 } | 151 } |
152 | 152 |
153 void* m_data; | 153 DataHandle m_data; |
154 unsigned m_sizeInBytes; | 154 unsigned m_sizeInBytes; |
155 SharingType m_isShared; | 155 SharingType m_isShared; |
156 }; | 156 }; |
157 | 157 |
158 RefPtr<DataHolder> m_holder; | 158 RefPtr<DataHolder> m_holder; |
159 static AdjustAmountOfExternalAllocatedMemoryFunction | 159 static AdjustAmountOfExternalAllocatedMemoryFunction |
160 s_adjustAmountOfExternalAllocatedMemoryFunction; | 160 s_adjustAmountOfExternalAllocatedMemoryFunction; |
161 #if DCHECK_IS_ON() | 161 #if DCHECK_IS_ON() |
162 static AdjustAmountOfExternalAllocatedMemoryFunction | 162 static AdjustAmountOfExternalAllocatedMemoryFunction |
163 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; | 163 s_lastUsedAdjustAmountOfExternalAllocatedMemoryFunction; |
164 #endif | 164 #endif |
165 }; | 165 }; |
166 | 166 |
167 } // namespace WTF | 167 } // namespace WTF |
168 | 168 |
169 #endif // ArrayBufferContents_h | 169 #endif // ArrayBufferContents_h |
OLD | NEW |