Skip to content

Commit b816ac2

Browse files
authored
Add new_with_capacity to Assembler (#115)
Allows for the pre-allocation of assembler buffer and executable section size, allowing for optimisation if these are already known in advance.
1 parent 00dc400 commit b816ac2

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

runtime/src/lib.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,28 @@ impl<R: Relocation> Assembler<R> {
583583
})
584584
}
585585

586+
/// Create a new, empty assembler, with a pre-allocated buffer of the specified capacity in bytes,
587+
/// and an initial allocation of `capacity.next_multiple_of(page_size)`.
588+
pub fn new_with_capacity(capacity: usize) -> io::Result<Self> {
589+
// Avoid panic or wraps on overflow.
590+
let memory_size =
591+
capacity
592+
.checked_next_multiple_of(R::page_size())
593+
.ok_or(io::Error::new(
594+
io::ErrorKind::InvalidInput,
595+
"Capacity is too large",
596+
))?;
597+
598+
Ok(Self {
599+
ops: Vec::with_capacity(capacity),
600+
memory: MemoryManager::new(memory_size)?,
601+
labels: LabelRegistry::new(),
602+
relocs: RelocRegistry::new(),
603+
managed: ManagedRelocs::new(),
604+
error: None,
605+
})
606+
}
607+
586608
/// Create a new dynamic label ID
587609
pub fn new_dynamic_label(&mut self) -> DynamicLabel {
588610
self.labels.new_dynamic_label()

0 commit comments

Comments
 (0)