GCVector.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef js_GCVector_h
  6. #define js_GCVector_h
  7. #include "mozilla/Vector.h"
  8. #include "js/GCPolicyAPI.h"
  9. #include "js/RootingAPI.h"
  10. #include "js/TracingAPI.h"
  11. #include "js/Vector.h"
  12. namespace JS {
  13. // A GCVector is a Vector with an additional trace method that knows how
  14. // to visit all of the items stored in the Vector. For vectors that contain GC
  15. // things, this is usually more convenient than manually iterating and marking
  16. // the contents.
  17. //
  18. // Most types of GC pointers as keys and values can be traced with no extra
  19. // infrastructure. For structs and non-gc-pointer members, ensure that there is
  20. // a specialization of GCPolicy<T> with an appropriate trace method available
  21. // to handle the custom type. Generic helpers can be found in
  22. // js/public/TracingAPI.h.
  23. //
  24. // Note that although this Vector's trace will deal correctly with moved items,
  25. // it does not itself know when to barrier or trace items. To function properly
  26. // it must either be used with Rooted, or barriered and traced manually.
  27. template <typename T,
  28. size_t MinInlineCapacity = 0,
  29. typename AllocPolicy = js::TempAllocPolicy>
  30. class GCVector
  31. {
  32. mozilla::Vector<T, MinInlineCapacity, AllocPolicy> vector;
  33. public:
  34. explicit GCVector(AllocPolicy alloc = AllocPolicy())
  35. : vector(alloc)
  36. {}
  37. GCVector(GCVector&& vec)
  38. : vector(mozilla::Move(vec.vector))
  39. {}
  40. GCVector& operator=(GCVector&& vec) {
  41. vector = mozilla::Move(vec.vector);
  42. return *this;
  43. }
  44. size_t length() const { return vector.length(); }
  45. bool empty() const { return vector.empty(); }
  46. size_t capacity() const { return vector.capacity(); }
  47. T* begin() { return vector.begin(); }
  48. const T* begin() const { return vector.begin(); }
  49. T* end() { return vector.end(); }
  50. const T* end() const { return vector.end(); }
  51. T& operator[](size_t i) { return vector[i]; }
  52. const T& operator[](size_t i) const { return vector[i]; }
  53. T& back() { return vector.back(); }
  54. const T& back() const { return vector.back(); }
  55. bool initCapacity(size_t cap) { return vector.initCapacity(cap); }
  56. bool reserve(size_t req) { return vector.reserve(req); }
  57. void shrinkBy(size_t amount) { return vector.shrinkBy(amount); }
  58. bool growBy(size_t amount) { return vector.growBy(amount); }
  59. bool resize(size_t newLen) { return vector.resize(newLen); }
  60. void clear() { return vector.clear(); }
  61. template<typename U> bool append(U&& item) { return vector.append(mozilla::Forward<U>(item)); }
  62. template<typename... Args>
  63. bool
  64. emplaceBack(Args&&... args) {
  65. return vector.emplaceBack(mozilla::Forward<Args>(args)...);
  66. }
  67. template<typename U>
  68. void infallibleAppend(U&& aU) {
  69. return vector.infallibleAppend(mozilla::Forward<U>(aU));
  70. }
  71. void infallibleAppendN(const T& aT, size_t aN) {
  72. return vector.infallibleAppendN(aT, aN);
  73. }
  74. template<typename U> void
  75. infallibleAppend(const U* aBegin, const U* aEnd) {
  76. return vector.infallibleAppend(aBegin, aEnd);
  77. }
  78. template<typename U> void infallibleAppend(const U* aBegin, size_t aLength) {
  79. return vector.infallibleAppend(aBegin, aLength);
  80. }
  81. template<typename U, size_t O, class BP>
  82. bool appendAll(const mozilla::Vector<U, O, BP>& aU) { return vector.appendAll(aU); }
  83. template<typename U, size_t O, class BP>
  84. bool appendAll(const GCVector<U, O, BP>& aU) { return vector.append(aU.begin(), aU.length()); }
  85. bool appendN(const T& val, size_t count) { return vector.appendN(val, count); }
  86. template<typename U> bool append(const U* aBegin, const U* aEnd) {
  87. return vector.append(aBegin, aEnd);
  88. }
  89. template<typename U> bool append(const U* aBegin, size_t aLength) {
  90. return vector.append(aBegin, aLength);
  91. }
  92. void popBack() { return vector.popBack(); }
  93. T popCopy() { return vector.popCopy(); }
  94. size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  95. return vector.sizeOfExcludingThis(mallocSizeOf);
  96. }
  97. size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
  98. return vector.sizeOfIncludingThis(mallocSizeOf);
  99. }
  100. static void trace(GCVector* vec, JSTracer* trc) { vec->trace(trc); }
  101. void trace(JSTracer* trc) {
  102. for (auto& elem : vector)
  103. GCPolicy<T>::trace(trc, &elem, "vector element");
  104. }
  105. };
  106. } // namespace JS
  107. namespace js {
  108. template <typename Outer, typename T, size_t Capacity, typename AllocPolicy>
  109. class GCVectorOperations
  110. {
  111. using Vec = JS::GCVector<T, Capacity, AllocPolicy>;
  112. const Vec& vec() const { return static_cast<const Outer*>(this)->get(); }
  113. public:
  114. const AllocPolicy& allocPolicy() const { return vec().allocPolicy(); }
  115. size_t length() const { return vec().length(); }
  116. bool empty() const { return vec().empty(); }
  117. size_t capacity() const { return vec().capacity(); }
  118. const T* begin() const { return vec().begin(); }
  119. const T* end() const { return vec().end(); }
  120. const T& back() const { return vec().back(); }
  121. JS::Handle<T> operator[](size_t aIndex) const {
  122. return JS::Handle<T>::fromMarkedLocation(&vec().operator[](aIndex));
  123. }
  124. };
  125. template <typename Outer, typename T, size_t Capacity, typename AllocPolicy>
  126. class MutableGCVectorOperations
  127. : public GCVectorOperations<Outer, T, Capacity, AllocPolicy>
  128. {
  129. using Vec = JS::GCVector<T, Capacity, AllocPolicy>;
  130. const Vec& vec() const { return static_cast<const Outer*>(this)->get(); }
  131. Vec& vec() { return static_cast<Outer*>(this)->get(); }
  132. public:
  133. const AllocPolicy& allocPolicy() const { return vec().allocPolicy(); }
  134. AllocPolicy& allocPolicy() { return vec().allocPolicy(); }
  135. const T* begin() const { return vec().begin(); }
  136. T* begin() { return vec().begin(); }
  137. const T* end() const { return vec().end(); }
  138. T* end() { return vec().end(); }
  139. const T& back() const { return vec().back(); }
  140. T& back() { return vec().back(); }
  141. JS::Handle<T> operator[](size_t aIndex) const {
  142. return JS::Handle<T>::fromMarkedLocation(&vec().operator[](aIndex));
  143. }
  144. JS::MutableHandle<T> operator[](size_t aIndex) {
  145. return JS::MutableHandle<T>::fromMarkedLocation(&vec().operator[](aIndex));
  146. }
  147. bool initCapacity(size_t aRequest) { return vec().initCapacity(aRequest); }
  148. bool reserve(size_t aRequest) { return vec().reserve(aRequest); }
  149. void shrinkBy(size_t aIncr) { vec().shrinkBy(aIncr); }
  150. bool growBy(size_t aIncr) { return vec().growBy(aIncr); }
  151. bool resize(size_t aNewLength) { return vec().resize(aNewLength); }
  152. bool growByUninitialized(size_t aIncr) { return vec().growByUninitialized(aIncr); }
  153. void infallibleGrowByUninitialized(size_t aIncr) { vec().infallibleGrowByUninitialized(aIncr); }
  154. bool resizeUninitialized(size_t aNewLength) { return vec().resizeUninitialized(aNewLength); }
  155. void clear() { vec().clear(); }
  156. void clearAndFree() { vec().clearAndFree(); }
  157. template<typename U> bool append(U&& aU) { return vec().append(mozilla::Forward<U>(aU)); }
  158. template<typename... Args> bool emplaceBack(Args&&... aArgs) {
  159. return vec().emplaceBack(mozilla::Forward<Args...>(aArgs...));
  160. }
  161. template<typename U, size_t O, class BP>
  162. bool appendAll(const mozilla::Vector<U, O, BP>& aU) { return vec().appendAll(aU); }
  163. template<typename U, size_t O, class BP>
  164. bool appendAll(const JS::GCVector<U, O, BP>& aU) { return vec().appendAll(aU); }
  165. bool appendN(const T& aT, size_t aN) { return vec().appendN(aT, aN); }
  166. template<typename U> bool append(const U* aBegin, const U* aEnd) {
  167. return vec().append(aBegin, aEnd);
  168. }
  169. template<typename U> bool append(const U* aBegin, size_t aLength) {
  170. return vec().append(aBegin, aLength);
  171. }
  172. template<typename U> void infallibleAppend(U&& aU) {
  173. vec().infallibleAppend(mozilla::Forward<U>(aU));
  174. }
  175. void infallibleAppendN(const T& aT, size_t aN) { vec().infallibleAppendN(aT, aN); }
  176. template<typename U> void infallibleAppend(const U* aBegin, const U* aEnd) {
  177. vec().infallibleAppend(aBegin, aEnd);
  178. }
  179. template<typename U> void infallibleAppend(const U* aBegin, size_t aLength) {
  180. vec().infallibleAppend(aBegin, aLength);
  181. }
  182. void popBack() { vec().popBack(); }
  183. T popCopy() { return vec().popCopy(); }
  184. template<typename U> T* insert(T* aP, U&& aVal) {
  185. return vec().insert(aP, mozilla::Forward<U>(aVal));
  186. }
  187. void erase(T* aT) { vec().erase(aT); }
  188. void erase(T* aBegin, T* aEnd) { vec().erase(aBegin, aEnd); }
  189. };
  190. template <typename T, size_t N, typename AP>
  191. class RootedBase<JS::GCVector<T,N,AP>>
  192. : public MutableGCVectorOperations<JS::Rooted<JS::GCVector<T,N,AP>>, T,N,AP>
  193. {};
  194. template <typename T, size_t N, typename AP>
  195. class MutableHandleBase<JS::GCVector<T,N,AP>>
  196. : public MutableGCVectorOperations<JS::MutableHandle<JS::GCVector<T,N,AP>>, T,N,AP>
  197. {};
  198. template <typename T, size_t N, typename AP>
  199. class HandleBase<JS::GCVector<T,N,AP>>
  200. : public GCVectorOperations<JS::Handle<JS::GCVector<T,N,AP>>, T,N,AP>
  201. {};
  202. template <typename T, size_t N, typename AP>
  203. class PersistentRootedBase<JS::GCVector<T,N,AP>>
  204. : public MutableGCVectorOperations<JS::PersistentRooted<JS::GCVector<T,N,AP>>, T,N,AP>
  205. {};
  206. } // namespace js
  207. #endif // js_GCVector_h