Skip to content

Commit 2ff7b21

Browse files
nyurikdanielrh
authored andcommitted
Use proper self for BrotliEncoderStateStruct
Big refactoring, updating all BrotliEncoderStateStruct-using functions, renaming them, and converting many to return bool.
1 parent 03d5501 commit 2ff7b21

File tree

8 files changed

+1203
-1205
lines changed

8 files changed

+1203
-1205
lines changed

src/enc/encode.rs

+1,110-1,090
Large diffs are not rendered by default.

src/enc/mod.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,9 @@ mod parameters;
6161
mod test;
6262
mod weights;
6363
pub use self::backward_references::{BrotliEncoderParams, UnionHasher};
64-
use self::encode::{
65-
BrotliEncoderCompressStream, BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
66-
BrotliEncoderIsFinished, BrotliEncoderOperation, BrotliEncoderSetCustomDictionary,
67-
};
64+
use self::encode::{BrotliEncoderDestroyInstance, BrotliEncoderOperation};
6865
pub use self::encode::{
6966
BrotliEncoderInitParams, BrotliEncoderMaxCompressedSize, BrotliEncoderMaxCompressedSizeMulti,
70-
BrotliEncoderSetParameter,
7167
};
7268
pub use self::hash_to_binary_tree::ZopfliNode;
7369
pub use self::interface::StaticCommand;
@@ -87,6 +83,7 @@ use std::io::{Error, ErrorKind, Read, Write};
8783

8884
#[cfg(feature = "std")]
8985
pub use brotli_decompressor::{IntoIoReader, IoReaderWrapper, IoWriterWrapper};
86+
use enc::encode::BrotliEncoderStateStruct;
9087

9188
#[cfg(not(feature = "std"))]
9289
pub use self::singlethreading::{compress_worker_pool, new_work_pool, WorkerPool};
@@ -258,10 +255,10 @@ where
258255
{
259256
assert!(!input_buffer.is_empty());
260257
assert!(!output_buffer.is_empty());
261-
let mut s_orig = BrotliEncoderCreateInstance(alloc);
258+
let mut s_orig = BrotliEncoderStateStruct::new(alloc);
262259
s_orig.params = params.clone();
263260
if !dict.is_empty() {
264-
BrotliEncoderSetCustomDictionary(&mut s_orig, dict.len(), dict);
261+
s_orig.set_custom_dictionary(dict.len(), dict);
265262
}
266263
let mut next_in_offset: usize = 0;
267264
let mut next_out_offset: usize = 0;
@@ -300,8 +297,7 @@ where
300297
} else {
301298
op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS;
302299
}
303-
let result = BrotliEncoderCompressStream(
304-
s,
300+
let result = s.compress_stream(
305301
op,
306302
&mut available_in,
307303
input_buffer,
@@ -312,8 +308,8 @@ where
312308
&mut total_out,
313309
metablock_callback,
314310
);
315-
let fin = BrotliEncoderIsFinished(s);
316-
if available_out == 0 || fin != 0 {
311+
let fin = s.is_finished();
312+
if available_out == 0 || fin {
317313
let lim = output_buffer.len() - available_out;
318314
assert_eq!(next_out_offset, lim);
319315
next_out_offset = 0;
@@ -332,13 +328,13 @@ where
332328
available_out = output_buffer.len();
333329
next_out_offset = 0;
334330
}
335-
if result <= 0 {
331+
if !result {
336332
if read_err.is_ok() {
337333
read_err = Err(unexpected_eof_error_constant);
338334
}
339335
break;
340336
}
341-
if fin != 0 {
337+
if fin {
342338
break;
343339
}
344340
}

src/enc/reader.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
use super::backward_references::BrotliEncoderParams;
44
use super::combined_alloc::BrotliAlloc;
55
use super::encode::{
6-
BrotliEncoderCompressStream, BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
7-
BrotliEncoderIsFinished, BrotliEncoderOperation, BrotliEncoderParameter,
8-
BrotliEncoderSetParameter, BrotliEncoderStateStruct,
6+
BrotliEncoderDestroyInstance, BrotliEncoderOperation, BrotliEncoderParameter,
7+
BrotliEncoderStateStruct,
98
};
109
use super::interface;
1110
use brotli_decompressor::CustomRead;
@@ -146,19 +145,15 @@ impl<ErrType, R: CustomRead<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: Br
146145
input_len: 0,
147146
input_eof: false,
148147
input: r,
149-
state: StateWrapper(BrotliEncoderCreateInstance(alloc)),
148+
state: StateWrapper(BrotliEncoderStateStruct::new(alloc)),
150149
error_if_invalid_data: Some(invalid_data_error_type),
151150
};
152-
BrotliEncoderSetParameter(
153-
&mut ret.state.0,
154-
BrotliEncoderParameter::BROTLI_PARAM_QUALITY,
155-
q,
156-
);
157-
BrotliEncoderSetParameter(
158-
&mut ret.state.0,
159-
BrotliEncoderParameter::BROTLI_PARAM_LGWIN,
160-
lgwin,
161-
);
151+
ret.state
152+
.0
153+
.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_QUALITY, q);
154+
ret.state
155+
.0
156+
.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_LGWIN, lgwin);
162157

