OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "cc/base/list_container_helper.h" | 5 #include "cc/base/list_container_helper.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <vector> | 10 #include <vector> |
(...skipping 11 matching lines...) Expand all Loading... |
22 //////////////////////////////////////////////////// | 22 //////////////////////////////////////////////////// |
23 // This class deals only with char* and void*. It does allocation and passing | 23 // This class deals only with char* and void*. It does allocation and passing |
24 // out raw pointers, as well as memory deallocation when being destroyed. | 24 // out raw pointers, as well as memory deallocation when being destroyed. |
25 class ListContainerHelper::CharAllocator { | 25 class ListContainerHelper::CharAllocator { |
26 public: | 26 public: |
27 // CharAllocator::InnerList | 27 // CharAllocator::InnerList |
28 ///////////////////////////////////////////// | 28 ///////////////////////////////////////////// |
29 // This class holds the raw memory chunk, as well as information about its | 29 // This class holds the raw memory chunk, as well as information about its |
30 // size and availability. | 30 // size and availability. |
31 struct InnerList { | 31 struct InnerList { |
32 scoped_ptr<char[]> data; | 32 std::unique_ptr<char[]> data; |
33 // The number of elements in total the memory can hold. The difference | 33 // The number of elements in total the memory can hold. The difference |
34 // between capacity and size is the how many more elements this list can | 34 // between capacity and size is the how many more elements this list can |
35 // hold. | 35 // hold. |
36 size_t capacity; | 36 size_t capacity; |
37 // The number of elements have been put into this list. | 37 // The number of elements have been put into this list. |
38 size_t size; | 38 size_t size; |
39 // The size of each element is in bytes. This is used to move from between | 39 // The size of each element is in bytes. This is used to move from between |
40 // elements' memory locations. | 40 // elements' memory locations. |
41 size_t step; | 41 size_t step; |
42 | 42 |
(...skipping 16 matching lines...) Expand all Loading... |
59 void InsertBefore(char** position, size_t count) { | 59 void InsertBefore(char** position, size_t count) { |
60 DCHECK_LE(*position, LastElement() + step); | 60 DCHECK_LE(*position, LastElement() + step); |
61 DCHECK_GE(*position, Begin()); | 61 DCHECK_GE(*position, Begin()); |
62 | 62 |
63 // Adjust the size and capacity | 63 // Adjust the size and capacity |
64 size_t old_size = size; | 64 size_t old_size = size; |
65 size += count; | 65 size += count; |
66 capacity = size; | 66 capacity = size; |
67 | 67 |
68 // Allocate the new data and update the iterator's pointer. | 68 // Allocate the new data and update the iterator's pointer. |
69 scoped_ptr<char[]> new_data(new char[size * step]); | 69 std::unique_ptr<char[]> new_data(new char[size * step]); |
70 size_t position_offset = *position - Begin(); | 70 size_t position_offset = *position - Begin(); |
71 *position = new_data.get() + position_offset; | 71 *position = new_data.get() + position_offset; |
72 | 72 |
73 // Copy the data before the inserted segment | 73 // Copy the data before the inserted segment |
74 memcpy(new_data.get(), data.get(), position_offset); | 74 memcpy(new_data.get(), data.get(), position_offset); |
75 // Copy the data after the inserted segment. | 75 // Copy the data after the inserted segment. |
76 memcpy(new_data.get() + position_offset + count * step, | 76 memcpy(new_data.get() + position_offset + count * step, |
77 data.get() + position_offset, old_size * step - position_offset); | 77 data.get() + position_offset, old_size * step - position_offset); |
78 new_data.swap(data); | 78 new_data.swap(data); |
79 } | 79 } |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 --id; | 233 --id; |
234 return id; | 234 return id; |
235 } | 235 } |
236 | 236 |
237 size_t NumAvailableElementsInLastList() const { | 237 size_t NumAvailableElementsInLastList() const { |
238 return last_list_->NumElementsAvailable(); | 238 return last_list_->NumElementsAvailable(); |
239 } | 239 } |
240 | 240 |
241 private: | 241 private: |
242 void AllocateNewList(size_t list_size) { | 242 void AllocateNewList(size_t list_size) { |
243 scoped_ptr<InnerList> new_list(new InnerList); | 243 std::unique_ptr<InnerList> new_list(new InnerList); |
244 new_list->capacity = list_size; | 244 new_list->capacity = list_size; |
245 new_list->size = 0; | 245 new_list->size = 0; |
246 new_list->step = element_size_; | 246 new_list->step = element_size_; |
247 new_list->data.reset(new char[list_size * element_size_]); | 247 new_list->data.reset(new char[list_size * element_size_]); |
248 storage_.push_back(std::move(new_list)); | 248 storage_.push_back(std::move(new_list)); |
249 } | 249 } |
250 | 250 |
251 std::vector<scoped_ptr<InnerList>> storage_; | 251 std::vector<std::unique_ptr<InnerList>> storage_; |
252 const size_t element_size_; | 252 const size_t element_size_; |
253 | 253 |
254 // The number of elements in the list. | 254 // The number of elements in the list. |
255 size_t size_; | 255 size_t size_; |
256 | 256 |
257 // The index of the last list to have had elements added to it, or the only | 257 // The index of the last list to have had elements added to it, or the only |
258 // list if the container has not had elements added since being cleared. | 258 // list if the container has not had elements added since being cleared. |
259 size_t last_list_index_; | 259 size_t last_list_index_; |
260 | 260 |
261 // This is equivalent to |storage_[last_list_index_]|. | 261 // This is equivalent to |storage_[last_list_index_]|. |
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
549 : PositionInCharAllocator(container, vector_ind, item_iter), | 549 : PositionInCharAllocator(container, vector_ind, item_iter), |
550 index_(index) {} | 550 index_(index) {} |
551 | 551 |
552 ListContainerHelper::ConstReverseIterator::~ConstReverseIterator() {} | 552 ListContainerHelper::ConstReverseIterator::~ConstReverseIterator() {} |
553 | 553 |
554 size_t ListContainerHelper::ConstReverseIterator::index() const { | 554 size_t ListContainerHelper::ConstReverseIterator::index() const { |
555 return index_; | 555 return index_; |
556 } | 556 } |
557 | 557 |
558 } // namespace cc | 558 } // namespace cc |
OLD | NEW |