From 38ecd04e039c8a3af61d82b0bad0606d5d473a16 Mon Sep 17 00:00:00 2001 From: sword_smith Date: Thu, 5 Sep 2024 20:00:32 +0200 Subject: [PATCH] refactor!(static memory allocation): Remove option to share allocs Remove unused the option to share static memory allocated values. This option (read: abomination) was used neither in this repo, nor in the downstreams `tasm-lang` or `neptune-core`. So I think it's fine just to delete it. --- tasm-lib/src/library.rs | 31 +------------------------------ 1 file changed, 1 insertion(+), 30 deletions(-) diff --git a/tasm-lib/src/library.rs b/tasm-lib/src/library.rs index 8db52e48..d64dc37e 100644 --- a/tasm-lib/src/library.rs +++ b/tasm-lib/src/library.rs @@ -29,10 +29,6 @@ pub struct Library { /// Imported dependencies. seen_snippets: HashMap>, - /// Known, and thus shareable, (static) memory allocations. Includes both their address and - /// their size. - pub_allocations: HashMap, - /// The number of statically allocated words num_allocated_words: u32, } @@ -51,7 +47,6 @@ impl Library { pub fn new() -> Self { Self { seen_snippets: HashMap::default(), - pub_allocations: HashMap::default(), num_allocated_words: 0, } } @@ -130,6 +125,7 @@ impl Library { /// Statically allocate `num_words` words of memory. Panics if more static /// memory is required than what the capacity allows for. pub fn kmalloc(&mut self, num_words: u32) -> BFieldElement { + assert!(num_words > 0, "must allocate a positive number of words"); let address = STATIC_MEMORY_FIRST_ADDRESS - bfe!(self.num_allocated_words) - BFieldElement::new(num_words as u64 - 1); @@ -140,24 +136,6 @@ impl Library { address } - - /// Statically allocate `num_words` words of memory and give it a name. - /// Allows sharing the allocation with other snippets. - pub fn pub_kmalloc(&mut self, num_words: u32, name: String) -> BFieldElement { - let address = self.kmalloc(num_words); - if let Some((addr, size)) = self - .pub_allocations - .insert(name.clone(), (address, num_words)) - { - panic!("Public kmalloc for \"{name}\" overwrote previous allocation: ({addr}, {size})"); - }; - address - } - - /// Get the address and size of a public allocation. - pub fn get_pub_allocation(&self, name: &str) -> (BFieldElement, u32) { - self.pub_allocations[name] - } } #[derive(Debug)] @@ -496,12 +474,5 @@ mod tests { let third_free_address = lib.kmalloc(1000); assert_eq!(-BFieldElement::new(1009), third_free_address); - - let fourth_free_address = lib.pub_kmalloc(10_000, "my_thing".to_string()); - assert_eq!(-BFieldElement::new(11_009), fourth_free_address); - - let (address, size) = lib.get_pub_allocation("my_thing"); - assert_eq!(fourth_free_address, address); - assert_eq!(10_000, size); } }