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 3 // found in the LICENSE file. 4 5 #include "base/posix/global_descriptors.h" 6 7 #include <vector> 8 #include <utility> 9 10 #include "base/logging.h" 11 12 namespace base { 13 14 GlobalDescriptors::Descriptor::Descriptor(Key key, int fd) 15 : key(key), fd(fd), region(base::MemoryMappedFile::Region::kWholeFile) { 16 } 17 18 GlobalDescriptors::Descriptor::Descriptor(Key key, 19 int fd, 20 base::MemoryMappedFile::Region region) 21 : key(key), fd(fd), region(region) { 22 } 23 24 // static 25 GlobalDescriptors* GlobalDescriptors::GetInstance() { 26 typedef Singleton<base::GlobalDescriptors, 27 LeakySingletonTraits<base::GlobalDescriptors> > 28 GlobalDescriptorsSingleton; 29 return GlobalDescriptorsSingleton::get(); 30 } 31 32 int GlobalDescriptors::Get(Key key) const { 33 const int ret = MaybeGet(key); 34 35 if (ret == -1) 36 DLOG(FATAL) << "Unknown global descriptor: " << key; 37 return ret; 38 } 39 40 int GlobalDescriptors::MaybeGet(Key key) const { 41 for (Mapping::const_iterator 42 i = descriptors_.begin(); i != descriptors_.end(); ++i) { 43 if (i->key == key) 44 return i->fd; 45 } 46 47 return -1; 48 } 49 50 void GlobalDescriptors::Set(Key key, int fd) { 51 Set(key, fd, base::MemoryMappedFile::Region::kWholeFile); 52 } 53 54 void GlobalDescriptors::Set(Key key, 55 int fd, 56 base::MemoryMappedFile::Region region) { 57 for (auto& i : descriptors_) { 58 if (i.key == key) { 59 i.fd = fd; 60 i.region = region; 61 return; 62 } 63 } 64 65 descriptors_.push_back(Descriptor(key, fd, region)); 66 } 67 68 base::MemoryMappedFile::Region GlobalDescriptors::GetRegion(Key key) const { 69 for (const auto& i : descriptors_) { 70 if (i.key == key) 71 return i.region; 72 } 73 DLOG(FATAL) << "Unknown global descriptor: " << key; 74 return base::MemoryMappedFile::Region::kWholeFile; 75 } 76 77 void GlobalDescriptors::Reset(const Mapping& mapping) { 78 descriptors_ = mapping; 79 } 80 81 GlobalDescriptors::GlobalDescriptors() {} 82 83 GlobalDescriptors::~GlobalDescriptors() {} 84 85 } // namespace base 86