v8  3.25.30(node0.11.13)
V8 is Google's open source JavaScript engine
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
smart-pointers.h
Go to the documentation of this file.
1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 #ifndef V8_SMART_POINTERS_H_
29 #define V8_SMART_POINTERS_H_
30 
31 namespace v8 {
32 namespace internal {
33 
34 
35 template<typename Deallocator, typename T>
37  public:
38  // Default constructor. Constructs an empty scoped pointer.
40 
41  // Constructs a scoped pointer from a plain one.
42  explicit SmartPointerBase(T* ptr) : p_(ptr) {}
43 
44  // Copy constructor removes the pointer from the original to avoid double
45  // freeing.
47  : p_(rhs.p_) {
48  const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
49  }
50 
51  T* operator->() const { return p_; }
52 
53  T& operator*() const { return *p_; }
54 
55  T* get() const { return p_; }
56 
57  // You can use [n] to index as if it was a plain pointer.
58  T& operator[](size_t i) {
59  return p_[i];
60  }
61 
62  // You can use [n] to index as if it was a plain pointer.
63  const T& operator[](size_t i) const {
64  return p_[i];
65  }
66 
67  // We don't have implicit conversion to a T* since that hinders migration:
68  // You would not be able to change a method from returning a T* to
69  // returning an SmartArrayPointer<T> and then get errors wherever it is used.
70 
71 
72  // If you want to take out the plain pointer and don't want it automatically
73  // deleted then call Detach(). Afterwards, the smart pointer is empty
74  // (NULL).
75  T* Detach() {
76  T* temp = p_;
77  p_ = NULL;
78  return temp;
79  }
80 
81  void Reset(T* new_value) {
82  ASSERT(p_ == NULL || p_ != new_value);
83  if (p_) Deallocator::Delete(p_);
84  p_ = new_value;
85  }
86 
87  // Assignment requires an empty (NULL) SmartArrayPointer as the receiver. Like
88  // the copy constructor it removes the pointer in the original to avoid
89  // double freeing.
92  ASSERT(is_empty());
93  T* tmp = rhs.p_; // swap to handle self-assignment
94  const_cast<SmartPointerBase<Deallocator, T>&>(rhs).p_ = NULL;
95  p_ = tmp;
96  return *this;
97  }
98 
99  bool is_empty() const { return p_ == NULL; }
100 
101  protected:
102  // When the destructor of the scoped pointer is executed the plain pointer
103  // is deleted using DeleteArray. This implies that you must allocate with
104  // NewArray.
105  ~SmartPointerBase() { if (p_) Deallocator::Delete(p_); }
106 
107  private:
108  T* p_;
109 };
110 
111 // A 'scoped array pointer' that calls DeleteArray on its pointer when the
112 // destructor is called.
113 
114 template<typename T>
116  static void Delete(T* array) {
117  DeleteArray(array);
118  }
119 };
120 
121 
122 template<typename T>
123 class SmartArrayPointer: public SmartPointerBase<ArrayDeallocator<T>, T> {
124  public:
126  explicit SmartArrayPointer(T* ptr)
127  : SmartPointerBase<ArrayDeallocator<T>, T>(ptr) { }
129  : SmartPointerBase<ArrayDeallocator<T>, T>(rhs) { }
130 };
131 
132 
133 template<typename T>
135  static void Delete(T* object) {
136  delete object;
137  }
138 };
139 
140 
141 template<typename T>
142 class SmartPointer: public SmartPointerBase<ObjectDeallocator<T>, T> {
143  public:
145  explicit SmartPointer(T* ptr)
146  : SmartPointerBase<ObjectDeallocator<T>, T>(ptr) { }
148  : SmartPointerBase<ObjectDeallocator<T>, T>(rhs) { }
149 };
150 
151 } } // namespace v8::internal
152 
153 #endif // V8_SMART_POINTERS_H_
enable upcoming ES6 features enable harmony block scoping enable harmony enable harmony proxies enable harmony generators enable harmony numeric enable harmony string enable harmony math functions harmony_scoping harmony_symbols harmony_collections harmony_iteration harmony_strings harmony_scoping harmony_maths tracks arrays with only smi values Optimize object Array DOM strings and string pretenure call new trace pretenuring decisions of HAllocate instructions track fields with only smi values track fields with heap values track_fields track_fields Enables optimizations which favor memory size over execution speed use string slices optimization filter maximum number of GVN fix point iterations use function inlining use allocation folding eliminate write barriers targeting allocations in optimized code maximum source size in bytes considered for a single inlining maximum cumulative number of AST nodes considered for inlining crankshaft harvests type feedback from stub cache trace check elimination phase hydrogen tracing filter NULL
Definition: flags.cc:269
#define ASSERT(condition)
Definition: checks.h:329
SmartPointer(const SmartPointer< T > &rhs)
const T & operator[](size_t i) const
static void Delete(T *object)
SmartPointerBase(const SmartPointerBase< Deallocator, T > &rhs)
#define T(name, string, precedence)
Definition: token.cc:48
SmartArrayPointer(const SmartArrayPointer< T > &rhs)
void DeleteArray(T *array)
Definition: allocation.h:91
SmartPointerBase< Deallocator, T > & operator=(const SmartPointerBase< Deallocator, T > &rhs)
static void Delete(T *array)