OLD | NEW |
---|---|
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 Loading... | |
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); | 73 neuter(); |
75 clear(); | |
76 } | 74 } |
77 | 75 |
78 void ArrayBufferContents::clear() | 76 void ArrayBufferContents::neuter() |
79 { | 77 { |
80 m_data = 0; | 78 m_holder.clear(); |
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()) |
89 clear(); | 86 neuter(); |
haraken
2015/06/20 00:59:31
I'm wondering if we should call neuter() for a sha
binji
2015/06/20 01:36:21
Right, but we shouldn't ever be transferring a sha
| |
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()); |
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) |
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() | |
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) | |
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 |
OLD | NEW |