Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(718)

Side by Side Diff: src/smart-pointer.h

Issue 7754007: Fix memory leak from d8 shell. (Closed) Base URL: git://github.com/v8/v8.git@trunk
Patch Set: fix thread_ as well Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/d8.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 19 matching lines...) Expand all
30 30
31 namespace v8 { 31 namespace v8 {
32 namespace internal { 32 namespace internal {
33 33
34 34
35 // A 'scoped array pointer' that calls DeleteArray on its pointer when the 35 // A 'scoped array pointer' that calls DeleteArray on its pointer when the
36 // destructor is called. 36 // destructor is called.
37 template<typename T> 37 template<typename T>
38 class SmartPointer { 38 class SmartPointer {
39 public: 39 public:
40 // Default constructor. Constructs an empty scoped pointer.
41 inline SmartPointer() : p_(NULL) {}
40 42
41 // Default constructor. Construct an empty scoped pointer. 43 // Constructs a scoped pointer from a plain one.
42 inline SmartPointer() : p(NULL) {} 44 explicit inline SmartPointer(T* ptr) : p_(ptr) {}
43
44
45 // Construct a scoped pointer from a plain one.
46 explicit inline SmartPointer(T* pointer) : p(pointer) {}
47
48 45
49 // Copy constructor removes the pointer from the original to avoid double 46 // Copy constructor removes the pointer from the original to avoid double
50 // freeing. 47 // freeing.
51 inline SmartPointer(const SmartPointer<T>& rhs) : p(rhs.p) { 48 inline SmartPointer(const SmartPointer<T>& rhs) : p_(rhs.p_) {
52 const_cast<SmartPointer<T>&>(rhs).p = NULL; 49 const_cast<SmartPointer<T>&>(rhs).p_ = NULL;
53 } 50 }
54 51
55
56 // When the destructor of the scoped pointer is executed the plain pointer 52 // When the destructor of the scoped pointer is executed the plain pointer
57 // is deleted using DeleteArray. This implies that you must allocate with 53 // is deleted using DeleteArray. This implies that you must allocate with
58 // NewArray. 54 // NewArray.
59 inline ~SmartPointer() { if (p) DeleteArray(p); } 55 inline ~SmartPointer() { if (p_) DeleteArray(p_); }
60 56
57 inline T* operator->() const { return p_; }
61 58
62 // You can get the underlying pointer out with the * operator. 59 // You can get the underlying pointer out with the * operator.
63 inline T* operator*() { return p; } 60 inline T* operator*() { return p_; }
64
65 61
66 // You can use [n] to index as if it was a plain pointer 62 // You can use [n] to index as if it was a plain pointer
67 inline T& operator[](size_t i) { 63 inline T& operator[](size_t i) {
68 return p[i]; 64 return p_[i];
69 } 65 }
70 66
71 // We don't have implicit conversion to a T* since that hinders migration: 67 // We don't have implicit conversion to a T* since that hinders migration:
72 // You would not be able to change a method from returning a T* to 68 // You would not be able to change a method from returning a T* to
73 // returning an SmartPointer<T> and then get errors wherever it is used. 69 // returning an SmartPointer<T> and then get errors wherever it is used.
74 70
75 71
76 // If you want to take out the plain pointer and don't want it automatically 72 // If you want to take out the plain pointer and don't want it automatically
77 // deleted then call Detach(). Afterwards, the smart pointer is empty 73 // deleted then call Detach(). Afterwards, the smart pointer is empty
78 // (NULL). 74 // (NULL).
79 inline T* Detach() { 75 inline T* Detach() {
80 T* temp = p; 76 T* temp = p_;
81 p = NULL; 77 p_ = NULL;
82 return temp; 78 return temp;
83 } 79 }
84 80
85
86 // Assignment requires an empty (NULL) SmartPointer as the receiver. Like 81 // Assignment requires an empty (NULL) SmartPointer as the receiver. Like
87 // the copy constructor it removes the pointer in the original to avoid 82 // the copy constructor it removes the pointer in the original to avoid
88 // double freeing. 83 // double freeing.
89 inline SmartPointer& operator=(const SmartPointer<T>& rhs) { 84 inline SmartPointer& operator=(const SmartPointer<T>& rhs) {
90 ASSERT(is_empty()); 85 ASSERT(is_empty());
91 T* tmp = rhs.p; // swap to handle self-assignment 86 T* tmp = rhs.p_; // swap to handle self-assignment
92 const_cast<SmartPointer<T>&>(rhs).p = NULL; 87 const_cast<SmartPointer<T>&>(rhs).p_ = NULL;
93 p = tmp; 88 p_ = tmp;
94 return *this; 89 return *this;
95 } 90 }
96 91
97 92 inline bool is_empty() { return p_ == NULL; }
98 inline bool is_empty() {
99 return p == NULL;
100 }
101
102 93
103 private: 94 private:
104 T* p; 95 T* p_;
105 }; 96 };
106 97
107 } } // namespace v8::internal 98 } } // namespace v8::internal
108 99
109 #endif // V8_SMART_POINTER_H_ 100 #endif // V8_SMART_POINTER_H_
OLDNEW
« no previous file with comments | « src/d8.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698