Skip to content

Commit

Permalink
refactor!(static memory allocation): Remove option to share allocs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sword-Smith committed Sep 5, 2024
1 parent 6464ac6 commit 38ecd04
Showing 1 changed file with 1 addition and 30 deletions.
31 changes: 1 addition & 30 deletions tasm-lib/src/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ pub struct Library {
/// Imported dependencies.
seen_snippets: HashMap<String, Vec<LabelledInstruction>>,

/// Known, and thus shareable, (static) memory allocations. Includes both their address and
/// their size.
pub_allocations: HashMap<String, (BFieldElement, u32)>,

/// The number of statically allocated words
num_allocated_words: u32,
}
Expand All @@ -51,7 +47,6 @@ impl Library {
pub fn new() -> Self {
Self {
seen_snippets: HashMap::default(),
pub_allocations: HashMap::default(),
num_allocated_words: 0,
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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)]
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 38ecd04

Please sign in to comment.