aya: preallocate the vector

This code badly needs tests :(
reviewable/pr716/r1
Tamir Duberstein 2 years ago
parent f095c591af
commit 89ef97e848
No known key found for this signature in database

@ -337,8 +337,8 @@ impl LdSoCache {
0 0
}; };
let mut entries = Vec::new(); let entries = (0..num_entries)
for _ in 0..num_entries { .map(|_: u32| {
let flags = read_i32(&mut cursor)?; let flags = read_i32(&mut cursor)?;
let k_pos = read_u32(&mut cursor)? as usize; let k_pos = read_u32(&mut cursor)? as usize;
let v_pos = read_u32(&mut cursor)? as usize; let v_pos = read_u32(&mut cursor)? as usize;
@ -347,23 +347,24 @@ impl LdSoCache {
cursor.consume(12); cursor.consume(12);
} }
let key = unsafe { let read_str = |pos| {
CStr::from_ptr(cursor.get_ref()[offset + k_pos..].as_ptr() as *const c_char) unsafe {
CStr::from_ptr(cursor.get_ref()[offset + pos..].as_ptr() as *const c_char)
} }
.to_string_lossy() .to_string_lossy()
.into_owned(); .into_owned()
let value = unsafe { };
CStr::from_ptr(cursor.get_ref()[offset + v_pos..].as_ptr() as *const c_char)
} let key = read_str(k_pos);
.to_string_lossy() let value = read_str(v_pos);
.into_owned();
entries.push(CacheEntry { Ok::<_, io::Error>(CacheEntry {
key, key,
value, value,
_flags: flags, _flags: flags,
}); })
} })
.collect::<Result<_, _>>()?;
Ok(LdSoCache { entries }) Ok(LdSoCache { entries })
} }

Loading…
Cancel
Save