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

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

Issue 1186863005: Add a ref-counted data holder to ArrayBufferContents (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: separate transfer() + neuter() logic Created 5 years, 6 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
« no previous file with comments | « Source/wtf/ArrayBufferContents.h ('k') | Source/wtf/ArrayBufferView.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 19 matching lines...) Expand all
30 #include "wtf/Assertions.h" 30 #include "wtf/Assertions.h"
31 #include "wtf/PartitionAlloc.h" 31 #include "wtf/PartitionAlloc.h"
32 #include "wtf/Partitions.h" 32 #include "wtf/Partitions.h"
33 #include <string.h> 33 #include <string.h>
34 34
35 namespace WTF { 35 namespace WTF {
36 36
37 AdjustAmountOfExternalAllocatedMemoryFunction ArrayBufferContents::s_adjustAmoun tOfExternalAllocatedMemoryFunction; 37 AdjustAmountOfExternalAllocatedMemoryFunction ArrayBufferContents::s_adjustAmoun tOfExternalAllocatedMemoryFunction;
38 38
39 ArrayBufferContents::ArrayBufferContents() 39 ArrayBufferContents::ArrayBufferContents()
40 : m_data(0) 40 : m_holder(adoptRef(new DataHolder())) { }
41 , m_sizeInBytes(0) { }
42 41
43 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB yteSize, ArrayBufferContents::InitializationPolicy policy) 42 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB yteSize, SharingType isShared, ArrayBufferContents::InitializationPolicy policy)
44 : m_data(0) 43 : m_holder(adoptRef(new DataHolder()))
45 , m_sizeInBytes(0)
46 { 44 {
47 // Do not allow 32-bit overflow of the total size. 45 // Do not allow 32-bit overflow of the total size.
46 unsigned totalSize = numElements * elementByteSize;
48 if (numElements) { 47 if (numElements) {
49 unsigned totalSize = numElements * elementByteSize;
50 if (totalSize / numElements != elementByteSize) { 48 if (totalSize / numElements != elementByteSize) {
51 m_data = 0;
52 return; 49 return;
53 } 50 }
54 } 51 }
55 allocateMemory(numElements * elementByteSize, policy, m_data); 52
56 m_sizeInBytes = numElements * elementByteSize; 53 m_holder->allocateNew(totalSize, isShared, policy);
57 } 54 }
58 55
59 ArrayBufferContents::ArrayBufferContents(void* data, unsigned sizeInBytes) 56 ArrayBufferContents::ArrayBufferContents(
60 : m_data(data) 57 void* data, unsigned sizeInBytes, SharingType isShared)
61 , m_sizeInBytes(sizeInBytes) 58 : m_holder(adoptRef(new DataHolder()))
62 { 59 {
63 if (!m_data) { 60 if (data) {
64 ASSERT(!m_sizeInBytes); 61 m_holder->adopt(data, sizeInBytes, isShared);
65 m_sizeInBytes = 0; 62 } else {
66 // Allow null data if size is 0 bytes, make sure m_data is valid pointer . 63 ASSERT(!sizeInBytes);
67 // (partitionAllocGeneric guarantees valid pointer for size 0) 64 sizeInBytes = 0;
68 allocateMemory(0, ZeroInitialize, m_data); 65 // Allow null data if size is 0 bytes, make sure data is valid pointer.
66 // (PartitionAlloc guarantees valid pointer for size 0)
67 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize);
69 } 68 }
70 } 69 }
71 70
72 ArrayBufferContents::~ArrayBufferContents() 71 ArrayBufferContents::~ArrayBufferContents()
73 { 72 {
74 freeMemory(m_data, m_sizeInBytes);
75 clear();
76 } 73 }
77 74
78 void ArrayBufferContents::clear() 75 void ArrayBufferContents::neuter()
79 { 76 {
80 m_data = 0; 77 m_holder.clear();
81 m_sizeInBytes = 0;
82 } 78 }
83 79
84 void ArrayBufferContents::transfer(ArrayBufferContents& other) 80 void ArrayBufferContents::transfer(ArrayBufferContents& other)
85 { 81 {
86 ASSERT(!other.m_data); 82 ASSERT(!other.m_holder->data());
87 other.m_data = m_data; 83 other.m_holder = m_holder;
kinuko 2015/06/23 10:44:37 Transfer used to mean transfering ownership and no
88 other.m_sizeInBytes = m_sizeInBytes;
89 clear();
90 } 84 }
91 85
92 void ArrayBufferContents::copyTo(ArrayBufferContents& other) 86 void ArrayBufferContents::copyTo(ArrayBufferContents& other)
93 { 87 {
94 ASSERT(!other.m_sizeInBytes); 88 ASSERT(!m_holder->isShared() && !other.m_holder->isShared());
95 other.freeMemory(other.m_data, other.m_sizeInBytes); 89 m_holder->copyMemoryTo(*other.m_holder);
96 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data);
97 if (!other.m_data)
98 return;
99 memcpy(other.m_data, m_data, m_sizeInBytes);
100 other.m_sizeInBytes = m_sizeInBytes;
101 } 90 }
102 91
103 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic y, void*& data) 92 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic y, void*& data)
104 { 93 {
105 if (s_adjustAmountOfExternalAllocatedMemoryFunction) 94 if (s_adjustAmountOfExternalAllocatedMemoryFunction)
106 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size)); 95 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size));
107 data = partitionAllocGenericFlags(WTF::Partitions::bufferPartition(), Partit ionAllocReturnNull, size); 96 data = partitionAllocGenericFlags(WTF::Partitions::bufferPartition(), Partit ionAllocReturnNull, size);
108 if (policy == ZeroInitialize && data) 97 if (policy == ZeroInitialize && data)
109 memset(data, '\0', size); 98 memset(data, '\0', size);
110 } 99 }
111 100
112 void ArrayBufferContents::freeMemory(void* data, size_t size) 101 void ArrayBufferContents::freeMemory(void* data, size_t size)
113 { 102 {
114 partitionFreeGeneric(WTF::Partitions::bufferPartition(), data); 103 partitionFreeGeneric(WTF::Partitions::bufferPartition(), data);
115 if (s_adjustAmountOfExternalAllocatedMemoryFunction) 104 if (s_adjustAmountOfExternalAllocatedMemoryFunction)
116 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size)) ; 105 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size)) ;
117 } 106 }
118 107
108 ArrayBufferContents::DataHolder::DataHolder()
109 : m_data(nullptr)
110 , m_sizeInBytes(0)
111 , m_isShared(NotShared) { }
112
113 ArrayBufferContents::DataHolder::~DataHolder()
114 {
115 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes);
116
117 m_data = nullptr;
118 m_sizeInBytes = 0;
119 m_isShared = NotShared;
120 }
121
122 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, SharingT ype isShared, InitializationPolicy policy)
123 {
124 ASSERT(!m_data);
125 void* data = nullptr;
126 allocateMemory(sizeInBytes, policy, data);
127 m_data = data;
128 m_sizeInBytes = sizeInBytes;
129 m_isShared = isShared;
130 }
131
132 void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, Sh aringType isShared)
133 {
134 ASSERT(!m_data);
135 m_data = data;
136 m_sizeInBytes = sizeInBytes;
137 m_isShared = isShared;
138 }
139
140 void ArrayBufferContents::DataHolder::copyMemoryTo(DataHolder& other)
141 {
142 ASSERT(!other.m_sizeInBytes);
143 ArrayBufferContents::freeMemory(other.m_data, other.m_sizeInBytes);
144 ArrayBufferContents::allocateMemory(m_sizeInBytes, DontInitialize, other.m_d ata);
145 if (!other.m_data)
146 return;
147 memcpy(other.m_data, m_data, m_sizeInBytes);
148 other.m_sizeInBytes = m_sizeInBytes;
149 }
150
119 } // namespace WTF 151 } // namespace WTF
OLDNEW
« no previous file with comments | « Source/wtf/ArrayBufferContents.h ('k') | Source/wtf/ArrayBufferView.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698