Skip to content

Commit

Permalink
Swap libc to core::ffi (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiceneNerd committed Jul 20, 2023
1 parent 9f5d865 commit 724b661
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 317 deletions.
1 change: 0 additions & 1 deletion zstd-safe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ features = ["experimental", "arrays", "std", "zdict_builder", "doc-cfg"]

[dependencies]
zstd-sys = { path = "zstd-sys", version = "2.0.7", default-features = false }
libc = "0.2.21"

[features]
default = ["legacy", "arrays", "zdict_builder"]
Expand Down
11 changes: 4 additions & 7 deletions zstd-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use zstd_sys::ZSTD_strategy as Strategy;
use std::os::raw::{c_char, c_int, c_ulonglong, c_void};

#[cfg(not(feature = "std"))]
use libc::{c_char, c_int, c_ulonglong, c_void};
use core::ffi::{c_char, c_int, c_ulonglong, c_void};

use core::marker::PhantomData;
use core::num::{NonZeroU32, NonZeroU64};
Expand Down Expand Up @@ -819,12 +819,9 @@ unsafe impl<'a> Send for CCtx<'a> {}
unsafe fn c_char_to_str(text: *const c_char) -> &'static str {
#[cfg(not(feature = "std"))]
{
// To be safe, we need to compute right now its length
let len = libc::strlen(text);
// Cast it to a slice
let slice = core::slice::from_raw_parts(text as *mut u8, len);
// And hope it's still text.
str::from_utf8(slice).expect("bad error message from zstd")
core::ffi::CStr::from_ptr(text)
.to_str()
.expect("bad error message from zstd")
}

#[cfg(feature = "std")]
Expand Down
3 changes: 0 additions & 3 deletions zstd-safe/zstd-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ version = "0.3"
version = "1.0.45"
features = ["parallel"]

[dependencies]
libc = "0.2.45"

[features]
default = ["legacy", "zdict_builder"]

Expand Down
30 changes: 15 additions & 15 deletions zstd-safe/zstd-sys/src/bindings_zdict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,51 +38,51 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
extern "C" {
#[doc = " ZDICT_trainFromBuffer():\n Train a dictionary from an array of samples.\n Redirect towards ZDICT_optimizeTrainFromBuffer_fastCover() single-threaded, with d=8, steps=4,\n f=20, and accel=1.\n Samples must be stored concatenated in a single flat buffer `samplesBuffer`,\n supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order.\n The resulting dictionary will be saved into `dictBuffer`.\n @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`)\n or an error code, which can be tested with ZDICT_isError().\n Note: Dictionary training will fail if there are not enough samples to construct a\n dictionary, or if most of the samples are too small (< 8 bytes being the lower limit).\n If dictionary training fails, you should use zstd without a dictionary, as the dictionary\n would've been ineffective anyways. If you believe your samples would benefit from a dictionary\n please open an issue with details, and we can look into it.\n Note: ZDICT_trainFromBuffer()'s memory usage is about 6 MB.\n Tips: In general, a reasonable dictionary has a size of ~ 100 KB.\n It's possible to select smaller or larger size, just by specifying `dictBufferCapacity`.\n In general, it's recommended to provide a few thousands samples, though this can vary a lot.\n It's recommended that total size of all samples be about ~x100 times the target size of dictionary."]
pub fn ZDICT_trainFromBuffer(
dictBuffer: *mut libc::c_void,
dictBuffer: *mut core::ffi::c_void,
dictBufferCapacity: usize,
samplesBuffer: *const libc::c_void,
samplesBuffer: *const core::ffi::c_void,
samplesSizes: *const usize,
nbSamples: libc::c_uint,
nbSamples: core::ffi::c_uint,
) -> usize;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct ZDICT_params_t {
#[doc = "< optimize for a specific zstd compression level; 0 means default"]
pub compressionLevel: libc::c_int,
pub compressionLevel: core::ffi::c_int,
#[doc = "< Write log to stderr; 0 = none (default); 1 = errors; 2 = progression; 3 = details; 4 = debug;"]
pub notificationLevel: libc::c_uint,
pub notificationLevel: core::ffi::c_uint,
#[doc = "< force dictID value; 0 means auto mode (32-bits random value)\n NOTE: The zstd format reserves some dictionary IDs for future use.\n You may use them in private settings, but be warned that they\n may be used by zstd in a public dictionary registry in the future.\n These dictionary IDs are:\n - low range : <= 32767\n - high range : >= (2^31)"]
pub dictID: libc::c_uint,
pub dictID: core::ffi::c_uint,
}
extern "C" {
#[doc = " ZDICT_finalizeDictionary():\n Given a custom content as a basis for dictionary, and a set of samples,\n finalize dictionary by adding headers and statistics according to the zstd\n dictionary format.\n\n Samples must be stored concatenated in a flat buffer `samplesBuffer`,\n supplied with an array of sizes `samplesSizes`, providing the size of each\n sample in order. The samples are used to construct the statistics, so they\n should be representative of what you will compress with this dictionary.\n\n The compression level can be set in `parameters`. You should pass the\n compression level you expect to use in production. The statistics for each\n compression level differ, so tuning the dictionary for the compression level\n can help quite a bit.\n\n You can set an explicit dictionary ID in `parameters`, or allow us to pick\n a random dictionary ID for you, but we can't guarantee no collisions.\n\n The dstDictBuffer and the dictContent may overlap, and the content will be\n appended to the end of the header. If the header + the content doesn't fit in\n maxDictSize the beginning of the content is truncated to make room, since it\n is presumed that the most profitable content is at the end of the dictionary,\n since that is the cheapest to reference.\n\n `maxDictSize` must be >= max(dictContentSize, ZSTD_DICTSIZE_MIN).\n\n @return: size of dictionary stored into `dstDictBuffer` (<= `maxDictSize`),\n or an error code, which can be tested by ZDICT_isError().\n Note: ZDICT_finalizeDictionary() will push notifications into stderr if\n instructed to, using notificationLevel>0.\n NOTE: This function currently may fail in several edge cases including:\n * Not enough samples\n * Samples are uncompressible\n * Samples are all exactly the same"]
pub fn ZDICT_finalizeDictionary(
dstDictBuffer: *mut libc::c_void,
dstDictBuffer: *mut core::ffi::c_void,
maxDictSize: usize,
dictContent: *const libc::c_void,
dictContent: *const core::ffi::c_void,
dictContentSize: usize,
samplesBuffer: *const libc::c_void,
samplesBuffer: *const core::ffi::c_void,
samplesSizes: *const usize,
nbSamples: libc::c_uint,
nbSamples: core::ffi::c_uint,
parameters: ZDICT_params_t,
) -> usize;
}
extern "C" {
pub fn ZDICT_getDictID(
dictBuffer: *const libc::c_void,
dictBuffer: *const core::ffi::c_void,
dictSize: usize,
) -> libc::c_uint;
) -> core::ffi::c_uint;
}
extern "C" {
pub fn ZDICT_getDictHeaderSize(
dictBuffer: *const libc::c_void,
dictBuffer: *const core::ffi::c_void,
dictSize: usize,
) -> usize;
}
extern "C" {
pub fn ZDICT_isError(errorCode: usize) -> libc::c_uint;
pub fn ZDICT_isError(errorCode: usize) -> core::ffi::c_uint;
}
extern "C" {
pub fn ZDICT_getErrorName(errorCode: usize) -> *const libc::c_char;
pub fn ZDICT_getErrorName(errorCode: usize) -> *const core::ffi::c_char;
}

0 comments on commit 724b661

Please sign in to comment.