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 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) | 172 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) |
173 return EBADF; | 173 return EBADF; |
174 | 174 |
175 *out_handle = handle_map_[fd].handle; | 175 *out_handle = handle_map_[fd].handle; |
176 if (out_handle) | 176 if (out_handle) |
177 return 0; | 177 return 0; |
178 | 178 |
179 return EBADF; | 179 return EBADF; |
180 } | 180 } |
181 | 181 |
182 int KernelObject::AllocateFD(const ScopedKernelHandle& handle) { | 182 Error KernelObject::AcquireHandleAndPath(int fd, ScopedKernelHandle* out_handle, |
| 183 std::string& out_path){ |
| 184 out_handle->reset(NULL); |
| 185 |
| 186 AUTO_LOCK(handle_lock_); |
| 187 if (fd < 0 || fd >= static_cast<int>(handle_map_.size())) |
| 188 return EBADF; |
| 189 |
| 190 *out_handle = handle_map_[fd].handle; |
| 191 if (!out_handle) |
| 192 return EBADF; |
| 193 |
| 194 out_path = handle_map_[fd].path; |
| 195 |
| 196 return 0; |
| 197 } |
| 198 |
| 199 int KernelObject::AllocateFD(const ScopedKernelHandle& handle, |
| 200 const std::string& path) { |
183 AUTO_LOCK(handle_lock_); | 201 AUTO_LOCK(handle_lock_); |
184 int id; | 202 int id; |
185 | 203 |
186 Descriptor_t descriptor(handle); | 204 std::string abs_path = GetAbsParts(path).Join(); |
| 205 Descriptor_t descriptor(handle, abs_path); |
187 | 206 |
188 // If we can recycle and FD, use that first | 207 // If we can recycle and FD, use that first |
189 if (free_fds_.size()) { | 208 if (free_fds_.size()) { |
190 id = free_fds_.front(); | 209 id = free_fds_.front(); |
191 // Force lower numbered FD to be available first. | 210 // Force lower numbered FD to be available first. |
192 std::pop_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); | 211 std::pop_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); |
193 free_fds_.pop_back(); | 212 free_fds_.pop_back(); |
194 handle_map_[id] = descriptor; | 213 handle_map_[id] = descriptor; |
195 } else { | 214 } else { |
196 id = handle_map_.size(); | 215 id = handle_map_.size(); |
197 handle_map_.push_back(descriptor); | 216 handle_map_.push_back(descriptor); |
198 } | 217 } |
| 218 |
199 return id; | 219 return id; |
200 } | 220 } |
201 | 221 |
202 void KernelObject::FreeAndReassignFD(int fd, const ScopedKernelHandle& handle) { | 222 void KernelObject::FreeAndReassignFD(int fd, const ScopedKernelHandle& handle, |
| 223 const std::string& path) { |
203 if (NULL == handle) { | 224 if (NULL == handle) { |
204 FreeFD(fd); | 225 FreeFD(fd); |
205 } else { | 226 } else { |
206 AUTO_LOCK(handle_lock_); | 227 AUTO_LOCK(handle_lock_); |
207 | 228 |
208 // If the required FD is larger than the current set, grow the set | 229 // If the required FD is larger than the current set, grow the set |
209 if (fd >= (int)handle_map_.size()) | 230 if (fd >= (int)handle_map_.size()) |
210 handle_map_.resize(fd + 1); | 231 handle_map_.resize(fd + 1); |
211 | 232 |
212 handle_map_[fd] = Descriptor_t(handle); | 233 // This path will be from an existing handle, and absolute. |
| 234 handle_map_[fd] = Descriptor_t(handle, path); |
213 } | 235 } |
214 } | 236 } |
215 | 237 |
216 void KernelObject::FreeFD(int fd) { | 238 void KernelObject::FreeFD(int fd) { |
217 AUTO_LOCK(handle_lock_); | 239 AUTO_LOCK(handle_lock_); |
218 | 240 |
219 handle_map_[fd].handle.reset(NULL); | 241 handle_map_[fd].handle.reset(NULL); |
220 free_fds_.push_back(fd); | 242 free_fds_.push_back(fd); |
221 | 243 |
222 // Force lower numbered FD to be available first. | 244 // Force lower numbered FD to be available first. |
223 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); | 245 std::push_heap(free_fds_.begin(), free_fds_.end(), std::greater<int>()); |
| 246 // |
224 } | 247 } |
225 | 248 |
226 } // namespace nacl_io | 249 } // namespace nacl_io |
OLD | NEW |