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); | |
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(!isShared()); |
87 other.m_data = m_data; | 83 ASSERT(!other.m_holder->data()); |
88 other.m_sizeInBytes = m_sizeInBytes; | 84 other.m_holder = m_holder; |
89 clear(); | 85 neuter(); |
| 86 } |
| 87 |
| 88 void ArrayBufferContents::shareWith(ArrayBufferContents& other) |
| 89 { |
| 90 ASSERT(isShared()); |
| 91 ASSERT(!other.m_holder->data()); |
| 92 other.m_holder = m_holder; |
90 } | 93 } |
91 | 94 |
92 void ArrayBufferContents::copyTo(ArrayBufferContents& other) | 95 void ArrayBufferContents::copyTo(ArrayBufferContents& other) |
93 { | 96 { |
94 ASSERT(!other.m_sizeInBytes); | 97 ASSERT(!m_holder->isShared() && !other.m_holder->isShared()); |
95 other.freeMemory(other.m_data, other.m_sizeInBytes); | 98 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 } | 99 } |
102 | 100 |
103 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) | 101 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) |
104 { | 102 { |
105 if (s_adjustAmountOfExternalAllocatedMemoryFunction) | 103 if (s_adjustAmountOfExternalAllocatedMemoryFunction) |
106 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size)); | 104 s_adjustAmountOfExternalAllocatedMemoryFunction(static_cast<int>(size)); |
107 data = partitionAllocGenericFlags(WTF::Partitions::bufferPartition(), Partit
ionAllocReturnNull, size); | 105 data = partitionAllocGenericFlags(WTF::Partitions::bufferPartition(), Partit
ionAllocReturnNull, size); |
108 if (policy == ZeroInitialize && data) | 106 if (policy == ZeroInitialize && data) |
109 memset(data, '\0', size); | 107 memset(data, '\0', size); |
110 } | 108 } |
111 | 109 |
112 void ArrayBufferContents::freeMemory(void* data, size_t size) | 110 void ArrayBufferContents::freeMemory(void* data, size_t size) |
113 { | 111 { |
114 partitionFreeGeneric(WTF::Partitions::bufferPartition(), data); | 112 partitionFreeGeneric(WTF::Partitions::bufferPartition(), data); |
115 if (s_adjustAmountOfExternalAllocatedMemoryFunction) | 113 if (s_adjustAmountOfExternalAllocatedMemoryFunction) |
116 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size))
; | 114 s_adjustAmountOfExternalAllocatedMemoryFunction(-static_cast<int>(size))
; |
117 } | 115 } |
118 | 116 |
| 117 ArrayBufferContents::DataHolder::DataHolder() |
| 118 : m_data(nullptr) |
| 119 , m_sizeInBytes(0) |
| 120 , m_isShared(NotShared) { } |
| 121 |
| 122 ArrayBufferContents::DataHolder::~DataHolder() |
| 123 { |
| 124 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes); |
| 125 |
| 126 m_data = nullptr; |
| 127 m_sizeInBytes = 0; |
| 128 m_isShared = NotShared; |
| 129 } |
| 130 |
| 131 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, SharingT
ype isShared, InitializationPolicy policy) |
| 132 { |
| 133 ASSERT(!m_data); |
| 134 void* data = nullptr; |
| 135 allocateMemory(sizeInBytes, policy, data); |
| 136 m_data = data; |
| 137 m_sizeInBytes = sizeInBytes; |
| 138 m_isShared = isShared; |
| 139 } |
| 140 |
| 141 void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, Sh
aringType isShared) |
| 142 { |
| 143 ASSERT(!m_data); |
| 144 m_data = data; |
| 145 m_sizeInBytes = sizeInBytes; |
| 146 m_isShared = isShared; |
| 147 } |
| 148 |
| 149 void ArrayBufferContents::DataHolder::copyMemoryTo(DataHolder& other) |
| 150 { |
| 151 ASSERT(!other.m_sizeInBytes); |
| 152 ArrayBufferContents::freeMemory(other.m_data, other.m_sizeInBytes); |
| 153 ArrayBufferContents::allocateMemory(m_sizeInBytes, DontInitialize, other.m_d
ata); |
| 154 if (!other.m_data) |
| 155 return; |
| 156 memcpy(other.m_data, m_data, m_sizeInBytes); |
| 157 other.m_sizeInBytes = m_sizeInBytes; |
| 158 } |
| 159 |
119 } // namespace WTF | 160 } // namespace WTF |
OLD | NEW |