| 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 20 matching lines...) Expand all Loading... |
| 31 #include "wtf/Assertions.h" | 31 #include "wtf/Assertions.h" |
| 32 #include "wtf/Noncopyable.h" | 32 #include "wtf/Noncopyable.h" |
| 33 #include "wtf/RefPtr.h" | 33 #include "wtf/RefPtr.h" |
| 34 #include "wtf/ThreadSafeRefCounted.h" | 34 #include "wtf/ThreadSafeRefCounted.h" |
| 35 #include "wtf/WTF.h" | 35 #include "wtf/WTF.h" |
| 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 public: | |
| 44 enum InitializationPolicy { | |
| 45 ZeroInitialize, | |
| 46 DontInitialize | |
| 47 }; | |
| 48 | 43 |
| 49 enum SharingType { | 44 public: |
| 50 NotShared, | 45 enum InitializationPolicy { ZeroInitialize, DontInitialize }; |
| 51 Shared, | |
| 52 }; | |
| 53 | 46 |
| 54 ArrayBufferContents(); | 47 enum SharingType { |
| 55 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, SharingT
ype isShared, ArrayBufferContents::InitializationPolicy); | 48 NotShared, |
| 49 Shared, |
| 50 }; |
| 56 | 51 |
| 57 // Use with care. data must be allocated with allocateMemory. | 52 ArrayBufferContents(); |
| 58 // ArrayBufferContents will take ownership of the data and free it (using fr
eeMemory) | 53 ArrayBufferContents(unsigned numElements, |
| 59 // upon destruction. | 54 unsigned elementByteSize, |
| 60 // This constructor will not call observer->StartObserving(), so it is a res
ponsibility | 55 SharingType isShared, |
| 61 // of the caller to make sure JS knows about external memory. | 56 ArrayBufferContents::InitializationPolicy); |
| 62 ArrayBufferContents(void* data, unsigned sizeInBytes, SharingType isShared); | |
| 63 | 57 |
| 64 ~ArrayBufferContents(); | 58 // Use with care. data must be allocated with allocateMemory. |
| 59 // ArrayBufferContents will take ownership of the data and free it (using free
Memory) |
| 60 // upon destruction. |
| 61 // This constructor will not call observer->StartObserving(), so it is a respo
nsibility |
| 62 // of the caller to make sure JS knows about external memory. |
| 63 ArrayBufferContents(void* data, unsigned sizeInBytes, SharingType isShared); |
| 65 | 64 |
| 66 void neuter(); | 65 ~ArrayBufferContents(); |
| 67 | 66 |
| 68 void* data() const { return m_holder ? m_holder->data() : nullptr; } | 67 void neuter(); |
| 69 unsigned sizeInBytes() const { return m_holder ? m_holder->sizeInBytes() : 0
; } | |
| 70 bool isShared() const { return m_holder ? m_holder->isShared() : false; } | |
| 71 | 68 |
| 72 void transfer(ArrayBufferContents& other); | 69 void* data() const { return m_holder ? m_holder->data() : nullptr; } |
| 73 void shareWith(ArrayBufferContents& other); | 70 unsigned sizeInBytes() const { |
| 74 void copyTo(ArrayBufferContents& other); | 71 return m_holder ? m_holder->sizeInBytes() : 0; |
| 72 } |
| 73 bool isShared() const { return m_holder ? m_holder->isShared() : false; } |
| 75 | 74 |
| 76 static void allocateMemory(size_t, InitializationPolicy, void*&); | 75 void transfer(ArrayBufferContents& other); |
| 77 static void allocateMemoryOrNull(size_t, InitializationPolicy, void*&); | 76 void shareWith(ArrayBufferContents& other); |
| 78 static void freeMemory(void*, size_t); | 77 void copyTo(ArrayBufferContents& other); |
| 79 static void setAdjustAmoutOfExternalAllocatedMemoryFunction(AdjustAmountOfEx
ternalAllocatedMemoryFunction function) | |
| 80 { | |
| 81 ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction); | |
| 82 s_adjustAmountOfExternalAllocatedMemoryFunction = function; | |
| 83 } | |
| 84 | 78 |
| 85 private: | 79 static void allocateMemory(size_t, InitializationPolicy, void*&); |
| 86 static void allocateMemoryWithFlags(size_t, InitializationPolicy, int, void*
&); | 80 static void allocateMemoryOrNull(size_t, InitializationPolicy, void*&); |
| 87 class DataHolder : public ThreadSafeRefCounted<DataHolder> { | 81 static void freeMemory(void*, size_t); |
| 88 WTF_MAKE_NONCOPYABLE(DataHolder); | 82 static void setAdjustAmoutOfExternalAllocatedMemoryFunction( |
| 89 public: | 83 AdjustAmountOfExternalAllocatedMemoryFunction function) { |
| 90 DataHolder(); | 84 ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction); |
| 91 ~DataHolder(); | 85 s_adjustAmountOfExternalAllocatedMemoryFunction = function; |
| 86 } |
| 92 | 87 |
| 93 void allocateNew(unsigned sizeInBytes, SharingType isShared, Initializat
ionPolicy); | 88 private: |
| 94 void adopt(void* data, unsigned sizeInBytes, SharingType isShared); | 89 static void allocateMemoryWithFlags(size_t, |
| 95 void copyMemoryTo(DataHolder& other); | 90 InitializationPolicy, |
| 91 int, |
| 92 void*&); |
| 93 class DataHolder : public ThreadSafeRefCounted<DataHolder> { |
| 94 WTF_MAKE_NONCOPYABLE(DataHolder); |
| 96 | 95 |
| 97 void* data() const { return m_data; } | 96 public: |
| 98 unsigned sizeInBytes() const { return m_sizeInBytes; } | 97 DataHolder(); |
| 99 bool isShared() const { return m_isShared == Shared; } | 98 ~DataHolder(); |
| 100 | 99 |
| 101 private: | 100 void allocateNew(unsigned sizeInBytes, |
| 102 void* m_data; | 101 SharingType isShared, |
| 103 unsigned m_sizeInBytes; | 102 InitializationPolicy); |
| 104 SharingType m_isShared; | 103 void adopt(void* data, unsigned sizeInBytes, SharingType isShared); |
| 105 }; | 104 void copyMemoryTo(DataHolder& other); |
| 106 | 105 |
| 107 RefPtr<DataHolder> m_holder; | 106 void* data() const { return m_data; } |
| 108 static AdjustAmountOfExternalAllocatedMemoryFunction s_adjustAmountOfExterna
lAllocatedMemoryFunction; | 107 unsigned sizeInBytes() const { return m_sizeInBytes; } |
| 108 bool isShared() const { return m_isShared == Shared; } |
| 109 |
| 110 private: |
| 111 void* m_data; |
| 112 unsigned m_sizeInBytes; |
| 113 SharingType m_isShared; |
| 114 }; |
| 115 |
| 116 RefPtr<DataHolder> m_holder; |
| 117 static AdjustAmountOfExternalAllocatedMemoryFunction |
| 118 s_adjustAmountOfExternalAllocatedMemoryFunction; |
| 109 }; | 119 }; |
| 110 | 120 |
| 111 } // namespace WTF | 121 } // namespace WTF |
| 112 | 122 |
| 113 #endif // ArrayBufferContents_h | 123 #endif // ArrayBufferContents_h |
| OLD | NEW |