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

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: 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
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) {
haraken 2015/06/19 12:05:11 Avoid using if(!...) {} else {} pattern.
binji 2015/06/19 18:14:57 Done.
64 ASSERT(!m_sizeInBytes); 61 ASSERT(!sizeInBytes);
65 m_sizeInBytes = 0; 62 sizeInBytes = 0;
66 // Allow null data if size is 0 bytes, make sure m_data is valid pointer . 63 // Allow null data if size is 0 bytes, make sure data is valid pointer.
67 // (partitionAllocGeneric guarantees valid pointer for size 0) 64 // (partitionAllocGeneric guarantees valid pointer for size 0)
haraken 2015/06/19 12:05:11 Nit: partitionAllocGeneric => PartitionAlloc (Arra
binji 2015/06/19 18:14:57 Done.
68 allocateMemory(0, ZeroInitialize, m_data); 65 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize);
66 } else {
67 m_holder->adopt(data, sizeInBytes, isShared);
haraken 2015/06/19 12:05:11 Help me understand: Why isn't this allocateNew? If
binji 2015/06/19 18:14:57 The previous behavior was to assign data to m_data
69 } 68 }
70 } 69 }
71 70
72 ArrayBufferContents::~ArrayBufferContents() 71 ArrayBufferContents::~ArrayBufferContents()
73 { 72 {
74 freeMemory(m_data, m_sizeInBytes);
75 clear(); 73 clear();
76 } 74 }
77 75
78 void ArrayBufferContents::clear() 76 void ArrayBufferContents::clear()
79 { 77 {
80 m_data = 0; 78 m_holder.clear();
haraken 2015/06/19 12:05:12 I think it's better to introduce DataHolder::clear
binji 2015/06/19 18:14:57 I think that has the wrong behavior if this ArrayB
81 m_sizeInBytes = 0;
82 } 79 }
83 80
84 void ArrayBufferContents::transfer(ArrayBufferContents& other) 81 void ArrayBufferContents::transfer(ArrayBufferContents& other)
85 { 82 {
86 ASSERT(!other.m_data); 83 ASSERT(!other.m_holder->data());
87 other.m_data = m_data; 84 other.m_holder = m_holder;
88 other.m_sizeInBytes = m_sizeInBytes; 85 if (!m_holder->isShared())
haraken 2015/06/19 12:05:12 It would be better to just call clear() here and l
binji 2015/06/19 18:14:57 AFAICT, we never want to clear() once we have shar
89 clear(); 86 clear();
90 } 87 }
91 88
92 void ArrayBufferContents::copyTo(ArrayBufferContents& other) 89 void ArrayBufferContents::copyTo(ArrayBufferContents& other)
93 { 90 {
94 ASSERT(!other.m_sizeInBytes); 91 ASSERT(!m_holder->isShared() && !other.m_holder->isShared());
haraken 2015/06/19 12:05:12 Is this a TODO? Or are you not planning to support
binji 2015/06/19 18:14:57 AFAICT, this is only used by ArrayBuffer::transfer
95 other.freeMemory(other.m_data, other.m_sizeInBytes); 92 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 } 93 }
102 94
103 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic y, void*& data) 95 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic y, void*& data)
104 { 96 {
105 if (s_adjustAmountOfExternalAllocatedMemoryFunction) 97 if (s_adjustAmountOfExternalAllocatedMemoryFunction)
106 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size)); 98 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size));
107 data = partitionAllocGenericFlags(WTF::Partitions::bufferPartition(), Partit ionAllocReturnNull, size); 99 data = partitionAllocGenericFlags(WTF::Partitions::bufferPartition(), Partit ionAllocReturnNull, size);
108 if (policy == ZeroInitialize && data) 100 if (policy == ZeroInitialize && data)
haraken 2015/06/19 12:05:11 Not related to this CL, but I think we should just
109 memset(data, '\0', size); 101 memset(data, '\0', size);
110 } 102 }
111 103
112 void ArrayBufferContents::freeMemory(void* data, size_t size) 104 void ArrayBufferContents::freeMemory(void* data, size_t size)
113 { 105 {
114 partitionFreeGeneric(WTF::Partitions::bufferPartition(), data); 106 partitionFreeGeneric(WTF::Partitions::bufferPartition(), data);
115 if (s_adjustAmountOfExternalAllocatedMemoryFunction) 107 if (s_adjustAmountOfExternalAllocatedMemoryFunction)
116 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size)) ; 108 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size)) ;
117 } 109 }
118 110
111 ArrayBufferContents::DataHolder::DataHolder()
112 : m_data(nullptr)
113 , m_sizeInBytes(0)
114 , m_isShared(NotShared) { }
115
116 ArrayBufferContents::DataHolder::~DataHolder()
haraken 2015/06/19 12:05:12 You can do this in DataHolder::clear().
117 {
118 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes);
119
120 m_data = nullptr;
121 m_sizeInBytes = 0;
122 m_isShared = NotShared;
123 }
124
125 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, SharingT ype isShared, InitializationPolicy policy)
126 {
127 ASSERT(!m_data);
128 void* data = nullptr;
129 allocateMemory(sizeInBytes, policy, data);
130 m_data = data;
131 m_sizeInBytes = sizeInBytes;
132 m_isShared = isShared;
133 }
134
135 void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, Sh aringType isShared)
haraken 2015/06/19 12:05:12 Question: What is the purpose of adopt?
binji 2015/06/19 18:14:57 To take over the passed in |data| pointer, without
136 {
137 ASSERT(!m_data);
138 m_data = data;
139 m_sizeInBytes = sizeInBytes;
140 m_isShared = isShared;
141 }
142
143 void ArrayBufferContents::DataHolder::copyMemoryTo(DataHolder& other)
144 {
145 ASSERT(!other.m_sizeInBytes);
146 ArrayBufferContents::freeMemory(other.m_data, other.m_sizeInBytes);
147 ArrayBufferContents::allocateMemory(m_sizeInBytes, DontInitialize, other.m_d ata);
148 if (!other.m_data)
149 return;
150 memcpy(other.m_data, m_data, m_sizeInBytes);
151 other.m_sizeInBytes = m_sizeInBytes;
152 }
153
119 } // namespace WTF 154 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698