@@ -308,18 +308,18 @@ impl<'a> CCtx<'a> {
308308 /// runtime's `malloc`.
309309 pub fn try_create ( ) -> Option < Self > {
310310 #[ cfg( feature = "with-rust-allocator" ) ]
311- {
312- Self :: try_create_with_global_allocator ( )
313- }
311+ // Safety: the callbacks satisfy zstd's allocation contract and remain
312+ // valid for the lifetime of the context.
313+ let context = unsafe {
314+ zstd_sys:: ZSTD_createCCtx_advanced (
315+ crate :: rust_allocator:: RUST_GLOBAL_ALLOCATOR ,
316+ )
317+ } ;
314318 #[ cfg( not( feature = "with-rust-allocator" ) ) ]
315- {
316- // Safety: Just FFI
317- Some ( CCtx (
318- NonNull :: new ( unsafe { zstd_sys:: ZSTD_createCCtx ( ) } ) ?,
319- PhantomData ,
320- Poison :: default ( ) ,
321- ) )
322- }
319+ // Safety: zstd returns a newly owned context or null.
320+ let context = unsafe { zstd_sys:: ZSTD_createCCtx ( ) } ;
321+
322+ Some ( Self ( NonNull :: new ( context) ?, PhantomData , Poison :: default ( ) ) )
323323 }
324324
325325 /// Wrap `ZSTD_createCCtx`
@@ -332,26 +332,6 @@ impl<'a> CCtx<'a> {
332332 . expect ( "zstd returned null pointer when creating new context" )
333333 }
334334
335- /// Tries to create a new context whose internal allocations go through
336- /// Rust's global allocator instead of the C runtime's `malloc`.
337- ///
338- /// Returns `None` if zstd returns a NULL pointer - may happen if
339- /// allocation fails.
340- #[ cfg( feature = "with-rust-allocator" ) ]
341- fn try_create_with_global_allocator ( ) -> Option < Self > {
342- // Safety: Just FFI. The customMem callbacks uphold the malloc/free
343- // contract (see `rust_allocator`).
344- Some ( CCtx (
345- NonNull :: new ( unsafe {
346- zstd_sys:: ZSTD_createCCtx_advanced (
347- crate :: rust_allocator:: RUST_GLOBAL_ALLOCATOR ,
348- )
349- } ) ?,
350- PhantomData ,
351- Poison :: default ( ) ,
352- ) )
353- }
354-
355335 /// Wraps the `ZSTD_compressCCtx()` function
356336 pub fn compress < C : WriteBuf + ?Sized > (
357337 & mut self ,
@@ -1045,18 +1025,18 @@ impl<'a> DCtx<'a> {
10451025 /// runtime's `malloc`.
10461026 pub fn try_create ( ) -> Option < Self > {
10471027 #[ cfg( feature = "with-rust-allocator" ) ]
1048- {
1049- Self :: try_create_with_global_allocator ( )
1050- }
1028+ // Safety: the callbacks satisfy zstd's allocation contract and remain
1029+ // valid for the lifetime of the context.
1030+ let context = unsafe {
1031+ zstd_sys:: ZSTD_createDCtx_advanced (
1032+ crate :: rust_allocator:: RUST_GLOBAL_ALLOCATOR ,
1033+ )
1034+ } ;
10511035 #[ cfg( not( feature = "with-rust-allocator" ) ) ]
1052- {
1053- // Safety: Just FFI
1054- Some ( DCtx (
1055- NonNull :: new ( unsafe { zstd_sys:: ZSTD_createDCtx ( ) } ) ?,
1056- PhantomData ,
1057- Poison :: default ( ) ,
1058- ) )
1059- }
1036+ // Safety: zstd returns a newly owned context or null.
1037+ let context = unsafe { zstd_sys:: ZSTD_createDCtx ( ) } ;
1038+
1039+ Some ( Self ( NonNull :: new ( context) ?, PhantomData , Poison :: default ( ) ) )
10601040 }
10611041
10621042 /// Creates a new decoding context.
@@ -1069,25 +1049,6 @@ impl<'a> DCtx<'a> {
10691049 . expect ( "zstd returned null pointer when creating new context" )
10701050 }
10711051
1072- /// Tries to create a new decompression context whose internal allocations
1073- /// go through Rust's global allocator instead of the C runtime's `malloc`.
1074- ///
1075- /// Returns `None` if the operation failed
1076- #[ cfg( feature = "with-rust-allocator" ) ]
1077- fn try_create_with_global_allocator ( ) -> Option < Self > {
1078- // Safety: Just FFI. The customMem callbacks uphold the malloc/free
1079- // contract (see `rust_allocator`).
1080- Some ( DCtx (
1081- NonNull :: new ( unsafe {
1082- zstd_sys:: ZSTD_createDCtx_advanced (
1083- crate :: rust_allocator:: RUST_GLOBAL_ALLOCATOR ,
1084- )
1085- } ) ?,
1086- PhantomData ,
1087- Poison :: default ( ) ,
1088- ) )
1089- }
1090-
10911052 /// Fully decompress the given frame.
10921053 ///
10931054 /// This decompress an entire frame in-memory. If you can have enough memory to store both the
0 commit comments