163158
ret
164159
}
@@ -232,8 +227,7 @@ impl<ErrType, R: CustomRead<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: Br
232227
} else {
233228
op = BrotliEncoderOperation::BROTLI_OPERATION_PROCESS;
234229
}
235-
let ret = BrotliEncoderCompressStream(
236-
&mut self.state.0,
230+
let ret = self.state.0.compress_stream(
237231
op,
238232
&mut avail_in,
239233
self.input_buffer.slice_mut(),
@@ -247,11 +241,10 @@ impl<ErrType, R: CustomRead<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: Br
247241
if avail_in == 0 {
248242
self.copy_to_front();
249243
}
250-
if ret <= 0 {
244+
if !ret {
251245
return Err(self.error_if_invalid_data.take().unwrap());
252246
}
253-
let fin = BrotliEncoderIsFinished(&mut self.state.0);
254-
if fin != 0 {
247+
if self.state.0.is_finished() {
255248
break;
256249
}
257250
}

src/enc/test.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ use super::super::alloc::{
88
bzero, AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator,
99
};
1010
use super::cluster::HistogramPair;
11-
use super::encode::{
12-
BrotliEncoderCompressStream, BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
13-
BrotliEncoderIsFinished, BrotliEncoderOperation, BrotliEncoderParameter,
14-
BrotliEncoderSetParameter,
15-
};
11+
use super::encode::{BrotliEncoderOperation, BrotliEncoderParameter};
1612
use super::histogram::{ContextType, HistogramCommand, HistogramDistance, HistogramLiteral};
1713
use super::StaticCommand;
1814
use super::ZopfliNode;
@@ -31,6 +27,7 @@ use super::interface;
3127
use super::pdf::PDF;
3228
use brotli_decompressor::HuffmanCode;
3329
use core::ops;
30+
use enc::encode::BrotliEncoderStateStruct;
3431

3532
declare_stack_allocator_struct!(MemPool, 128, stack);
3633
declare_stack_allocator_struct!(CallocatedFreelist4096, 128, calloc);
@@ -46,7 +43,7 @@ fn oneshot_compress(
4643
magic: bool,
4744
in_batch_size: usize,
4845
out_batch_size: usize,
49-
) -> (i32, usize) {
46+
) -> (bool, usize) {
5047
let stack_u8_buffer =
5148
unsafe { define_allocator_memory_pool!(96, u8, [0; 24 * 1024 * 1024], calloc) };
5249
let stack_u16_buffer =
@@ -113,7 +110,7 @@ fn oneshot_compress(
113110
let mhp = CallocatedFreelist2048::<HistogramPair>::new_allocator(stack_hp_buffer.data, bzero);
114111
let mct = CallocatedFreelist2048::<ContextType>::new_allocator(stack_ct_buffer.data, bzero);
115112
let mht = CallocatedFreelist2048::<HuffmanTree>::new_allocator(stack_ht_buffer.data, bzero);
116-
let mut s_orig = BrotliEncoderCreateInstance(CombiningAllocator::new(
113+
let mut s_orig = BrotliEncoderStateStruct::new(CombiningAllocator::new(
117114
stack_u8_allocator,
118115
stack_u16_allocator,
119116
stack_i32_allocator,
@@ -138,21 +135,19 @@ fn oneshot_compress(
138135
{
139136
let s = &mut s_orig;
140137

141-
BrotliEncoderSetParameter(s, BrotliEncoderParameter::BROTLI_PARAM_QUALITY, quality);
138+
s.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_QUALITY, quality);
142139
if magic {
143-
BrotliEncoderSetParameter(
144-
s,
140+
s.set_parameter(
145141
BrotliEncoderParameter::BROTLI_PARAM_MAGIC_NUMBER,
146142
magic as u32,
147143
);
148144
}
149145
if quality >= 10 {
150-
BrotliEncoderSetParameter(s, BrotliEncoderParameter::BROTLI_PARAM_Q9_5, 1);
146+
s.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_Q9_5, 1);
151147
}
152-
BrotliEncoderSetParameter(s, BrotliEncoderParameter::BROTLI_PARAM_LGWIN, lgwin);
153-
BrotliEncoderSetParameter(s, BrotliEncoderParameter::BROTLI_PARAM_MODE, 0); // gen, text, font
154-
BrotliEncoderSetParameter(
155-
s,
148+
s.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_LGWIN, lgwin);
149+
s.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_MODE, 0); // gen, text, font
150+
s.set_parameter(
156151
BrotliEncoderParameter::BROTLI_PARAM_SIZE_HINT,
157152
input.len() as u32,
158153
);
@@ -194,8 +189,7 @@ fn oneshot_compress(
194189
_,
195190
>| ();
196191

197-
let result = BrotliEncoderCompressStream(
198-
s,
192+
let result = s.compress_stream(
199193
op,
200194
&mut available_in,
201195
input,
@@ -206,18 +200,16 @@ fn oneshot_compress(
206200
&mut total_out,
207201
&mut nop_callback,
208202
);
209-
if result <= 0 {
203+
if !result {
210204
return (result, next_out_offset);
211205
}
212-
if BrotliEncoderIsFinished(s) != 0 {
206+
if s.is_finished() {
213207
break;
214208
}
215209
}
216-
217-
BrotliEncoderDestroyInstance(s);
218210
}
219211

220-
(1, next_out_offset)
212+
(true, next_out_offset)
221213
}
222214

223215
fn oneshot_decompress(compressed: &[u8], output: &mut [u8]) -> (BrotliResult, usize, usize) {
@@ -272,7 +264,7 @@ fn oneshot(
272264
in_buffer_size,
273265
out_buffer_size,
274266
);
275-
if success == 0 {
267+
if !success {
276268
//return (BrotliResult::ResultFailure, 0, 0);
277269
available_in = compressed.len();
278270
}

src/enc/threading.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use super::backward_references::{AnyHasher, BrotliEncoderParams, CloneWithAlloc, UnionHasher};
22
use super::encode::{
3-
BrotliEncoderCompressStream, BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
4-
BrotliEncoderMaxCompressedSize, BrotliEncoderOperation,
5-
BrotliEncoderSetCustomDictionaryWithOptionalPrecomputedHasher, HasherSetup, SanitizeParams,
3+
BrotliEncoderDestroyInstance, BrotliEncoderMaxCompressedSize, BrotliEncoderOperation,
4+
HasherSetup, SanitizeParams,
65
};
76
use super::BrotliAlloc;
87
use alloc::{Allocator, SliceWrapper, SliceWrapperMut};
@@ -11,8 +10,10 @@ use core::any;
1110
use core::marker::PhantomData;
1211
use core::mem;
1312
use core::ops::Range;
13+
use enc::encode::BrotliEncoderStateStruct;
1414
#[cfg(feature = "std")]
1515
use std;
16+
1617
pub type PoisonedThreadError = ();
1718

1819
#[cfg(feature = "std")]
@@ -338,16 +339,15 @@ where
338339
&mut alloc,
339340
BrotliEncoderMaxCompressedSize(range.end - range.start),
340341
);
341-
let mut state = BrotliEncoderCreateInstance(alloc);
342+
let mut state = BrotliEncoderStateStruct::new(alloc);
342343
state.params = input_and_params.1.clone();
343344
if thread_index != 0 {
344345
state.params.catable = true; // make sure we can concatenate this to the other work results
345346
state.params.magic_number = false; // no reason to pepper this around
346347
}
347348
state.params.appendable = true; // make sure we are at least appendable, so that future items can be catted in
348349
if thread_index != 0 {
349-
BrotliEncoderSetCustomDictionaryWithOptionalPrecomputedHasher(
350-
&mut state,
350+
state.set_custom_dictionary_with_optional_precomputed_hasher(
351351
range.start,
352352
&input_and_params.0.slice()[..range.start],
353353
hasher,
@@ -359,8 +359,7 @@ where
359359
loop {
360360
let mut next_in_offset = 0usize;
361361
let mut available_in = range.end - range.start;
362-
let result = BrotliEncoderCompressStream(
363-
&mut state,
362+
let result = state.compress_stream(
364363
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
365364
&mut available_in,
366365
&input_and_params.0.slice()[range.clone()],
@@ -373,7 +372,7 @@ where
373372
);
374373
let new_range = range.start + next_in_offset..range.end;
375374
range = new_range;
376-
if result != 0 {
375+
if result {
377376
compression_result = Ok(out_offset);
378377
break;
379378
} else if available_out == 0 {

src/enc/writer.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
use super::backward_references::BrotliEncoderParams;
33
use super::combined_alloc::BrotliAlloc;
44
use super::encode::{
5-
BrotliEncoderCompressStream, BrotliEncoderCreateInstance, BrotliEncoderDestroyInstance,
6-
BrotliEncoderHasMoreOutput, BrotliEncoderIsFinished, BrotliEncoderOperation,
7-
BrotliEncoderParameter, BrotliEncoderSetParameter, BrotliEncoderStateStruct,
5+
BrotliEncoderDestroyInstance, BrotliEncoderOperation, BrotliEncoderParameter,
6+
BrotliEncoderStateStruct,
87
};
98
use super::interface;
109
pub use alloc::{AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator};
@@ -156,19 +155,13 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
156155
output_buffer: buffer,
157156
total_out: Some(0),
158157
output: Some(w),
159-
state: BrotliEncoderCreateInstance(alloc),
158+
state: BrotliEncoderStateStruct::new(alloc),
160159
error_if_invalid_data: Some(invalid_data_error_type),
161160
};
162-
BrotliEncoderSetParameter(
163-
&mut ret.state,
164-
BrotliEncoderParameter::BROTLI_PARAM_QUALITY,
165-
q,
166-
);
167-
BrotliEncoderSetParameter(
168-
&mut ret.state,
169-
BrotliEncoderParameter::BROTLI_PARAM_LGWIN,
170-
lgwin,
171-
);
161+
ret.state
162+
.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_QUALITY, q);
163+
ret.state
164+
.set_parameter(BrotliEncoderParameter::BROTLI_PARAM_LGWIN, lgwin);
172165

173166
ret
174167
}
@@ -184,8 +177,7 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
184177
let mut input_offset: usize = 0;
185178
let mut avail_out: usize = self.output_buffer.slice_mut().len();
186179
let mut output_offset: usize = 0;
187-
let ret = BrotliEncoderCompressStream(
188-
&mut self.state,
180+
let ret = self.state.compress_stream(
189181
op,
190182
&mut avail_in,
191183
&[],
@@ -205,16 +197,16 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
205197
Err(e) => return Err(e),
206198
}
207199
}
208-
if ret <= 0 {
200+
if !ret {
209201
return Err(self.error_if_invalid_data.take().unwrap());
210202
}
211203
if let BrotliEncoderOperation::BROTLI_OPERATION_FLUSH = op {
212-
if BrotliEncoderHasMoreOutput(&mut self.state) != 0 {
204+
if self.state.has_more_output() {
213205
continue;
214206
}
215207
return Ok(());
216208
}
217-
if BrotliEncoderIsFinished(&mut self.state) != 0 {
209+
if self.state.is_finished() {
218210
return Ok(());
219211
}
220212
}
@@ -262,8 +254,7 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
262254
while avail_in != 0 {
263255
let mut output_offset = 0;
264256
let mut avail_out = self.output_buffer.slice_mut().len();
265-
let ret = BrotliEncoderCompressStream(
266-
&mut self.state,
257+
let ret = self.state.compress_stream(
267258
BrotliEncoderOperation::BROTLI_OPERATION_PROCESS,
268259
&mut avail_in,
269260
buf,
@@ -283,7 +274,7 @@ impl<ErrType, W: CustomWrite<ErrType>, BufferType: SliceWrapperMut<u8>, Alloc: B
283274
Err(e) => return Err(e),
284275
}
285276
}
286-
if ret <= 0 {
277+
if !ret {
287278
return Err(self.error_if_invalid_data.take().unwrap());
288279
}
289280
}

0 commit comments

Comments
 (0)