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 16 matching lines...) Expand all Loading... |
27 #include "config.h" | 27 #include "config.h" |
28 #include "wtf/ArrayBufferContents.h" | 28 #include "wtf/ArrayBufferContents.h" |
29 | 29 |
30 #include "wtf/Assertions.h" | 30 #include "wtf/Assertions.h" |
31 #include "wtf/PartitionAlloc.h" | 31 #include "wtf/PartitionAlloc.h" |
32 #include "wtf/WTF.h" | 32 #include "wtf/WTF.h" |
33 #include <string.h> | 33 #include <string.h> |
34 | 34 |
35 namespace WTF { | 35 namespace WTF { |
36 | 36 |
37 ArrayBufferContents::ArrayBufferContents() | 37 |
| 38 DataHolder::DataHolder() |
38 : m_data(0) | 39 : m_data(0) |
39 , m_sizeInBytes(0) | 40 , m_sizeInBytes(0) |
| 41 , m_shared(false) { } |
| 42 |
| 43 DataHolder::~DataHolder() |
| 44 { |
| 45 // if (m_data) { |
| 46 // printf("Destroy data: %p size: %d shared: %d refs: %d\n", m_data, m_s
izeInBytes, m_shared, refCount()); |
| 47 // } |
| 48 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), m_data); |
| 49 m_data = 0; |
| 50 m_sizeInBytes = 0; |
| 51 m_shared = false; |
| 52 } |
| 53 |
| 54 void DataHolder::adopt(void* data, unsigned sizeInBytes, bool shared) |
| 55 { |
| 56 ASSERT(!m_data); |
| 57 m_data = data; |
| 58 m_sizeInBytes = sizeInBytes; |
| 59 m_shared = shared; |
| 60 // printf("Adopt data: %p size: %d shared: %d refs: %d\n", m_data, m_sizeInB
ytes, m_shared, refCount()); |
| 61 } |
| 62 |
| 63 ArrayBufferContents::ArrayBufferContents() |
| 64 : m_holder(adoptRef(new DataHolder())) |
40 , m_deallocationObserver(0) { } | 65 , m_deallocationObserver(0) { } |
41 | 66 |
42 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, ArrayBufferContents::InitializationPolicy policy) | 67 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB
yteSize, ArrayBufferContents::InitializationPolicy policy) |
43 : m_data(0) | 68 : m_holder(adoptRef(new DataHolder())) |
44 , m_sizeInBytes(0) | |
45 , m_deallocationObserver(0) | 69 , m_deallocationObserver(0) |
46 { | 70 { |
| 71 unsigned totalSize = numElements * elementByteSize; |
47 // Do not allow 32-bit overflow of the total size. | 72 // Do not allow 32-bit overflow of the total size. |
48 if (numElements) { | 73 if (numElements) { |
49 unsigned totalSize = numElements * elementByteSize; | |
50 if (totalSize / numElements != elementByteSize) { | 74 if (totalSize / numElements != elementByteSize) { |
51 m_data = 0; | |
52 return; | 75 return; |
53 } | 76 } |
54 } | 77 } |
55 allocateMemory(numElements * elementByteSize, policy, m_data); | 78 void* data = 0; |
56 m_sizeInBytes = numElements * elementByteSize; | 79 allocateMemory(totalSize, policy, data); |
| 80 m_holder->adopt(data, totalSize, false); |
57 } | 81 } |
58 | 82 |
59 ArrayBufferContents::ArrayBufferContents( | 83 ArrayBufferContents::ArrayBufferContents( |
60 void* data, unsigned sizeInBytes, ArrayBufferDeallocationObserver* observer) | 84 void* data, unsigned sizeInBytes, bool shared, ArrayBufferDeallocationObserv
er* observer) |
61 : m_data(data) | 85 : m_holder(adoptRef(new DataHolder())) |
62 , m_sizeInBytes(sizeInBytes) | |
63 , m_deallocationObserver(observer) | 86 , m_deallocationObserver(observer) |
64 { | 87 { |
65 if (!m_data) { | 88 if (!data) { |
66 ASSERT(!m_sizeInBytes); | 89 ASSERT(!sizeInBytes); |
67 m_sizeInBytes = 0; | 90 sizeInBytes = 0; |
68 // Allow null data if size is 0 bytes, make sure m_data is valid pointer
. | 91 // Allow null data if size is 0 bytes, make sure m_data is valid pointer
. |
69 // (partitionAllocGeneric guarantees valid pointer for size 0) | 92 // (partitionAllocGeneric guarantees valid pointer for size 0) |
70 allocateMemory(0, ZeroInitialize, m_data); | 93 allocateMemory(0, ZeroInitialize, data); |
71 } | 94 } |
| 95 m_holder->adopt(data, sizeInBytes, shared); |
72 } | 96 } |
73 | 97 |
74 ArrayBufferContents::~ArrayBufferContents() | 98 ArrayBufferContents::~ArrayBufferContents() |
75 { | 99 { |
76 freeMemory(m_data, m_sizeInBytes); | 100 // freeMemory(m_data, m_sizeInBytes); |
77 clear(); | 101 clear(); |
78 } | 102 } |
79 | 103 |
80 void ArrayBufferContents::clear() | 104 void ArrayBufferContents::clear() |
81 { | 105 { |
82 if (m_data && m_deallocationObserver) | 106 if (m_holder->m_data && m_deallocationObserver) |
83 m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes); | 107 m_deallocationObserver->arrayBufferDeallocated(m_holder->m_sizeInBytes); |
84 m_data = 0; | 108 m_holder = adoptRef(new DataHolder()); |
85 m_sizeInBytes = 0; | |
86 m_deallocationObserver = 0; | 109 m_deallocationObserver = 0; |
87 } | 110 } |
88 | 111 |
89 void ArrayBufferContents::transfer(ArrayBufferContents& other) | 112 void ArrayBufferContents::transfer(ArrayBufferContents& other) |
90 { | 113 { |
91 ASSERT(!other.m_data); | 114 ASSERT(!other.m_holder->m_data); |
92 other.m_data = m_data; | 115 // TODO mark the memory as allocated in new context? |
93 other.m_sizeInBytes = m_sizeInBytes; | 116 other.m_holder = m_holder; |
94 clear(); | 117 if (!m_holder->m_shared) { |
| 118 clear(); |
| 119 } |
95 } | 120 } |
96 | 121 |
97 void ArrayBufferContents::copyTo(ArrayBufferContents& other) | 122 void ArrayBufferContents::copyTo(ArrayBufferContents& other) |
98 { | 123 { |
99 ASSERT(!other.m_sizeInBytes); | 124 ASSERT(!other.m_holder->m_sizeInBytes); |
100 other.freeMemory(other.m_data, other.m_sizeInBytes); | 125 other.m_holder = adoptRef(new DataHolder()); |
101 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data); | 126 void* data = 0; |
102 if (!other.m_data) | 127 allocateMemory(m_holder->m_sizeInBytes, DontInitialize, data); |
| 128 if (!data) |
103 return; | 129 return; |
104 memcpy(other.m_data, m_data, m_sizeInBytes); | 130 memcpy(data, m_holder->m_data, m_holder->m_sizeInBytes); |
105 other.m_sizeInBytes = m_sizeInBytes; | 131 other.m_holder->adopt(data, m_holder->m_sizeInBytes, false); |
106 } | 132 } |
107 | 133 |
108 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) | 134 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic
y, void*& data) |
109 { | 135 { |
110 data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), Par
titionAllocReturnNull, size); | 136 data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), Par
titionAllocReturnNull, size); |
111 if (policy == ZeroInitialize && data) | 137 if (policy == ZeroInitialize && data) |
112 memset(data, '\0', size); | 138 memset(data, '\0', size); |
113 } | 139 } |
114 | 140 |
115 void ArrayBufferContents::freeMemory(void* data, size_t) | 141 void ArrayBufferContents::freeMemory(void* data, size_t) |
116 { | 142 { |
117 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data); | 143 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data); |
118 } | 144 } |
119 | 145 |
120 } // namespace WTF | 146 } // namespace WTF |
OLD | NEW |