Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using default in HashBuffers::reset #147

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions miniz_oxide/src/deflate/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//! to avoid stack copies. Box::new() doesn't at the moment, and using a vec means we would lose
//! static length info.

use alloc::boxed::Box;
use alloc::vec;
use core::convert::TryInto;

use crate::deflate::core::{LZ_DICT_SIZE, MAX_MATCH_LEN};

/// Size of the buffer of lz77 encoded data.
Expand All @@ -23,36 +27,41 @@ pub fn update_hash(current_hash: u16, byte: u8) -> u16 {
}

pub struct HashBuffers {
pub dict: [u8; LZ_DICT_FULL_SIZE],
pub next: [u16; LZ_DICT_SIZE],
pub hash: [u16; LZ_DICT_SIZE],
pub dict: Box<[u8; LZ_DICT_FULL_SIZE]>,
pub next: Box<[u16; LZ_DICT_SIZE]>,
pub hash: Box<[u16; LZ_DICT_SIZE]>,
}

impl HashBuffers {
#[inline]
pub fn reset(&mut self) {
*self = HashBuffers::default();
self.dict.fill(0);
self.next.fill(0);
self.hash.fill(0);
}
}

impl Default for HashBuffers {
fn default() -> HashBuffers {
HashBuffers {
dict: [0; LZ_DICT_FULL_SIZE],
next: [0; LZ_DICT_SIZE],
hash: [0; LZ_DICT_SIZE],
dict: vec![0; LZ_DICT_FULL_SIZE]
.into_boxed_slice()
.try_into()
.unwrap(),
next: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(),
hash: vec![0; LZ_DICT_SIZE].into_boxed_slice().try_into().unwrap(),
}
}
}

pub struct LocalBuf {
pub b: [u8; OUT_BUF_SIZE],
pub b: Box<[u8; OUT_BUF_SIZE]>,
}

impl Default for LocalBuf {
fn default() -> LocalBuf {
LocalBuf {
b: [0; OUT_BUF_SIZE],
b: vec![0; OUT_BUF_SIZE].into_boxed_slice().try_into().unwrap(),
}
}
}
12 changes: 6 additions & 6 deletions miniz_oxide/src/deflate/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@ struct DictOxide {
pub max_probes: [u32; 2],
/// Buffer of input data.
/// Padded with 1 byte to simplify matching code in `compress_fast`.
pub b: Box<HashBuffers>,
pub b: HashBuffers,

pub code_buf_dict_pos: usize,
pub lookahead_size: usize,
Expand All @@ -1214,7 +1214,7 @@ impl DictOxide {
fn new(flags: u32) -> Self {
DictOxide {
max_probes: probes_from_flags(flags),
b: Box::default(),
b: HashBuffers::default(),
code_buf_dict_pos: 0,
lookahead_size: 0,
lookahead_pos: 0,
Expand Down Expand Up @@ -1401,7 +1401,7 @@ struct ParamsOxide {
pub saved_bit_buffer: u32,
pub saved_bits_in: u32,

pub local_buf: Box<LocalBuf>,
pub local_buf: LocalBuf,
}

impl ParamsOxide {
Expand All @@ -1423,7 +1423,7 @@ impl ParamsOxide {
prev_return_status: TDEFLStatus::Okay,
saved_bit_buffer: 0,
saved_bits_in: 0,
local_buf: Box::default(),
local_buf: LocalBuf::default(),
}
}

Expand All @@ -1448,7 +1448,7 @@ impl ParamsOxide {
self.prev_return_status = TDEFLStatus::Okay;
self.saved_bit_buffer = 0;
self.saved_bits_in = 0;
self.local_buf.b = [0; OUT_BUF_SIZE];
self.local_buf.b.fill(0);
}
}

Expand Down Expand Up @@ -1628,7 +1628,7 @@ fn flush_block(
{
let mut output = callback
.out
.new_output_buffer(&mut d.params.local_buf.b, d.params.out_buf_ofs);
.new_output_buffer(d.params.local_buf.b.as_mut(), d.params.out_buf_ofs);
output.bit_buffer = d.params.saved_bit_buffer;
output.bits_in = d.params.saved_bits_in;

Expand Down