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

Side by Side Diff: mojo/services/files/public/c/lib/fd_table.cc

Issue 1388413005: Move //mojo/services/X/public/... to //mojo/services/X/... (part 1). (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 2 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
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "files/public/c/lib/fd_table.h"
6
7 #include <errno.h>
8
9 #include <limits>
10 #include <utility>
11
12 #include "files/public/c/lib/errno_impl.h"
13 #include "mojo/public/cpp/environment/logging.h"
14
15 namespace mojio {
16
17 FDTable::FDTable(ErrnoImpl* errno_impl, size_t max_num_fds)
18 : errno_impl_(errno_impl), table_(max_num_fds) {
19 MOJO_DCHECK(max_num_fds > 0);
20 // The index of the last FD has to fit into an |int|.
21 MOJO_DCHECK(max_num_fds - 1 <=
22 static_cast<size_t>(std::numeric_limits<int>::max()));
23 }
24
25 FDTable::~FDTable() {
26 // TODO(vtl): Warn if there are still non-null entries?
27 }
28
29 int FDTable::Add(std::unique_ptr<FDImpl> fd_impl) {
30 ErrnoImpl::Setter errno_setter(errno_impl_);
31 MOJO_DCHECK(fd_impl);
32
33 for (size_t i = 0; i < table_.size(); i++) {
34 if (!table_[i]) {
35 table_[i] = std::move(fd_impl);
36 return static_cast<int>(i);
37 }
38 }
39 errno_setter.Set(EMFILE);
40 return -1;
41 }
42
43 FDImpl* FDTable::Get(int fd) const {
44 ErrnoImpl::Setter errno_setter(errno_impl_);
45 if (fd < 0 || static_cast<size_t>(fd) >= table_.size() || !table_[fd]) {
46 errno_setter.Set(EBADF);
47 return nullptr;
48 }
49 return table_[fd].get();
50 }
51
52 std::unique_ptr<FDImpl> FDTable::Remove(int fd) {
53 ErrnoImpl::Setter errno_setter(errno_impl_);
54 if (fd < 0 || static_cast<size_t>(fd) >= table_.size() || !table_[fd]) {
55 errno_setter.Set(EBADF);
56 return nullptr;
57 }
58 return std::move(table_[fd]);
59 }
60
61 } // namespace mojio
OLDNEW
« no previous file with comments | « mojo/services/files/public/c/lib/fd_table.h ('k') | mojo/services/files/public/c/lib/file_fd_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698