OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/posix/global_descriptors.h" | 5 #include "base/posix/global_descriptors.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 | 11 |
12 namespace base { | 12 namespace base { |
13 | 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 |
14 // static | 24 // static |
15 GlobalDescriptors* GlobalDescriptors::GetInstance() { | 25 GlobalDescriptors* GlobalDescriptors::GetInstance() { |
16 typedef Singleton<base::GlobalDescriptors, | 26 typedef Singleton<base::GlobalDescriptors, |
17 LeakySingletonTraits<base::GlobalDescriptors> > | 27 LeakySingletonTraits<base::GlobalDescriptors> > |
18 GlobalDescriptorsSingleton; | 28 GlobalDescriptorsSingleton; |
19 return GlobalDescriptorsSingleton::get(); | 29 return GlobalDescriptorsSingleton::get(); |
20 } | 30 } |
21 | 31 |
22 int GlobalDescriptors::Get(Key key) const { | 32 int GlobalDescriptors::Get(Key key) const { |
23 const int ret = MaybeGet(key); | 33 const int ret = MaybeGet(key); |
24 | 34 |
25 if (ret == -1) | 35 if (ret == -1) |
26 DLOG(FATAL) << "Unknown global descriptor: " << key; | 36 DLOG(FATAL) << "Unknown global descriptor: " << key; |
27 return ret; | 37 return ret; |
28 } | 38 } |
29 | 39 |
30 int GlobalDescriptors::MaybeGet(Key key) const { | 40 int GlobalDescriptors::MaybeGet(Key key) const { |
31 for (Mapping::const_iterator | 41 for (Mapping::const_iterator |
32 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 42 i = descriptors_.begin(); i != descriptors_.end(); ++i) { |
33 if (i->first == key) | 43 if (i->key == key) |
34 return i->second; | 44 return i->fd; |
35 } | 45 } |
36 | 46 |
37 return -1; | 47 return -1; |
38 } | 48 } |
39 | 49 |
40 void GlobalDescriptors::Set(Key key, int fd) { | 50 void GlobalDescriptors::Set(Key key, int fd) { |
41 for (Mapping::iterator | 51 Set(key, fd, base::MemoryMappedFile::Region::kWholeFile); |
42 i = descriptors_.begin(); i != descriptors_.end(); ++i) { | 52 } |
43 if (i->first == key) { | 53 |
44 i->second = fd; | 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; |
45 return; | 61 return; |
46 } | 62 } |
47 } | 63 } |
48 | 64 |
49 descriptors_.push_back(std::make_pair(key, fd)); | 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; |
50 } | 75 } |
51 | 76 |
52 void GlobalDescriptors::Reset(const Mapping& mapping) { | 77 void GlobalDescriptors::Reset(const Mapping& mapping) { |
53 descriptors_ = mapping; | 78 descriptors_ = mapping; |
54 } | 79 } |
55 | 80 |
56 GlobalDescriptors::GlobalDescriptors() {} | 81 GlobalDescriptors::GlobalDescriptors() {} |
57 | 82 |
58 GlobalDescriptors::~GlobalDescriptors() {} | 83 GlobalDescriptors::~GlobalDescriptors() {} |
59 | 84 |
60 } // namespace base | 85 } // namespace base |
OLD | NEW |