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

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

Issue 1781883002: Fixes some memory leaks in //runtime/bin (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Cleanup Created 4 years, 9 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
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/thread.h" 10 #include "bin/thread.h"
(...skipping 17 matching lines...) Expand all
28 ~PathBuffer() { 28 ~PathBuffer() {
29 free(data_); 29 free(data_);
30 } 30 }
31 31
32 bool Add(const char* name); 32 bool Add(const char* name);
33 bool AddW(const wchar_t* name); 33 bool AddW(const wchar_t* name);
34 34
35 char* AsString() const; 35 char* AsString() const;
36 wchar_t* AsStringW() const; 36 wchar_t* AsStringW() const;
37 37
38 void Reset(int new_length); 38 // Makes a scope allocated copy of the string.
39 const char* AsScopedString() const;
39 40
40 int length() const { 41 void Reset(intptr_t new_length);
42
43 intptr_t length() const {
41 return length_; 44 return length_;
42 } 45 }
43 46
44 private: 47 private:
45 void* data_; 48 void* data_;
46 int length_; 49 intptr_t length_;
47 50
48 DISALLOW_COPY_AND_ASSIGN(PathBuffer); 51 DISALLOW_COPY_AND_ASSIGN(PathBuffer);
49 }; 52 };
50 53
51 class DirectoryListing; 54 class DirectoryListing;
52 55
53 struct LinkList; 56 struct LinkList;
54 57
55 // DirectoryListingEntry is used as a stack item, when performing recursive 58 // DirectoryListingEntry is used as a stack item, when performing recursive
56 // directory listing. By using DirectoryListingEntry as stack elements, a 59 // directory listing. By using DirectoryListingEntry as stack elements, a
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 } 108 }
106 Push(new DirectoryListingEntry(NULL)); 109 Push(new DirectoryListingEntry(NULL));
107 } 110 }
108 111
109 virtual ~DirectoryListing() { 112 virtual ~DirectoryListing() {
110 while (!IsEmpty()) { 113 while (!IsEmpty()) {
111 Pop(); 114 Pop();
112 } 115 }
113 } 116 }
114 117
115 virtual bool HandleDirectory(char* dir_name) = 0; 118 virtual bool HandleDirectory(const char* dir_name) = 0;
116 virtual bool HandleFile(char* file_name) = 0; 119 virtual bool HandleFile(const char* file_name) = 0;
117 virtual bool HandleLink(char* link_name) = 0; 120 virtual bool HandleLink(const char* link_name) = 0;
118 virtual bool HandleError(const char* dir_name) = 0; 121 virtual bool HandleError() = 0;
119 virtual void HandleDone() {} 122 virtual void HandleDone() {}
120 123
121 void Push(DirectoryListingEntry* directory) { 124 void Push(DirectoryListingEntry* directory) {
122 top_ = directory; 125 top_ = directory;
123 } 126 }
124 127
125 void Pop() { 128 void Pop() {
126 ASSERT(!IsEmpty()); 129 ASSERT(!IsEmpty());
127 DirectoryListingEntry* current = top_; 130 DirectoryListingEntry* current = top_;
128 top_ = top_->parent(); 131 top_ = top_->parent();
129 delete current; 132 delete current;
130 } 133 }
131 134
132 bool IsEmpty() const { 135 bool IsEmpty() const {
133 return top_ == NULL; 136 return top_ == NULL;
134 } 137 }
135 138
136 DirectoryListingEntry* top() const { 139 DirectoryListingEntry* top() const {
137 return top_; 140 return top_;
138 } 141 }
139 142
140 bool recursive() const { 143 bool recursive() const {
141 return recursive_; 144 return recursive_;
142 } 145 }
143 146
144 bool follow_links() const { 147 bool follow_links() const {
145 return follow_links_; 148 return follow_links_;
146 } 149 }
147 150
148 char* CurrentPath() { 151 const char* CurrentPath() {
149 return path_buffer_.AsString(); 152 return path_buffer_.AsScopedString();
150 } 153 }
151 154
152 PathBuffer& path_buffer() { 155 PathBuffer& path_buffer() {
153 return path_buffer_; 156 return path_buffer_;
154 } 157 }
155 158
156 bool error() const { 159 bool error() const {
157 return error_; 160 return error_;
158 } 161 }
159 162
(...skipping 15 matching lines...) Expand all
175 kListError = 3, 178 kListError = 3,
176 kListDone = 4 179 kListDone = 4
177 }; 180 };
178 181
179 AsyncDirectoryListing(const char* dir_name, 182 AsyncDirectoryListing(const char* dir_name,
180 bool recursive, 183 bool recursive,
181 bool follow_links) 184 bool follow_links)
182 : DirectoryListing(dir_name, recursive, follow_links) {} 185 : DirectoryListing(dir_name, recursive, follow_links) {}
183 186
184 virtual ~AsyncDirectoryListing() {} 187 virtual ~AsyncDirectoryListing() {}
185 virtual bool HandleDirectory(char* dir_name); 188 virtual bool HandleDirectory(const char* dir_name);
186 virtual bool HandleFile(char* file_name); 189 virtual bool HandleFile(const char* file_name);
187 virtual bool HandleLink(char* file_name); 190 virtual bool HandleLink(const char* file_name);
188 virtual bool HandleError(const char* dir_name); 191 virtual bool HandleError();
189 virtual void HandleDone(); 192 virtual void HandleDone();
190 193
191 void SetArray(CObjectArray* array, intptr_t length) { 194 void SetArray(CObjectArray* array, intptr_t length) {
192 ASSERT(length % 2 == 0); 195 ASSERT(length % 2 == 0);
193 array_ = array; 196 array_ = array;
194 index_ = 0; 197 index_ = 0;
195 length_ = length; 198 length_ = length;
196 } 199 }
197 200
198 intptr_t index() const { 201 intptr_t index() const {
199 return index_; 202 return index_;
200 } 203 }
201 204
202 private: 205 private:
203 bool AddFileSystemEntityToResponse(Response response, char* arg); 206 bool AddFileSystemEntityToResponse(Response response, const char* arg);
204 CObjectArray* array_; 207 CObjectArray* array_;
205 intptr_t index_; 208 intptr_t index_;
206 intptr_t length_; 209 intptr_t length_;
207 210
208 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing); 211 DISALLOW_IMPLICIT_CONSTRUCTORS(AsyncDirectoryListing);
209 }; 212 };
210 213
211 214
212 class SyncDirectoryListing: public DirectoryListing { 215 class SyncDirectoryListing: public DirectoryListing {
213 public: 216 public:
214 SyncDirectoryListing(Dart_Handle results, 217 SyncDirectoryListing(Dart_Handle results,
215 const char* dir_name, 218 const char* dir_name,
216 bool recursive, 219 bool recursive,
217 bool follow_links) 220 bool follow_links)
218 : DirectoryListing(dir_name, recursive, follow_links), 221 : DirectoryListing(dir_name, recursive, follow_links),
219 results_(results) { 222 results_(results) {
220 add_string_ = DartUtils::NewString("add"); 223 add_string_ = DartUtils::NewString("add");
221 directory_type_ = 224 directory_type_ =
222 DartUtils::GetDartType(DartUtils::kIOLibURL, "Directory"); 225 DartUtils::GetDartType(DartUtils::kIOLibURL, "Directory");
223 file_type_ = 226 file_type_ =
224 DartUtils::GetDartType(DartUtils::kIOLibURL, "File"); 227 DartUtils::GetDartType(DartUtils::kIOLibURL, "File");
225 link_type_ = 228 link_type_ =
226 DartUtils::GetDartType(DartUtils::kIOLibURL, "Link"); 229 DartUtils::GetDartType(DartUtils::kIOLibURL, "Link");
227 } 230 }
228 virtual ~SyncDirectoryListing() {} 231 virtual ~SyncDirectoryListing() {}
229 virtual bool HandleDirectory(char* dir_name); 232 virtual bool HandleDirectory(const char* dir_name);
230 virtual bool HandleFile(char* file_name); 233 virtual bool HandleFile(const char* file_name);
231 virtual bool HandleLink(char* file_name); 234 virtual bool HandleLink(const char* file_name);
232 virtual bool HandleError(const char* dir_name); 235 virtual bool HandleError();
233 236
234 private: 237 private:
235 Dart_Handle results_; 238 Dart_Handle results_;
236 Dart_Handle add_string_; 239 Dart_Handle add_string_;
237 Dart_Handle directory_type_; 240 Dart_Handle directory_type_;
238 Dart_Handle file_type_; 241 Dart_Handle file_type_;
239 Dart_Handle link_type_; 242 Dart_Handle link_type_;
240 243
241 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing); 244 DISALLOW_IMPLICIT_CONSTRUCTORS(SyncDirectoryListing);
242 }; 245 };
243 246
244 247
245 class Directory { 248 class Directory {
246 public: 249 public:
247 enum ExistsResult { 250 enum ExistsResult {
248 UNKNOWN, 251 UNKNOWN,
249 EXISTS, 252 EXISTS,
250 DOES_NOT_EXIST 253 DOES_NOT_EXIST
251 }; 254 };
252 255
253 static void List(DirectoryListing* listing); 256 static void List(DirectoryListing* listing);
254 static ExistsResult Exists(const char* path); 257 static ExistsResult Exists(const char* path);
255 static char* Current(); 258
259 // Returns the current working directory. The caller must call
260 // free() on the result.
261 static char* CurrentNoScope();
262
263 // Returns the current working directory. The returned string is allocated
264 // with Dart_ScopeAllocate(). It lasts only as long as the current API scope.
265 static const char* Current();
266 static const char* SystemTemp();
267 static const char* CreateTemp(const char* path);
256 static bool SetCurrent(const char* path); 268 static bool SetCurrent(const char* path);
257 static bool Create(const char* path); 269 static bool Create(const char* path);
258 static char* SystemTemp();
259 static char* CreateTemp(const char* path);
260 static bool Delete(const char* path, bool recursive); 270 static bool Delete(const char* path, bool recursive);
261 static bool Rename(const char* path, const char* new_path); 271 static bool Rename(const char* path, const char* new_path);
262 272
263 static CObject* CreateRequest(const CObjectArray& request); 273 static CObject* CreateRequest(const CObjectArray& request);
264 static CObject* DeleteRequest(const CObjectArray& request); 274 static CObject* DeleteRequest(const CObjectArray& request);
265 static CObject* ExistsRequest(const CObjectArray& request); 275 static CObject* ExistsRequest(const CObjectArray& request);
266 static CObject* CreateTempRequest(const CObjectArray& request); 276 static CObject* CreateTempRequest(const CObjectArray& request);
267 static CObject* CreateSystemTempRequest(const CObjectArray& request); 277 static CObject* CreateSystemTempRequest(const CObjectArray& request);
268 static CObject* ListStartRequest(const CObjectArray& request); 278 static CObject* ListStartRequest(const CObjectArray& request);
269 static CObject* ListNextRequest(const CObjectArray& request); 279 static CObject* ListNextRequest(const CObjectArray& request);
270 static CObject* ListStopRequest(const CObjectArray& request); 280 static CObject* ListStopRequest(const CObjectArray& request);
271 static CObject* RenameRequest(const CObjectArray& request); 281 static CObject* RenameRequest(const CObjectArray& request);
272 282
273 private: 283 private:
274 DISALLOW_ALLOCATION(); 284 DISALLOW_ALLOCATION();
275 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory); 285 DISALLOW_IMPLICIT_CONSTRUCTORS(Directory);
276 }; 286 };
277 287
278 } // namespace bin 288 } // namespace bin
279 } // namespace dart 289 } // namespace dart
280 290
281 #endif // BIN_DIRECTORY_H_ 291 #endif // BIN_DIRECTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698