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 <algorithm> | 7 #include <algorithm> |
8 #include <memory> | 8 #include <memory> |
9 #include "platform/wtf/Allocator.h" | 9 #include "platform/wtf/Allocator.h" |
10 #include "platform/wtf/ContainerAnnotations.h" | 10 #include "platform/wtf/ContainerAnnotations.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 ANNOTATE_DELETE_BUFFER(begin_, capacity_, UsedCapacity()); | 33 ANNOTATE_DELETE_BUFFER(begin_, capacity_, UsedCapacity()); |
34 WTF::Partitions::BufferFree(begin_); | 34 WTF::Partitions::BufferFree(begin_); |
35 } | 35 } |
36 | 36 |
37 size_t Capacity() const { return capacity_; } | 37 size_t Capacity() const { return capacity_; } |
38 size_t UsedCapacity() const { return end_ - begin_; } | 38 size_t UsedCapacity() const { return end_ - begin_; } |
39 size_t UnusedCapacity() const { return Capacity() - UsedCapacity(); } | 39 size_t UnusedCapacity() const { return Capacity() - UsedCapacity(); } |
40 bool IsEmpty() const { return UsedCapacity() == 0; } | 40 bool IsEmpty() const { return UsedCapacity() == 0; } |
41 | 41 |
42 void* Allocate(size_t object_size) { | 42 void* Allocate(size_t object_size) { |
43 ASSERT(UnusedCapacity() >= object_size); | 43 DCHECK_GE(UnusedCapacity(), object_size); |
44 ANNOTATE_CHANGE_SIZE(begin_, capacity_, UsedCapacity(), | 44 ANNOTATE_CHANGE_SIZE(begin_, capacity_, UsedCapacity(), |
45 UsedCapacity() + object_size); | 45 UsedCapacity() + object_size); |
46 void* result = end_; | 46 void* result = end_; |
47 end_ += object_size; | 47 end_ += object_size; |
48 return result; | 48 return result; |
49 } | 49 } |
50 | 50 |
51 void DeallocateLastObject(void* object) { | 51 void DeallocateLastObject(void* object) { |
52 RELEASE_ASSERT(begin_ <= object && object < end_); | 52 CHECK_LE(begin_, object); |
| 53 CHECK_LT(object, end_); |
53 ANNOTATE_CHANGE_SIZE(begin_, capacity_, UsedCapacity(), | 54 ANNOTATE_CHANGE_SIZE(begin_, capacity_, UsedCapacity(), |
54 static_cast<char*>(object) - begin_); | 55 static_cast<char*>(object) - begin_); |
55 end_ = static_cast<char*>(object); | 56 end_ = static_cast<char*>(object); |
56 } | 57 } |
57 | 58 |
58 private: | 59 private: |
59 // m_begin <= m_end <= m_begin + m_capacity | 60 // m_begin <= m_end <= m_begin + m_capacity |
60 char* begin_; | 61 char* begin_; |
61 char* end_; | 62 char* end_; |
62 size_t capacity_; | 63 size_t capacity_; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 elements_.Capacity() * sizeof(elements_[0]); | 99 elements_.Capacity() * sizeof(elements_[0]); |
99 } | 100 } |
100 | 101 |
101 void ContiguousContainerBase::ReserveInitialCapacity(size_t buffer_size, | 102 void ContiguousContainerBase::ReserveInitialCapacity(size_t buffer_size, |
102 const char* type_name) { | 103 const char* type_name) { |
103 AllocateNewBufferForNextAllocation(buffer_size, type_name); | 104 AllocateNewBufferForNextAllocation(buffer_size, type_name); |
104 } | 105 } |
105 | 106 |
106 void* ContiguousContainerBase::Allocate(size_t object_size, | 107 void* ContiguousContainerBase::Allocate(size_t object_size, |
107 const char* type_name) { | 108 const char* type_name) { |
108 ASSERT(object_size <= max_object_size_); | 109 DCHECK_LE(object_size, max_object_size_); |
109 | 110 |
110 Buffer* buffer_for_alloc = nullptr; | 111 Buffer* buffer_for_alloc = nullptr; |
111 if (!buffers_.IsEmpty()) { | 112 if (!buffers_.IsEmpty()) { |
112 Buffer* end_buffer = buffers_[end_index_].get(); | 113 Buffer* end_buffer = buffers_[end_index_].get(); |
113 if (end_buffer->UnusedCapacity() >= object_size) | 114 if (end_buffer->UnusedCapacity() >= object_size) |
114 buffer_for_alloc = end_buffer; | 115 buffer_for_alloc = end_buffer; |
115 else if (end_index_ + 1 < buffers_.size()) | 116 else if (end_index_ + 1 < buffers_.size()) |
116 buffer_for_alloc = buffers_[++end_index_].get(); | 117 buffer_for_alloc = buffers_[++end_index_].get(); |
117 } | 118 } |
118 | 119 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 while (end_index_ < buffers_.size() - 1) { | 162 while (end_index_ < buffers_.size() - 1) { |
162 DCHECK(buffers_.back()->IsEmpty()); | 163 DCHECK(buffers_.back()->IsEmpty()); |
163 buffers_.pop_back(); | 164 buffers_.pop_back(); |
164 } | 165 } |
165 } | 166 } |
166 | 167 |
167 ContiguousContainerBase::Buffer* | 168 ContiguousContainerBase::Buffer* |
168 ContiguousContainerBase::AllocateNewBufferForNextAllocation( | 169 ContiguousContainerBase::AllocateNewBufferForNextAllocation( |
169 size_t buffer_size, | 170 size_t buffer_size, |
170 const char* type_name) { | 171 const char* type_name) { |
171 ASSERT(buffers_.IsEmpty() || end_index_ == buffers_.size() - 1); | 172 DCHECK(buffers_.IsEmpty() || end_index_ == buffers_.size() - 1); |
172 std::unique_ptr<Buffer> new_buffer = | 173 std::unique_ptr<Buffer> new_buffer = |
173 WTF::MakeUnique<Buffer>(buffer_size, type_name); | 174 WTF::MakeUnique<Buffer>(buffer_size, type_name); |
174 Buffer* buffer_to_return = new_buffer.get(); | 175 Buffer* buffer_to_return = new_buffer.get(); |
175 buffers_.push_back(std::move(new_buffer)); | 176 buffers_.push_back(std::move(new_buffer)); |
176 end_index_ = buffers_.size() - 1; | 177 end_index_ = buffers_.size() - 1; |
177 return buffer_to_return; | 178 return buffer_to_return; |
178 } | 179 } |
179 | 180 |
180 } // namespace blink | 181 } // namespace blink |
OLD | NEW |