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

Side by Side Diff: third_party/WebKit/Source/wtf/ArrayBufferContents.cpp

Issue 1875343002: Move WTF classes related to typed arrays to wtf/typed_arrays (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
(Empty)
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 #include "wtf/ArrayBufferContents.h"
28
29 #include "wtf/Assertions.h"
30 #include "wtf/PartitionAlloc.h"
31 #include "wtf/Partitions.h"
32 #include <string.h>
33
34 namespace WTF {
35
36 AdjustAmountOfExternalAllocatedMemoryFunction ArrayBufferContents::s_adjustAmoun tOfExternalAllocatedMemoryFunction;
37
38 ArrayBufferContents::ArrayBufferContents()
39 : m_holder(adoptRef(new DataHolder())) { }
40
41 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB yteSize, SharingType isShared, ArrayBufferContents::InitializationPolicy policy)
42 : m_holder(adoptRef(new DataHolder()))
43 {
44 // Do not allow 32-bit overflow of the total size.
45 unsigned totalSize = numElements * elementByteSize;
46 if (numElements) {
47 if (totalSize / numElements != elementByteSize) {
48 return;
49 }
50 }
51
52 m_holder->allocateNew(totalSize, isShared, policy);
53 }
54
55 ArrayBufferContents::ArrayBufferContents(
56 void* data, unsigned sizeInBytes, SharingType isShared)
57 : m_holder(adoptRef(new DataHolder()))
58 {
59 if (data) {
60 m_holder->adopt(data, sizeInBytes, isShared);
61 } else {
62 ASSERT(!sizeInBytes);
63 sizeInBytes = 0;
64 // Allow null data if size is 0 bytes, make sure data is valid pointer.
65 // (PartitionAlloc guarantees valid pointer for size 0)
66 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize);
67 }
68 }
69
70 ArrayBufferContents::~ArrayBufferContents()
71 {
72 }
73
74 void ArrayBufferContents::neuter()
75 {
76 m_holder.clear();
77 }
78
79 void ArrayBufferContents::transfer(ArrayBufferContents& other)
80 {
81 ASSERT(!isShared());
82 ASSERT(!other.m_holder->data());
83 other.m_holder = m_holder;
84 neuter();
85 }
86
87 void ArrayBufferContents::shareWith(ArrayBufferContents& other)
88 {
89 ASSERT(isShared());
90 ASSERT(!other.m_holder->data());
91 other.m_holder = m_holder;
92 }
93
94 void ArrayBufferContents::copyTo(ArrayBufferContents& other)
95 {
96 ASSERT(!m_holder->isShared() && !other.m_holder->isShared());
97 m_holder->copyMemoryTo(*other.m_holder);
98 }
99
100 void ArrayBufferContents::allocateMemoryWithFlags(size_t size, InitializationPol icy policy, int flags, void*& data)
101 {
102 if (s_adjustAmountOfExternalAllocatedMemoryFunction)
103 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size));
104 data = partitionAllocGenericFlags(WTF::Partitions::bufferPartition(), flags, size, WTF_HEAP_PROFILER_TYPE_NAME(ArrayBufferContents));
105 if (policy == ZeroInitialize && data)
106 memset(data, '\0', size);
107 }
108
109 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic y, void*& data)
110 {
111 allocateMemoryWithFlags(size, policy, 0, data);
112 }
113
114 void ArrayBufferContents::allocateMemoryOrNull(size_t size, InitializationPolicy policy, void*& data)
115 {
116 allocateMemoryWithFlags(size, policy, PartitionAllocReturnNull, data);
117 }
118
119 void ArrayBufferContents::freeMemory(void* data, size_t size)
120 {
121 Partitions::bufferFree(data);
122 if (s_adjustAmountOfExternalAllocatedMemoryFunction)
123 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size)) ;
124 }
125
126 ArrayBufferContents::DataHolder::DataHolder()
127 : m_data(nullptr)
128 , m_sizeInBytes(0)
129 , m_isShared(NotShared) { }
130
131 ArrayBufferContents::DataHolder::~DataHolder()
132 {
133 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes);
134
135 m_data = nullptr;
136 m_sizeInBytes = 0;
137 m_isShared = NotShared;
138 }
139
140 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, SharingT ype isShared, InitializationPolicy policy)
141 {
142 ASSERT(!m_data);
143 void* data = nullptr;
144 allocateMemory(sizeInBytes, policy, data);
145 m_data = data;
146 m_sizeInBytes = data ? sizeInBytes : 0;
147 m_isShared = isShared;
148 }
149
150 void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, Sh aringType isShared)
151 {
152 ASSERT(!m_data);
153 m_data = data;
154 m_sizeInBytes = sizeInBytes;
155 m_isShared = isShared;
156 }
157
158 void ArrayBufferContents::DataHolder::copyMemoryTo(DataHolder& other)
159 {
160 ASSERT(!other.m_sizeInBytes);
161 ArrayBufferContents::freeMemory(other.m_data, other.m_sizeInBytes);
162 ArrayBufferContents::allocateMemory(m_sizeInBytes, DontInitialize, other.m_d ata);
163 if (!other.m_data)
164 return;
165 memcpy(other.m_data, m_data, m_sizeInBytes);
166 other.m_sizeInBytes = m_sizeInBytes;
167 }
168
169 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/ArrayBufferContents.h ('k') | third_party/WebKit/Source/wtf/ArrayBufferView.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698