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

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

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