[GCM] Investigatory CHECKs for crash in parsing stream
[chromium-blink-merge.git] / cc / quads / list_container.h
blob34a30aadd6e74c2403fce5d339d61b9f00c428e6
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 #ifndef CC_QUADS_LIST_CONTAINER_H_
6 #define CC_QUADS_LIST_CONTAINER_H_
8 #include "base/macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "cc/base/cc_export.h"
12 namespace cc {
13 class SharedQuadState;
14 class DrawQuad;
16 // This class is a container type that handles allocating contiguous memory for
17 // new elements and traversing through elements with either iterator or reverse
18 // iterator. Since this container hands out raw pointers of its elements, it is
19 // very important that this container never reallocate its memory so those raw
20 // pointer will continue to be valid. This class is used to contain
21 // SharedQuadState or DrawQuad. Since the size of each DrawQuad varies, to hold
22 // DrawQuads, the allocations size of each element in this class is
23 // kLargestDrawQuad while BaseElementType is DrawQuad.
24 template <class BaseElementType>
25 class CC_EXPORT ListContainer {
26 public:
27 // BaseElementType is the type of raw pointers this class hands out; however,
28 // its derived classes might require different memory sizes.
29 // max_size_for_derived_class the largest memory size required for all the
30 // derived classes to use for allocation.
31 explicit ListContainer(size_t max_size_for_derived_class);
32 // This constructor omits input variable for max_size_for_derived_class. This
33 // is used when there is no derived classes from BaseElementType we need to
34 // worry about, and allocation size is just sizeof(BaseElementType).
35 ListContainer();
36 // This constructor reserves the requested memory up front so only a single
37 // allocation is needed.
38 ListContainer(size_t max_size_for_derived_class,
39 size_t num_of_elements_to_reserve_for);
41 ~ListContainer();
43 // This class deals only with char* and void*. It does allocation and passing
44 // out raw pointers, as well as memory deallocation when being destroyed.
45 class CC_EXPORT ListContainerCharAllocator;
47 // This class points to a certain position inside memory of
48 // ListContainerCharAllocator. It is a base class for ListContainer iterators.
49 struct CC_EXPORT PositionInListContainerCharAllocator {
50 ListContainerCharAllocator* ptr_to_container;
51 size_t vector_index;
52 char* item_iterator;
54 PositionInListContainerCharAllocator(
55 const PositionInListContainerCharAllocator& other);
57 PositionInListContainerCharAllocator(ListContainerCharAllocator* container,
58 size_t vector_ind,
59 char* item_iter);
61 bool operator==(const PositionInListContainerCharAllocator& other) const;
62 bool operator!=(const PositionInListContainerCharAllocator& other) const;
64 PositionInListContainerCharAllocator Increment();
65 PositionInListContainerCharAllocator ReverseIncrement();
68 // Iterator classes that can be used to access data.
69 /////////////////////////////////////////////////////////////////
70 class CC_EXPORT Iterator : public PositionInListContainerCharAllocator {
71 // This class is only defined to forward iterate through
72 // ListContainerCharAllocator.
73 public:
74 Iterator(ListContainerCharAllocator* container,
75 size_t vector_ind,
76 char* item_iter);
77 ~Iterator();
78 BaseElementType* operator->() const;
79 BaseElementType& operator*() const;
80 Iterator operator++(int unused_post_increment);
81 Iterator operator++();
84 class CC_EXPORT ConstIterator : public PositionInListContainerCharAllocator {
85 // This class is only defined to forward iterate through
86 // ListContainerCharAllocator.
87 public:
88 ConstIterator(ListContainerCharAllocator* container,
89 size_t vector_ind,
90 char* item_iter);
91 ConstIterator(const Iterator& other); // NOLINT
92 ~ConstIterator();
93 const BaseElementType* operator->() const;
94 const BaseElementType& operator*() const;
95 ConstIterator operator++(int unused_post_increment);
96 ConstIterator operator++();
99 class CC_EXPORT ReverseIterator
100 : public PositionInListContainerCharAllocator {
101 // This class is only defined to reverse iterate through
102 // ListContainerCharAllocator.
103 public:
104 ReverseIterator(ListContainerCharAllocator* container,
105 size_t vector_ind,
106 char* item_iter);
107 ~ReverseIterator();
108 BaseElementType* operator->() const;
109 BaseElementType& operator*() const;
110 ReverseIterator operator++(int unused_post_increment);
111 ReverseIterator operator++();
114 class CC_EXPORT ConstReverseIterator
115 : public PositionInListContainerCharAllocator {
116 // This class is only defined to reverse iterate through
117 // ListContainerCharAllocator.
118 public:
119 ConstReverseIterator(ListContainerCharAllocator* container,
120 size_t vector_ind,
121 char* item_iter);
122 ConstReverseIterator(const ReverseIterator& other); // NOLINT
123 ~ConstReverseIterator();
124 const BaseElementType* operator->() const;
125 const BaseElementType& operator*() const;
126 ConstReverseIterator operator++(int unused_post_increment);
127 ConstReverseIterator operator++();
130 // When called, all raw pointers that have been handed out are no longer
131 // valid. Use with caution.
132 // This function does not deallocate memory.
133 void EraseAndInvalidateAllPointers(Iterator position);
135 ConstReverseIterator rbegin() const;
136 ConstReverseIterator rend() const;
137 ReverseIterator rbegin();
138 ReverseIterator rend();
139 ConstIterator begin() const;
140 ConstIterator end() const;
141 Iterator begin();
142 Iterator end();
144 BaseElementType* front();
145 BaseElementType* back();
146 const BaseElementType* front() const;
147 const BaseElementType* back() const;
149 // Take in derived element type and construct it at location generated by
150 // Allocate().
151 template <typename DerivedElementType>
152 DerivedElementType* AllocateAndConstruct() {
153 DerivedElementType* result =
154 new (Allocate(sizeof(DerivedElementType))) DerivedElementType;
155 return result;
158 size_t size() const;
159 bool empty() const;
160 void clear();
162 size_t AvailableSizeWithoutAnotherAllocationForTesting() const;
164 private:
165 // Hands out memory location for an element at the end of data structure.
166 BaseElementType* Allocate(size_t size_of_actual_element_in_bytes);
168 scoped_ptr<ListContainerCharAllocator> data_;
170 DISALLOW_COPY_AND_ASSIGN(ListContainer);
173 #if !defined(COMPILER_MSVC)
174 extern template class ListContainer<SharedQuadState>;
175 extern template class ListContainer<DrawQuad>;
176 #endif
177 } // namespace cc
179 #endif // CC_QUADS_LIST_CONTAINER_H_