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

Side by Side Diff: runtime/bin/socket.cc

Issue 2533303005: VM: Fix memory leak during shutdown (Closed)
Patch Set: Simplify socket.cc by not updating hashmaps during shutdown Created 4 years 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/socket.h ('k') | runtime/platform/hashmap.h » ('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 #if !defined(DART_IO_DISABLED) 5 #if !defined(DART_IO_DISABLED)
6 6
7 #include "bin/socket.h" 7 #include "bin/socket.h"
8 8
9 #include "bin/dartutils.h" 9 #include "bin/dartutils.h"
10 #include "bin/io_buffer.h" 10 #include "bin/io_buffer.h"
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 InsertByPort(allocated_port, os_socket); 189 InsertByPort(allocated_port, os_socket);
190 InsertByFd(socketfd, os_socket); 190 InsertByFd(socketfd, os_socket);
191 191
192 // We set as a side-effect the port on the dart socket_object. 192 // We set as a side-effect the port on the dart socket_object.
193 Socket::SetSocketIdNativeField(socket_object, socketfd); 193 Socket::SetSocketIdNativeField(socket_object, socketfd);
194 194
195 return Dart_True(); 195 return Dart_True();
196 } 196 }
197 197
198 198
199 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket) { 199 bool ListeningSocketRegistry::CloseOneSafe(OSSocket* os_socket,
200 bool update_hash_maps) {
200 ASSERT(!mutex_->TryLock()); 201 ASSERT(!mutex_->TryLock());
201 ASSERT(os_socket != NULL); 202 ASSERT(os_socket != NULL);
202 ASSERT(os_socket->ref_count > 0); 203 ASSERT(os_socket->ref_count > 0);
203 os_socket->ref_count--; 204 os_socket->ref_count--;
204 if (os_socket->ref_count > 0) { 205 if (os_socket->ref_count > 0) {
205 return false; 206 return false;
206 } 207 }
207 // We free the OS socket by removing it from two datastructures. 208 if (update_hash_maps) {
208 RemoveByFd(os_socket->socketfd); 209 // We free the OS socket by removing it from two datastructures.
210 RemoveByFd(os_socket->socketfd);
209 211
210 OSSocket* prev = NULL; 212 OSSocket* prev = NULL;
211 OSSocket* current = LookupByPort(os_socket->port); 213 OSSocket* current = LookupByPort(os_socket->port);
212 while (current != os_socket) { 214 while (current != os_socket) {
213 ASSERT(current != NULL); 215 ASSERT(current != NULL);
214 prev = current; 216 prev = current;
215 current = current->next; 217 current = current->next;
216 } 218 }
217 219
218 if ((prev == NULL) && (current->next == NULL)) { 220 if ((prev == NULL) && (current->next == NULL)) {
219 // Remove last element from the list. 221 // Remove last element from the list.
220 RemoveByPort(os_socket->port); 222 RemoveByPort(os_socket->port);
221 } else if (prev == NULL) { 223 } else if (prev == NULL) {
222 // Remove first element of the list. 224 // Remove first element of the list.
223 InsertByPort(os_socket->port, current->next); 225 InsertByPort(os_socket->port, current->next);
224 } else { 226 } else {
225 // Remove element from the list which is not the first one. 227 // Remove element from the list which is not the first one.
226 prev->next = os_socket->next; 228 prev->next = os_socket->next;
229 }
227 } 230 }
228 231
229 ASSERT(os_socket->ref_count == 0); 232 ASSERT(os_socket->ref_count == 0);
230 delete os_socket; 233 delete os_socket;
231 return true; 234 return true;
232 } 235 }
233 236
234 237
235 void ListeningSocketRegistry::CloseAllSafe() { 238 void ListeningSocketRegistry::CloseAllSafe() {
236 MutexLocker ml(mutex_); 239 MutexLocker ml(mutex_);
237 for (HashMap::Entry* p = sockets_by_fd_.Start(); p != NULL; 240
238 p = sockets_by_fd_.Next(p)) { 241 for (HashMap::Entry* cursor = sockets_by_fd_.Start(); cursor != NULL;
239 CloseOneSafe(reinterpret_cast<OSSocket*>(p->value)); 242 cursor = sockets_by_fd_.Next(cursor)) {
243 CloseOneSafe(reinterpret_cast<OSSocket*>(cursor->value), false);
240 } 244 }
241 } 245 }
242 246
243 247
244 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) { 248 bool ListeningSocketRegistry::CloseSafe(intptr_t socketfd) {
245 ASSERT(!mutex_->TryLock()); 249 ASSERT(!mutex_->TryLock());
246 OSSocket* os_socket = LookupByFd(socketfd); 250 OSSocket* os_socket = LookupByFd(socketfd);
247 if (os_socket != NULL) { 251 if (os_socket != NULL) {
248 return CloseOneSafe(os_socket); 252 return CloseOneSafe(os_socket, true);
249 } else { 253 } else {
250 // It should be impossible for the event handler to close something that 254 // It should be impossible for the event handler to close something that
251 // hasn't been created before. 255 // hasn't been created before.
252 UNREACHABLE(); 256 UNREACHABLE();
253 return false; 257 return false;
254 } 258 }
255 } 259 }
256 260
257 261
258 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) { 262 void FUNCTION_NAME(InternetAddress_Parse)(Dart_NativeArguments args) {
(...skipping 682 matching lines...) Expand 10 before | Expand all | Expand 10 after
941 if (Dart_IsError(err)) { 945 if (Dart_IsError(err)) {
942 Dart_PropagateError(err); 946 Dart_PropagateError(err);
943 } 947 }
944 return socket; 948 return socket;
945 } 949 }
946 950
947 } // namespace bin 951 } // namespace bin
948 } // namespace dart 952 } // namespace dart
949 953
950 #endif // !defined(DART_IO_DISABLED) 954 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/socket.h ('k') | runtime/platform/hashmap.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698