table keys can wrap around

This commit is contained in:
Pat Hickey
2021-02-03 11:09:05 -08:00
parent b29a3bfbb2
commit d62b0a1f97

View File

@@ -31,14 +31,14 @@ impl Table {
/// Insert a resource at the next available index. /// Insert a resource at the next available index.
pub fn push(&mut self, a: Box<dyn Any>) -> Result<u32, Error> { pub fn push(&mut self, a: Box<dyn Any>) -> Result<u32, Error> {
// NOTE: The performance of this new key calculation could be very bad once keys wrap
// around.
if self.map.len() == u32::MAX as usize {
return Err(Error::trap("table has no free keys"));
}
loop { loop {
let key = self.next_key; let key = self.next_key;
// XXX this is not correct. The table may still have empty entries, but our self.next_key = self.next_key.wrapping_add(1);
// linear search strategy is quite bad
self.next_key = self
.next_key
.checked_add(1)
.ok_or_else(|| Error::trap("out of keys in table"))?;
if self.map.contains_key(&key) { if self.map.contains_key(&key) {
continue; continue;
} }