Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: cc/quads/list_container.cc

Issue 551013002: Use Custome ListContainer to Allocate SharedQuadState (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@DQAllo
Patch Set: use C++ range based loop Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/quads/list_container.h" 5 #include "cc/quads/list_container.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "cc/base/scoped_ptr_vector.h" 10 #include "cc/base/scoped_ptr_vector.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 list_count_(0), 80 list_count_(0),
81 last_list_(NULL) { 81 last_list_(NULL) {
82 AllocateNewList(kDefaultNumElementTypesToReserve); 82 AllocateNewList(kDefaultNumElementTypesToReserve);
83 } 83 }
84 84
85 ListContainerCharAllocator(size_t element_size, size_t element_count) 85 ListContainerCharAllocator(size_t element_size, size_t element_count)
86 : element_size_(element_size), 86 : element_size_(element_size),
87 size_(0), 87 size_(0),
88 list_count_(0), 88 list_count_(0),
89 last_list_(NULL) { 89 last_list_(NULL) {
90 DCHECK_NE(0u, element_count); 90 AllocateNewList(element_count > 0 ? element_count
91 AllocateNewList(element_count); 91 : kDefaultNumElementTypesToReserve);
92 } 92 }
93 93
94 ~ListContainerCharAllocator() {} 94 ~ListContainerCharAllocator() {}
95 95
96 void* Allocate() { 96 void* Allocate() {
97 if (last_list_->IsFull()) 97 if (last_list_->IsFull())
98 AllocateNewList(last_list_->capacity * 2); 98 AllocateNewList(last_list_->capacity * 2);
99 99
100 ++size_; 100 ++size_;
101 return last_list_->AddElement(); 101 return last_list_->AddElement();
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
274 typename ListContainer<BaseElementType>::Iterator position) { 274 typename ListContainer<BaseElementType>::Iterator position) {
275 BaseElementType* item = &*position; 275 BaseElementType* item = &*position;
276 item->~BaseElementType(); 276 item->~BaseElementType();
277 data_->Erase(position); 277 data_->Erase(position);
278 } 278 }
279 279
280 template <typename BaseElementType> 280 template <typename BaseElementType>
281 typename ListContainer<BaseElementType>::ConstReverseIterator 281 typename ListContainer<BaseElementType>::ConstReverseIterator
282 ListContainer<BaseElementType>::rbegin() const { 282 ListContainer<BaseElementType>::rbegin() const {
283 if (data_->IsEmpty()) 283 if (data_->IsEmpty())
284 return ConstReverseIterator(data_.get(), 0, NULL); 284 return ConstReverseIterator(data_.get(), 0, NULL, 0);
285 285
286 size_t last_id = data_->list_count() - 1; 286 size_t last_id = data_->list_count() - 1;
287 return ConstReverseIterator( 287 return ConstReverseIterator(
288 data_.get(), last_id, data_->InnerListById(last_id)->LastElement()); 288 data_.get(), last_id, data_->InnerListById(last_id)->LastElement(), 0);
289 } 289 }
290 290
291 template <typename BaseElementType> 291 template <typename BaseElementType>
292 typename ListContainer<BaseElementType>::ConstReverseIterator 292 typename ListContainer<BaseElementType>::ConstReverseIterator
293 ListContainer<BaseElementType>::rend() const { 293 ListContainer<BaseElementType>::rend() const {
294 return ConstReverseIterator(data_.get(), 0, NULL); 294 return ConstReverseIterator(data_.get(), 0, NULL, size());
295 } 295 }
296 296
297 template <typename BaseElementType> 297 template <typename BaseElementType>
298 typename ListContainer<BaseElementType>::ReverseIterator 298 typename ListContainer<BaseElementType>::ReverseIterator
299 ListContainer<BaseElementType>::rbegin() { 299 ListContainer<BaseElementType>::rbegin() {
300 if (data_->IsEmpty()) 300 if (data_->IsEmpty())
301 return ReverseIterator(data_.get(), 0, NULL); 301 return ReverseIterator(data_.get(), 0, NULL, 0);
302 302
303 size_t last_id = data_->list_count() - 1; 303 size_t last_id = data_->list_count() - 1;
304 return ReverseIterator( 304 return ReverseIterator(
305 data_.get(), last_id, data_->InnerListById(last_id)->LastElement()); 305 data_.get(), last_id, data_->InnerListById(last_id)->LastElement(), 0);
306 } 306 }
307 307
308 template <typename BaseElementType> 308 template <typename BaseElementType>
309 typename ListContainer<BaseElementType>::ReverseIterator 309 typename ListContainer<BaseElementType>::ReverseIterator
310 ListContainer<BaseElementType>::rend() { 310 ListContainer<BaseElementType>::rend() {
311 return ReverseIterator(data_.get(), 0, NULL); 311 return ReverseIterator(data_.get(), 0, NULL, size());
312 } 312 }
313 313
314 template <typename BaseElementType> 314 template <typename BaseElementType>
315 typename ListContainer<BaseElementType>::ConstIterator 315 typename ListContainer<BaseElementType>::ConstIterator
316 ListContainer<BaseElementType>::begin() const { 316 ListContainer<BaseElementType>::begin() const {
317 if (data_->IsEmpty()) 317 if (data_->IsEmpty())
318 return ConstIterator(data_.get(), 0, NULL); 318 return ConstIterator(data_.get(), 0, NULL, 0);
319 319
320 return ConstIterator(data_.get(), 0, data_->InnerListById(0)->Begin()); 320 return ConstIterator(data_.get(), 0, data_->InnerListById(0)->Begin(), 0);
321 } 321 }
322 322
323 template <typename BaseElementType> 323 template <typename BaseElementType>
324 typename ListContainer<BaseElementType>::ConstIterator 324 typename ListContainer<BaseElementType>::ConstIterator
325 ListContainer<BaseElementType>::end() const { 325 ListContainer<BaseElementType>::end() const {
326 if (data_->IsEmpty()) 326 if (data_->IsEmpty())
327 return ConstIterator(data_.get(), 0, NULL); 327 return ConstIterator(data_.get(), 0, NULL, size());
328 328
329 size_t last_id = data_->list_count() - 1; 329 size_t last_id = data_->list_count() - 1;
330 return ConstIterator(data_.get(), last_id, NULL); 330 return ConstIterator(data_.get(), last_id, NULL, size());
331 } 331 }
332 332
333 template <typename BaseElementType> 333 template <typename BaseElementType>
334 typename ListContainer<BaseElementType>::Iterator 334 typename ListContainer<BaseElementType>::Iterator
335 ListContainer<BaseElementType>::begin() { 335 ListContainer<BaseElementType>::begin() {
336 if (data_->IsEmpty()) 336 if (data_->IsEmpty())
337 return Iterator(data_.get(), 0, NULL); 337 return Iterator(data_.get(), 0, NULL, 0);
338 338
339 return Iterator(data_.get(), 0, data_->InnerListById(0)->Begin()); 339 return Iterator(data_.get(), 0, data_->InnerListById(0)->Begin(), 0);
340 } 340 }
341 341
342 template <typename BaseElementType> 342 template <typename BaseElementType>
343 typename ListContainer<BaseElementType>::Iterator 343 typename ListContainer<BaseElementType>::Iterator
344 ListContainer<BaseElementType>::end() { 344 ListContainer<BaseElementType>::end() {
345 if (data_->IsEmpty()) 345 if (data_->IsEmpty())
346 return Iterator(data_.get(), 0, NULL); 346 return Iterator(data_.get(), 0, NULL, size());
347 347
348 size_t last_id = data_->list_count() - 1; 348 size_t last_id = data_->list_count() - 1;
349 return Iterator(data_.get(), last_id, NULL); 349 return Iterator(data_.get(), last_id, NULL, size());
350 } 350 }
351 351
352 template <typename BaseElementType> 352 template <typename BaseElementType>
353 BaseElementType* ListContainer<BaseElementType>::front() { 353 BaseElementType* ListContainer<BaseElementType>::front() {
354 Iterator iter = begin(); 354 Iterator iter = begin();
355 return &*iter; 355 return &*iter;
356 } 356 }
357 357
358 template <typename BaseElementType> 358 template <typename BaseElementType>
359 BaseElementType* ListContainer<BaseElementType>::back() { 359 BaseElementType* ListContainer<BaseElementType>::back() {
(...skipping 10 matching lines...) Expand all
370 template <typename BaseElementType> 370 template <typename BaseElementType>
371 const BaseElementType* ListContainer<BaseElementType>::back() const { 371 const BaseElementType* ListContainer<BaseElementType>::back() const {
372 ConstReverseIterator iter = rbegin(); 372 ConstReverseIterator iter = rbegin();
373 return &*iter; 373 return &*iter;
374 } 374 }
375 375
376 template <typename BaseElementType> 376 template <typename BaseElementType>
377 const BaseElementType* ListContainer<BaseElementType>::ElementAt( 377 const BaseElementType* ListContainer<BaseElementType>::ElementAt(
378 size_t index) const { 378 size_t index) const {
379 DCHECK_LT(index, size()); 379 DCHECK_LT(index, size());
380 size_t cache_index = index;
danakj 2014/10/02 15:32:47 s/cache/original/
weiliangc 2014/10/02 22:01:39 Done.
380 size_t list_index; 381 size_t list_index;
381 for (list_index = 0; list_index < data_->list_count(); ++list_index) { 382 for (list_index = 0; list_index < data_->list_count(); ++list_index) {
382 size_t current_size = data_->InnerListById(list_index)->size; 383 size_t current_size = data_->InnerListById(list_index)->size;
383 if (index < current_size) 384 if (index < current_size)
384 break; 385 break;
385 index -= current_size; 386 index -= current_size;
386 } 387 }
387 return &*ConstIterator(data_.get(), 388 return &*ConstIterator(data_.get(),
388 list_index, 389 list_index,
389 data_->InnerListById(list_index)->ElementAt(index)); 390 data_->InnerListById(list_index)->ElementAt(index),
391 cache_index);
390 } 392 }
391 393
392 template <typename BaseElementType> 394 template <typename BaseElementType>
393 BaseElementType* ListContainer<BaseElementType>::ElementAt(size_t index) { 395 BaseElementType* ListContainer<BaseElementType>::ElementAt(size_t index) {
394 DCHECK_LT(index, size()); 396 DCHECK_LT(index, size());
397 size_t cache_index = index;
danakj 2014/10/02 15:32:47 same
weiliangc 2014/10/02 22:01:39 Done.
395 size_t list_index; 398 size_t list_index;
396 for (list_index = 0; list_index < data_->list_count(); ++list_index) { 399 for (list_index = 0; list_index < data_->list_count(); ++list_index) {
397 size_t current_size = data_->InnerListById(list_index)->size; 400 size_t current_size = data_->InnerListById(list_index)->size;
398 if (index < current_size) 401 if (index < current_size)
399 break; 402 break;
400 index -= current_size; 403 index -= current_size;
401 } 404 }
402 return &*Iterator(data_.get(), 405 return &*Iterator(data_.get(),
403 list_index, 406 list_index,
404 data_->InnerListById(list_index)->ElementAt(index)); 407 data_->InnerListById(list_index)->ElementAt(index),
408 cache_index);
405 } 409 }
406 410
407 template <typename BaseElementType> 411 template <typename BaseElementType>
408 BaseElementType* ListContainer<BaseElementType>::Allocate( 412 BaseElementType* ListContainer<BaseElementType>::Allocate(
409 size_t size_of_actual_element_in_bytes) { 413 size_t size_of_actual_element_in_bytes) {
410 DCHECK_LE(size_of_actual_element_in_bytes, data_->element_size()); 414 DCHECK_LE(size_of_actual_element_in_bytes, data_->element_size());
411 void* result = data_->Allocate(); 415 void* result = data_->Allocate();
412 return static_cast<BaseElementType*>(result); 416 return static_cast<BaseElementType*>(result);
413 } 417 }
414 418
(...skipping 20 matching lines...) Expand all
435 BaseElementType>::AvailableSizeWithoutAnotherAllocationForTesting() const { 439 BaseElementType>::AvailableSizeWithoutAnotherAllocationForTesting() const {
436 return data_->NumAvailableElementsInLastList(); 440 return data_->NumAvailableElementsInLastList();
437 } 441 }
438 442
439 // ListContainer::Iterator 443 // ListContainer::Iterator
440 ///////////////////////////////////////////////// 444 /////////////////////////////////////////////////
441 template <typename BaseElementType> 445 template <typename BaseElementType>
442 ListContainer<BaseElementType>::Iterator::Iterator( 446 ListContainer<BaseElementType>::Iterator::Iterator(
443 ListContainerCharAllocator* container, 447 ListContainerCharAllocator* container,
444 size_t vector_ind, 448 size_t vector_ind,
445 char* item_iter) 449 char* item_iter,
446 : PositionInListContainerCharAllocator(container, vector_ind, item_iter) { 450 size_t index)
451 : PositionInListContainerCharAllocator(container, vector_ind, item_iter),
452 index_(index) {
447 } 453 }
448 454
449 template <typename BaseElementType> 455 template <typename BaseElementType>
450 ListContainer<BaseElementType>::Iterator::~Iterator() { 456 ListContainer<BaseElementType>::Iterator::~Iterator() {
451 } 457 }
452 458
453 template <typename BaseElementType> 459 template <typename BaseElementType>
454 BaseElementType* ListContainer<BaseElementType>::Iterator::operator->() const { 460 BaseElementType* ListContainer<BaseElementType>::Iterator::operator->() const {
455 return reinterpret_cast<BaseElementType*>(this->item_iterator); 461 return reinterpret_cast<BaseElementType*>(this->item_iterator);
456 } 462 }
(...skipping 10 matching lines...) Expand all
467 Iterator tmp = *this; 473 Iterator tmp = *this;
468 operator++(); 474 operator++();
469 return tmp; 475 return tmp;
470 } 476 }
471 477
472 template <typename BaseElementType> 478 template <typename BaseElementType>
473 typename ListContainer<BaseElementType>::Iterator 479 typename ListContainer<BaseElementType>::Iterator
474 ListContainer<BaseElementType>::Iterator:: 480 ListContainer<BaseElementType>::Iterator::
475 operator++() { 481 operator++() {
476 this->Increment(); 482 this->Increment();
483 ++index_;
477 return *this; 484 return *this;
478 } 485 }
479 486
487 template <typename BaseElementType>
488 size_t ListContainer<BaseElementType>::Iterator::index() const {
489 return index_;
490 }
491
480 // ListContainer::ConstIterator 492 // ListContainer::ConstIterator
481 ///////////////////////////////////////////////// 493 /////////////////////////////////////////////////
482 template <typename BaseElementType> 494 template <typename BaseElementType>
483 ListContainer<BaseElementType>::ConstIterator::ConstIterator( 495 ListContainer<BaseElementType>::ConstIterator::ConstIterator(
484 const typename ListContainer<BaseElementType>::Iterator& other) 496 const typename ListContainer<BaseElementType>::Iterator& other)
485 : PositionInListContainerCharAllocator(other) { 497 : PositionInListContainerCharAllocator(other), index_(other.index()) {
486 } 498 }
487 499
488 template <typename BaseElementType> 500 template <typename BaseElementType>
489 ListContainer<BaseElementType>::ConstIterator::ConstIterator( 501 ListContainer<BaseElementType>::ConstIterator::ConstIterator(
490 ListContainerCharAllocator* container, 502 ListContainerCharAllocator* container,
491 size_t vector_ind, 503 size_t vector_ind,
492 char* item_iter) 504 char* item_iter,
493 : PositionInListContainerCharAllocator(container, vector_ind, item_iter) { 505 size_t index)
506 : PositionInListContainerCharAllocator(container, vector_ind, item_iter),
507 index_(index) {
494 } 508 }
495 509
496 template <typename BaseElementType> 510 template <typename BaseElementType>
497 ListContainer<BaseElementType>::ConstIterator::~ConstIterator() { 511 ListContainer<BaseElementType>::ConstIterator::~ConstIterator() {
498 } 512 }
499 513
500 template <typename BaseElementType> 514 template <typename BaseElementType>
501 const BaseElementType* ListContainer<BaseElementType>::ConstIterator:: 515 const BaseElementType* ListContainer<BaseElementType>::ConstIterator::
502 operator->() const { 516 operator->() const {
503 return reinterpret_cast<const BaseElementType*>(this->item_iterator); 517 return reinterpret_cast<const BaseElementType*>(this->item_iterator);
(...skipping 12 matching lines...) Expand all
516 ConstIterator tmp = *this; 530 ConstIterator tmp = *this;
517 operator++(); 531 operator++();
518 return tmp; 532 return tmp;
519 } 533 }
520 534
521 template <typename BaseElementType> 535 template <typename BaseElementType>
522 typename ListContainer<BaseElementType>::ConstIterator 536 typename ListContainer<BaseElementType>::ConstIterator
523 ListContainer<BaseElementType>::ConstIterator:: 537 ListContainer<BaseElementType>::ConstIterator::
524 operator++() { 538 operator++() {
525 this->Increment(); 539 this->Increment();
540 ++index_;
526 return *this; 541 return *this;
527 } 542 }
528 543
544 template <typename BaseElementType>
545 size_t ListContainer<BaseElementType>::ConstIterator::index() const {
546 return index_;
547 }
548
529 // ListContainer::ReverseIterator 549 // ListContainer::ReverseIterator
530 ///////////////////////////////////////////////// 550 /////////////////////////////////////////////////
531 template <typename BaseElementType> 551 template <typename BaseElementType>
532 ListContainer<BaseElementType>::ReverseIterator::ReverseIterator( 552 ListContainer<BaseElementType>::ReverseIterator::ReverseIterator(
533 ListContainerCharAllocator* container, 553 ListContainerCharAllocator* container,
534 size_t vector_ind, 554 size_t vector_ind,
535 char* item_iter) 555 char* item_iter,
536 : PositionInListContainerCharAllocator(container, vector_ind, item_iter) { 556 size_t index)
557 : PositionInListContainerCharAllocator(container, vector_ind, item_iter),
558 index_(index) {
537 } 559 }
538 560
539 template <typename BaseElementType> 561 template <typename BaseElementType>
540 ListContainer<BaseElementType>::ReverseIterator::~ReverseIterator() { 562 ListContainer<BaseElementType>::ReverseIterator::~ReverseIterator() {
541 } 563 }
542 564
543 template <typename BaseElementType> 565 template <typename BaseElementType>
544 BaseElementType* ListContainer<BaseElementType>::ReverseIterator::operator->() 566 BaseElementType* ListContainer<BaseElementType>::ReverseIterator::operator->()
545 const { 567 const {
546 return reinterpret_cast<BaseElementType*>(this->item_iterator); 568 return reinterpret_cast<BaseElementType*>(this->item_iterator);
(...skipping 12 matching lines...) Expand all
559 ReverseIterator tmp = *this; 581 ReverseIterator tmp = *this;
560 operator++(); 582 operator++();
561 return tmp; 583 return tmp;
562 } 584 }
563 585
564 template <typename BaseElementType> 586 template <typename BaseElementType>
565 typename ListContainer<BaseElementType>::ReverseIterator 587 typename ListContainer<BaseElementType>::ReverseIterator
566 ListContainer<BaseElementType>::ReverseIterator:: 588 ListContainer<BaseElementType>::ReverseIterator::
567 operator++() { 589 operator++() {
568 this->ReverseIncrement(); 590 this->ReverseIncrement();
591 ++index_;
569 return *this; 592 return *this;
570 } 593 }
571 594
595 template <typename BaseElementType>
596 size_t ListContainer<BaseElementType>::ReverseIterator::index() const {
597 return index_;
598 }
599
572 // ListContainer::ConstReverseIterator 600 // ListContainer::ConstReverseIterator
573 ///////////////////////////////////////////////// 601 /////////////////////////////////////////////////
574 template <typename BaseElementType> 602 template <typename BaseElementType>
575 ListContainer<BaseElementType>::ConstReverseIterator::ConstReverseIterator( 603 ListContainer<BaseElementType>::ConstReverseIterator::ConstReverseIterator(
576 const typename ListContainer<BaseElementType>::ReverseIterator& other) 604 const typename ListContainer<BaseElementType>::ReverseIterator& other)
577 : PositionInListContainerCharAllocator(other) { 605 : PositionInListContainerCharAllocator(other), index_(other.index()) {
578 } 606 }
579 607
580 template <typename BaseElementType> 608 template <typename BaseElementType>
581 ListContainer<BaseElementType>::ConstReverseIterator::ConstReverseIterator( 609 ListContainer<BaseElementType>::ConstReverseIterator::ConstReverseIterator(
582 ListContainerCharAllocator* container, 610 ListContainerCharAllocator* container,
583 size_t vector_ind, 611 size_t vector_ind,
584 char* item_iter) 612 char* item_iter,
585 : PositionInListContainerCharAllocator(container, vector_ind, item_iter) { 613 size_t index)
614 : PositionInListContainerCharAllocator(container, vector_ind, item_iter),
615 index_(index) {
586 } 616 }
587 617
588 template <typename BaseElementType> 618 template <typename BaseElementType>
589 ListContainer<BaseElementType>::ConstReverseIterator::~ConstReverseIterator() { 619 ListContainer<BaseElementType>::ConstReverseIterator::~ConstReverseIterator() {
590 } 620 }
591 621
592 template <typename BaseElementType> 622 template <typename BaseElementType>
593 const BaseElementType* ListContainer<BaseElementType>::ConstReverseIterator:: 623 const BaseElementType* ListContainer<BaseElementType>::ConstReverseIterator::
594 operator->() const { 624 operator->() const {
595 return reinterpret_cast<const BaseElementType*>(this->item_iterator); 625 return reinterpret_cast<const BaseElementType*>(this->item_iterator);
(...skipping 12 matching lines...) Expand all
608 ConstReverseIterator tmp = *this; 638 ConstReverseIterator tmp = *this;
609 operator++(); 639 operator++();
610 return tmp; 640 return tmp;
611 } 641 }
612 642
613 template <typename BaseElementType> 643 template <typename BaseElementType>
614 typename ListContainer<BaseElementType>::ConstReverseIterator 644 typename ListContainer<BaseElementType>::ConstReverseIterator
615 ListContainer<BaseElementType>::ConstReverseIterator:: 645 ListContainer<BaseElementType>::ConstReverseIterator::
616 operator++() { 646 operator++() {
617 this->ReverseIncrement(); 647 this->ReverseIncrement();
648 ++index_;
618 return *this; 649 return *this;
619 } 650 }
620 651
652 template <typename BaseElementType>
653 size_t ListContainer<BaseElementType>::ConstReverseIterator::index() const {
654 return index_;
655 }
656
621 template class ListContainer<SharedQuadState>; 657 template class ListContainer<SharedQuadState>;
622 template class ListContainer<DrawQuad>; 658 template class ListContainer<DrawQuad>;
623 659
624 } // namespace cc 660 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698