| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "nacl_io/kernel_object.h" | 5 #include "nacl_io/kernel_object.h" |
| 6 | 6 |
| 7 #include <assert.h> | 7 #include <assert.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <pthread.h> | 10 #include <pthread.h> |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 } | 245 } |
| 246 | 246 |
| 247 void KernelObject::FreeAndReassignFD(int fd, | 247 void KernelObject::FreeAndReassignFD(int fd, |
| 248 const ScopedKernelHandle& handle, | 248 const ScopedKernelHandle& handle, |
| 249 const std::string& path) { | 249 const std::string& path) { |
| 250 if (NULL == handle) { | 250 if (NULL == handle) { |
| 251 FreeFD(fd); | 251 FreeFD(fd); |
| 252 } else { | 252 } else { |
| 253 AUTO_LOCK(handle_lock_); | 253 AUTO_LOCK(handle_lock_); |
| 254 | 254 |
| 255 // If the required FD is larger than the current set, grow the set | 255 // If the required FD is larger than the current set, grow the set. |
| 256 if (fd >= (int)handle_map_.size()) | 256 int sz = static_cast<int>(handle_map_.size()); |
| 257 if (fd >= sz) { |
| 258 // Expand the handle map to include all the extra descriptors |
| 259 // up to and including fd. |
| 257 handle_map_.resize(fd + 1); | 260 handle_map_.resize(fd + 1); |
| 261 // Add all the new descriptors, except fd, to the free list. |
| 262 for (; sz < fd; ++sz) { |
| 263 free_fds_.push_back(sz); |
| 264 std::push_heap(free_fds_.begin(), free_fds_.end(), |
| 265 std::greater<int>()); |
| 266 } |
| 267 } |
| 258 | 268 |
| 259 // This path will be from an existing handle, and absolute. | 269 // This path will be from an existing handle, and absolute. |
| 260 handle_map_[fd] = Descriptor_t(handle, path); | 270 handle_map_[fd] = Descriptor_t(handle, path); |
| 261 } | 271 } |
| 262 } | 272 } |
| 263 | 273 |
| 264 void KernelObject::FreeFD(int fd) { | 274 void KernelObject::FreeFD(int fd) { |
| 265 AUTO_LOCK(handle_lock_); | 275 AUTO_LOCK(handle_lock_); |
| 266 | 276 |
| 267 handle_map_[fd].handle.reset(NULL); | 277 handle_map_[fd].handle.reset(NULL); |
| 268 free_fds_.push_back(fd); | 278 free_fds_.push_back(fd); |
| 269 | 279 |
| 270 // Force lower numbered FD to be available first. | 280 // Force lower numbered FD to be available first. |
| 271 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); | 281 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); |
| 272 } | 282 } |
| 273 | 283 |
| 274 } // namespace nacl_io | 284 } // namespace nacl_io |
| OLD | NEW |