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 #include "base/rand_util.h" |
11 | 12 |
12 namespace base { | 13 namespace base { |
13 | 14 |
14 GlobalDescriptors::Descriptor::Descriptor(Key key, int fd) | 15 GlobalDescriptors::Descriptor::Descriptor(Key key, int fd) |
15 : key(key), fd(fd), region(base::MemoryMappedFile::Region::kWholeFile) { | 16 : key(key), fd(fd), region(base::MemoryMappedFile::Region::kWholeFile) { |
16 } | 17 } |
17 | 18 |
18 GlobalDescriptors::Descriptor::Descriptor(Key key, | 19 GlobalDescriptors::Descriptor::Descriptor(Key key, |
19 int fd, | 20 int fd, |
20 base::MemoryMappedFile::Region region) | 21 base::MemoryMappedFile::Region region) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 if (i.key == key) { | 59 if (i.key == key) { |
59 i.fd = fd; | 60 i.fd = fd; |
60 i.region = region; | 61 i.region = region; |
61 return; | 62 return; |
62 } | 63 } |
63 } | 64 } |
64 | 65 |
65 descriptors_.push_back(Descriptor(key, fd, region)); | 66 descriptors_.push_back(Descriptor(key, fd, region)); |
66 } | 67 } |
67 | 68 |
| 69 GlobalDescriptors::Key GlobalDescriptors::Register(int fd) { |
| 70 // Find a random unused key. |
| 71 Key key; |
| 72 do { |
| 73 base::RandBytes(&key, sizeof(Key)); |
| 74 } while (MaybeGet(key) != -1); |
| 75 Set(key, fd); |
| 76 return key; |
| 77 } |
| 78 |
68 base::MemoryMappedFile::Region GlobalDescriptors::GetRegion(Key key) const { | 79 base::MemoryMappedFile::Region GlobalDescriptors::GetRegion(Key key) const { |
69 for (const auto& i : descriptors_) { | 80 for (const auto& i : descriptors_) { |
70 if (i.key == key) | 81 if (i.key == key) |
71 return i.region; | 82 return i.region; |
72 } | 83 } |
73 DLOG(FATAL) << "Unknown global descriptor: " << key; | 84 DLOG(FATAL) << "Unknown global descriptor: " << key; |
74 return base::MemoryMappedFile::Region::kWholeFile; | 85 return base::MemoryMappedFile::Region::kWholeFile; |
75 } | 86 } |
76 | 87 |
77 void GlobalDescriptors::Reset(const Mapping& mapping) { | 88 void GlobalDescriptors::Reset(const Mapping& mapping) { |
78 descriptors_ = mapping; | 89 descriptors_ = mapping; |
79 } | 90 } |
80 | 91 |
81 GlobalDescriptors::GlobalDescriptors() {} | 92 GlobalDescriptors::GlobalDescriptors() {} |
82 | 93 |
83 GlobalDescriptors::~GlobalDescriptors() {} | 94 GlobalDescriptors::~GlobalDescriptors() {} |
84 | 95 |
85 } // namespace base | 96 } // namespace base |
OLD | NEW |