Merge pull request #743 from aya-rs/avoid-vec-ksyms

util: avoid vector allocation when parsing ksyms
reviewable/pr742/r1
Tamir Duberstein 1 year ago committed by GitHub
commit 90cf13163b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -5,7 +5,9 @@ use std::{
ffi::{CStr, CString}, ffi::{CStr, CString},
fs::{self, File}, fs::{self, File},
io::{self, BufRead, BufReader}, io::{self, BufRead, BufReader},
mem, slice, mem,
num::ParseIntError,
slice,
str::{FromStr, Utf8Error}, str::{FromStr, Utf8Error},
}; };
@ -210,18 +212,24 @@ pub fn kernel_symbols() -> Result<BTreeMap<u64, String>, io::Error> {
} }
fn parse_kernel_symbols(reader: impl BufRead) -> Result<BTreeMap<u64, String>, io::Error> { fn parse_kernel_symbols(reader: impl BufRead) -> Result<BTreeMap<u64, String>, io::Error> {
let mut syms = BTreeMap::new(); reader
.lines()
for line in reader.lines() { .map(|line| {
let line = line?; let line = line?;
let parts = line.splitn(4, ' ').collect::<Vec<_>>(); (|| {
let addr = u64::from_str_radix(parts[0], 16) let mut parts = line.splitn(4, ' ');
.map_err(|_| io::Error::new(io::ErrorKind::InvalidData, line.clone()))?; let addr = parts.next()?;
let name = parts[2].to_owned(); let _kind = parts.next()?;
syms.insert(addr, name); let name = parts.next()?;
} let addr = match u64::from_str_radix(addr, 16) {
Ok(addr) => Some(addr),
Ok(syms) Err(ParseIntError { .. }) => None,
}?;
Some((addr, name.to_owned()))
})()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, line.clone()))
})
.collect()
} }
/// Returns the prefix used by syscalls. /// Returns the prefix used by syscalls.

Loading…
Cancel
Save