From a8c44fb83d25c4867059d2219441f17fc924237c Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Tue, 5 Oct 2021 12:01:29 +0200 Subject: [PATCH] small memory usage improvement (v0.1.3) --- Cargo.toml | 2 +- src/archive.rs | 11 ++++------- src/header.rs | 6 +++--- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 73dfbaa..b38376c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ as GNU Longname. The maximum supported file name length is 100 characters includ The maximum supported file size is 8GiB. Also, directories are not supported yet but only flat collections of files. """ -version = "0.1.2" +version = "0.1.3" edition = "2018" keywords = ["tar", "tarball", "archive"] categories = ["data-structures", "no-std", "parser-implementations"] diff --git a/src/archive.rs b/src/archive.rs index 2ef864f..224280c 100644 --- a/src/archive.rs +++ b/src/archive.rs @@ -27,7 +27,6 @@ use crate::header::PosixHeader; use crate::{TypeFlag, BLOCKSIZE}; use arrayvec::ArrayString; use core::fmt::{Debug, Formatter}; -use core::ptr; use core::str::FromStr; /// Describes an entry in an archive. @@ -116,11 +115,10 @@ impl<'a> ArchiveIterator<'a> { } } - /// Returns a pointer to the next Header. - const fn next_hdr(&self, block_index: usize) -> *const PosixHeader { + /// Returns a reference to the next Header. + fn next_hdr(&self, block_index: usize) -> &'a PosixHeader { let hdr_ptr = &self.archive.data[block_index * BLOCKSIZE]; - let hdr_ptr = hdr_ptr as *const u8; - hdr_ptr as *const PosixHeader + unsafe { (hdr_ptr as *const u8).cast::().as_ref() }.unwrap() } } @@ -134,11 +132,10 @@ impl<'a> Iterator for ArchiveIterator<'a> { } let hdr = self.next_hdr(self.block_index); - let hdr = unsafe { ptr::read(hdr) }; // check if we found end of archive if hdr.is_zero_block() { - let next_hdr = unsafe { ptr::read(self.next_hdr(self.block_index + 1)) }; + let next_hdr = self.next_hdr(self.block_index + 1); if next_hdr.is_zero_block() { // gracefully terminated Archive log::debug!("End of Tar archive with two zero blocks!"); diff --git a/src/header.rs b/src/header.rs index 5fbf7b4..5298dfb 100644 --- a/src/header.rs +++ b/src/header.rs @@ -284,9 +284,9 @@ mod tests { use crate::BLOCKSIZE; use std::mem::size_of; - fn bytes_to_archive(bytes: &[u8]) -> PosixHeader { - let hdr = bytes.as_ptr() as *const PosixHeader; - unsafe { core::ptr::read(hdr) } + /// Casts the bytes to a reference to a PosixhHeader. + fn bytes_to_archive(bytes: &[u8]) -> &PosixHeader { + unsafe { (bytes.as_ptr() as *const PosixHeader).as_ref() }.unwrap() } #[test]