[email protected] | 6654df8 | 2013-05-08 20:56:05 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 3 | * Copyright (C) 2013 Google Inc. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 25 | */ |
| 26 | |
| 27 | #ifndef ArrayBufferContents_h |
| 28 | #define ArrayBufferContents_h |
| 29 | |
tasak | 4faca4c4 | 2016-01-19 08:38:55 | [diff] [blame] | 30 | #include "wtf/Allocator.h" |
[email protected] | cce1e96 | 2015-06-16 08:13:15 | [diff] [blame] | 31 | #include "wtf/Assertions.h" |
[email protected] | 6654df8 | 2013-05-08 20:56:05 | [diff] [blame] | 32 | #include "wtf/Noncopyable.h" |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 33 | #include "wtf/RefPtr.h" |
| 34 | #include "wtf/ThreadSafeRefCounted.h" |
[email protected] | cce1e96 | 2015-06-16 08:13:15 | [diff] [blame] | 35 | #include "wtf/WTF.h" |
[email protected] | b7616fb6 | 2013-05-27 23:43:05 | [diff] [blame] | 36 | #include "wtf/WTFExport.h" |
[email protected] | 6654df8 | 2013-05-08 20:56:05 | [diff] [blame] | 37 | |
| 38 | namespace WTF { |
| 39 | |
haraken | 02cfffa7 | 2016-03-10 19:55:46 | [diff] [blame] | 40 | typedef void(*AdjustAmountOfExternalAllocatedMemoryFunction)(int size); |
| 41 | |
[email protected] | b7616fb6 | 2013-05-27 23:43:05 | [diff] [blame] | 42 | class WTF_EXPORT ArrayBufferContents { |
[email protected] | 6654df8 | 2013-05-08 20:56:05 | [diff] [blame] | 43 | WTF_MAKE_NONCOPYABLE(ArrayBufferContents); |
tasak | 4faca4c4 | 2016-01-19 08:38:55 | [diff] [blame] | 44 | DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); |
[email protected] | 6654df8 | 2013-05-08 20:56:05 | [diff] [blame] | 45 | public: |
[email protected] | 27de049 | 2013-05-15 01:50:49 | [diff] [blame] | 46 | enum InitializationPolicy { |
| 47 | ZeroInitialize, |
| 48 | DontInitialize |
| 49 | }; |
| 50 | |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 51 | enum SharingType { |
| 52 | NotShared, |
| 53 | Shared, |
| 54 | }; |
| 55 | |
[email protected] | 687f21fd | 2013-05-10 14:52:46 | [diff] [blame] | 56 | ArrayBufferContents(); |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 57 | ArrayBufferContents(unsigned numElements, unsigned elementByteSize, SharingType isShared, ArrayBufferContents::InitializationPolicy); |
[email protected] | 6654df8 | 2013-05-08 20:56:05 | [diff] [blame] | 58 | |
[email protected] | bfcac9b | 2013-07-05 10:08:21 | [diff] [blame] | 59 | // Use with care. data must be allocated with allocateMemory. |
[email protected] | 3dfbd2e | 2014-10-20 11:37:28 | [diff] [blame] | 60 | // ArrayBufferContents will take ownership of the data and free it (using freeMemory) |
[email protected] | bfcac9b | 2013-07-05 10:08:21 | [diff] [blame] | 61 | // upon destruction. |
[email protected] | 238bd49 | 2013-09-23 15:15:17 | [diff] [blame] | 62 | // This constructor will not call observer->StartObserving(), so it is a responsibility |
| 63 | // of the caller to make sure JS knows about external memory. |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 64 | ArrayBufferContents(void* data, unsigned sizeInBytes, SharingType isShared); |
[email protected] | bfcac9b | 2013-07-05 10:08:21 | [diff] [blame] | 65 | |
[email protected] | 687f21fd | 2013-05-10 14:52:46 | [diff] [blame] | 66 | ~ArrayBufferContents(); |
| 67 | |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 68 | void neuter(); |
[email protected] | 687f21fd | 2013-05-10 14:52:46 | [diff] [blame] | 69 | |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 70 | void* data() const { return m_holder ? m_holder->data() : nullptr; } |
| 71 | unsigned sizeInBytes() const { return m_holder ? m_holder->sizeInBytes() : 0; } |
| 72 | bool isShared() const { return m_holder ? m_holder->isShared() : false; } |
[email protected] | 27de049 | 2013-05-15 01:50:49 | [diff] [blame] | 73 | |
[email protected] | 27de049 | 2013-05-15 01:50:49 | [diff] [blame] | 74 | void transfer(ArrayBufferContents& other); |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 75 | void shareWith(ArrayBufferContents& other); |
[email protected] | 1d31c12e | 2013-07-27 00:13:57 | [diff] [blame] | 76 | void copyTo(ArrayBufferContents& other); |
[email protected] | 27de049 | 2013-05-15 01:50:49 | [diff] [blame] | 77 | |
[email protected] | 664133b | 2013-08-07 23:34:12 | [diff] [blame] | 78 | static void allocateMemory(size_t, InitializationPolicy, void*&); |
caitpotter88 | 74b20fa | 2016-01-14 19:06:20 | [diff] [blame] | 79 | static void allocateMemoryOrNull(size_t, InitializationPolicy, void*&); |
[email protected] | 664133b | 2013-08-07 23:34:12 | [diff] [blame] | 80 | static void freeMemory(void*, size_t); |
haraken | 02cfffa7 | 2016-03-10 19:55:46 | [diff] [blame] | 81 | static void initialize(AdjustAmountOfExternalAllocatedMemoryFunction function) |
[email protected] | cce1e96 | 2015-06-16 08:13:15 | [diff] [blame] | 82 | { |
haraken | 02cfffa7 | 2016-03-10 19:55:46 | [diff] [blame] | 83 | ASSERT(isMainThread()); |
[email protected] | cce1e96 | 2015-06-16 08:13:15 | [diff] [blame] | 84 | ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction); |
| 85 | s_adjustAmountOfExternalAllocatedMemoryFunction = function; |
| 86 | } |
[email protected] | bfcac9b | 2013-07-05 10:08:21 | [diff] [blame] | 87 | |
[email protected] | 687f21fd | 2013-05-10 14:52:46 | [diff] [blame] | 88 | private: |
caitpotter88 | 74b20fa | 2016-01-14 19:06:20 | [diff] [blame] | 89 | static void allocateMemoryWithFlags(size_t, InitializationPolicy, int, void*&); |
[email protected] | 1a91b71 | 2015-06-29 22:07:10 | [diff] [blame] | 90 | class DataHolder : public ThreadSafeRefCounted<DataHolder> { |
| 91 | WTF_MAKE_NONCOPYABLE(DataHolder); |
| 92 | public: |
| 93 | DataHolder(); |
| 94 | ~DataHolder(); |
| 95 | |
| 96 | void allocateNew(unsigned sizeInBytes, SharingType isShared, InitializationPolicy); |
| 97 | void adopt(void* data, unsigned sizeInBytes, SharingType isShared); |
| 98 | void copyMemoryTo(DataHolder& other); |
| 99 | |
| 100 | void* data() const { return m_data; } |
| 101 | unsigned sizeInBytes() const { return m_sizeInBytes; } |
| 102 | bool isShared() const { return m_isShared == Shared; } |
| 103 | |
| 104 | private: |
| 105 | void* m_data; |
| 106 | unsigned m_sizeInBytes; |
| 107 | SharingType m_isShared; |
| 108 | }; |
| 109 | |
| 110 | RefPtr<DataHolder> m_holder; |
[email protected] | cce1e96 | 2015-06-16 08:13:15 | [diff] [blame] | 111 | static AdjustAmountOfExternalAllocatedMemoryFunction s_adjustAmountOfExternalAllocatedMemoryFunction; |
[email protected] | 6654df8 | 2013-05-08 20:56:05 | [diff] [blame] | 112 | }; |
| 113 | |
| 114 | } // namespace WTF |
| 115 | |
| 116 | #endif // ArrayBufferContents_h |