Skip to content

Commit

Permalink
factor out checking whether we reached the max number of atoms into i…
Browse files Browse the repository at this point in the history
…ts own function
  • Loading branch information
arvidn committed Feb 7, 2024
1 parent 2d2f7b0 commit e90cd12
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,7 @@ impl Allocator {
return err(self.nil(), "out of memory");
}
let idx = self.atom_vec.len();
if idx + self.small_atoms == MAX_NUM_ATOMS {
return err(self.nil(), "too many atoms");
}
self.check_atom_limit()?;
if v.len() <= 3 && canonical_positive_integer(v) {
let mut ret: u32 = 0;
for b in v {
Expand All @@ -258,9 +256,7 @@ impl Allocator {

pub fn new_small_number(&mut self, v: u32) -> Result<NodePtr, EvalErr> {
debug_assert!(v <= NODE_PTR_IDX_MASK);
if self.atom_vec.len() + self.small_atoms == MAX_NUM_ATOMS {
return err(self.nil(), "too many atoms");
}
self.check_atom_limit()?;
self.small_atoms += 1;
Ok(NodePtr::new(ObjectType::SmallAtom, v as usize))
}
Expand Down Expand Up @@ -293,9 +289,7 @@ impl Allocator {
}

pub fn new_substr(&mut self, node: NodePtr, start: u32, end: u32) -> Result<NodePtr, EvalErr> {
if self.atom_vec.len() + self.small_atoms == MAX_NUM_ATOMS {
return err(self.nil(), "too many atoms");
}
self.check_atom_limit()?;

fn bounds_check(node: NodePtr, start: u32, end: u32, len: u32) -> Result<(), EvalErr> {
if start > len {
Expand Down Expand Up @@ -353,9 +347,7 @@ impl Allocator {
}

pub fn new_concat(&mut self, new_size: usize, nodes: &[NodePtr]) -> Result<NodePtr, EvalErr> {
if self.atom_vec.len() + self.small_atoms == MAX_NUM_ATOMS {
return err(self.nil(), "too many atoms");
}
self.check_atom_limit()?;
let start = self.u8_vec.len();
if self.heap_limit - start < new_size {
return err(self.nil(), "out of memory");
Expand Down Expand Up @@ -595,6 +587,15 @@ impl Allocator {
NodePtr::new(ObjectType::SmallAtom, 1)
}

#[inline]
fn check_atom_limit(&self) -> Result<(), EvalErr> {
if self.atom_vec.len() + self.small_atoms == MAX_NUM_ATOMS {
err(self.nil(), "too many atoms")
} else {
Ok(())
}
}

#[cfg(feature = "counters")]
pub fn atom_count(&self) -> usize {
self.atom_vec.len()
Expand Down

0 comments on commit e90cd12

Please sign in to comment.