Chromium Code Reviews| 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 17 matching lines...) Expand all Loading... | |
| 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/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 ArrayBufferContents::ArrayBufferContents() | 37 ArrayBufferContents::ArrayBufferContents() |
| 38 : m_data(0) | 38 : m_holder(adoptRef(new DataHolder())) { } |
| 39 , m_sizeInBytes(0) | |
| 40 , m_deallocationObserver(0) { } | |
| 41 | 39 |
| 42 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB yteSize, ArrayBufferContents::InitializationPolicy policy) | 40 ArrayBufferContents::ArrayBufferContents(unsigned numElements, unsigned elementB yteSize, SharingType isShared, ArrayBufferContents::InitializationPolicy policy) |
| 43 : m_data(0) | 41 : m_holder(adoptRef(new DataHolder())) |
| 44 , m_sizeInBytes(0) | |
| 45 , m_deallocationObserver(0) | |
| 46 { | 42 { |
| 47 // Do not allow 32-bit overflow of the total size. | 43 // Do not allow 32-bit overflow of the total size. |
| 44 unsigned totalSize = numElements * elementByteSize; | |
| 48 if (numElements) { | 45 if (numElements) { |
| 49 unsigned totalSize = numElements * elementByteSize; | |
| 50 if (totalSize / numElements != elementByteSize) { | 46 if (totalSize / numElements != elementByteSize) { |
| 51 m_data = 0; | |
| 52 return; | 47 return; |
| 53 } | 48 } |
| 54 } | 49 } |
| 55 allocateMemory(numElements * elementByteSize, policy, m_data); | 50 |
| 56 m_sizeInBytes = numElements * elementByteSize; | 51 m_holder->allocateNew(totalSize, isShared, policy, nullptr); |
| 57 } | 52 } |
| 58 | 53 |
| 59 ArrayBufferContents::ArrayBufferContents( | 54 ArrayBufferContents::ArrayBufferContents( |
| 60 void* data, unsigned sizeInBytes, ArrayBufferDeallocationObserver* observer) | 55 void* data, unsigned sizeInBytes, SharingType isShared, ArrayBufferDeallocat ionObserver* observer) |
| 61 : m_data(data) | 56 : m_holder(adoptRef(new DataHolder())) |
| 62 , m_sizeInBytes(sizeInBytes) | |
| 63 , m_deallocationObserver(observer) | |
| 64 { | 57 { |
| 65 if (!m_data) { | 58 if (!data) { |
| 66 ASSERT(!m_sizeInBytes); | 59 ASSERT(!sizeInBytes); |
| 67 m_sizeInBytes = 0; | 60 sizeInBytes = 0; |
| 68 // Allow null data if size is 0 bytes, make sure m_data is valid pointer . | 61 // Allow null data if size is 0 bytes, make sure data is valid pointer. |
| 69 // (partitionAllocGeneric guarantees valid pointer for size 0) | 62 // (partitionAllocGeneric guarantees valid pointer for size 0) |
| 70 allocateMemory(0, ZeroInitialize, m_data); | 63 m_holder->allocateNew(sizeInBytes, isShared, ZeroInitialize, observer); |
| 64 } else { | |
| 65 m_holder->adopt(data, sizeInBytes, isShared, observer); | |
| 71 } | 66 } |
| 72 } | 67 } |
| 73 | 68 |
| 74 ArrayBufferContents::~ArrayBufferContents() | 69 ArrayBufferContents::~ArrayBufferContents() |
| 75 { | 70 { |
| 76 freeMemory(m_data, m_sizeInBytes); | |
| 77 clear(); | 71 clear(); |
| 78 } | 72 } |
| 79 | 73 |
| 80 void ArrayBufferContents::clear() | 74 void ArrayBufferContents::clear() |
| 81 { | 75 { |
| 82 if (m_data && m_deallocationObserver) | 76 m_holder.clear(); |
| 83 m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes); | |
| 84 m_data = 0; | |
| 85 m_sizeInBytes = 0; | |
| 86 m_deallocationObserver = 0; | |
| 87 } | 77 } |
| 88 | 78 |
| 89 void ArrayBufferContents::transfer(ArrayBufferContents& other) | 79 void ArrayBufferContents::transfer(ArrayBufferContents& other) |
| 90 { | 80 { |
| 91 ASSERT(!other.m_data); | 81 ASSERT(!other.m_holder->data()); |
| 92 other.m_data = m_data; | 82 other.m_holder = m_holder; |
| 93 other.m_sizeInBytes = m_sizeInBytes; | 83 if (!m_holder->isShared()) |
| 94 clear(); | 84 clear(); |
| 95 } | 85 } |
| 96 | 86 |
| 97 void ArrayBufferContents::copyTo(ArrayBufferContents& other) | 87 void ArrayBufferContents::copyTo(ArrayBufferContents& other) |
| 98 { | 88 { |
| 99 ASSERT(!other.m_sizeInBytes); | 89 // TODO(binji): where is this function used? It probably shouldn't be used |
|
haraken
2015/06/11 05:59:29
If it's unused, we can remove the function.
| |
| 100 other.freeMemory(other.m_data, other.m_sizeInBytes); | 90 // for shared array buffers. |
| 101 allocateMemory(m_sizeInBytes, DontInitialize, other.m_data); | 91 ASSERT(!m_holder->isShared() && !other.m_holder->isShared()); |
| 102 if (!other.m_data) | 92 m_holder->copyMemoryTo(*other.m_holder); |
| 103 return; | |
| 104 memcpy(other.m_data, m_data, m_sizeInBytes); | |
| 105 other.m_sizeInBytes = m_sizeInBytes; | |
| 106 } | 93 } |
| 107 | 94 |
| 108 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic y, void*& data) | 95 void ArrayBufferContents::allocateMemory(size_t size, InitializationPolicy polic y, void*& data) |
| 109 { | 96 { |
| 110 data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), Par titionAllocReturnNull, size); | 97 data = partitionAllocGenericFlags(WTF::Partitions::getBufferPartition(), Par titionAllocReturnNull, size); |
| 111 if (policy == ZeroInitialize && data) | 98 if (policy == ZeroInitialize && data) |
| 112 memset(data, '\0', size); | 99 memset(data, '\0', size); |
| 113 } | 100 } |
| 114 | 101 |
| 115 void ArrayBufferContents::freeMemory(void* data, size_t) | 102 void ArrayBufferContents::freeMemory(void* data, size_t) |
| 116 { | 103 { |
| 117 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data); | 104 partitionFreeGeneric(WTF::Partitions::getBufferPartition(), data); |
| 118 } | 105 } |
| 119 | 106 |
| 107 ArrayBufferContents::DataHolder::DataHolder() | |
| 108 : m_data(nullptr) | |
| 109 , m_sizeInBytes(0) | |
| 110 , m_isShared(NotShared) | |
| 111 , m_deallocationObserver(nullptr) { } | |
| 112 | |
| 113 ArrayBufferContents::DataHolder::~DataHolder() | |
| 114 { | |
| 115 ArrayBufferContents::freeMemory(m_data, m_sizeInBytes); | |
| 116 | |
| 117 if (m_data && m_deallocationObserver) | |
|
haraken
2015/06/11 05:59:29
Is it possible that we have m_deallocationObserver
| |
| 118 m_deallocationObserver->arrayBufferDeallocated(m_sizeInBytes); | |
| 119 | |
| 120 m_data = nullptr; | |
| 121 m_sizeInBytes = 0; | |
| 122 m_isShared = NotShared; | |
| 123 m_deallocationObserver = nullptr; | |
| 124 } | |
| 125 | |
| 126 void ArrayBufferContents::DataHolder::allocateNew(unsigned sizeInBytes, SharingT ype isShared, InitializationPolicy policy, ArrayBufferDeallocationObserver* obse rver) | |
| 127 { | |
| 128 ASSERT(!m_data); | |
| 129 void* data = nullptr; | |
| 130 allocateMemory(sizeInBytes, policy, data); | |
|
Yuki
2015/06/11 05:16:14
nit: Better to be consistent with the same style.
| |
| 131 m_data = data; | |
| 132 m_sizeInBytes = sizeInBytes; | |
| 133 m_isShared = isShared; | |
| 134 m_deallocationObserver = observer; | |
| 135 } | |
| 136 | |
| 137 void ArrayBufferContents::DataHolder::adopt(void* data, unsigned sizeInBytes, Sh aringType isShared, ArrayBufferDeallocationObserver* observer) | |
| 138 { | |
| 139 ASSERT(!m_data); | |
| 140 m_data = data; | |
| 141 m_sizeInBytes = sizeInBytes; | |
| 142 m_isShared = isShared; | |
| 143 m_deallocationObserver = observer; | |
| 144 } | |
| 145 | |
| 146 void ArrayBufferContents::DataHolder::copyMemoryTo(DataHolder& other) | |
| 147 { | |
| 148 ASSERT(!other.m_sizeInBytes); | |
| 149 ArrayBufferContents::freeMemory(other.m_data, other.m_sizeInBytes); | |
| 150 ArrayBufferContents::allocateMemory(m_sizeInBytes, DontInitialize, other.m_d ata); | |
| 151 if (!other.m_data) | |
| 152 return; | |
| 153 memcpy(other.m_data, m_data, m_sizeInBytes); | |
| 154 other.m_sizeInBytes = m_sizeInBytes; | |
| 155 } | |
| 156 | |
| 157 void ArrayBufferContents::DataHolder::setDeallocationObserver(ArrayBufferDealloc ationObserver& observer) | |
| 158 { | |
| 159 if (!m_deallocationObserver) { | |
| 160 m_deallocationObserver = &observer; | |
| 161 m_deallocationObserver->blinkAllocatedMemory(m_sizeInBytes); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 void ArrayBufferContents::DataHolder::setDeallocationObserverWithoutAllocationNo tification(ArrayBufferDeallocationObserver& observer) | |
| 166 { | |
| 167 if (!m_deallocationObserver) { | |
| 168 m_deallocationObserver = &observer; | |
| 169 } | |
| 170 } | |
| 171 | |
| 120 } // namespace WTF | 172 } // namespace WTF |
| OLD | NEW |