From 2859e54bb9b7b19a957f52d760fe29aa380fb512 Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 22 Nov 2021 01:46:53 +0800 Subject: [PATCH] that was a long walk to fix a bad architectural decision trying to allocate key indicies within the dictionary pool in a late-binding way is hard to do in rust, because of various interior mutability problems. i guess it could be done but it turned out to be easier to pro-actively track everything and generate them on demand. allocations are hard in rust. i guess that's why the alloc thing is still not stable. --- services/pddb/src/backend/basis.rs | 2 +- services/pddb/src/backend/dictionary.rs | 22 +++++++++++----------- services/pddb/src/main.rs | 3 +-- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/services/pddb/src/backend/basis.rs b/services/pddb/src/backend/basis.rs index 63e48a9d8..8c77ce92f 100644 --- a/services/pddb/src/backend/basis.rs +++ b/services/pddb/src/backend/basis.rs @@ -283,7 +283,7 @@ impl BasisCache { keys: HashMap::::new(), clean: false, age: 0, - free_keys: RefCell::new(free_keys), + free_keys, last_disk_key_index: KEY_MAXCOUNT as u32, flags: init_flags, key_count: 0, diff --git a/services/pddb/src/backend/dictionary.rs b/services/pddb/src/backend/dictionary.rs index 4c5d47755..73753fd80 100644 --- a/services/pddb/src/backend/dictionary.rs +++ b/services/pddb/src/backend/dictionary.rs @@ -121,7 +121,7 @@ pub(crate) struct DictCacheEntry { /// count of total keys in the dictionary -- may be equal to or larger than the number of elements in `keys` pub(crate) key_count: u32, /// track the pool of free key indices. Wrapped in a refcell so we can work the index mechanism while updating the keys HashMap - pub(crate) free_keys: RefCell>, + pub(crate) free_keys: BinaryHeap::, /// hint for when to stop doing a brute-force search for the existence of a key in the disk records. /// This field is set to the max count on a new, naive record; and set only upon a sync() or a fill() call. pub(crate) last_disk_key_index: u32, @@ -161,7 +161,7 @@ impl DictCacheEntry { index: index as u32, keys: HashMap::::new(), key_count: dict.num_keys, - free_keys: RefCell::new(free_keys), + free_keys, last_disk_key_index: KEY_MAXCOUNT as u32, clean: true, age: dict.age, @@ -715,10 +715,10 @@ impl DictCacheEntry { } pub(crate) fn get_free_key_index(&mut self) -> Option { - if let Some(free_key) = self.free_keys.borrow_mut().pop() { + if let Some(free_key) = self.free_keys.pop() { let index = free_key.start; if free_key.run > 0 { - self.free_keys.borrow_mut().push( + self.free_keys.push( FreeKeyRange { start: index + 1, run: free_key.run - 1, @@ -735,8 +735,8 @@ impl DictCacheEntry { None } } - pub(crate) fn put_free_key_index(&self, index: u32) { - let free_keys = self.free_keys.replace(BinaryHeap::::new()); + pub(crate) fn put_free_key_index(&mut self, index: u32) { + let free_keys = std::mem::replace(&mut self.free_keys, BinaryHeap::::new()); let free_key_vec = free_keys.into_sorted_vec(); // this is a bit weird because we have three cases: // - the new key is more than 1 away from any element, in which case we insert it as a singleton (start = index, run = 0) @@ -751,11 +751,11 @@ impl DictCacheEntry { } match free_key_vec[i].compare_to(i as u32) { FreeKeyCases::LessThan => { - self.free_keys.borrow_mut().push(FreeKeyRange{start: index as u32, run: 0}); + self.free_keys.push(FreeKeyRange{start: index as u32, run: 0}); break; } FreeKeyCases::LeftAdjacent => { - self.free_keys.borrow_mut().push(FreeKeyRange{start: index as u32, run: free_key_vec[i].run + 1}); + self.free_keys.push(FreeKeyRange{start: index as u32, run: free_key_vec[i].run + 1}); } FreeKeyCases::Within => { log::error!("Double-free error in free_keys()"); @@ -765,18 +765,18 @@ impl DictCacheEntry { // see if we should merge to the right if i + 1 < free_key_vec.len() { if free_key_vec[i+1].compare_to(i as u32) == FreeKeyCases::LeftAdjacent { - self.free_keys.borrow_mut().push(FreeKeyRange{ + self.free_keys.push(FreeKeyRange{ start: free_key_vec[i].start, run: free_key_vec[i].run + free_key_vec[i+1].run + 2 }); skip = true } } else { - self.free_keys.borrow_mut().push(FreeKeyRange { start: free_key_vec[i].start, run: free_key_vec[i].run + 1 }) + self.free_keys.push(FreeKeyRange { start: free_key_vec[i].start, run: free_key_vec[i].run + 1 }) } } FreeKeyCases::GreaterThan => { - self.free_keys.borrow_mut().push(free_key_vec[i]); + self.free_keys.push(free_key_vec[i]); } } } diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index 93813385e..234a2b2db 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -399,14 +399,13 @@ fn xmain() -> ! { } basis_cache.key_update(&mut pddb_os, "system settings", "wifi/wpa_keys/e4200", "12345678".as_bytes(), None, None, None, false).expect("couldn't add a key"); - /* // add a "big" key let mut bigdata = [0u8; 5000]; for (i, d) in bigdata.iter_mut().enumerate() { *d = i as u8; } basis_cache.key_update(&mut pddb_os, "system settings", "big_pool1", &bigdata, None, None, None, false).expect("couldn't add a key"); -*/ + #[cfg(not(any(target_os = "none", target_os = "xous")))] pddb_os.dbg_dump();