ab645f87ed070149f714cf6ac652fe657d274a00
[platform/framework/web/crosswalk-tizen.git] /
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.
4
5 #ifndef HeapTerminatedArray_h
6 #define HeapTerminatedArray_h
7
8 #include "platform/heap/Heap.h"
9 #include "wtf/TerminatedArray.h"
10 #include "wtf/TerminatedArrayBuilder.h"
11
12 namespace blink {
13
14 template <typename T>
15 class HeapTerminatedArray : public TerminatedArray<T> {
16   DISALLOW_NEW();
17   IS_GARBAGE_COLLECTED_TYPE();
18
19  public:
20   using TerminatedArray<T>::begin;
21   using TerminatedArray<T>::end;
22
23   DEFINE_INLINE_TRACE() {
24     for (typename TerminatedArray<T>::iterator it = begin(); it != end(); ++it)
25       visitor->trace(*it);
26   }
27
28  private:
29   // Allocator describes how HeapTerminatedArrayBuilder should create new
30   // instances of HeapTerminatedArray and manage their lifetimes.
31   struct Allocator final {
32     STATIC_ONLY(Allocator);
33     using PassPtr = HeapTerminatedArray*;
34     using Ptr = Member<HeapTerminatedArray>;
35
36     static PassPtr release(Ptr& ptr) { return ptr; }
37
38     static PassPtr create(size_t capacity) {
39       return reinterpret_cast<HeapTerminatedArray*>(
40           ThreadHeap::allocate<HeapTerminatedArray>(
41               capacity * sizeof(T), IsEagerlyFinalizedType<T>::value));
42     }
43
44     static PassPtr resize(PassPtr ptr, size_t capacity) {
45       return reinterpret_cast<HeapTerminatedArray*>(
46           ThreadHeap::reallocate<HeapTerminatedArray>(ptr,
47                                                       capacity * sizeof(T)));
48     }
49   };
50
51   // Prohibit construction. Allocator makes HeapTerminatedArray instances for
52   // HeapTerminatedArrayBuilder by pointer casting.
53   HeapTerminatedArray();
54
55   template <typename U, template <typename> class>
56   friend class WTF::TerminatedArrayBuilder;
57 };
58
59 template <typename T>
60 class TraceEagerlyTrait<HeapTerminatedArray<T>> {
61  public:
62   static const bool value = TraceEagerlyTrait<T>::value;
63 };
64
65 }  // namespace blink
66
67 #endif  // HeapTerminatedArray_h