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_helper.h"
10 #include "cc/base/scoped_ptr_vector.h"
13 const size_t kDefaultNumElementTypesToReserve
= 32;
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 ListContainerHelper::CharAllocator
{
24 // CharAllocator::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 // CharAllocator 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 void InsertBefore(char** position
, size_t count
) {
57 DCHECK_LE(*position
, LastElement() + step
);
58 DCHECK_GE(*position
, Begin());
60 // Adjust the size and capacity
61 size_t old_size
= size
;
65 // Allocate the new data and update the iterator's pointer.
66 scoped_ptr
<char[]> new_data(new char[size
* step
]);
67 size_t position_offset
= *position
- Begin();
68 *position
= new_data
.get() + position_offset
;
70 // Copy the data before the inserted segment
71 memcpy(new_data
.get(), data
.get(), position_offset
);
72 // Copy the data after the inserted segment.
73 memcpy(new_data
.get() + position_offset
+ count
* step
,
74 data
.get() + position_offset
, old_size
* step
- position_offset
);
78 bool IsEmpty() const { return !size
; }
79 bool IsFull() { return capacity
== size
; }
80 size_t NumElementsAvailable() const { return capacity
- size
; }
83 DCHECK_LT(size
, capacity
);
93 char* Begin() const { return data
.get(); }
94 char* End() const { return data
.get() + size
* step
; }
95 char* LastElement() const { return data
.get() + (size
- 1) * step
; }
96 char* ElementAt(size_t index
) const { return data
.get() + index
* step
; }
99 DISALLOW_COPY_AND_ASSIGN(InnerList
);
102 explicit CharAllocator(size_t element_size
)
103 : element_size_(element_size
),
107 AllocateNewList(kDefaultNumElementTypesToReserve
);
108 last_list_
= storage_
[last_list_index_
];
111 CharAllocator(size_t element_size
, size_t element_count
)
112 : element_size_(element_size
),
116 AllocateNewList(element_count
> 0 ? element_count
117 : kDefaultNumElementTypesToReserve
);
118 last_list_
= storage_
[last_list_index_
];
124 if (last_list_
->IsFull()) {
125 // Only allocate a new list if there isn't a spare one still there from
127 if (last_list_index_
+ 1 >= storage_
.size())
128 AllocateNewList(last_list_
->capacity
* 2);
131 last_list_
= storage_
[last_list_index_
];
135 return last_list_
->AddElement();
138 size_t element_size() const { return element_size_
; }
139 size_t list_count() const { return storage_
.size(); }
140 size_t size() const { return size_
; }
141 bool IsEmpty() const { return size() == 0; }
143 size_t Capacity() const {
144 size_t capacity_sum
= 0;
145 for (const auto& inner_list
: storage_
)
146 capacity_sum
+= inner_list
->capacity
;
151 // Remove all except for the first InnerList.
152 DCHECK(!storage_
.empty());
153 storage_
.erase(storage_
.begin() + 1, storage_
.end());
154 last_list_index_
= 0;
155 last_list_
= storage_
[0];
156 last_list_
->size
= 0;
162 last_list_
->RemoveLast();
163 if (last_list_
->IsEmpty() && last_list_index_
> 0) {
165 last_list_
= storage_
[last_list_index_
];
167 // If there are now two empty inner lists, free one of them.
168 if (last_list_index_
+ 2 < storage_
.size())
174 void Erase(PositionInCharAllocator
* position
) {
175 DCHECK_EQ(this, position
->ptr_to_container
);
177 // Update |position| to point to the element after the erased element.
178 InnerList
* list
= storage_
[position
->vector_index
];
179 char* item_iterator
= position
->item_iterator
;
180 if (item_iterator
== list
->LastElement())
181 position
->Increment();
183 list
->Erase(item_iterator
);
184 // TODO(weiliangc): Free the InnerList if it is empty.
188 void InsertBefore(ListContainerHelper::Iterator
* position
, size_t count
) {
192 // If |position| is End(), then append |count| elements at the end. This
193 // will happen to not invalidate any iterators or memory.
194 if (!position
->item_iterator
) {
195 // Set |position| to be the first inserted element.
197 position
->vector_index
= storage_
.size() - 1;
198 position
->item_iterator
= storage_
[position
->vector_index
]->LastElement();
199 // Allocate the rest.
200 for (size_t i
= 1; i
< count
; ++i
)
203 storage_
[position
->vector_index
]->InsertBefore(&position
->item_iterator
,
209 InnerList
* InnerListById(size_t id
) const {
210 DCHECK_LT(id
, storage_
.size());
214 size_t FirstInnerListId() const {
215 // |size_| > 0 means that at least one vector in |storage_| will be
217 DCHECK_GT(size_
, 0u);
219 while (storage_
[id
]->size
== 0)
224 size_t LastInnerListId() const {
225 // |size_| > 0 means that at least one vector in |storage_| will be
227 DCHECK_GT(size_
, 0u);
228 size_t id
= storage_
.size() - 1;
229 while (storage_
[id
]->size
== 0)
234 size_t NumAvailableElementsInLastList() const {
235 return last_list_
->NumElementsAvailable();
239 void AllocateNewList(size_t list_size
) {
240 scoped_ptr
<InnerList
> new_list(new InnerList
);
241 new_list
->capacity
= list_size
;
243 new_list
->step
= element_size_
;
244 new_list
->data
.reset(new char[list_size
* element_size_
]);
245 storage_
.push_back(new_list
.Pass());
248 ScopedPtrVector
<InnerList
> storage_
;
249 const size_t element_size_
;
251 // The number of elements in the list.
254 // The index of the last list to have had elements added to it, or the only
255 // list if the container has not had elements added since being cleared.
256 size_t last_list_index_
;
258 // This is equivalent to |storage_[last_list_index_]|.
259 InnerList
* last_list_
;
261 DISALLOW_COPY_AND_ASSIGN(CharAllocator
);
264 // PositionInCharAllocator
265 //////////////////////////////////////////////////////
266 ListContainerHelper::PositionInCharAllocator::PositionInCharAllocator(
267 const ListContainerHelper::PositionInCharAllocator
& other
)
268 : ptr_to_container(other
.ptr_to_container
),
269 vector_index(other
.vector_index
),
270 item_iterator(other
.item_iterator
) {}
272 ListContainerHelper::PositionInCharAllocator::PositionInCharAllocator(
273 ListContainerHelper::CharAllocator
* container
,
276 : ptr_to_container(container
),
277 vector_index(vector_ind
),
278 item_iterator(item_iter
) {}
280 bool ListContainerHelper::PositionInCharAllocator::operator==(
281 const ListContainerHelper::PositionInCharAllocator
& other
) const {
282 DCHECK_EQ(ptr_to_container
, other
.ptr_to_container
);
283 return vector_index
== other
.vector_index
&&
284 item_iterator
== other
.item_iterator
;
287 bool ListContainerHelper::PositionInCharAllocator::operator!=(
288 const ListContainerHelper::PositionInCharAllocator
& other
) const {
289 return !(*this == other
);
292 ListContainerHelper::PositionInCharAllocator
293 ListContainerHelper::PositionInCharAllocator::Increment() {
294 CharAllocator::InnerList
* list
=
295 ptr_to_container
->InnerListById(vector_index
);
296 if (item_iterator
== list
->LastElement()) {
298 while (vector_index
< ptr_to_container
->list_count()) {
299 if (ptr_to_container
->InnerListById(vector_index
)->size
!= 0)
303 if (vector_index
< ptr_to_container
->list_count())
304 item_iterator
= ptr_to_container
->InnerListById(vector_index
)->Begin();
306 item_iterator
= NULL
;
308 item_iterator
+= list
->step
;
313 ListContainerHelper::PositionInCharAllocator
314 ListContainerHelper::PositionInCharAllocator::ReverseIncrement() {
315 CharAllocator::InnerList
* list
=
316 ptr_to_container
->InnerListById(vector_index
);
317 if (item_iterator
== list
->Begin()) {
319 // Since |vector_index| is unsigned, we compare < list_count() instead of
320 // comparing >= 0, as the variable will wrap around when it goes out of
322 while (vector_index
< ptr_to_container
->list_count()) {
323 if (ptr_to_container
->InnerListById(vector_index
)->size
!= 0)
327 if (vector_index
< ptr_to_container
->list_count()) {
329 ptr_to_container
->InnerListById(vector_index
)->LastElement();
331 item_iterator
= NULL
;
334 item_iterator
-= list
->step
;
339 // ListContainerHelper
340 ////////////////////////////////////////////
341 ListContainerHelper::ListContainerHelper(size_t max_size_for_derived_class
)
342 : data_(new CharAllocator(max_size_for_derived_class
)) {}
344 ListContainerHelper::ListContainerHelper(size_t max_size_for_derived_class
,
345 size_t num_of_elements_to_reserve_for
)
346 : data_(new CharAllocator(max_size_for_derived_class
,
347 num_of_elements_to_reserve_for
)) {}
349 ListContainerHelper::~ListContainerHelper() {}
351 void ListContainerHelper::RemoveLast() {
355 void ListContainerHelper::EraseAndInvalidateAllPointers(
356 ListContainerHelper::Iterator
* position
) {
357 data_
->Erase(position
);
360 void ListContainerHelper::InsertBeforeAndInvalidateAllPointers(
361 ListContainerHelper::Iterator
* position
,
363 data_
->InsertBefore(position
, count
);
366 ListContainerHelper::ConstReverseIterator
ListContainerHelper::crbegin() const {
367 if (data_
->IsEmpty())
370 size_t id
= data_
->LastInnerListId();
371 return ConstReverseIterator(data_
.get(), id
,
372 data_
->InnerListById(id
)->LastElement(), 0);
375 ListContainerHelper::ConstReverseIterator
ListContainerHelper::crend() const {
376 return ConstReverseIterator(data_
.get(), static_cast<size_t>(-1), NULL
,
380 ListContainerHelper::ReverseIterator
ListContainerHelper::rbegin() {
381 if (data_
->IsEmpty())
384 size_t id
= data_
->LastInnerListId();
385 return ReverseIterator(data_
.get(), id
,
386 data_
->InnerListById(id
)->LastElement(), 0);
389 ListContainerHelper::ReverseIterator
ListContainerHelper::rend() {
390 return ReverseIterator(data_
.get(), static_cast<size_t>(-1), NULL
, size());
393 ListContainerHelper::ConstIterator
ListContainerHelper::cbegin() const {
394 if (data_
->IsEmpty())
397 size_t id
= data_
->FirstInnerListId();
398 return ConstIterator(data_
.get(), id
, data_
->InnerListById(id
)->Begin(), 0);
401 ListContainerHelper::ConstIterator
ListContainerHelper::cend() const {
402 if (data_
->IsEmpty())
403 return ConstIterator(data_
.get(), 0, NULL
, size());
405 size_t id
= data_
->list_count();
406 return ConstIterator(data_
.get(), id
, NULL
, size());
409 ListContainerHelper::Iterator
ListContainerHelper::begin() {
410 if (data_
->IsEmpty())
413 size_t id
= data_
->FirstInnerListId();
414 return Iterator(data_
.get(), id
, data_
->InnerListById(id
)->Begin(), 0);
417 ListContainerHelper::Iterator
ListContainerHelper::end() {
418 if (data_
->IsEmpty())
419 return Iterator(data_
.get(), 0, NULL
, size());
421 size_t id
= data_
->list_count();
422 return Iterator(data_
.get(), id
, NULL
, size());
425 ListContainerHelper::ConstIterator
ListContainerHelper::IteratorAt(
426 size_t index
) const {
427 DCHECK_LT(index
, size());
428 size_t original_index
= index
;
430 for (list_index
= 0; list_index
< data_
->list_count(); ++list_index
) {
431 size_t current_size
= data_
->InnerListById(list_index
)->size
;
432 if (index
< current_size
)
434 index
-= current_size
;
436 return ConstIterator(data_
.get(), list_index
,
437 data_
->InnerListById(list_index
)->ElementAt(index
),
441 ListContainerHelper::Iterator
ListContainerHelper::IteratorAt(size_t index
) {
442 DCHECK_LT(index
, size());
443 size_t original_index
= index
;
445 for (list_index
= 0; list_index
< data_
->list_count(); ++list_index
) {
446 size_t current_size
= data_
->InnerListById(list_index
)->size
;
447 if (index
< current_size
)
449 index
-= current_size
;
451 return Iterator(data_
.get(), list_index
,
452 data_
->InnerListById(list_index
)->ElementAt(index
),
456 void* ListContainerHelper::Allocate(size_t size_of_actual_element_in_bytes
) {
457 DCHECK_LE(size_of_actual_element_in_bytes
, data_
->element_size());
458 return data_
->Allocate();
461 size_t ListContainerHelper::size() const {
462 return data_
->size();
465 bool ListContainerHelper::empty() const {
466 return data_
->IsEmpty();
469 size_t ListContainerHelper::MaxSizeForDerivedClass() const {
470 return data_
->element_size();
473 size_t ListContainerHelper::GetCapacityInBytes() const {
474 return data_
->Capacity() * data_
->element_size();
477 void ListContainerHelper::clear() {
481 size_t ListContainerHelper::AvailableSizeWithoutAnotherAllocationForTesting()
483 return data_
->NumAvailableElementsInLastList();
486 // ListContainerHelper::Iterator
487 /////////////////////////////////////////////////
488 ListContainerHelper::Iterator::Iterator(CharAllocator
* container
,
492 : PositionInCharAllocator(container
, vector_ind
, item_iter
),
495 ListContainerHelper::Iterator::~Iterator() {}
497 size_t ListContainerHelper::Iterator::index() const {
501 // ListContainerHelper::ConstIterator
502 /////////////////////////////////////////////////
503 ListContainerHelper::ConstIterator::ConstIterator(
504 const ListContainerHelper::Iterator
& other
)
505 : PositionInCharAllocator(other
), index_(other
.index()) {}
507 ListContainerHelper::ConstIterator::ConstIterator(CharAllocator
* container
,
511 : PositionInCharAllocator(container
, vector_ind
, item_iter
),
514 ListContainerHelper::ConstIterator::~ConstIterator() {}
516 size_t ListContainerHelper::ConstIterator::index() const {
520 // ListContainerHelper::ReverseIterator
521 /////////////////////////////////////////////////
522 ListContainerHelper::ReverseIterator::ReverseIterator(CharAllocator
* container
,
526 : PositionInCharAllocator(container
, vector_ind
, item_iter
),
529 ListContainerHelper::ReverseIterator::~ReverseIterator() {}
531 size_t ListContainerHelper::ReverseIterator::index() const {
535 // ListContainerHelper::ConstReverseIterator
536 /////////////////////////////////////////////////
537 ListContainerHelper::ConstReverseIterator::ConstReverseIterator(
538 const ListContainerHelper::ReverseIterator
& other
)
539 : PositionInCharAllocator(other
), index_(other
.index()) {}
541 ListContainerHelper::ConstReverseIterator::ConstReverseIterator(
542 CharAllocator
* container
,
546 : PositionInCharAllocator(container
, vector_ind
, item_iter
),
549 ListContainerHelper::ConstReverseIterator::~ConstReverseIterator() {}
551 size_t ListContainerHelper::ConstReverseIterator::index() const {