OLD | NEW |
1 // Copyright (c) 2012 The Native Client Authors. All rights reserved. | 1 // Copyright (c) 2012 The Native Client 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 #ifndef PACKAGES_LIBRARIES_NACL_MOUNTS_UTIL_SLOTALLOCATOR_H_ | 5 #ifndef PACKAGES_LIBRARIES_NACL_MOUNTS_UTIL_SLOTALLOCATOR_H_ |
6 #define PACKAGES_LIBRARIES_NACL_MOUNTS_UTIL_SLOTALLOCATOR_H_ | 6 #define PACKAGES_LIBRARIES_NACL_MOUNTS_UTIL_SLOTALLOCATOR_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <set> | 10 #include <set> |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 return slots_.size()-1; | 74 return slots_.size()-1; |
75 } | 75 } |
76 int index = *(free_fds_.begin()); | 76 int index = *(free_fds_.begin()); |
77 free_fds_.erase(free_fds_.begin()); | 77 free_fds_.erase(free_fds_.begin()); |
78 slots_[index] = new T; | 78 slots_[index] = new T; |
79 return index; | 79 return index; |
80 } | 80 } |
81 | 81 |
82 template <class T> | 82 template <class T> |
83 int SlotAllocator<T>::AllocAt(int fd) { | 83 int SlotAllocator<T>::AllocAt(int fd) { |
84 int prev_size = slots_.size(); | |
85 if (slots_.size() < fd + 1) { | 84 if (slots_.size() < fd + 1) { |
| 85 int prev_size = slots_.size(); |
86 slots_.resize(fd + 1); | 86 slots_.resize(fd + 1); |
87 } else { | 87 for (int i = prev_size; i < fd + 1; ++i) free_fds_.insert(i); |
88 if (free_fds_.find(fd) != free_fds_.end()) { | |
89 slots_[fd] = new T; | |
90 for (int i = prev_size; i < fd; ++i) free_fds_.insert(i); | |
91 } else { | |
92 return -1; | |
93 } | |
94 } | 88 } |
95 return fd; | 89 |
| 90 if (free_fds_.erase(fd)) { |
| 91 slots_[fd] = new T; |
| 92 return fd; |
| 93 } |
| 94 return -1; |
96 } | 95 } |
97 | 96 |
98 template <class T> | 97 template <class T> |
99 void SlotAllocator<T>::Free(int slot) { | 98 void SlotAllocator<T>::Free(int slot) { |
100 if (slot < 0 || | 99 if (slot < 0 || |
101 static_cast<uint32_t>(slot) >= slots_.size() || | 100 static_cast<uint32_t>(slot) >= slots_.size() || |
102 !slots_[slot]) { | 101 !slots_[slot]) { |
103 return; | 102 return; |
104 } | 103 } |
105 delete slots_[slot]; | 104 delete slots_[slot]; |
106 slots_[slot] = NULL; | 105 slots_[slot] = NULL; |
107 free_fds_.insert(slot); | 106 free_fds_.insert(slot); |
108 } | 107 } |
109 | 108 |
110 template <class T> | 109 template <class T> |
111 T *SlotAllocator<T>::At(int slot) { | 110 T *SlotAllocator<T>::At(int slot) { |
112 if (slot < 0 || static_cast<uint32_t>(slot) >= slots_.size()) { | 111 if (slot < 0 || static_cast<uint32_t>(slot) >= slots_.size()) { |
113 return NULL; | 112 return NULL; |
114 } | 113 } |
115 return slots_[slot]; | 114 return slots_[slot]; |
116 } | 115 } |
117 | 116 |
118 #endif // PACKAGES_LIBRARIES_NACL_MOUNTS_UTIL_SLOTALLOCATOR_H_ | 117 #endif // PACKAGES_LIBRARIES_NACL_MOUNTS_UTIL_SLOTALLOCATOR_H_ |
OLD | NEW |