| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef CC_BASE_SIDECAR_LIST_CONTAINER_H_ | 5 #ifndef CC_BASE_SIDECAR_LIST_CONTAINER_H_ |
| 6 #define CC_BASE_SIDECAR_LIST_CONTAINER_H_ | 6 #define CC_BASE_SIDECAR_LIST_CONTAINER_H_ |
| 7 | 7 |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "cc/base/list_container.h" | 9 #include "cc/base/list_container.h" |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 // | 22 // |
| 23 // TODO(jbroman): It would be nice to be clear about the memory alignment | 23 // TODO(jbroman): It would be nice to be clear about the memory alignment |
| 24 // constraints here, but that probably requires closer inspection of | 24 // constraints here, but that probably requires closer inspection of |
| 25 // ListContainer first. | 25 // ListContainer first. |
| 26 template <class BaseElementType> | 26 template <class BaseElementType> |
| 27 class SidecarListContainer { | 27 class SidecarListContainer { |
| 28 public: | 28 public: |
| 29 using SidecarDestroyer = void (*)(void* sidecar); | 29 using SidecarDestroyer = void (*)(void* sidecar); |
| 30 using Iterator = typename ListContainer<BaseElementType>::Iterator; | 30 using Iterator = typename ListContainer<BaseElementType>::Iterator; |
| 31 using ConstIterator = typename ListContainer<BaseElementType>::ConstIterator; | 31 using ConstIterator = typename ListContainer<BaseElementType>::ConstIterator; |
| 32 using ReverseIterator = |
| 33 typename ListContainer<BaseElementType>::ReverseIterator; |
| 34 using ConstReverseIterator = |
| 35 typename ListContainer<BaseElementType>::ConstReverseIterator; |
| 32 | 36 |
| 33 explicit SidecarListContainer(size_t max_size_for_derived_class, | 37 explicit SidecarListContainer(size_t max_size_for_derived_class, |
| 34 size_t max_size_for_sidecar, | 38 size_t max_size_for_sidecar, |
| 35 size_t num_of_elements_to_reserve_for, | 39 size_t num_of_elements_to_reserve_for, |
| 36 SidecarDestroyer destroyer) | 40 SidecarDestroyer destroyer) |
| 37 : list_(max_size_for_derived_class + max_size_for_sidecar, | 41 : list_(max_size_for_derived_class + max_size_for_sidecar, |
| 38 num_of_elements_to_reserve_for), | 42 num_of_elements_to_reserve_for), |
| 39 destroyer_(destroyer), | 43 destroyer_(destroyer), |
| 40 sidecar_offset_(max_size_for_derived_class) {} | 44 sidecar_offset_(max_size_for_derived_class) {} |
| 41 ~SidecarListContainer() { DestroyAllSidecars(); } | 45 ~SidecarListContainer() { DestroyAllSidecars(); } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 53 template <typename DerivedElementType> | 57 template <typename DerivedElementType> |
| 54 DerivedElementType* AllocateAndCopyFrom(const DerivedElementType* source) { | 58 DerivedElementType* AllocateAndCopyFrom(const DerivedElementType* source) { |
| 55 return list_.template AllocateAndCopyFrom<DerivedElementType>(source); | 59 return list_.template AllocateAndCopyFrom<DerivedElementType>(source); |
| 56 } | 60 } |
| 57 | 61 |
| 58 void clear() { | 62 void clear() { |
| 59 DestroyAllSidecars(); | 63 DestroyAllSidecars(); |
| 60 list_.clear(); | 64 list_.clear(); |
| 61 } | 65 } |
| 62 | 66 |
| 67 void RemoveLast() { |
| 68 destroyer_(GetSidecar(*list_.rbegin())); |
| 69 list_.RemoveLast(); |
| 70 } |
| 71 |
| 63 // This permits a client to exchange a pointer to an element to a pointer to | 72 // This permits a client to exchange a pointer to an element to a pointer to |
| 64 // its corresponding sidecar. | 73 // its corresponding sidecar. |
| 65 void* GetSidecar(BaseElementType* element) { | 74 void* GetSidecar(BaseElementType* element) { |
| 66 DCHECK_GT(sidecar_offset_, 0u); | 75 DCHECK_GT(sidecar_offset_, 0u); |
| 67 return reinterpret_cast<char*>(element) + sidecar_offset_; | 76 return reinterpret_cast<char*>(element) + sidecar_offset_; |
| 68 } | 77 } |
| 69 const void* GetSidecar(const BaseElementType* element) { | 78 const void* GetSidecar(const BaseElementType* element) { |
| 70 DCHECK_GT(sidecar_offset_, 0u); | 79 DCHECK_GT(sidecar_offset_, 0u); |
| 71 return reinterpret_cast<const char*>(element) + sidecar_offset_; | 80 return reinterpret_cast<const char*>(element) + sidecar_offset_; |
| 72 } | 81 } |
| 73 | 82 |
| 74 private: | 83 private: |
| 75 void DestroyAllSidecars() { | 84 void DestroyAllSidecars() { |
| 76 for (auto* element : list_) | 85 for (auto* element : list_) |
| 77 destroyer_(GetSidecar(element)); | 86 destroyer_(GetSidecar(element)); |
| 78 } | 87 } |
| 79 | 88 |
| 80 ListContainer<BaseElementType> list_; | 89 ListContainer<BaseElementType> list_; |
| 81 SidecarDestroyer destroyer_; | 90 SidecarDestroyer destroyer_; |
| 82 size_t sidecar_offset_; | 91 size_t sidecar_offset_; |
| 83 }; | 92 }; |
| 84 | 93 |
| 85 } // namespace cc | 94 } // namespace cc |
| 86 | 95 |
| 87 #endif // CC_BASE_SIDECAR_LIST_CONTAINER_H_ | 96 #endif // CC_BASE_SIDECAR_LIST_CONTAINER_H_ |
| OLD | NEW |