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

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

Issue 1892623002: Fixes leak of native File objects. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Address comments 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 | « runtime/bin/dartutils.cc ('k') | runtime/bin/file.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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_FILE_H_ 5 #ifndef BIN_FILE_H_
6 #define BIN_FILE_H_ 6 #define BIN_FILE_H_
7 7
8 #include <stdio.h> 8 #include <stdio.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <string.h> 10 #include <string.h>
11 #include <sys/types.h> 11 #include <sys/types.h>
12 12
13 #include "bin/builtin.h" 13 #include "bin/builtin.h"
14 #include "bin/dartutils.h" 14 #include "bin/dartutils.h"
15 #include "bin/log.h"
16 #include "bin/reference_counting.h"
15 17
16 namespace dart { 18 namespace dart {
17 namespace bin { 19 namespace bin {
18 20
19 // Forward declaration. 21 // Forward declaration.
20 class FileHandle; 22 class FileHandle;
21 23
22 class File { 24 class File : public ReferenceCounted<File> {
23 public: 25 public:
24 enum FileOpenMode { 26 enum FileOpenMode {
25 kRead = 0, 27 kRead = 0,
26 kWrite = 1, 28 kWrite = 1,
27 kTruncate = 1 << 2, 29 kTruncate = 1 << 2,
28 kWriteOnly = 1 << 3, 30 kWriteOnly = 1 << 3,
29 kWriteTruncate = kWrite | kTruncate, 31 kWriteTruncate = kWrite | kTruncate,
30 kWriteOnlyTruncate = kWriteOnly | kTruncate 32 kWriteOnlyTruncate = kWriteOnly | kTruncate
31 }; 33 };
32 34
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
75 77
76 enum LockType { 78 enum LockType {
77 // These match the constants in FileStat in file_impl.dart. 79 // These match the constants in FileStat in file_impl.dart.
78 kLockMin = 0, 80 kLockMin = 0,
79 kLockUnlock = 0, 81 kLockUnlock = 0,
80 kLockShared = 1, 82 kLockShared = 1,
81 kLockExclusive = 2, 83 kLockExclusive = 2,
82 kLockMax = 2 84 kLockMax = 2
83 }; 85 };
84 86
85 ~File();
86
87 intptr_t GetFD(); 87 intptr_t GetFD();
88 88
89 // Read/Write attempt to transfer num_bytes to/from buffer. It returns 89 // Read/Write attempt to transfer num_bytes to/from buffer. It returns
90 // the number of bytes read/written. 90 // the number of bytes read/written.
91 int64_t Read(void* buffer, int64_t num_bytes); 91 int64_t Read(void* buffer, int64_t num_bytes);
92 int64_t Write(const void* buffer, int64_t num_bytes); 92 int64_t Write(const void* buffer, int64_t num_bytes);
93 93
94 // ReadFully and WriteFully do attempt to transfer num_bytes to/from 94 // ReadFully and WriteFully do attempt to transfer num_bytes to/from
95 // the buffer. In the event of short accesses they will loop internally until 95 // the buffer. In the event of short accesses they will loop internally until
96 // the whole buffer has been transferred or an error occurs. If an error 96 // the whole buffer has been transferred or an error occurs. If an error
(...skipping 20 matching lines...) Expand all
117 117
118 // Flush contents of file. 118 // Flush contents of file.
119 bool Flush(); 119 bool Flush();
120 120
121 // Lock range of a file. 121 // Lock range of a file.
122 bool Lock(LockType lock, int64_t start, int64_t end); 122 bool Lock(LockType lock, int64_t start, int64_t end);
123 123
124 // Returns whether the file has been closed. 124 // Returns whether the file has been closed.
125 bool IsClosed(); 125 bool IsClosed();
126 126
127 // Calls the platform-specific functions to close the file.
128 void Close();
129
130 // Returns the weak persistent handle for the File's Dart wrapper.
131 Dart_WeakPersistentHandle WeakHandle() const { return weak_handle_; }
132
133 // Set the weak persistent handle for the File's Dart wrapper.
134 void SetWeakHandle(Dart_WeakPersistentHandle handle) {
135 ASSERT(weak_handle_ == NULL);
136 weak_handle_ = handle;
137 }
138
139 // Deletes the weak persistent handle for the File's Dart wrapper. Call
140 // when the file is explicitly closed and the finalizer is no longer
141 // needed.
142 void DeleteWeakHandle(Dart_Isolate isolate) {
143 Dart_DeleteWeakPersistentHandle(isolate, weak_handle_);
144 weak_handle_ = NULL;
145 }
146
127 // Open the file with the given path. The file is always opened for 147 // Open the file with the given path. The file is always opened for
128 // reading. If mode contains kWrite the file is opened for both 148 // reading. If mode contains kWrite the file is opened for both
129 // reading and writing. If mode contains kWrite and the file does 149 // reading and writing. If mode contains kWrite and the file does
130 // not exist the file is created. The file is truncated to length 0 if 150 // not exist the file is created. The file is truncated to length 0 if
131 // mode contains kTruncate. Assumes we are in an API scope. 151 // mode contains kTruncate. Assumes we are in an API scope.
132 static File* ScopedOpen(const char* path, FileOpenMode mode); 152 static File* ScopedOpen(const char* path, FileOpenMode mode);
133 153
134 // Like ScopedOpen(), but no API scope is needed. 154 // Like ScopedOpen(), but no API scope is needed.
135 static File* Open(const char* path, FileOpenMode mode); 155 static File* Open(const char* path, FileOpenMode mode);
136 156
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 static CObject* CreateLinkRequest(const CObjectArray& request); 203 static CObject* CreateLinkRequest(const CObjectArray& request);
184 static CObject* DeleteLinkRequest(const CObjectArray& request); 204 static CObject* DeleteLinkRequest(const CObjectArray& request);
185 static CObject* RenameLinkRequest(const CObjectArray& request); 205 static CObject* RenameLinkRequest(const CObjectArray& request);
186 static CObject* LinkTargetRequest(const CObjectArray& request); 206 static CObject* LinkTargetRequest(const CObjectArray& request);
187 static CObject* TypeRequest(const CObjectArray& request); 207 static CObject* TypeRequest(const CObjectArray& request);
188 static CObject* IdenticalRequest(const CObjectArray& request); 208 static CObject* IdenticalRequest(const CObjectArray& request);
189 static CObject* StatRequest(const CObjectArray& request); 209 static CObject* StatRequest(const CObjectArray& request);
190 static CObject* LockRequest(const CObjectArray& request); 210 static CObject* LockRequest(const CObjectArray& request);
191 211
192 private: 212 private:
193 explicit File(FileHandle* handle) : handle_(handle) { } 213 explicit File(FileHandle* handle) :
194 void Close(); 214 ReferenceCounted(),
215 handle_(handle),
216 weak_handle_(NULL) {}
217
218 ~File();
195 219
196 static File* FileOpenW(const wchar_t* system_name, FileOpenMode mode); 220 static File* FileOpenW(const wchar_t* system_name, FileOpenMode mode);
197 221
198 static const int kClosedFd = -1; 222 static const int kClosedFd = -1;
199 223
200 // FileHandle is an OS specific class which stores data about the file. 224 // FileHandle is an OS specific class which stores data about the file.
201 FileHandle* handle_; // OS specific handle for the file. 225 FileHandle* handle_; // OS specific handle for the file.
202 226
227 // We retain the weak handle because we can do cleanup eagerly when Dart code
228 // calls closeSync(). In that case, we delete the weak handle so that the
229 // finalizer doesn't run.
230 Dart_WeakPersistentHandle weak_handle_;
231
232 friend class ReferenceCounted<File>;
203 DISALLOW_COPY_AND_ASSIGN(File); 233 DISALLOW_COPY_AND_ASSIGN(File);
204 }; 234 };
205 235
206 } // namespace bin 236 } // namespace bin
207 } // namespace dart 237 } // namespace dart
208 238
209 #endif // BIN_FILE_H_ 239 #endif // BIN_FILE_H_
OLDNEW
« no previous file with comments | « runtime/bin/dartutils.cc ('k') | runtime/bin/file.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698