Skip to content

Commit

Permalink
aya: preallocate the vector
Browse files Browse the repository at this point in the history
This code badly needs tests :(
  • Loading branch information
tamird committed Aug 1, 2023
1 parent f095c59 commit 89ef97e
Showing 1 changed file with 28 additions and 27 deletions.
55 changes: 28 additions & 27 deletions aya/src/programs/uprobe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,33 +337,34 @@ impl LdSoCache {
0
};

let mut entries = Vec::new();
for _ in 0..num_entries {
let flags = read_i32(&mut cursor)?;
let k_pos = read_u32(&mut cursor)? as usize;
let v_pos = read_u32(&mut cursor)? as usize;

if new_format {
cursor.consume(12);
}

let key = unsafe {
CStr::from_ptr(cursor.get_ref()[offset + k_pos..].as_ptr() as *const c_char)
}
.to_string_lossy()
.into_owned();
let value = unsafe {
CStr::from_ptr(cursor.get_ref()[offset + v_pos..].as_ptr() as *const c_char)
}
.to_string_lossy()
.into_owned();

entries.push(CacheEntry {
key,
value,
_flags: flags,
});
}
let entries = (0..num_entries)
.map(|_: u32| {
let flags = read_i32(&mut cursor)?;
let k_pos = read_u32(&mut cursor)? as usize;
let v_pos = read_u32(&mut cursor)? as usize;

if new_format {
cursor.consume(12);
}

let read_str = |pos| {
unsafe {
CStr::from_ptr(cursor.get_ref()[offset + pos..].as_ptr() as *const c_char)
}
.to_string_lossy()
.into_owned()
};

let key = read_str(k_pos);
let value = read_str(v_pos);

Ok::<_, io::Error>(CacheEntry {
key,
value,
_flags: flags,
})
})
.collect::<Result<_, _>>()?;

Ok(LdSoCache { entries })
}
Expand Down

0 comments on commit 89ef97e

Please sign in to comment.