OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 BASE_FILE_DESCRIPTOR_STORE_H_ |
| 6 #define BASE_FILE_DESCRIPTOR_STORE_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 #include "base/files/memory_mapped_file.h" |
| 13 #include "base/memory/singleton.h" |
| 14 |
| 15 namespace base { |
| 16 |
| 17 // The file descriptor store is used to associate file descriptors with keys |
| 18 // that must be unique. |
| 19 // It is used to share file descriptors from a process to its child. |
| 20 class BASE_EXPORT FileDescriptorStore { |
| 21 public: |
| 22 struct Descriptor { |
| 23 Descriptor(const std::string& key, int fd); |
| 24 Descriptor(const std::string& key, |
| 25 int fd, |
| 26 base::MemoryMappedFile::Region region); |
| 27 |
| 28 // Globally unique key. |
| 29 std::string key; |
| 30 // Actual FD. |
| 31 int fd; |
| 32 // Optional region, defaults to kWholeFile. |
| 33 base::MemoryMappedFile::Region region; |
| 34 }; |
| 35 typedef std::vector<Descriptor> Mapping; |
| 36 |
| 37 // Returns the singleton instance of FileDescriptorStore. |
| 38 static FileDescriptorStore* GetInstance(); |
| 39 |
| 40 // Gets a descriptor given a key. It is a fatal error if the key is not known. |
| 41 int Get(const std::string& key) const; |
| 42 |
| 43 // Gets a descriptor given a key. Returns -1 on error. |
| 44 int MaybeGet(const std::string& key) const; |
| 45 |
| 46 // Gets a region given a key. It is a fatal error if the key is not known. |
| 47 base::MemoryMappedFile::Region GetRegion(const std::string& key) const; |
| 48 |
| 49 // Sets the descriptor for the given |key|. This sets the region associated |
| 50 // with |key| to kWholeFile. |
| 51 void Set(const std::string& key, int fd); |
| 52 |
| 53 // Sets the descriptor and |region| for the given |key|. |
| 54 void Set(const std::string& key, |
| 55 int fd, |
| 56 base::MemoryMappedFile::Region region); |
| 57 |
| 58 void Reset(const Mapping& mapping); |
| 59 |
| 60 private: |
| 61 friend struct DefaultSingletonTraits<FileDescriptorStore>; |
| 62 FileDescriptorStore(); |
| 63 ~FileDescriptorStore(); |
| 64 |
| 65 Mapping descriptors_; |
| 66 }; |
| 67 |
| 68 } // namespace base |
| 69 |
| 70 #endif // BASE_FILE_DESCRIPTOR_STORE_H_ |
OLD | NEW |