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

Side by Side Diff: runtime/bin/directory.h

Issue 1893033002: Fixes memory leak of async directory lister (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 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 | « no previous file | runtime/bin/directory.cc » ('j') | sdk/lib/io/directory_impl.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef BIN_DIRECTORY_H_ 5 #ifndef BIN_DIRECTORY_H_
6 #define BIN_DIRECTORY_H_ 6 #define BIN_DIRECTORY_H_
7 7
8 #include "bin/builtin.h" 8 #include "bin/builtin.h"
9 #include "bin/dartutils.h" 9 #include "bin/dartutils.h"
10 #include "bin/reference_counting.h"
10 #include "bin/thread.h" 11 #include "bin/thread.h"
11 #include "platform/globals.h" 12 #include "platform/globals.h"
12 13
13 namespace dart { 14 namespace dart {
14 namespace bin { 15 namespace bin {
15 16
16 enum ListType { 17 enum ListType {
17 kListFile = 0, 18 kListFile = 0,
18 kListDirectory = 1, 19 kListDirectory = 1,
19 kListLink = 2, 20 kListLink = 2,
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 error_(false), 101 error_(false),
101 recursive_(recursive), 102 recursive_(recursive),
102 follow_links_(follow_links) { 103 follow_links_(follow_links) {
103 if (!path_buffer_.Add(dir_name)) { 104 if (!path_buffer_.Add(dir_name)) {
104 error_ = true; 105 error_ = true;
105 } 106 }
106 Push(new DirectoryListingEntry(NULL)); 107 Push(new DirectoryListingEntry(NULL));
107 } 108 }
108 109
109 virtual ~DirectoryListing() { 110 virtual ~DirectoryListing() {
110 while (!IsEmpty()) { 111 PopAll();
111 Pop();
112 }
113 } 112 }
114 113
115 virtual bool HandleDirectory(const char* dir_name) = 0; 114 virtual bool HandleDirectory(const char* dir_name) = 0;
116 virtual bool HandleFile(const char* file_name) = 0; 115 virtual bool HandleFile(const char* file_name) = 0;
117 virtual bool HandleLink(const char* link_name) = 0; 116 virtual bool HandleLink(const char* link_name) = 0;
118 virtual bool HandleError() = 0; 117 virtual bool HandleError() = 0;
119 virtual void HandleDone() {} 118 virtual void HandleDone() {}
120 119
121 void Push(DirectoryListingEntry* directory) { 120 void Push(DirectoryListingEntry* directory) {
122 top_ = directory; 121 top_ = directory;
123 } 122 }
124 123
125 void Pop() { 124 void Pop() {
126 ASSERT(!IsEmpty()); 125 ASSERT(!IsEmpty());
127 DirectoryListingEntry* current = top_; 126 DirectoryListingEntry* current = top_;
128 top_ = top_->parent(); 127 top_ = top_->parent();
129 delete current; 128 delete current;
130 } 129 }
131 130
132 bool IsEmpty() const { 131 bool IsEmpty() const {
133 return top_ == NULL; 132 return top_ == NULL;
134 } 133 }
135 134
135 void PopAll() {
136 while (!IsEmpty()) {
137 Pop();
138 }
139 }
140
136 DirectoryListingEntry* top() const { 141 DirectoryListingEntry* top() const {
137 return top_; 142 return top_;
138 } 143 }
139 144
140 bool recursive() const { 145 bool recursive() const {
141 return recursive_; 146 return recursive_;
142 } 147 }
143 148
144 bool follow_links() const { 149 bool follow_links() const {
145 return follow_links_; 150 return follow_links_;
(...skipping 13 matching lines...) Expand all
159 164
160 private: 165 private:
161 PathBuffer path_buffer_; 166 PathBuffer path_buffer_;
162 DirectoryListingEntry* top_; 167 DirectoryListingEntry* top_;
163 bool error_; 168 bool error_;
164 bool recursive_; 169 bool recursive_;
165 bool follow_links_; 170 bool follow_links_;
166 }; 171 };
167 172
168 173
169 class AsyncDirectoryListing : public DirectoryListing { 174 class AsyncDirectoryListing : public ReferenceCounted<AsyncDirectoryListing>,
175 public DirectoryListing {
170 public: 176 public:
171 enum Response { 177 enum Response {
172 kListFile = 0, 178 kListFile = 0,
173 kListDirectory = 1, 179 kListDirectory = 1,
174 kListLink = 2, 180 kListLink = 2,
175 kListError = 3, 181 kListError = 3,
176 kListDone = 4 182 kListDone = 4
177 }; 183 };
178 184
179 AsyncDirectoryListing(const char* dir_name, 185 AsyncDirectoryListing(const char* dir_name,
180 bool recursive, 186 bool recursive,
181 bool follow_links) 187 bool follow_links)
182 : DirectoryListing(dir_name, recursive, follow_links) {} 188 : ReferenceCounted(),
189 DirectoryListing(dir_name, recursive, follow_links),
190 array_(NULL),
191 index_(0),
192 length_(0),
193 weak_handle_(NULL) {}
183 194
184 virtual ~AsyncDirectoryListing() {}
185 virtual bool HandleDirectory(const char* dir_name); 195 virtual bool HandleDirectory(const char* dir_name);
186 virtual bool HandleFile(const char* file_name); 196 virtual bool HandleFile(const char* file_name);
187 virtual bool HandleLink(const char* file_name); 197 virtual bool HandleLink(const char* file_name);
188 virtual bool HandleError(); 198 virtual bool HandleError();
189 virtual void HandleDone(); 199 virtual void HandleDone();
190 200
191 void SetArray(CObjectArray* array, intptr_t length) { 201 void SetArray(CObjectArray* array, intptr_t length) {
192 ASSERT(length % 2 == 0); 202 ASSERT(length % 2 == 0);
193 array_ = array; 203 array_ = array;
194 index_ = 0; 204 index_ = 0;
195 length_ = length; 205 length_ = length;
196 } 206 }
197 207
198 intptr_t index() const { 208 intptr_t index() const {
199 return index_; 209 return index_;
200 } 210 }
201 211
212 // Returns the weak persistent handle for the Dart wrapper.
213 Dart_WeakPersistentHandle WeakHandle() const { return weak_handle_; }
214
215 // Set the weak persistent handle for the Dart wrapper.
216 void SetWeakHandle(Dart_WeakPersistentHandle handle) {
217 ASSERT(weak_handle_ == NULL);
218 weak_handle_ = handle;
219 }
220
221 // Deletes the weak persistent handle for the Dart wrapper. Call
222 // when the listing is explicitly closed and the finalizer is no longer
223 // needed.
224 void DeleteWeakHandle(Dart_Isolate isolate) {
225 Dart_DeleteWeakPersistentHandle(isolate, weak_handle_);
226 weak_handle_ = NULL;
227 }
228
202 private: 229 private:
230 virtual ~AsyncDirectoryListing() {}
203 bool AddFileSystemEntityToResponse(Response response, const char* arg); 231 bool AddFileSystemEntityToResponse(Response response, const char* arg);
204 CObjectArray* array_; 232 CObjectArray* array_;
205 intptr_t index_; 233 intptr_t index_;
206 intptr_t length_; 234 intptr_t length_;
235 Dart_WeakPersistentHandle weak_handle_;
207 236
237 friend class ReferenceCounted<AsyncDirectoryListing>;
208 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); 238 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing);
209 }; 239 };
210 240
211 241
212 class SyncDirectoryListing: public DirectoryListing { 242 class SyncDirectoryListing: public DirectoryListing {
213 public: 243 public:
214 SyncDirectoryListing(Dart_Handle results, 244 SyncDirectoryListing(Dart_Handle results,
215 const char* dir_name, 245 const char* dir_name,
216 bool recursive, 246 bool recursive,
217 bool follow_links) 247 bool follow_links)
(...skipping 13 matching lines...) Expand all
231 virtual bool HandleLink(const char* file_name); 261 virtual bool HandleLink(const char* file_name);
232 virtual bool HandleError(); 262 virtual bool HandleError();
233 263
234 private: 264 private:
235 Dart_Handle results_; 265 Dart_Handle results_;
236 Dart_Handle add_string_; 266 Dart_Handle add_string_;
237 Dart_Handle directory_type_; 267 Dart_Handle directory_type_;
238 Dart_Handle file_type_; 268 Dart_Handle file_type_;
239 Dart_Handle link_type_; 269 Dart_Handle link_type_;
240 270
271 DISALLOW_ALLOCATION()
241 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing); 272 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing);
242 }; 273 };
243 274
244 275
245 class Directory { 276 class Directory {
246 public: 277 public:
247 enum ExistsResult { 278 enum ExistsResult {
248 UNKNOWN, 279 UNKNOWN,
249 EXISTS, 280 EXISTS,
250 DOES_NOT_EXIST 281 DOES_NOT_EXIST
(...skipping 28 matching lines...) Expand all
279 310
280 private: 311 private:
281 DISALLOW_ALLOCATION(); 312 DISALLOW_ALLOCATION();
282 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); 313 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory);
283 }; 314 };
284 315
285 } // namespace bin 316 } // namespace bin
286 } // namespace dart 317 } // namespace dart
287 318
288 #endif // BIN_DIRECTORY_H_ 319 #endif // BIN_DIRECTORY_H_
OLDNEW
« no previous file with comments | « no previous file | runtime/bin/directory.cc » ('j') | sdk/lib/io/directory_impl.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698