blob: d8b57e381817a4ec03336cd77ab893c1b5c7fda8 [file] [log] [blame]
[email protected]6654df82013-05-08 20:56:051/*
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
tasak4faca4c42016-01-19 08:38:5530#include "wtf/Allocator.h"
[email protected]cce1e962015-06-16 08:13:1531#include "wtf/Assertions.h"
[email protected]6654df82013-05-08 20:56:0532#include "wtf/Noncopyable.h"
[email protected]1a91b712015-06-29 22:07:1033#include "wtf/RefPtr.h"
34#include "wtf/ThreadSafeRefCounted.h"
[email protected]cce1e962015-06-16 08:13:1535#include "wtf/WTF.h"
[email protected]b7616fb62013-05-27 23:43:0536#include "wtf/WTFExport.h"
[email protected]6654df82013-05-08 20:56:0537
38namespace WTF {
39
haraken02cfffa72016-03-10 19:55:4640typedef void(*AdjustAmountOfExternalAllocatedMemoryFunction)(int size);
41
[email protected]b7616fb62013-05-27 23:43:0542class WTF_EXPORT ArrayBufferContents {
[email protected]6654df82013-05-08 20:56:0543 WTF_MAKE_NONCOPYABLE(ArrayBufferContents);
tasak4faca4c42016-01-19 08:38:5544 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
[email protected]6654df82013-05-08 20:56:0545public:
[email protected]27de0492013-05-15 01:50:4946 enum InitializationPolicy {
47 ZeroInitialize,
48 DontInitialize
49 };
50
[email protected]1a91b712015-06-29 22:07:1051 enum SharingType {
52 NotShared,
53 Shared,
54 };
55
[email protected]687f21fd2013-05-10 14:52:4656 ArrayBufferContents();
[email protected]1a91b712015-06-29 22:07:1057 ArrayBufferContents(unsigned numElements, unsigned elementByteSize, SharingType isShared, ArrayBufferContents::InitializationPolicy);
[email protected]6654df82013-05-08 20:56:0558
[email protected]bfcac9b2013-07-05 10:08:2159 // Use with care. data must be allocated with allocateMemory.
[email protected]3dfbd2e2014-10-20 11:37:2860 // ArrayBufferContents will take ownership of the data and free it (using freeMemory)
[email protected]bfcac9b2013-07-05 10:08:2161 // upon destruction.
[email protected]238bd492013-09-23 15:15:1762 // 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]1a91b712015-06-29 22:07:1064 ArrayBufferContents(void* data, unsigned sizeInBytes, SharingType isShared);
[email protected]bfcac9b2013-07-05 10:08:2165
[email protected]687f21fd2013-05-10 14:52:4666 ~ArrayBufferContents();
67
[email protected]1a91b712015-06-29 22:07:1068 void neuter();
[email protected]687f21fd2013-05-10 14:52:4669
[email protected]1a91b712015-06-29 22:07:1070 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]27de0492013-05-15 01:50:4973
[email protected]27de0492013-05-15 01:50:4974 void transfer(ArrayBufferContents& other);
[email protected]1a91b712015-06-29 22:07:1075 void shareWith(ArrayBufferContents& other);
[email protected]1d31c12e2013-07-27 00:13:5776 void copyTo(ArrayBufferContents& other);
[email protected]27de0492013-05-15 01:50:4977
[email protected]664133b2013-08-07 23:34:1278 static void allocateMemory(size_t, InitializationPolicy, void*&);
caitpotter8874b20fa2016-01-14 19:06:2079 static void allocateMemoryOrNull(size_t, InitializationPolicy, void*&);
[email protected]664133b2013-08-07 23:34:1280 static void freeMemory(void*, size_t);
haraken02cfffa72016-03-10 19:55:4681 static void initialize(AdjustAmountOfExternalAllocatedMemoryFunction function)
[email protected]cce1e962015-06-16 08:13:1582 {
haraken02cfffa72016-03-10 19:55:4683 ASSERT(isMainThread());
[email protected]cce1e962015-06-16 08:13:1584 ASSERT(!s_adjustAmountOfExternalAllocatedMemoryFunction);
85 s_adjustAmountOfExternalAllocatedMemoryFunction = function;
86 }
[email protected]bfcac9b2013-07-05 10:08:2187
[email protected]687f21fd2013-05-10 14:52:4688private:
caitpotter8874b20fa2016-01-14 19:06:2089 static void allocateMemoryWithFlags(size_t, InitializationPolicy, int, void*&);
[email protected]1a91b712015-06-29 22:07:1090 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]cce1e962015-06-16 08:13:15111 static AdjustAmountOfExternalAllocatedMemoryFunction s_adjustAmountOfExternalAllocatedMemoryFunction;
[email protected]6654df82013-05-08 20:56:05112};
113
114} // namespace WTF
115
116#endif // ArrayBufferContents_h