prs_rs/
comp.rs

1use crate::prelude::{Allocator, Global};
2
3use crate::{
4    impls::comp::{
5        comp_dict::{CompDict, MaxOffset},
6        compress::prs_compress,
7    },
8    MutablePointerSrc,
9};
10
11/// BENCHMARK ONLY, DO NOT USE
12#[doc(hidden)]
13pub fn create_comp_dict(data: &[u8]) -> MaxOffset {
14    unsafe {
15        let mut dict = CompDict::new(data.len());
16        dict.init(data, 0);
17        dict.get_item(0, 0, u32::MAX as usize)[0]
18    }
19}
20
21/// Compresses the given data in `source`, placing it in `destimation`.
22///
23/// Parameters
24///
25/// - `src`: A pointer to the decompressed data.
26/// - `src_len`: Length of the decompressed data.
27/// - `destination`: A pointer to the compressed data to be written.
28///
29/// # Returns
30///
31/// Number of bytes written to `destination`.
32///
33/// # Safety
34///
35/// It's safe as long as `dest` has sufficient length (max length: [`crate::util::prs_calculate_max_compressed_size`])
36/// and the remaining parameters are valid.
37pub unsafe fn prs_compress_unsafe<T: MutablePointerSrc>(
38    src: *const u8,
39    src_len: usize,
40    mut dest: T,
41) -> usize {
42    prs_compress::<Global, Global>(src, dest.as_mut_ptr(), src_len, Global, Global)
43}
44
45/// Compresses the given data in `source`, placing it in `destimation`.
46/// Uses a custom allocator for short and long lived memory.
47///
48/// Parameters
49///
50/// - `src`: A pointer to the decompressed data.
51/// - `src_len`: Length of the decompressed data.
52/// - `destination`: A pointer to the compressed data to be written.
53/// - `long_lived_allocator`: The allocator to use for long-lived memory allocation.
54/// - `short_lived_allocator`: The allocator to use for short-lived memory allocation.
55///
56/// # Returns
57///
58/// Number of bytes written to `destination`.
59///
60/// # Safety
61///
62/// It's safe as long as `dest` has sufficient length (max length: [`crate::util::prs_calculate_max_compressed_size`])
63/// and the remaining parameters are valid.
64pub unsafe fn prs_compress_unsafe_with_allocator<
65    T: MutablePointerSrc,
66    L: Allocator + Copy,
67    S: Allocator + Copy,
68>(
69    src: *const u8,
70    src_len: usize,
71    mut dest: T,
72    long_lived_allocator: L,
73    short_lived_allocator: S,
74) -> usize {
75    prs_compress::<L, S>(
76        src,
77        dest.as_mut_ptr(),
78        src_len,
79        long_lived_allocator,
80        short_lived_allocator,
81    )
82}