Merge pull request #192 from gwilymk/custom-hashmap

Custom hashmap
This commit is contained in:
Gwilym Kuiper 2022-03-20 16:08:55 +00:00 committed by GitHub
commit c677347621
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1058 additions and 180 deletions

View file

@ -25,7 +25,6 @@ agb_sound_converter = { version = "0.1.0", path = "../agb-sound-converter" }
agb_macros = { version = "0.1.0", path = "../agb-macros" }
agb_fixnum = { version = "0.1.0", path = "../agb-fixnum" }
bare-metal = "1"
hashbrown = "0.12"
modular-bitfield = "0.11"
rustc-hash = { version = "1", default-features = false }

View file

@ -6,9 +6,6 @@ use core::ptr::NonNull;
use core::slice;
use modular_bitfield::prelude::{B10, B2, B3, B4, B5, B8, B9};
use modular_bitfield::{bitfield, BitfieldSpecifier};
use rustc_hash::FxHasher;
use hashbrown::HashMap;
const BYTES_PER_TILE_4BPP: usize = 32;
@ -18,6 +15,7 @@ use crate::agb_alloc::block_allocator::BlockAllocator;
use crate::agb_alloc::bump_allocator::StartEnd;
use crate::dma;
use crate::fixnum::Vector2D;
use crate::hash_map::HashMap;
use attributes::*;
@ -316,8 +314,8 @@ pub struct Object<'a, 'b> {
}
struct SpriteControllerInner {
palette: HashMap<PaletteId, Storage, BuildHasherDefault<FxHasher>>,
sprite: HashMap<SpriteId, Storage, BuildHasherDefault<FxHasher>>,
palette: HashMap<PaletteId, Storage>,
sprite: HashMap<SpriteId, Storage>,
}
pub struct SpriteController {
@ -629,36 +627,31 @@ impl SpriteControllerInner {
}
fn return_sprite(&mut self, sprite: &'static Sprite) {
self.sprite
.entry(sprite.get_id())
.and_replace_entry_with(|_, mut storage| {
let storage = self.sprite.get_mut(&sprite.get_id());
if let Some(storage) = storage {
storage.count -= 1;
if storage.count == 0 {
unsafe { SPRITE_ALLOCATOR.dealloc(storage.as_sprite_ptr(), sprite.layout()) }
None
} else {
Some(storage)
unsafe { SPRITE_ALLOCATOR.dealloc(storage.as_sprite_ptr(), sprite.layout()) };
self.sprite.remove(&sprite.get_id());
}
}
});
self.return_palette(sprite.palette)
}
fn return_palette(&mut self, palette: &'static Palette16) {
let id = palette.get_id();
self.palette
.entry(id)
.and_replace_entry_with(|_, mut storage| {
if let Some(storage) = self.palette.get_mut(&id) {
storage.count -= 1;
if storage.count == 0 {
unsafe {
PALETTE_ALLOCATOR.dealloc(storage.as_palette_ptr(), Palette16::layout());
unsafe { PALETTE_ALLOCATOR.dealloc(storage.as_palette_ptr(), Palette16::layout()) };
self.palette.remove(&id);
}
None
} else {
Some(storage)
}
});
}
}

1034
agb/src/hash_map.rs Normal file

File diff suppressed because it is too large Load diff

View file

@ -152,6 +152,8 @@ mod memory_mapped;
pub mod mgba;
/// Implementation of fixnums for working with non-integer values.
pub use agb_fixnum as fixnum;
/// Contains an implementation of a hashmap which suits the gameboy advance's hardware
pub mod hash_map;
mod single;
/// Implements sound output.
pub mod sound;

View file

@ -24,7 +24,6 @@ dependencies = [
"agb_sound_converter",
"bare-metal",
"bitflags",
"hashbrown",
"modular-bitfield",
"rustc-hash",
]
@ -68,17 +67,6 @@ dependencies = [
"syn",
]
[[package]]
name = "ahash"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "asefile"
version = "0.3.2"
@ -166,26 +154,6 @@ dependencies = [
"miniz_oxide 0.4.4",
]
[[package]]
name = "getrandom"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c21d40587b92fa6a6c6e3c1bdbf87d75511db5672f9c93175574b3a00df1758"
dependencies = [
"ahash",
]
[[package]]
name = "hound"
version = "3.4.0"
@ -309,12 +277,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "png"
version = "0.16.8"
@ -409,15 +371,3 @@ name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

View file

@ -24,7 +24,6 @@ dependencies = [
"agb_sound_converter",
"bare-metal",
"bitflags",
"hashbrown",
"modular-bitfield",
"rustc-hash",
]
@ -68,17 +67,6 @@ dependencies = [
"syn",
]
[[package]]
name = "ahash"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "asefile"
version = "0.3.2"
@ -166,26 +154,6 @@ dependencies = [
"miniz_oxide 0.4.4",
]
[[package]]
name = "getrandom"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c21d40587b92fa6a6c6e3c1bdbf87d75511db5672f9c93175574b3a00df1758"
dependencies = [
"ahash",
]
[[package]]
name = "hound"
version = "3.4.0"
@ -315,12 +283,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "png"
version = "0.16.8"
@ -434,15 +396,3 @@ name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

View file

@ -24,7 +24,6 @@ dependencies = [
"agb_sound_converter",
"bare-metal",
"bitflags",
"hashbrown",
"modular-bitfield",
"rustc-hash",
]
@ -68,17 +67,6 @@ dependencies = [
"syn",
]
[[package]]
name = "ahash"
version = "0.7.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47"
dependencies = [
"getrandom",
"once_cell",
"version_check",
]
[[package]]
name = "asefile"
version = "0.3.2"
@ -190,26 +178,6 @@ dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "getrandom"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c21d40587b92fa6a6c6e3c1bdbf87d75511db5672f9c93175574b3a00df1758"
dependencies = [
"ahash",
]
[[package]]
name = "hound"
version = "3.4.0"
@ -345,12 +313,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "png"
version = "0.16.8"
@ -472,18 +434,6 @@ version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
[[package]]
name = "xml-rs"
version = "0.8.4"