| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/graphics/ContiguousContainer.h" | 5 #include "platform/graphics/ContiguousContainer.h" |
| 6 | 6 |
| 7 #include "wtf/Allocator.h" | 7 #include "wtf/Allocator.h" |
| 8 #include "wtf/ContainerAnnotations.h" | 8 #include "wtf/ContainerAnnotations.h" |
| 9 #include "wtf/PartitionAlloc.h" | 9 #include "wtf/PartitionAlloc.h" |
| 10 #include "wtf/Partitions.h" | 10 #include "wtf/Partitions.h" |
| 11 #include "wtf/PassOwnPtr.h" | 11 #include "wtf/PassOwnPtr.h" |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 | 13 |
| 14 namespace blink { | 14 namespace blink { |
| 15 | 15 |
| 16 // Default number of max-sized elements to allocate space for, if there is no | 16 // Default number of max-sized elements to allocate space for, if there is no |
| 17 // initial buffer. | 17 // initial buffer. |
| 18 static const unsigned kDefaultInitialBufferSize = 32; | 18 static const unsigned kDefaultInitialBufferSize = 32; |
| 19 | 19 |
| 20 class ContiguousContainerBase::Buffer { | 20 class ContiguousContainerBase::Buffer { |
| 21 WTF_MAKE_NONCOPYABLE(Buffer); | 21 WTF_MAKE_NONCOPYABLE(Buffer); |
| 22 USING_FAST_MALLOC(Buffer); | 22 USING_FAST_MALLOC(Buffer); |
| 23 public: | 23 public: |
| 24 explicit Buffer(size_t bufferSize, const char* typeName) | 24 Buffer(size_t bufferSize, const char* typeName) |
| 25 { | 25 { |
| 26 m_capacity = WTF::Partitions::bufferActualSize(bufferSize); | 26 m_capacity = WTF::Partitions::bufferActualSize(bufferSize); |
| 27 m_begin = m_end = static_cast<char*>( | 27 m_begin = m_end = static_cast<char*>( |
| 28 WTF::Partitions::bufferMalloc(m_capacity, typeName)); | 28 WTF::Partitions::bufferMalloc(m_capacity, typeName)); |
| 29 ANNOTATE_NEW_BUFFER(m_begin, m_capacity, 0); | 29 ANNOTATE_NEW_BUFFER(m_begin, m_capacity, 0); |
| 30 } | 30 } |
| 31 | 31 |
| 32 ~Buffer() | 32 ~Buffer() |
| 33 { | 33 { |
| 34 ANNOTATE_DELETE_BUFFER(m_begin, m_capacity, usedCapacity()); | 34 ANNOTATE_DELETE_BUFFER(m_begin, m_capacity, usedCapacity()); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 58 m_end = static_cast<char*>(object); | 58 m_end = static_cast<char*>(object); |
| 59 } | 59 } |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 // m_begin <= m_end <= m_begin + m_capacity | 62 // m_begin <= m_end <= m_begin + m_capacity |
| 63 char* m_begin; | 63 char* m_begin; |
| 64 char* m_end; | 64 char* m_end; |
| 65 size_t m_capacity; | 65 size_t m_capacity; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 ContiguousContainerBase::ContiguousContainerBase(size_t maxObjectSize, const cha
r* typeName) | 68 ContiguousContainerBase::ContiguousContainerBase(size_t maxObjectSize) |
| 69 : m_endIndex(0) | 69 : m_endIndex(0) |
| 70 , m_maxObjectSize(maxObjectSize) | 70 , m_maxObjectSize(maxObjectSize) |
| 71 { | 71 { |
| 72 } | 72 } |
| 73 | 73 |
| 74 ContiguousContainerBase::ContiguousContainerBase( | |
| 75 size_t maxObjectSize, size_t initialSizeBytes, const char* typeName) | |
| 76 : ContiguousContainerBase(maxObjectSize, typeName) | |
| 77 { | |
| 78 allocateNewBufferForNextAllocation(std::max(maxObjectSize, initialSizeBytes)
, typeName); | |
| 79 } | |
| 80 | |
| 81 ContiguousContainerBase::~ContiguousContainerBase() | 74 ContiguousContainerBase::~ContiguousContainerBase() |
| 82 { | 75 { |
| 83 } | 76 } |
| 84 | 77 |
| 85 size_t ContiguousContainerBase::capacityInBytes() const | 78 size_t ContiguousContainerBase::capacityInBytes() const |
| 86 { | 79 { |
| 87 size_t capacity = 0; | 80 size_t capacity = 0; |
| 88 for (const auto& buffer : m_buffers) | 81 for (const auto& buffer : m_buffers) |
| 89 capacity += buffer->capacity(); | 82 capacity += buffer->capacity(); |
| 90 return capacity; | 83 return capacity; |
| 91 } | 84 } |
| 92 | 85 |
| 93 size_t ContiguousContainerBase::usedCapacityInBytes() const | 86 size_t ContiguousContainerBase::usedCapacityInBytes() const |
| 94 { | 87 { |
| 95 size_t usedCapacity = 0; | 88 size_t usedCapacity = 0; |
| 96 for (const auto& buffer : m_buffers) | 89 for (const auto& buffer : m_buffers) |
| 97 usedCapacity += buffer->usedCapacity(); | 90 usedCapacity += buffer->usedCapacity(); |
| 98 return usedCapacity; | 91 return usedCapacity; |
| 99 } | 92 } |
| 100 | 93 |
| 101 size_t ContiguousContainerBase::memoryUsageInBytes() const | 94 size_t ContiguousContainerBase::memoryUsageInBytes() const |
| 102 { | 95 { |
| 103 return sizeof(*this) + capacityInBytes() | 96 return sizeof(*this) + capacityInBytes() |
| 104 + m_elements.capacity() * sizeof(m_elements[0]); | 97 + m_elements.capacity() * sizeof(m_elements[0]); |
| 105 } | 98 } |
| 106 | 99 |
| 100 void ContiguousContainerBase::reserveInitialCapacity(size_t bufferSize, const ch
ar* typeName) |
| 101 { |
| 102 allocateNewBufferForNextAllocation(bufferSize, typeName); |
| 103 } |
| 104 |
| 107 void* ContiguousContainerBase::allocate(size_t objectSize, const char* typeName) | 105 void* ContiguousContainerBase::allocate(size_t objectSize, const char* typeName) |
| 108 { | 106 { |
| 109 ASSERT(objectSize <= m_maxObjectSize); | 107 ASSERT(objectSize <= m_maxObjectSize); |
| 110 | 108 |
| 111 Buffer* bufferForAlloc = nullptr; | 109 Buffer* bufferForAlloc = nullptr; |
| 112 if (!m_buffers.isEmpty()) { | 110 if (!m_buffers.isEmpty()) { |
| 113 Buffer* endBuffer = m_buffers[m_endIndex].get(); | 111 Buffer* endBuffer = m_buffers[m_endIndex].get(); |
| 114 if (endBuffer->unusedCapacity() >= objectSize) | 112 if (endBuffer->unusedCapacity() >= objectSize) |
| 115 bufferForAlloc = endBuffer; | 113 bufferForAlloc = endBuffer; |
| 116 else if (m_endIndex + 1 < m_buffers.size()) | 114 else if (m_endIndex + 1 < m_buffers.size()) |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 165 { | 163 { |
| 166 ASSERT(m_buffers.isEmpty() || m_endIndex == m_buffers.size() - 1); | 164 ASSERT(m_buffers.isEmpty() || m_endIndex == m_buffers.size() - 1); |
| 167 OwnPtr<Buffer> newBuffer = adoptPtr(new Buffer(bufferSize, typeName)); | 165 OwnPtr<Buffer> newBuffer = adoptPtr(new Buffer(bufferSize, typeName)); |
| 168 Buffer* bufferToReturn = newBuffer.get(); | 166 Buffer* bufferToReturn = newBuffer.get(); |
| 169 m_buffers.append(newBuffer.release()); | 167 m_buffers.append(newBuffer.release()); |
| 170 m_endIndex = m_buffers.size() - 1; | 168 m_endIndex = m_buffers.size() - 1; |
| 171 return bufferToReturn; | 169 return bufferToReturn; |
| 172 } | 170 } |
| 173 | 171 |
| 174 } // namespace blink | 172 } // namespace blink |
| OLD | NEW |