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
3 // found in the LICENSE file.
5 #include "cc/base/list_container.h"
10 #include "cc/base/scoped_ptr_vector.h"
13 const size_t kDefaultNumElementTypesToReserve
= 32;
18 // ListContainerCharAllocator
19 ////////////////////////////////////////////////////
20 // This class deals only with char* and void*. It does allocation and passing
21 // out raw pointers, as well as memory deallocation when being destroyed.
22 class ListContainerBase::ListContainerCharAllocator
{
24 // ListContainerCharAllocator::InnerList
25 /////////////////////////////////////////////
26 // This class holds the raw memory chunk, as well as information about its
27 // size and availability.
29 scoped_ptr
<char[]> data
;
30 // The number of elements in total the memory can hold. The difference
31 // between capacity and size is the how many more elements this list can
34 // The number of elements have been put into this list.
36 // The size of each element is in bytes. This is used to move from between
37 // elements' memory locations.
40 InnerList() : capacity(0), size(0), step(0) {}
42 void Erase(char* position
) {
43 // Confident that destructor is called by caller of this function. Since
44 // ListContainerCharAllocator does not handle construction after
45 // allocation, it doesn't handle desctrution before deallocation.
46 DCHECK_LE(position
, LastElement());
47 DCHECK_GE(position
, Begin());
48 char* start
= position
+ step
;
49 std::copy(start
, End(), position
);
52 // Decrease capacity to avoid creating not full not last InnerList.
56 bool IsEmpty() const { return !size
; }
57 bool IsFull() { return capacity
== size
; }
58 size_t NumElementsAvailable() const { return capacity
- size
; }
61 DCHECK_LT(size
, capacity
);
71 char* Begin() const { return data
.get(); }
72 char* End() const { return data
.get() + size
* step
; }
73 char* LastElement() const { return data
.get() + (size
- 1) * step
; }
74 char* ElementAt(size_t index
) const { return data
.get() + index
* step
; }
77 DISALLOW_COPY_AND_ASSIGN(InnerList
);
80 explicit ListContainerCharAllocator(size_t element_size
)
81 : element_size_(element_size
),
85 AllocateNewList(kDefaultNumElementTypesToReserve
);
86 last_list_
= storage_
[last_list_index_
];
89 ListContainerCharAllocator(size_t element_size
, size_t element_count
)
90 : element_size_(element_size
),
94 AllocateNewList(element_count
> 0 ? element_count
95 : kDefaultNumElementTypesToReserve
);
96 last_list_
= storage_
[last_list_index_
];
99 ~ListContainerCharAllocator() {}
102 if (last_list_
->IsFull()) {
103 // Only allocate a new list if there isn't a spare one still there from
105 if (last_list_index_
+ 1 >= storage_
.size())
106 AllocateNewList(last_list_
->capacity
* 2);
109 last_list_
= storage_
[last_list_index_
];
113 return last_list_
->AddElement();
116 size_t element_size() const { return element_size_
; }
117 size_t list_count() const { return storage_
.size(); }
118 size_t size() const { return size_
; }
119 bool IsEmpty() const { return size() == 0; }
121 size_t Capacity() const {
122 size_t capacity_sum
= 0;
123 for (ScopedPtrVector
<InnerList
>::const_iterator iter
= storage_
.begin();
124 iter
!= storage_
.end(); ++iter
) {
125 capacity_sum
+= (*iter
)->capacity
;
131 size_t initial_allocation_size
= storage_
.front()->capacity
;
134 last_list_index_
= 0;
136 AllocateNewList(initial_allocation_size
);
137 last_list_
= storage_
[last_list_index_
];
142 last_list_
->RemoveLast();
143 if (last_list_
->IsEmpty() && last_list_index_
> 0) {
145 last_list_
= storage_
[last_list_index_
];
147 // If there are now two empty inner lists, free one of them.
148 if (last_list_index_
+ 2 < storage_
.size())
154 void Erase(PositionInListContainerCharAllocator position
) {
155 DCHECK_EQ(this, position
.ptr_to_container
);
156 storage_
[position
.vector_index
]->Erase(position
.item_iterator
);
157 // TODO(weiliangc): Free the InnerList if it is empty.
161 InnerList
* InnerListById(size_t id
) const {
162 DCHECK_LT(id
, storage_
.size());
166 size_t FirstInnerListId() const {
167 // |size_| > 0 means that at least one vector in |storage_| will be
169 DCHECK_GT(size_
, 0u);
171 while (storage_
[id
]->size
== 0)
176 size_t LastInnerListId() const {
177 // |size_| > 0 means that at least one vector in |storage_| will be
179 DCHECK_GT(size_
, 0u);
180 size_t id
= storage_
.size() - 1;
181 while (storage_
[id
]->size
== 0)
186 size_t NumAvailableElementsInLastList() const {
187 return last_list_
->NumElementsAvailable();
191 void AllocateNewList(size_t list_size
) {
192 scoped_ptr
<InnerList
> new_list(new InnerList
);
193 new_list
->capacity
= list_size
;
195 new_list
->step
= element_size_
;
196 new_list
->data
.reset(new char[list_size
* element_size_
]);
197 storage_
.push_back(new_list
.Pass());
200 ScopedPtrVector
<InnerList
> storage_
;
201 const size_t element_size_
;
203 // The number of elements in the list.
206 // The index of the last list to have had elements added to it, or the only
207 // list if the container has not had elements added since being cleared.
208 size_t last_list_index_
;
210 // This is equivalent to |storage_[last_list_index_]|.
211 InnerList
* last_list_
;
213 DISALLOW_COPY_AND_ASSIGN(ListContainerCharAllocator
);
216 // PositionInListContainerCharAllocator
217 //////////////////////////////////////////////////////
218 ListContainerBase::PositionInListContainerCharAllocator::
219 PositionInListContainerCharAllocator(
220 const ListContainerBase::PositionInListContainerCharAllocator
& other
)
221 : ptr_to_container(other
.ptr_to_container
),
222 vector_index(other
.vector_index
),
223 item_iterator(other
.item_iterator
) {
226 ListContainerBase::PositionInListContainerCharAllocator::
227 PositionInListContainerCharAllocator(
228 ListContainerBase::ListContainerCharAllocator
* container
,
231 : ptr_to_container(container
),
232 vector_index(vector_ind
),
233 item_iterator(item_iter
) {
236 bool ListContainerBase::PositionInListContainerCharAllocator::operator==(
237 const ListContainerBase::PositionInListContainerCharAllocator
& other
)
239 DCHECK_EQ(ptr_to_container
, other
.ptr_to_container
);
240 return vector_index
== other
.vector_index
&&
241 item_iterator
== other
.item_iterator
;
244 bool ListContainerBase::PositionInListContainerCharAllocator::operator!=(
245 const ListContainerBase::PositionInListContainerCharAllocator
& other
)
247 return !(*this == other
);
250 ListContainerBase::PositionInListContainerCharAllocator
251 ListContainerBase::PositionInListContainerCharAllocator::Increment() {
252 ListContainerCharAllocator::InnerList
* list
=
253 ptr_to_container
->InnerListById(vector_index
);
254 if (item_iterator
== list
->LastElement()) {
256 while (vector_index
< ptr_to_container
->list_count()) {
257 if (ptr_to_container
->InnerListById(vector_index
)->size
!= 0)
261 if (vector_index
< ptr_to_container
->list_count())
262 item_iterator
= ptr_to_container
->InnerListById(vector_index
)->Begin();
264 item_iterator
= NULL
;
266 item_iterator
+= list
->step
;
271 ListContainerBase::PositionInListContainerCharAllocator
272 ListContainerBase::PositionInListContainerCharAllocator::ReverseIncrement() {
273 ListContainerCharAllocator::InnerList
* list
=
274 ptr_to_container
->InnerListById(vector_index
);
275 if (item_iterator
== list
->Begin()) {
277 // Since |vector_index| is unsigned, we compare < list_count() instead of
278 // comparing >= 0, as the variable will wrap around when it goes out of
280 while (vector_index
< ptr_to_container
->list_count()) {
281 if (ptr_to_container
->InnerListById(vector_index
)->size
!= 0)
285 if (vector_index
< ptr_to_container
->list_count()) {
287 ptr_to_container
->InnerListById(vector_index
)->LastElement();
289 item_iterator
= NULL
;
292 item_iterator
-= list
->step
;
298 ////////////////////////////////////////////
299 ListContainerBase::ListContainerBase(size_t max_size_for_derived_class
)
300 : data_(new ListContainerCharAllocator(max_size_for_derived_class
)) {
303 ListContainerBase::ListContainerBase(size_t max_size_for_derived_class
,
304 size_t num_of_elements_to_reserve_for
)
305 : data_(new ListContainerCharAllocator(max_size_for_derived_class
,
306 num_of_elements_to_reserve_for
)) {
309 ListContainerBase::~ListContainerBase() {
312 void ListContainerBase::RemoveLast() {
316 void ListContainerBase::EraseAndInvalidateAllPointers(
317 ListContainerBase::Iterator position
) {
318 data_
->Erase(position
);
321 ListContainerBase::ConstReverseIterator
ListContainerBase::crbegin() const {
322 if (data_
->IsEmpty())
325 size_t id
= data_
->LastInnerListId();
326 return ConstReverseIterator(data_
.get(), id
,
327 data_
->InnerListById(id
)->LastElement(), 0);
330 ListContainerBase::ConstReverseIterator
ListContainerBase::crend() const {
331 return ConstReverseIterator(data_
.get(), static_cast<size_t>(-1), NULL
,
335 ListContainerBase::ReverseIterator
ListContainerBase::rbegin() {
336 if (data_
->IsEmpty())
339 size_t id
= data_
->LastInnerListId();
340 return ReverseIterator(data_
.get(), id
,
341 data_
->InnerListById(id
)->LastElement(), 0);
344 ListContainerBase::ReverseIterator
ListContainerBase::rend() {
345 return ReverseIterator(data_
.get(), static_cast<size_t>(-1), NULL
, size());
348 ListContainerBase::ConstIterator
ListContainerBase::cbegin() const {
349 if (data_
->IsEmpty())
352 size_t id
= data_
->FirstInnerListId();
353 return ConstIterator(data_
.get(), id
, data_
->InnerListById(id
)->Begin(), 0);
356 ListContainerBase::ConstIterator
ListContainerBase::cend() const {
357 if (data_
->IsEmpty())
358 return ConstIterator(data_
.get(), 0, NULL
, size());
360 size_t id
= data_
->list_count();
361 return ConstIterator(data_
.get(), id
, NULL
, size());
364 ListContainerBase::Iterator
ListContainerBase::begin() {
365 if (data_
->IsEmpty())
368 size_t id
= data_
->FirstInnerListId();
369 return Iterator(data_
.get(), id
, data_
->InnerListById(id
)->Begin(), 0);
372 ListContainerBase::Iterator
ListContainerBase::end() {
373 if (data_
->IsEmpty())
374 return Iterator(data_
.get(), 0, NULL
, size());
376 size_t id
= data_
->list_count();
377 return Iterator(data_
.get(), id
, NULL
, size());
380 ListContainerBase::ConstIterator
ListContainerBase::IteratorAt(
381 size_t index
) const {
382 DCHECK_LT(index
, size());
383 size_t original_index
= index
;
385 for (list_index
= 0; list_index
< data_
->list_count(); ++list_index
) {
386 size_t current_size
= data_
->InnerListById(list_index
)->size
;
387 if (index
< current_size
)
389 index
-= current_size
;
391 return ConstIterator(data_
.get(), list_index
,
392 data_
->InnerListById(list_index
)->ElementAt(index
),
396 ListContainerBase::Iterator
ListContainerBase::IteratorAt(size_t index
) {
397 DCHECK_LT(index
, size());
398 size_t original_index
= index
;
400 for (list_index
= 0; list_index
< data_
->list_count(); ++list_index
) {
401 size_t current_size
= data_
->InnerListById(list_index
)->size
;
402 if (index
< current_size
)
404 index
-= current_size
;
406 return Iterator(data_
.get(), list_index
,
407 data_
->InnerListById(list_index
)->ElementAt(index
),
411 void* ListContainerBase::Allocate(size_t size_of_actual_element_in_bytes
) {
412 DCHECK_LE(size_of_actual_element_in_bytes
, data_
->element_size());
413 return data_
->Allocate();
416 size_t ListContainerBase::size() const {
417 return data_
->size();
420 bool ListContainerBase::empty() const {
421 return data_
->IsEmpty();
424 size_t ListContainerBase::MaxSizeForDerivedClass() const {
425 return data_
->element_size();
428 void ListContainerBase::clear() {
432 size_t ListContainerBase::AvailableSizeWithoutAnotherAllocationForTesting()
434 return data_
->NumAvailableElementsInLastList();
437 // ListContainerBase::Iterator
438 /////////////////////////////////////////////////
439 ListContainerBase::Iterator::Iterator(ListContainerCharAllocator
* container
,
443 : PositionInListContainerCharAllocator(container
, vector_ind
, item_iter
),
447 ListContainerBase::Iterator::~Iterator() {
450 size_t ListContainerBase::Iterator::index() const {
454 // ListContainerBase::ConstIterator
455 /////////////////////////////////////////////////
456 ListContainerBase::ConstIterator::ConstIterator(
457 const ListContainerBase::Iterator
& other
)
458 : PositionInListContainerCharAllocator(other
), index_(other
.index()) {
461 ListContainerBase::ConstIterator::ConstIterator(
462 ListContainerCharAllocator
* container
,
466 : PositionInListContainerCharAllocator(container
, vector_ind
, item_iter
),
470 ListContainerBase::ConstIterator::~ConstIterator() {
473 size_t ListContainerBase::ConstIterator::index() const {
477 // ListContainerBase::ReverseIterator
478 /////////////////////////////////////////////////
479 ListContainerBase::ReverseIterator::ReverseIterator(
480 ListContainerCharAllocator
* container
,
484 : PositionInListContainerCharAllocator(container
, vector_ind
, item_iter
),
488 ListContainerBase::ReverseIterator::~ReverseIterator() {
491 size_t ListContainerBase::ReverseIterator::index() const {
495 // ListContainerBase::ConstReverseIterator
496 /////////////////////////////////////////////////
497 ListContainerBase::ConstReverseIterator::ConstReverseIterator(
498 const ListContainerBase::ReverseIterator
& other
)
499 : PositionInListContainerCharAllocator(other
), index_(other
.index()) {
502 ListContainerBase::ConstReverseIterator::ConstReverseIterator(
503 ListContainerCharAllocator
* container
,
507 : PositionInListContainerCharAllocator(container
, vector_ind
, item_iter
),
511 ListContainerBase::ConstReverseIterator::~ConstReverseIterator() {
514 size_t ListContainerBase::ConstReverseIterator::index() const {