Index: base/file_descriptor_store.cc |
diff --git a/base/file_descriptor_store.cc b/base/file_descriptor_store.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..4a3c233b7fefb2c00a1510ad579c7a120958fda1 |
--- /dev/null |
+++ b/base/file_descriptor_store.cc |
@@ -0,0 +1,84 @@ |
+// Copyright 2017 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/file_descriptor_store.h" |
+ |
+#include <utility> |
+#include <vector> |
+ |
+#include "base/logging.h" |
+ |
+namespace base { |
+ |
+FileDescriptorStore::Descriptor::Descriptor(const std::string& key, int fd) |
+ : key(key), fd(fd), region(base::MemoryMappedFile::Region::kWholeFile) {} |
+ |
+FileDescriptorStore::Descriptor::Descriptor( |
+ const std::string& key, |
+ int fd, |
+ base::MemoryMappedFile::Region region) |
+ : key(key), fd(fd), region(region) {} |
+ |
+// static |
+FileDescriptorStore* FileDescriptorStore::GetInstance() { |
+ typedef Singleton<base::FileDescriptorStore, |
Ken Rockot(use gerrit already)
2017/02/09 17:17:27
Not sure Singleton is necessary, though I honestly
Jay Civelli
2017/02/09 22:08:26
Done.
|
+ LeakySingletonTraits<base::FileDescriptorStore>> |
+ FileDescriptorStoreSingleton; |
+ return FileDescriptorStoreSingleton::get(); |
+} |
+ |
+int FileDescriptorStore::Get(const std::string& key) const { |
+ const int ret = MaybeGet(key); |
+ |
+ if (ret == -1) |
+ DLOG(FATAL) << "Unknown global descriptor: " << key; |
+ return ret; |
+} |
+ |
+int FileDescriptorStore::MaybeGet(const std::string& key) const { |
+ for (Mapping::const_iterator i = descriptors_.begin(); |
+ i != descriptors_.end(); ++i) { |
+ if (i->key == key) |
+ return i->fd; |
+ } |
+ return -1; |
+} |
+ |
+void FileDescriptorStore::Set(const std::string& key, int fd) { |
+ Set(key, fd, base::MemoryMappedFile::Region::kWholeFile); |
+} |
+ |
+void FileDescriptorStore::Set(const std::string& key, |
+ int fd, |
+ base::MemoryMappedFile::Region region) { |
+ for (auto& i : descriptors_) { |
+ if (i.key == key) { |
+ i.fd = fd; |
+ i.region = region; |
+ return; |
+ } |
+ } |
+ |
+ descriptors_.push_back(Descriptor(key, fd, region)); |
+} |
+ |
+base::MemoryMappedFile::Region FileDescriptorStore::GetRegion( |
+ const std::string& key) const { |
+ for (const auto& i : descriptors_) { |
+ if (i.key == key) |
+ return i.region; |
+ } |
+ DLOG(FATAL) << "Unknown global descriptor: " << key; |
+ return base::MemoryMappedFile::Region::kWholeFile; |
+} |
+ |
+void FileDescriptorStore::Reset(const Mapping& mapping) { |
+ descriptors_ = mapping; |
+} |
+ |
+FileDescriptorStore::FileDescriptorStore() {} |
+ |
+FileDescriptorStore::~FileDescriptorStore() {} |
+ |
+} // namespace base |