OLD | NEW |
| (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 #ifndef SERVICES_FILES_C_LIB_FD_TABLE_H_ | |
6 #define SERVICES_FILES_C_LIB_FD_TABLE_H_ | |
7 | |
8 #include <stddef.h> | |
9 | |
10 #include <memory> | |
11 #include <vector> | |
12 | |
13 #include "files/public/c/lib/fd_impl.h" | |
14 #include "mojo/public/cpp/system/macros.h" | |
15 | |
16 namespace mojio { | |
17 | |
18 class ErrnoImpl; | |
19 | |
20 // A simple (thread-unsafe) implementation of an FD (file descriptor) table, | |
21 // mapping FDs (integers) to |FDImpl|s. (Note that on failure, all the methods | |
22 // set "errno" using the |ErrnoImpl| provided to the constructor.) | |
23 class FDTable { | |
24 public: | |
25 // The |ErrnoImpl| must outlive this object. |max_num_fds| is the maximum | |
26 // number of FDs allowed (simultaneously). This number should be relatively | |
27 // small (hundreds to maybe tens of thousands, certainly not millions). | |
28 FDTable(ErrnoImpl* errno_impl, size_t max_num_fds); | |
29 // Note: This does not |Close()| any remaining |FDImpl|s. | |
30 ~FDTable(); | |
31 | |
32 // Returns the new FD (>= 0) on success and -1 on failure. Note that the | |
33 // lowest-valued FD available is always allocated. | |
34 int Add(std::unique_ptr<FDImpl> fd_impl); | |
35 | |
36 // Returns the |FDImpl| associated to |fd| (null if |fd| is not valid), | |
37 // keeping the |FDImpl| in the table (and thus |fd| valid). | |
38 FDImpl* Get(int fd) const; | |
39 | |
40 // Removes and returns the |FDImpl| associated to |fd| (null if |fd| is not | |
41 // valid). Note that |Close()| is not called on the |FDImpl|. | |
42 std::unique_ptr<FDImpl> Remove(int fd); | |
43 | |
44 ErrnoImpl* errno_impl() const { return errno_impl_; } | |
45 size_t max_num_fds() const { return table_.size(); } | |
46 | |
47 private: | |
48 ErrnoImpl* const errno_impl_; | |
49 std::vector<std::unique_ptr<FDImpl>> table_; | |
50 | |
51 MOJO_DISALLOW_COPY_AND_ASSIGN(FDTable); | |
52 }; | |
53 | |
54 } // namespace mojio | |
55 | |
56 #endif // SERVICES_FILES_C_LIB_FD_TABLE_H_ | |
OLD | NEW |